本文提到的代码在最后都会提供
labelme 安装
pip install labelme
labelme 使用
转换图片的大小
这里我使用的模型是:ssd_resnet50_v1_fpn_640x640_coco17_tpu-8,所以我们将图片更改到640*640的尺寸。resize_images.py
python resize_images.py -d images/ -s 640 640
转换成coco数据集格式
将labelme打好标签的文件生成两个文件夹:train,test 分别生成。labelme2coco.py
python labelme2coco.py train --output train.json
python labelme2coco.py test --output test.json
转成TFRecord格式
我们需要TFRecord格式的文件用于在Tensorflow中训练模型。models项目里提供了create_coco_tf_record.py 文件,但是在我使用的过程中出现了一些小问题,所以我使用了网络上提供的经过更改的文件。
python create_coco_tf_record.py
--logtostderr
--train_image_dir=images/train
--test_image_dir=images/test
--train_annotations_file=images/train.json
--test_annotations_file=images/test.json
--include_masks=True
--output_dir=./
Labelmap准备
train.json
这是我们生成的coco类型的Json文件,我们写的Labelmap要和他的id匹配,注意:Labelme的Id是categories的id+1.
{
"images": [...],
"categories": [
{
"supercategory": "iphone",
"id": 0,
"name": "iphone"
},
{
"supercategory": "keyboard",
"id": 1,
"name": "keyboard"
},
{
"supercategory": "markCup",
"id": 2,
"name": "markCup"
}
],
"annotations": [...]
labelmap:
我自己这个文件叫做:coco_format.pbtxt。
item {
id: 1
name: 'iphone'
}
item {
id: 2
name: 'keyboard'
}
item {
id: 3
name: 'markCup'
}
至此数据准备工作结束。
参考:
https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/training.html#preparing-the-dataset
https://gilberttanner.com/blog/train-a-mask-r-cnn-model-with-the-tensorflow-object-detection-api
上面提到的代码同时提供CSDN下载地址