目标检测---数据集格式转化及训练集和验证集划分
?1 VOC標簽格式轉yolo格式并劃分訓練集和測試集
?????????我們經常從網上獲取一些目標檢測的數據集資源標簽的格式都是VOC(xml格式)的,而yolov5訓練所需要的文件格式是yolo(txt格式)的,這里就需要對xml格式的標簽文件轉換為txt文件。同時訓練自己的yolov5檢測模型的時候,數據集需要劃分為訓練集和驗證集。這里提供了一份代碼將xml格式的標注文件轉換為txt格式的標注文件,并按比例劃分為訓練集和驗證集。先上代碼再講解代碼的注意事項。
import xml.etree.ElementTree as ET import pickle import os from os import listdir, getcwd from os.path import join import random from shutil import copyfileclasses = ["hat", "person"] #classes=["ball"]TRAIN_RATIO = 80def clear_hidden_files(path):dir_list = os.listdir(path)for i in dir_list:abspath = os.path.join(os.path.abspath(path), i)if os.path.isfile(abspath):if i.startswith("._"):os.remove(abspath)else:clear_hidden_files(abspath)def convert(size, box):dw = 1./size[0]dh = 1./size[1]x = (box[0] + box[1])/2.0y = (box[2] + box[3])/2.0w = box[1] - box[0]h = box[3] - box[2]x = x*dww = w*dwy = y*dhh = h*dhreturn (x,y,w,h)def convert_annotation(image_id):in_file = open('VOCdevkit/VOC2007/Annotations/%s.xml' %image_id)out_file = open('VOCdevkit/VOC2007/YOLOLabels/%s.txt' %image_id, 'w')tree=ET.parse(in_file)root = tree.getroot()size = root.find('size')w = int(size.find('width').text)h = int(size.find('height').text)for obj in root.iter('object'):difficult = obj.find('difficult').textcls = obj.find('name').textif cls not in classes or int(difficult) == 1:continuecls_id = classes.index(cls)xmlbox = obj.find('bndbox')b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))bb = convert((w,h), b)out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')in_file.close()out_file.close()wd = os.getcwd() wd = os.getcwd() data_base_dir = os.path.join(wd, "VOCdevkit/") if not os.path.isdir(data_base_dir):os.mkdir(data_base_dir) work_sapce_dir = os.path.join(data_base_dir, "VOC2007/") if not os.path.isdir(work_sapce_dir):os.mkdir(work_sapce_dir) annotation_dir = os.path.join(work_sapce_dir, "Annotations/") if not os.path.isdir(annotation_dir):os.mkdir(annotation_dir) clear_hidden_files(annotation_dir) image_dir = os.path.join(work_sapce_dir, "JPEGImages/") if not os.path.isdir(image_dir):os.mkdir(image_dir) clear_hidden_files(image_dir) yolo_labels_dir = os.path.join(work_sapce_dir, "YOLOLabels/") if not os.path.isdir(yolo_labels_dir):os.mkdir(yolo_labels_dir) clear_hidden_files(yolo_labels_dir) yolov5_images_dir = os.path.join(data_base_dir, "images/") if not os.path.isdir(yolov5_images_dir):os.mkdir(yolov5_images_dir) clear_hidden_files(yolov5_images_dir) yolov5_labels_dir = os.path.join(data_base_dir, "labels/") if not os.path.isdir(yolov5_labels_dir):os.mkdir(yolov5_labels_dir) clear_hidden_files(yolov5_labels_dir) yolov5_images_train_dir = os.path.join(yolov5_images_dir, "train/") if not os.path.isdir(yolov5_images_train_dir):os.mkdir(yolov5_images_train_dir) clear_hidden_files(yolov5_images_train_dir) yolov5_images_test_dir = os.path.join(yolov5_images_dir, "val/") if not os.path.isdir(yolov5_images_test_dir):os.mkdir(yolov5_images_test_dir) clear_hidden_files(yolov5_images_test_dir) yolov5_labels_train_dir = os.path.join(yolov5_labels_dir, "train/") if not os.path.isdir(yolov5_labels_train_dir):os.mkdir(yolov5_labels_train_dir) clear_hidden_files(yolov5_labels_train_dir) yolov5_labels_test_dir = os.path.join(yolov5_labels_dir, "val/") if not os.path.isdir(yolov5_labels_test_dir):os.mkdir(yolov5_labels_test_dir) clear_hidden_files(yolov5_labels_test_dir)train_file = open(os.path.join(wd, "yolov5_train.txt"), 'w') test_file = open(os.path.join(wd, "yolov5_val.txt"), 'w') train_file.close() test_file.close() train_file = open(os.path.join(wd, "yolov5_train.txt"), 'a') test_file = open(os.path.join(wd, "yolov5_val.txt"), 'a') list_imgs = os.listdir(image_dir) # list image files prob = random.randint(1, 100) print("Probability: %d" % prob) for i in range(0,len(list_imgs)):path = os.path.join(image_dir,list_imgs[i])if os.path.isfile(path):image_path = image_dir + list_imgs[i]voc_path = list_imgs[i](nameWithoutExtention, extention) = os.path.splitext(os.path.basename(image_path))(voc_nameWithoutExtention, voc_extention) = os.path.splitext(os.path.basename(voc_path))annotation_name = nameWithoutExtention + '.xml'annotation_path = os.path.join(annotation_dir, annotation_name)label_name = nameWithoutExtention + '.txt'label_path = os.path.join(yolo_labels_dir, label_name)prob = random.randint(1, 100)print("Probability: %d" % prob)if(prob < TRAIN_RATIO): # train datasetif os.path.exists(annotation_path):train_file.write(image_path + '\n')convert_annotation(nameWithoutExtention) # convert labelcopyfile(image_path, yolov5_images_train_dir + voc_path)copyfile(label_path, yolov5_labels_train_dir + label_name)else: # test datasetif os.path.exists(annotation_path):test_file.write(image_path + '\n')convert_annotation(nameWithoutExtention) # convert labelcopyfile(image_path, yolov5_images_test_dir + voc_path)copyfile(label_path, yolov5_labels_test_dir + label_name) train_file.close() test_file.close()????????首先數據集的格式結構必須嚴格按照如圖的樣式來,因為代碼已經將文件名寫死了。其實這樣也好,因為統一就會規范 。
Annotations里面存放著xml格式的標簽文件
JPEGImages里面存放著照片數據文件
? ? ? ? 特別要注意的是,classes里面必須正確填寫xml里面已經標注好的類,要不然生成的txt的文件是不對的。TRAIN_RATIO是訓練集和驗證集的比例,當等于80的時候,說明劃分80%給訓練集,20%給驗證集。
? ? ? ? ?將代碼和數據在同一目錄下運行,得到如下的結果
? ? ? ? ?在VOCdevkit目錄下生成images和labels文件夾,文件夾下分別生成了train文件夾和val文件夾,里面分別保存著訓練集的照片和txt格式的標簽,還有驗證集的照片和txt格式的標簽。images文件夾和labels文件夾就是訓練yolov5模型所需的訓練集和驗證集。在VOCdevkit/VOC2007目錄下還生成了一個YOLOLabels文件夾,里面存放著所有的txt格式的標簽文件。
? ? ? ? 至此,xml格式的標簽文件轉換為txt格式的標簽文件并劃分為訓練集和測試集就講完了。
2? 標簽為yolo格式數據集劃分訓練集和驗證集
????????由于yolov5訓練需要的數據標簽格式為txt格式,所以大家在利用labelimg標注的時候會用yolo格式(標注生成的標簽為txt格式)。標注好的數據集訓練的時候就要劃分為訓練集和驗證集,因此就需要有劃分為訓練集和測試集的代碼。這里需要講的是我寫的腳本代碼可以成功將數據集劃分為訓練集和驗證集,但是在訓練模型的時候,加載數據集一直會出現問題。因此我就想到了,先把txt格式的數據集替換成xml格式的數據集,然后再按上述將xml格式標簽轉化為txt格式標簽并劃分為訓練集和驗證集的方法劃分就好了。但是這里建議大家以后標注的時候就標注為voc格式(xml格式),因為該格式的標簽里面有圖片標注的具體內容,例如標注類別,圖片大小,標注坐標。但是yolo格式(txt格式)里面是用數字來代表類別,這樣很不直觀,而且標注的坐標也是經過轉化歸一化的,坐標信息更加不直觀。先上yolo轉voc的代碼。
from xml.dom.minidom import Document import os import cv2# def makexml(txtPath, xmlPath, picPath): # txt所在文件夾路徑,xml文件保存路徑,圖片所在文件夾路徑 def makexml(picPath, txtPath, xmlPath): # txt所在文件夾路徑,xml文件保存路徑,圖片所在文件夾路徑"""此函數用于將yolo格式txt標注文件轉換為voc格式xml標注文件在自己的標注圖片文件夾下建三個子文件夾,分別命名為picture、txt、xml"""dic = {'0': "hat", # 創建字典用來對類型進行轉換'1': "person", # 此處的字典要與自己的classes.txt文件中的類對應,且順序要一致}files = os.listdir(txtPath)for i, name in enumerate(files):xmlBuilder = Document()annotation = xmlBuilder.createElement("annotation") # 創建annotation標簽xmlBuilder.appendChild(annotation)txtFile = open(txtPath + name)txtList = txtFile.readlines()img = cv2.imread(picPath + name[0:-4] + ".jpg")Pheight, Pwidth, Pdepth = img.shapefolder = xmlBuilder.createElement("folder") # folder標簽foldercontent = xmlBuilder.createTextNode("driving_annotation_dataset")folder.appendChild(foldercontent)annotation.appendChild(folder) # folder標簽結束filename = xmlBuilder.createElement("filename") # filename標簽filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".jpg")filename.appendChild(filenamecontent)annotation.appendChild(filename) # filename標簽結束size = xmlBuilder.createElement("size") # size標簽width = xmlBuilder.createElement("width") # size子標簽widthwidthcontent = xmlBuilder.createTextNode(str(Pwidth))width.appendChild(widthcontent)size.appendChild(width) # size子標簽width結束height = xmlBuilder.createElement("height") # size子標簽heightheightcontent = xmlBuilder.createTextNode(str(Pheight))height.appendChild(heightcontent)size.appendChild(height) # size子標簽height結束depth = xmlBuilder.createElement("depth") # size子標簽depthdepthcontent = xmlBuilder.createTextNode(str(Pdepth))depth.appendChild(depthcontent)size.appendChild(depth) # size子標簽depth結束annotation.appendChild(size) # size標簽結束for j in txtList:oneline = j.strip().split(" ")object = xmlBuilder.createElement("object") # object 標簽picname = xmlBuilder.createElement("name") # name標簽namecontent = xmlBuilder.createTextNode(dic[oneline[0]])picname.appendChild(namecontent)object.appendChild(picname) # name標簽結束pose = xmlBuilder.createElement("pose") # pose標簽posecontent = xmlBuilder.createTextNode("Unspecified")pose.appendChild(posecontent)object.appendChild(pose) # pose標簽結束truncated = xmlBuilder.createElement("truncated") # truncated標簽truncatedContent = xmlBuilder.createTextNode("0")truncated.appendChild(truncatedContent)object.appendChild(truncated) # truncated標簽結束difficult = xmlBuilder.createElement("difficult") # difficult標簽difficultcontent = xmlBuilder.createTextNode("0")difficult.appendChild(difficultcontent)object.appendChild(difficult) # difficult標簽結束bndbox = xmlBuilder.createElement("bndbox") # bndbox標簽xmin = xmlBuilder.createElement("xmin") # xmin標簽mathData = int(((float(oneline[1])) * Pwidth + 1) - (float(oneline[3])) * 0.5 * Pwidth)xminContent = xmlBuilder.createTextNode(str(mathData))xmin.appendChild(xminContent)bndbox.appendChild(xmin) # xmin標簽結束ymin = xmlBuilder.createElement("ymin") # ymin標簽mathData = int(((float(oneline[2])) * Pheight + 1) - (float(oneline[4])) * 0.5 * Pheight)yminContent = xmlBuilder.createTextNode(str(mathData))ymin.appendChild(yminContent)bndbox.appendChild(ymin) # ymin標簽結束xmax = xmlBuilder.createElement("xmax") # xmax標簽mathData = int(((float(oneline[1])) * Pwidth + 1) + (float(oneline[3])) * 0.5 * Pwidth)xmaxContent = xmlBuilder.createTextNode(str(mathData))xmax.appendChild(xmaxContent)bndbox.appendChild(xmax) # xmax標簽結束ymax = xmlBuilder.createElement("ymax") # ymax標簽mathData = int(((float(oneline[2])) * Pheight + 1) + (float(oneline[4])) * 0.5 * Pheight)ymaxContent = xmlBuilder.createTextNode(str(mathData))ymax.appendChild(ymaxContent)bndbox.appendChild(ymax) # ymax標簽結束object.appendChild(bndbox) # bndbox標簽結束annotation.appendChild(object) # object標簽結束f = open(xmlPath + name[0:-4] + ".xml", 'w')xmlBuilder.writexml(f, indent='\t', newl='\n', addindent='\t', encoding='utf-8')f.close()if __name__ == "__main__":picPath = "VOCdevkit/VOC2007/JPEGImages/" # 圖片所在文件夾路徑,后面的/一定要帶上txtPath = "VOCdevkit/VOC2007/YOLO/" # txt所在文件夾路徑,后面的/一定要帶上xmlPath = "VOCdevkit/VOC2007/Annotations/" # xml文件保存路徑,后面的/一定要帶上makexml(picPath, txtPath, xmlPath)????????首先講一下數據的格式,要嚴格按照下圖的目錄結構來。為了后續數據集的劃分做出統一數據集目錄結構。且數據目錄要和代碼在同一目錄下,這樣就可以一鍵運行了。
?? ?JPEGImages為圖片數據所在的目錄
?? ?YOLO為yolo格式的標簽數據所在目錄
?? ?Annotations為生成的voc格式數據標簽目錄(程序運行前這是一個空目錄)
如下圖這里要對應好,且順序要一致,例如我這里是0對應hat,1對應person。
? ? ? ? 運行如上的代碼,就可以將yolo格式的標簽轉化為voc格式,并保存在 ?Annotations目錄中,最后可以按照上述1的方法,將voc轉為yolo再劃分數據集就可以了。
? ? ? ? 至此yolo格式數據集劃分訓練集和驗證集就結束了
?
總結
以上是生活随笔為你收集整理的目标检测---数据集格式转化及训练集和验证集划分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 科学家计算机模拟宇宙,科学家利用计算机模
- 下一篇: 【三维目标检测】PointRCNN(二)