概述
Mask-RCNN,是一个处于像素级别的目标检测手段.目标检测的发展主要历程大概是:RCNN,Fast-RCNN,Fster-RCNN,Darknet,YOLO,YOLOv2,YOLO3(参考目标检测:keras-yolo3之制作VOC数据集训练指南),Mask-RCNN.本文参考的论文来源于https://arxiv.org/abs/1703.06870.
下面,开始制作用于Mask训练的数据集。
首先展示一下成果,由于个人设备有限,cpu仅迭代5次的结果。
使用labelme进行图片标注
注意:
**标注之前将图片的名字通过linux或者python脚本改名,改为有序即可,我的命名格式为升序,下面为linux脚本。
i=1; for x in *; do mv $x $i.png; let i=i+1; done
**将所有图片的尺寸改为600*800.(一般设置为2的整数次幂,否则,后序训练时会报错).脚本自取https://github.com/hyhouyong/Mask-RCNN/blob/master/train_data/resize.py
pip install labelme
labelme
1.新建文件夹train_data,并创建子文件夹json,将标注后的json格式的文件放入该文件夹中
2.当你安装lableme的时候,默认安装到了Anaconda目录下/envs/名字/Scripts/下,使用labelme_json_to_dataset.exe将json文件转化为5个文件
转化方法,切换到labelme安装目录下,执行:
labelme_json_to_dataset.exe [文件名]
注意:文件名为绝对路径 . eg:(chineseocr) D:\anaconda\envs\chineseocr\Scripts>labelme_json_to_dataset.exe F:\samples\shapes\train_data\json\1.json
***这样只能一次转化一个json文件,故开始批量转。
切换到D:\anaconda\envs\py3.6\Lib\site-packages\labelme\cli下,修改json_to_dataset.py,然后切换到Scripts,执行命令:
labelme_json_to_dataset.exe [存放json文件夹的绝对路径]
***生成的json文件夹会在当前目录,将文件夹拷贝到train_data下的labelme_json文件夹中
import argparse
import json
import os
import os.path as osp
import warnings
import PIL.Image
import yaml
from labelme import utils
import base64
def main():
warnings.warn("This script is aimed to demonstrate how to convert the\n"
"JSON file to a single image dataset, and not to handle\n"
"multiple JSON files to generate a real-use dataset.")
parser = argparse.ArgumentParser()
parser.add_argument('json_file')
parser.add_argument('-o', '--out', default=None)
args = parser.parse_args()
json_file = args.json_file
if args.out is None:
out_dir = osp.basename(json_file).replace('.', '_')
out_dir = osp.join(osp.dirname(json_file), out_dir)
else:
out_dir = args.out
if not osp.exists(out_dir):
os.mkdir(out_dir)
count = os.listdir(json_file)
for i in range(0, len(count)):
path = os.path.join(json_file, count[i])
if os.path.isfile(path):
data = json.load(open(path))
if data['imageData']:
imageData = data['imageData']
else:
imagePath = os.path.join(os.path.dirname(path), data['imagePath'])
with open(imagePath, 'rb') as f:
imageData = f.read()
imageData = base64.b64encode(imageData).decode('utf-8')
img = utils.img_b64_to_arr(imageData)
label_name_to_value = {'_background_': 0}
for shape in data['shapes']:
label_name = shape['label']
if label_name in label_name_to_value:
label_value = label_name_to_value[label_name]
else:
label_value = len(label_name_to_value)
label_name_to_value[label_name] = label_value
# label_values must be dense
label_values, label_names = [], []
for ln, lv in sorted(label_name_to_value.items(), key=lambda x: x[1]):
label_values.append(lv)
label_names.append(ln)
assert label_values == list(range(len(label_values)))
lbl = utils.shapes_to_label(img.shape, data['shapes'], label_name_to_value)
captions = ['{}: {}'.format(lv, ln)
for ln, lv in label_name_to_value.items()]
lbl_viz = utils.draw_label(lbl, img, captions)
out_dir = osp.basename(count[i]).replace('.', '_')
out_dir = osp.join(osp.dirname(count[i]), out_dir)
if not osp.exists(out_dir):
os.mkdir(out_dir)
PIL.Image.fromarray(img).save(osp.join(out_dir, 'img.png'))
#PIL.Image.fromarray(lbl).save(osp.join(out_dir, 'label.png'))
utils.lblsave(osp.join(out_dir, 'label.png'), lbl)
PIL.Image.fromarray(lbl_viz).save(osp.join(out_dir, 'label_viz.png'))
with open(osp.join(out_dir, 'label_names.txt'), 'w') as f:
for lbl_name in label_names:
f.write(lbl_name + '\n')
warnings.warn('info.yaml is being replaced by label_names.txt')
info = dict(label_names=label_names)
with open(osp.join(out_dir, 'info.yaml'), 'w') as f:
yaml.safe_dump(info, f, default_flow_style=False)
print('Saved to: %s' % out_dir)
if __name__ == '__main__':
main()
3.生成Mask文件,由于labelme生成的掩码标签 label.png为16位存储,opencv默认读取8位,需要将16位转8位
脚本自取https://github.com/hyhouyong/Mask-RCNN/blob/master/train_data/uint16_to_uint8.py,
4.最后生成的文件夹结构如下:
开始训练:
1.安装环境
pip install -r requirements.txt
2.下载预训练模型mask_rcnn_coco.h5
百度云链接:https://pan.baidu.com/s/1CmcfVleyw7QpVZRo3JxS2w 提取码:tf7f
3.执行命令:
python train_shape.py
开始测试:
1.将想要测试的图片放入imges文件夹中
2.执行命令:
python test_shape.py
详细代码见:我的github自取。欢迎Fork和Star并交流
来源:oschina
链接:https://my.oschina.net/u/4385242/blog/4524411