[In mmdetection, use YOLOv3 to train and test your own data set—convert voc format to coco format]

Convert the dataset in voc format to coco format dataset

I recently learned deep learning in mmdetection, and recorded the problems and solutions I encountered.
Before, I used faster rcnn to train and test on my own voc format data set, and the effect was average. Now I plan to switch to yolov3 for training.
The data set format used by the yolov3 model in mmdetection is the coco data set format, so the data format conversion must be performed first. Most of the other tutorials on the Internet are not complete, so I sort it out by myself.
coco
As shown above, it is the format of the coco dataset.
Among them, train2017, val2017, and test2017 are respectively placed with pictures for training, verification, and testing.
annotations contains instances_train.json, instances_val.json and instances_test.json files. .json contains "images", "annotations", "type", "categories" and other information. "images" stores the name width and height of each image and the image id; "annotations" stores the four coordinate positions of the image box corresponding to the same image id and the category id of the box; "categories" indicates that each category id belongs to the category Correspondence to real names.
————————————————
Original link: https://blog.csdn.net/qq_45584615/article/details/115802378

1.voc_to_coco

For reference:
voc_to_coco https://github.com/Stephenfang51/VOC_to_COCO

According to the prompt, after modifying the number and path of the three types of img, the following errors were found, and the original text did not give an answer.
Permission denied
This error should be an operation permission problem. After modifying the folder permission to no avail, change the relative path of "voc_annotations" in the original program to an absolute path to solve this problem.

2. Divide the picture into three folders

However, after running the above program, it was found that only three .json files under the annotations folder, images folder (all imgs) and three xml folders (which are used for train, val, and test respectively) were generated as required. The xml file of the picture), and the folders for the three types of img pictures used for train, val and test are not generated. So I wrote a program myself to split the images folder according to the xml file name (or file_name in the .json file). `
code

#本程序用于img文件分割,根据train、val以及test的xml文件目录生成对应的jpg文件夹  2022/02/19 phil
import os
import cv2
xml='/database/phy/code/mmdetection/data/VOCdevkit/xml/'  #train、val、test三个xml文件夹的根目录
xmltrain_path=os.path.join(xml,'xml_train/')
xmlval_path=os.path.join(xml,'xml_val/')
xmltest_path=os.path.join(xml,'xml_test/')
img_path='/database/phy/code/mmdetection/data/VOCdevkit/VOC2007_COCO/images/' #所有img的目录
#目标目录,包括三类img的文件夹
splitimg_path='/database/phy/code/mmdetection/data/VOCdevkit/VOC2007_COCO/splitimages/'
os.mkdir(splitimg_path)   #创建新目录
print(xmltrain_path)
print(xmlval_path)
print(xmltest_path)
traindirs=os.listdir(xmltrain_path)
valdirs=os.listdir(xmlval_path)
testdirs=os.listdir(xmltest_path)
print(traindirs)
print(valdirs)
print(testdirs)
trainimg=os.path.join(splitimg_path,'train2017/')
valimg=os.path.join(splitimg_path,'val2017/')
testimg=os.path.join(splitimg_path,'test2017/')
os.mkdir(trainimg)
os.mkdir(valimg)
os.mkdir(testimg)
for i in os.listdir(img_path):
    ixml=i[0:-3]+'xml'
    if ixml in traindirs:
        img=cv2.imread(os.path.join(img_path,i))
        cv2.imwrite(os.path.join(trainimg,i),img)
    elif ixml in valdirs:
        img=cv2.imread(os.path.join(img_path,i))
        cv2.imwrite(os.path.join(valimg,i),img)
    elif ixml in testdirs:
        img=cv2.imread(os.path.join(img_path,i))
        cv2.imwrite(os.path.join(testimg,i),img)

After simply modifying the path and file name, three corresponding folders can be generated, namely train2017, val2017 and test2017, which contain img images for three types of purposes. Add the three .json files in the previously generated annotations folder, and the data set in coco format is ready. Pro test is available.
So far, the voc format dataset has been successfully converted to a coco format dataset! It's a bit of a hassle, but it's good enough. Next, you can train and test yolov3 on this coco dataset.

Reference:
[1]: https://blog.csdn.net/qq_45584615/article/details/115802378?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2 aggregatepage first_rank_ecpm_v1~rank_v31_ecpm-1-115802378.pc_agg_new_rank&utm_term= mmdetection+ yolov3%E8%AE%AD%E7%BB%83&spm=1000.2123.3001.4430

Guess you like

Origin blog.csdn.net/weixin_46600060/article/details/123030436