Make your own VOC dataset

       It's really been a long time since I wrote something. I'm doing machine learning recently. I want to leave something as a summary of my research during this period of time. It would be better if it could help others.

        This article mainly produces its own training set for object detection, taking the VOC dataset as an example.

 

        1. Background

            To make your own VOC dataset, you must first understand the directory structure of the VOC dataset. It mainly contains the following directories: Annotations, ImageSets, JPEGImages, SegmentationClass and SegmentationObject several directories. Our main focus is on the Annotations, ImageSets and JPEGImages directories.

          Annotations: store the coordinates of the detected object on the picture

          JPEGImages: store training and validation images

          ImageSets: There are three subfolders under the file, Layout, Main and Segmentation, among which we focus on the Main folder and it is OK

       2. Steps

          2.1 : First, put the training image in the JPEGImages folder, the image name is recommended to be named 0000001.jpg

          2.2: Second, generate the xml file of the object coordinates in the picture in the Annotations folder. The name of the xml file is the same as the name of the picture. In this step, it is recommended to use the software LabelImage provided by Daniel. The operation method of the software is very simple and will not be introduced here. When saving, save the xml file to the Annotations folder

         3.3: Third, after the long coordinate labeling, the four files (test.txt, train.txt, val.txt, trainval.txt) in the Main folder can be generated. Here you can write relevant scripts to Generated and can also be found online. I post one here.

 

import os
import random

trainval_percent = 0.8
train_percent = 0.7
xmlfilepath = 'Annotations'
txtsavepath = 'ImageSets\Main'
total_xml = os.listdir(xmlfilepath)

num=len(total_xml)
list=range(num)
tv=int(num*trainval_percent)
tr=int(tv*train_percent)
trainval= random.sample(list,tv)
train=random.sample(trainval,tr)

ftrainval = open('ImageSets/Main/trainval.txt', 'w')
ftest = open('ImageSets/Main/test.txt', 'w')
ftrain = open('ImageSets/Main/train.txt', 'w')
fval = open('ImageSets/Main/val.txt', 'w')

for i in list:
name=total_xml[i][:-4]+'\n'
if i in trainval:
ftrainval.write(name)
if i in train:
ftrain.write(name)
else:
fval.write(name)
else:
ftest.write(name)

ftrainval.close()
ftrain.close()
fval.close()
ftest .close()

       3: Conclusion

          After the above steps, you will generate your own VOC data set, and you can proceed to the next training.

 

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326196414&siteId=291194637