YOLO data set processing train own data set __ data

Source: Internet
Author: User
Tags cos save file sin
1. Data sets

Only trained a class of hand.
First we need to process our dataset to get a data set similar to VOC:

where Jpegimages,labels name do not change Oh, the program can not find the very trouble

Find_replace (Path, "Dout", "labels", Labelpath);
Find_replace (Labelpath, "jpegimages", "labels", Labelpath);
Find_replace (Labelpath, ". jpg", "txt", labelpath);
Find_replace (Labelpath, ". JPEG ",". txt ", labelpath);

Here we only need three folders:
annotations--Store XML files
jpegimages--inside is a picture
labels--for storing tags generated by XML files
remind everyone to note the number of files generated in the folder, as well as TXT file number of rows to avoid training errors

Below we need to generate three TXT file for storing picture name (note not picture address): Train.txt,val.txt,trainval.txt

Import OS from OS import Listdir, getcwd from Os.path import join if __name__ = = ' __main__ ': source_folder= '/home/papa /downloads/gesture/jpegimages/' #地址是所有图片的保存地点 #dest = '/home/papa/downloads/gesture/train.txt ' #保存train. txt address #dest 2= '/home/papa/downloads/gesture/val.txt ' #保存val. txt address dest3= '/home/papa/downloads/gesture/trainval.txt ' #                 Save Trainval.txt Address File_list=os.listdir (source_folder) #赋值图片所在文件夹的文件列表 #train_file =open (dest, ' a ') #打开文件 #val_file =open (dest2, ' a ') #打开文件 Trainval_file=open (Dest3, ' a ') #打开文件 F Ile_num=0 for File_obj in file_list: #访问文件列表中的每一个文件 file_path=os.path.join (source_folder,file_o BJ) #file_path保存每一个文件的完整路径 File_name,file_extend=os.path.splitext (file_obj) #file_name Save the name of the file, fi
            Le_extend Save File extension if (file_num<12949): #and file_num%4!=0): #前面是你的图片数目, followed by a retention file for training #print File_num TRAinval_file.write (file_name+ ' \ n ') #train_file. Write (file_name+ ' \ n ') #用于训练前900个的图片路径保存在train. txt, ending with carriage return and line wrapping #else: #val_file. Write (file_name+ ' \ n ') #其余的文件保存在val. txt inside file_num+=1 #train_file. Close () #关闭文件 #val_file. Close () Trainval_file.close ()

This is the end of the run:

below to generate the label

I'm here for the longest time, at the end I'll say my question, we need to modify the official build label file voc_label.py

If your XML file is in this format, you can modify it according to the following comments

Import xml.etree.ElementTree as ET import pickle import os from OS import Listdir, getcwd from Os.path import join Classe s = ["aeroplane"] #对应里面的name, modify #这个函数是voc自己的不用修改 according to the actual situation, call Def convert (size, box) in the following function: DW = 1./size[0] DH = 1./ SIZE[1] x = (Box[0] + box[1])/2.0 y = (box[2] + box[3])/2.0 w = box[1]-box[0] h = box[3]-box[2] X = X*dw W = w*dw y = y*dh h = h*dh return (x,y,w,h) #生成标签函数, extract useful information from an XML file write TXT file def convert_annotation (i MAGE_ID): in_file = open (' vocdevkit/voc/annotations/%s.xml '% (image_id)) #Annotations文件夹地址 out_file = open (' Vocdevk It/voc/labels/%s.txt '% (image_id), ' W ') #labels文件夹地址 Tree=et.parse (in_file) root = tree.getroot () size = Root.f IND (' size ') w = Int (size.find (' width '). Text) h = Int (size.find (' height '). Text) for obj in Root.iter (' object ')
        : Difficult = obj.find (' difficult '). Text CLS = obj.find (' name '). Text cls_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") #主函数, from the previously generated train.txt/val.txt/trainval.txt gets the filename loop image_ids = open (' Trainval.txt ' (Image_set)). Re AD (). Strip (). Split () #之前生成的train. txt/val.txt/trainval.txt Address #list_file = open (' Infrared_train.txt ', ' W ') You want the picture path txt save address for image_id in Image_ids: #list_file. Write ('/vocdevkit/voc/jpegimage/%s.jpg\n '% (image_id)) # Your actual picture path, which is directly written to the program must be written to the Convert_annotation (image_id) #list_file. Close ()

