这几天为了在maskrcnn_benchmark中训练模型,需要把自己的数据集(VOC)转为COCO数据集的格式,过程中遇到了很多问题。在此记录一下整个过程。
1.下载COCOAPI,Matlab安装gcc,因为我是在windows下转换的,所以需要自己安装gcc,听说好像Linux下转换不需要安装;
2.matlab中设置工作路径:
3.进入Visual Object Classes Challenge 2007(VOC2007)
4.把VOCcode放入VOCdevkit
并根据自己的数据集修改Init.c
5.进行转换:
mex(‘COMPFLAGS=$COMPFLAGS -std=c++11 -Wall’,’-largeArrayDims’,…
‘private/gasonMex.cpp’,’…/common/gason.cpp’,…
‘-I…/common/’,’-outdir’,‘private’);
遇到的问题:
出现这个问题是因为我的数据集的标注信息不太正确,虽然在跑模型的时候没有报错,但是Matlab对数据的标注有更严格的要求。我的XML如下:
需要把空格换为/t ,因此写了一个脚本来进行替换:
import os
OLD_DIR = 'Annotations'
NEW_DIR = 'Annotations2'
if not os.path.exists(NEW_DIR):
os.mkdir(NEW_DIR)
for filename in os.listdir(OLD_DIR):
with open(os.path.join(OLD_DIR, filename), 'r', encoding='utf-8') as file:
lines = file.readlines()
lines[0] = '<annotation>\n'
for i in range(len(lines)):
lines[i] = lines[i].replace(' ', '\t')
with open(os.path.join(NEW_DIR, filename), 'w') as file:
file.write(''.join(lines))