【YOLOv5实战2】基于YOLOv5的交通标志识别系统-自定义数据集
實戰博客指引:
項目源代碼可聯系博主獲取。
一、數據準備
1.1 從官網下載YOLOv5
打開官網YOLOv5,使用git或者直接下載壓縮包
- git下載
or
git clone https://github.com/ultralytics/yolov5.git- 直接下載zip壓縮包
二、自定義數據集
2.1 創建目錄
在yolov5目錄下創建文件夾VOCdata,創建圖片路徑以及標注路徑,注意每個標注都對應一個圖片名稱,并區分訓練集與驗證集。目錄如下:
VOCdata--images--train--val--labels--train--val2.2 數據預覽
下載數據集后,數據集格式如下:
一共有6列,簡要解釋一下每列的意義:
- 第1列:圖片名稱以及圖片格式
- 第2-5列:圖片標注的坐標,以像素為坐標,左上角為原點坐標。
- 第6列:該標注的分類類別。
2.3 數據格式轉換
2.3.1 圖像格式轉換
首先將ppm格式轉換為jpg格式,在VOCdata/下目錄下創建py文件ppm_to_jpg.py:
def ppm_to_jpg(img_out_path, ppm_path):ppm_list = os.listdir(ppm_path)for ppm in ppm_list:if ppm.endswith(".ppm"):path = ppm_path + ppmimg = Image.open(path)new_path = img_out_path + ppm.split(".ppm")[0] + ".jpg"img.save(new_path)print("transfer finish!")首先指定ppm圖片的輸入路徑以及輸出路徑,并創建文件夾,用于保存訓練集和測試集。注意測試機保存在yolov5/data/images/test/目錄下,用于后面進行數據集的測試。
2.3.2 標注格式轉換
在YOLOv5中,標注坐標有要求格式,因此需要進行轉換。官方的原文如下:
然后準備labels,將xml文件轉換成yolo需要的txt格式。 After using a tool like CVAT, makesense.ai or Labelbox to label your images, export your labels to YOLO format, with one *.txt file per image (if no objects in image, no *.txt file is required). The *.txt file specifications are:- One row per object - Each row is class x_center y_center width height format. - Box coordinates must be in normalized xywh format (from 0 - 1). If your boxes are in pixels, divide x_center and width by image width, and y_center and height by image height. - Class numbers are zero-indexed (start from 0).
The label file corresponding to the above image contains 2 persons (class 0) and a tie (class 27):
接下來進行標注轉換。新建py文件split_labels.py,用于將標注文件進行格式轉換并劃分數據集。這里采用訓練集:驗證集=9:1,在后續中的圖片劃分中將按照該labels文件夾劃分圖像數據集。
def gt2yolo():i = 0file_name = ''for line in open(input_path, "r"): # 設置文件對象words = line.split(';')img_name = words[0].split(".")[0]label = int(words[-1])x = float(words[1]) / orign_ww = float(words[3]) / orign_w - xy = float(words[2]) / orign_hh = float(words[4]) / orign_h - y# 保存路徑。并設置是保存為訓練集還是驗證集if output_path + img_name + ".txt" != file_name:if i < int(nums * 0.9):file_name = output_path + 'train/' + img_name + ".txt"else:file_name = output_path + 'val/' + img_name + ".txt"fw = open(file_name, 'w')else:fw = open(file_name, 'a')# print(file_name)# 寫入文件中fw.write(str(label) + " " + str(x) + " " + str(y) + " " + str(w) + " " + str(h) + "\n")fw.close()i += 1再看一遍標注文件的格式:
因此基本思路為按照";"劃分數據,并將中間四個像素坐標歸一化到[0,1]區間上。詳細過程在代碼中寫的很詳細,注意要修改自己的輸入輸出路徑以及圖片的大小。
劃分完成后,將得到如下的目錄結構:
2.3.3 劃分圖片
劃分好標注數據集后,將按照該數據集劃分圖像數據?;舅悸窞楸闅v訓練集和驗證集,獲取對應標注的文件名稱,然后找到相應的文件名稱后將圖片移動至對應的訓練集或驗證集中。詳細注釋見代碼,注意修改路徑。
創建split_train_val.py:
劃分完成后,將得到如下目錄結構:
瀏覽目錄文件,確定訓練集與驗證集中的圖片名稱與標注目錄中相對應:
2.4 創建配置文件
在yolov5的data目錄下創建myvoc.yaml文件,用于指定訓練集與驗證集的路徑。注意指定的訓練集與驗證集路徑與前文保持一致。修改nc,及你需要分類的數量。在names里指定分類的名稱。
其內容如下:
注意nc的值要和names的個數相同,yaml文件的冒號后面需要添加一個空格!!!train,val,nc,names的冒號后面都有一個空格,別忘了。
參考文章
- 1.https://blog.csdn.net/qq_36756866/article/details/109111065
- 2.https://blog.csdn.net/qq_45945548/article/details/121701492
總結
以上是生活随笔為你收集整理的【YOLOv5实战2】基于YOLOv5的交通标志识别系统-自定义数据集的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 杂谈百合
- 下一篇: ubuntu20.04双系统启动盘制作、