Note that when you use Trainval.txt to run the function convert_annotation (image_id) Other times, you need to run this program three times, respectively, for Train.txt/val.txt/trainval.txt

At this time we will have a label generated, the format and VOC in the same label ~

It will also generate Infrared_train.txt,infrared_vol.txt

Complete.. 2. Modify the document

Cfg/voc.data


Modify backup based on address to store weights

Cfg/yolo_voc.cfg

At the end of the file, the slashes need to be modified, classes changed to 1,,filters value to be modified (classes+ coords+ 1) * (NUM), in my case: (1+4+1) * 5=30, I changed the filters value to 30. Data/gesture.names
Only one line, hand 3. Training

./darknet Detector Train Cfg/gesture.data cfg/yolo-gesture.2.0.cfg darknet19_448.conv.23 | Tee Gesture.txt

You can also evaluate the results ~ Refer to my previous article: http://blog.csdn.net/shangpapa3/article/details/76687191 4. My question

My dataset is in this format, the box is a rotated rectangle, and the build tag program is modified as follows to turn it into the dataset I need.

Import xml.etree.ElementTree as ET import pickle import os import math from OS import listdir, getcwd from Os.path Import Join Def convert (size, box): #voc_label. PY has its own function, not modifying DW = 1./size[0] DH = 1./size[1] x = (Box[0] + box[1])/2.
    0 y = (box[2] + box[3])/2.0 w = box[1]-box[0] h = box[3]-box[2] x = x*dw W = w*dw y = Y*dh H = h*dh return (X,Y,W,H) def convert_annotation (image_id): in_file = open ('/home/papa/downloads/gesture/annotat Ions/%s.xml '% (image_id)) Out_file = open ('/home/papa/downloads/gesture/labels2/%s.txt '% (image_id), ' W ') # This XML corresponds to the converted txt, this txt save complete path tree = Et.parse (in_file) hand = Tree.find ("Hand") W = 1920 h = 1080 Cls_ id = 0 Point1 = hand.attrib[' Leftup '].split (",") Point2 = hand.attrib[' Rightup '].split (",") Point3 = Hand.att rib[' Rightdown '].split (",") Point4 = hand.attrib[' Leftdown '].split (",") x1 = float (point1[0)) x2 = Float (point 2[0]) x3 = float (point3[0]) x4 = Float (point4[0]) y1 = float (point1[1]) y2 = float (point2[1)) Y3 = float (point3[1)) Y4 = float (point4[1) A = float (hand.attrib[' rad ']) x0 = (x3-x1) * 0.5 + x1 y0 = (y1-y3) * 0.5 + y3 Xmax = (x2-x0) * MA Th.cos (a) + (y2-y0) * Math.sin (a) + x0 ymax = (y2-y0) * Math.Cos (a)-(x2-x0) * Math.sin (a) + y0 xmin = (x4

    -x0) * Math.Cos (a) + (y4-y0) * Math.sin (a) + x0 ymin = (y4-y0) * Math.Cos (a)-(x4-x0) * Math.sin (a) + y0 b = (xmin, xmax, ymin, ymax) BB = Convert ((w,h), b) #print (BB) out_file.write (str (cls_id) + "" + "". Join ([St R (a) for a in BB]) + ' \ n ') #image_ids = open ('/home/papa/downloads/gesture/imagesets/main/val.txt '). Read (). Strip (). Split () Image_ids = open ('/home/papa/downloads/gesture/imagesets/main/trainval.txt '). Read (). strip (). Split () #list_ File = open ('/home/papa/downloads/gesture/infrared_val.txt ', ' W ') for image_id in Image_ids: #list_file. Write ('/home/p apa/downloads/gesture/jpegimages/%s.jpg\n '% (image_id)) convert_annotation (image_id) #list_file. Close () 

Let class equals 0, and you enter the length of the data, and the coordinates are processed to get the required xmin. There is a need to refer to. Reference:

Http://blog.csdn.net/hysteric314/article/details/54097845#comments

Thanks to the author.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.