在目标检测方法研究中,数据集的格式至关重要,为了减小模型的训练时长,需要现在小数据集进行模型研究,从而需要将VOC数据集转化为coco格式。下面一步一步来看:
1. 下载VOC数据集
Pascal VOC,即Pattern Analysis, Statical Modeling and Computational Learning Visual Object Classes。详细可参考:http://host.robots.ox.ac.uk/pascal/VOC/
以及论文:http://host.robots.ox.ac.uk/pascal/VOC/pubs/everingham10.pdf
数据集下载,在linux系统用以下命令直接下载即可:
# VOC 2007
# 训练验证集
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
# 测试集
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar
# 开发工具包代码和文档
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCdevkit_08-Jun-2007.tar
# VOC 2012
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2012/VOCtrainval_11-May-2012.tar
解压方式可参考我之前的文章,进行批量一键解压:https://blog.csdn.net/qq_33854260/article/details/121861169?spm=1001.2014.3001.5502
2. 生成coco格式的标签文件:
这里参考github的代码:https://github.com/KKKSQJ/DeepLearning/blob/master/others/label_convert/voc2coco.py
注意这里需设置voc的根目录,如:
voc_data_dir = './VOC_2007/VOCtrainval_06-Nov-2007/VOCdevkit/VOC2007'
json_save_path = './VOC_2007_COCO/voc2007_train.json'
split = 'train' # train val test
parseXmlFilse(data_dir=voc_data_dir, json_save_path=json_save_path, split=split)
运行一次,可在目标文件夹得到对应的标签json文件;修改参数,再次运行,从而分别得到train,val, test的json文件。
3. 配置文件目录:
先来看coco文件目录:
annotations:存放标签文件,即生成的json文件,需要创建相同的文件,并拷贝过来:
创建数据图片文件夹,并把voc对应的数据拷贝过来。然而,VOC的训练和测试图片都放在一起。提前创建好目标文件夹,之后用我撰写的以下脚本来划分,脚本代码如下:
import os
import shutil
voc_train_val_img = "./voc_coco/voc2007/JPEGImages"
train_txt = "./voc_coco/VOCtrainval_06-Nov-2007/VOCdevkit/VOC2007/ImageSets/Main/train.txt"
val_txt = "./voc_coco/VOCtrainval_06-Nov-2007/VOCdevkit/VOC2007/ImageSets/Main/val.txt"
voc_coco_train_target_dir = "./voc_coco/voc2007/train2007"
voc_coco_val_target_dir = "./voc_coco/voc2007/val2007"
with open(train_txt, "r") as f:
train = f.read()
tp = train.split("\n")
for j in range(len(tp)):
file_path = os.path.join(voc_train_val_img, tp[j]+".jpg")
tar_path = os.path.join(voc_coco_train_target_dir, tp[j]+".jpg")
if os.path.exists(file_path):
shutil.move(file_path, tar_path)
else:
print("Non-exist file", file_path)
print("Train data splited!...")
with open(val_txt, "r") as f:
train = f.read()
tp = train.split("\n")
for j in range(len(tp)):
file_path = os.path.join(voc_train_val_img, tp[j]+".jpg")
tar_path = os.path.join(voc_coco_val_target_dir, tp[j]+".jpg")
if os.path.exists(file_path):
shutil.move(file_path, tar_path)
else:
print("Non-exist file", file_path)
print("Val data splited!...")
转换完成后的结果:
希望能对大家有帮助~~~~