目标检测---数据集格式转化及训练集和验证集划分
?1 VOC標(biāo)簽格式轉(zhuǎn)yolo格式并劃分訓(xùn)練集和測(cè)試集
?????????我們經(jīng)常從網(wǎng)上獲取一些目標(biāo)檢測(cè)的數(shù)據(jù)集資源標(biāo)簽的格式都是VOC(xml格式)的,而yolov5訓(xùn)練所需要的文件格式是yolo(txt格式)的,這里就需要對(duì)xml格式的標(biāo)簽文件轉(zhuǎn)換為txt文件。同時(shí)訓(xùn)練自己的yolov5檢測(cè)模型的時(shí)候,數(shù)據(jù)集需要?jiǎng)澐譃橛?xùn)練集和驗(yàn)證集。這里提供了一份代碼將xml格式的標(biāo)注文件轉(zhuǎn)換為txt格式的標(biāo)注文件,并按比例劃分為訓(xùn)練集和驗(yàn)證集。先上代碼再講解代碼的注意事項(xiàng)。
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()????????首先數(shù)據(jù)集的格式結(jié)構(gòu)必須嚴(yán)格按照如圖的樣式來(lái),因?yàn)榇a已經(jīng)將文件名寫死了。其實(shí)這樣也好,因?yàn)榻y(tǒng)一就會(huì)規(guī)范 。
Annotations里面存放著xml格式的標(biāo)簽文件
JPEGImages里面存放著照片數(shù)據(jù)文件
? ? ? ? 特別要注意的是,classes里面必須正確填寫xml里面已經(jīng)標(biāo)注好的類,要不然生成的txt的文件是不對(duì)的。TRAIN_RATIO是訓(xùn)練集和驗(yàn)證集的比例,當(dāng)?shù)扔?0的時(shí)候,說(shuō)明劃分80%給訓(xùn)練集,20%給驗(yàn)證集。
? ? ? ? ?將代碼和數(shù)據(jù)在同一目錄下運(yùn)行,得到如下的結(jié)果
? ? ? ? ?在VOCdevkit目錄下生成images和labels文件夾,文件夾下分別生成了train文件夾和val文件夾,里面分別保存著訓(xùn)練集的照片和txt格式的標(biāo)簽,還有驗(yàn)證集的照片和txt格式的標(biāo)簽。images文件夾和labels文件夾就是訓(xùn)練yolov5模型所需的訓(xùn)練集和驗(yàn)證集。在VOCdevkit/VOC2007目錄下還生成了一個(gè)YOLOLabels文件夾,里面存放著所有的txt格式的標(biāo)簽文件。
? ? ? ? 至此,xml格式的標(biāo)簽文件轉(zhuǎn)換為txt格式的標(biāo)簽文件并劃分為訓(xùn)練集和測(cè)試集就講完了。
2? 標(biāo)簽為yolo格式數(shù)據(jù)集劃分訓(xùn)練集和驗(yàn)證集
????????由于yolov5訓(xùn)練需要的數(shù)據(jù)標(biāo)簽格式為txt格式,所以大家在利用labelimg標(biāo)注的時(shí)候會(huì)用yolo格式(標(biāo)注生成的標(biāo)簽為txt格式)。標(biāo)注好的數(shù)據(jù)集訓(xùn)練的時(shí)候就要?jiǎng)澐譃橛?xùn)練集和驗(yàn)證集,因此就需要有劃分為訓(xùn)練集和測(cè)試集的代碼。這里需要講的是我寫的腳本代碼可以成功將數(shù)據(jù)集劃分為訓(xùn)練集和驗(yàn)證集,但是在訓(xùn)練模型的時(shí)候,加載數(shù)據(jù)集一直會(huì)出現(xiàn)問(wèn)題。因此我就想到了,先把txt格式的數(shù)據(jù)集替換成xml格式的數(shù)據(jù)集,然后再按上述將xml格式標(biāo)簽轉(zhuǎn)化為txt格式標(biāo)簽并劃分為訓(xùn)練集和驗(yàn)證集的方法劃分就好了。但是這里建議大家以后標(biāo)注的時(shí)候就標(biāo)注為voc格式(xml格式),因?yàn)樵摳袷降臉?biāo)簽里面有圖片標(biāo)注的具體內(nèi)容,例如標(biāo)注類別,圖片大小,標(biāo)注坐標(biāo)。但是yolo格式(txt格式)里面是用數(shù)字來(lái)代表類別,這樣很不直觀,而且標(biāo)注的坐標(biāo)也是經(jīng)過(guò)轉(zhuǎn)化歸一化的,坐標(biāo)信息更加不直觀。先上yolo轉(zhuǎn)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文件保存路徑,圖片所在文件夾路徑"""此函數(shù)用于將yolo格式txt標(biāo)注文件轉(zhuǎn)換為voc格式xml標(biāo)注文件在自己的標(biāo)注圖片文件夾下建三個(gè)子文件夾,分別命名為picture、txt、xml"""dic = {'0': "hat", # 創(chuàng)建字典用來(lái)對(duì)類型進(jìn)行轉(zhuǎn)換'1': "person", # 此處的字典要與自己的classes.txt文件中的類對(duì)應(yīng),且順序要一致}files = os.listdir(txtPath)for i, name in enumerate(files):xmlBuilder = Document()annotation = xmlBuilder.createElement("annotation") # 創(chuàng)建annotation標(biāo)簽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標(biāo)簽foldercontent = xmlBuilder.createTextNode("driving_annotation_dataset")folder.appendChild(foldercontent)annotation.appendChild(folder) # folder標(biāo)簽結(jié)束filename = xmlBuilder.createElement("filename") # filename標(biāo)簽filenamecontent = xmlBuilder.createTextNode(name[0:-4] + ".jpg")filename.appendChild(filenamecontent)annotation.appendChild(filename) # filename標(biāo)簽結(jié)束size = xmlBuilder.createElement("size") # size標(biāo)簽width = xmlBuilder.createElement("width") # size子標(biāo)簽widthwidthcontent = xmlBuilder.createTextNode(str(Pwidth))width.appendChild(widthcontent)size.appendChild(width) # size子標(biāo)簽width結(jié)束height = xmlBuilder.createElement("height") # size子標(biāo)簽heightheightcontent = xmlBuilder.createTextNode(str(Pheight))height.appendChild(heightcontent)size.appendChild(height) # size子標(biāo)簽height結(jié)束depth = xmlBuilder.createElement("depth") # size子標(biāo)簽depthdepthcontent = xmlBuilder.createTextNode(str(Pdepth))depth.appendChild(depthcontent)size.appendChild(depth) # size子標(biāo)簽depth結(jié)束annotation.appendChild(size) # size標(biāo)簽結(jié)束for j in txtList:oneline = j.strip().split(" ")object = xmlBuilder.createElement("object") # object 標(biāo)簽picname = xmlBuilder.createElement("name") # name標(biāo)簽namecontent = xmlBuilder.createTextNode(dic[oneline[0]])picname.appendChild(namecontent)object.appendChild(picname) # name標(biāo)簽結(jié)束pose = xmlBuilder.createElement("pose") # pose標(biāo)簽posecontent = xmlBuilder.createTextNode("Unspecified")pose.appendChild(posecontent)object.appendChild(pose) # pose標(biāo)簽結(jié)束truncated = xmlBuilder.createElement("truncated") # truncated標(biāo)簽truncatedContent = xmlBuilder.createTextNode("0")truncated.appendChild(truncatedContent)object.appendChild(truncated) # truncated標(biāo)簽結(jié)束difficult = xmlBuilder.createElement("difficult") # difficult標(biāo)簽difficultcontent = xmlBuilder.createTextNode("0")difficult.appendChild(difficultcontent)object.appendChild(difficult) # difficult標(biāo)簽結(jié)束bndbox = xmlBuilder.createElement("bndbox") # bndbox標(biāo)簽xmin = xmlBuilder.createElement("xmin") # xmin標(biāo)簽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標(biāo)簽結(jié)束ymin = xmlBuilder.createElement("ymin") # ymin標(biāo)簽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標(biāo)簽結(jié)束xmax = xmlBuilder.createElement("xmax") # xmax標(biāo)簽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標(biāo)簽結(jié)束ymax = xmlBuilder.createElement("ymax") # ymax標(biāo)簽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標(biāo)簽結(jié)束object.appendChild(bndbox) # bndbox標(biāo)簽結(jié)束annotation.appendChild(object) # object標(biāo)簽結(jié)束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)????????首先講一下數(shù)據(jù)的格式,要嚴(yán)格按照下圖的目錄結(jié)構(gòu)來(lái)。為了后續(xù)數(shù)據(jù)集的劃分做出統(tǒng)一數(shù)據(jù)集目錄結(jié)構(gòu)。且數(shù)據(jù)目錄要和代碼在同一目錄下,這樣就可以一鍵運(yùn)行了。
?? ?JPEGImages為圖片數(shù)據(jù)所在的目錄
 ?? ?YOLO為yolo格式的標(biāo)簽數(shù)據(jù)所在目錄
 ?? ?Annotations為生成的voc格式數(shù)據(jù)標(biāo)簽?zāi)夸?#xff08;程序運(yùn)行前這是一個(gè)空目錄)
如下圖這里要對(duì)應(yīng)好,且順序要一致,例如我這里是0對(duì)應(yīng)hat,1對(duì)應(yīng)person。
? ? ? ? 運(yùn)行如上的代碼,就可以將yolo格式的標(biāo)簽轉(zhuǎn)化為voc格式,并保存在 ?Annotations目錄中,最后可以按照上述1的方法,將voc轉(zhuǎn)為yolo再劃分?jǐn)?shù)據(jù)集就可以了。
? ? ? ? 至此yolo格式數(shù)據(jù)集劃分訓(xùn)練集和驗(yàn)證集就結(jié)束了
?
總結(jié)
以上是生活随笔為你收集整理的目标检测---数据集格式转化及训练集和验证集划分的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
                            
                        - 上一篇: 科学家计算机模拟宇宙,科学家利用计算机模
 - 下一篇: 贝叶斯网络应用在疾病预测