制作VOC数据集并生成tf训练文件

制作VOC数据集

1、VOC2007下载地址:https://pjreddie.com/projects/pascal-voc-dataset-mirror/
2、仿照其结构在项目中构造如下文件夹:
/mnt/text_detection/dataset/VOCdevkit/VOC2007
在这里插入图片描述

  • JPEGImages中存放.jpg图片
  • Annotations中存放标记好的.xml文件
  • ImageSets中创建Main文件夹

3、运行 set.py ,其代码如下:

import os  
import random  
  
trainval_percent = 0.5  
train_percent = 0.5  
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(txtsavepath+'/trainval.txt', 'w')  
ftest = open(txtsavepath+'/test.txt', 'w')  
ftrain = open(txtsavepath+'/train.txt', 'w')  
fval = open(txtsavepath+'/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()

此时,ImageSets下的Main文件夹中生成4个.txt文件
在这里插入图片描述

制作tf训练文件

1、tensorflow models下载地址:https://github.com/tensorflow/models
2、将其中的 research/object_detection 放到与tensorflow同级的目录下
3、将 object_detection/data/pascal_label_map.pbtxt 复制到 /mnt/text_detection/dataset/ 下并修改为自己所需的标签
4、修改 object_detection/dataset_tools/create_pascal_tf_record.py

#49行改为自己的 .pbtxt文件的地址
flags.DEFINE_string('label_map_path', '/mnt/text_detection/dataset/pascal_label_map.pbtxt',
                    'Path to label map proto')
#164行修改如下
examples_path = os.path.join(data_dir, year, 'ImageSets', 'Main',
                                 FLAGS.set + '.txt')

5、在 object_detection 的上一级目录,命令行运行

protoc object_detection/protos/*.proto --python_out=.

6、在 /mnt/text_detection/dataset/VOCdevkit 文件夹下新建train data文件夹,并将JPEGImages文件夹复制进去(不然会报错,找不到文件)
7、在 object_detection 下运行

python dataset_tools/create_pascal_tf_record.py --data_dir=/mnt/text_detection/dataset/VOCdevkit --year=VOC2007 --set=train --output_path=/mnt/text_detection/dataset/pascal_train.record
python dataset_tools/create_pascal_tf_record.py --data_dir=/mnt/text_detection/dataset/VOCdevkit --year=VOC2007 --set=val --output_path=/mnt/text_detection/dataset/pascal_val.record

此时, /mnt/text_detection/dataset 下生成需要的 pascal_train.record 和 pascal_val.record 文件

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值