源码链接:https://github.com/matterport/Mask_RCNN
1、数据集制作
由于模型采用的是coco数据的数据的格式,coco数据包括训练图片和标注信息json文件,标注信息json文件包括很多字段信息,如果完全按照这个格式来做感觉较为复杂,但如果可以用标注工具能直接生成的话也还可以。
1.1深度学习图像标注工具VGG Image Annotator (VIA)
网上查询确实有可以直接标注的工具,这个直接在网页端使用好像挺不错,可以自由添加字段,适合coco数据的格式,但是我操作一通后感觉没有labelme好用,因此舍弃。
参考:https://blog.csdn.net/heiheiya/article/details/81530952
1.2Labelme
首先使用工具,直接保存在同目录下得到对应json文件。
labelme保存的都是xxx.json文件,需要用labelme_json_to_dataset.exe转换一下,在命令行下定位到D:\anaconda3\Scripts中labelme_json_to_dataset.exe。
for /r 数据位置 %i in (*.json) do labelme_json_to_dataset %i
这样会生成一个同名文件夹,里面包含了我们需要的mask文件,label.png,不过这个文件是16bit点,而cv2中使用的都是8bit点,所以需要转换一下。
参考:https://blog.csdn.net/qq_36810544/article/details/83582397
def img_16to8():
from PIL import Image
import numpy as np
import shutil
import os
src_dir = r'E:\code\Tongue_detect\train_data\labelme_json'
dest_dir = r'E:\code\Tongue_detect\train_data\cv2_mask'
for child_dir in os.listdir(src_dir):
new_name = child_dir.split('_')[0] + '.png'
old_mask = os.path.join(os.path.join(src_dir, child_dir), 'label.png')
img = Image.open(old_mask)
img = Image.fromarray(np.uint8(np.array(img)))
new_mask = os.path.join(dest_dir, new_name)
img.save(new_mask)
或者直接运行json_to_dataset.py也行。
最终得到目录如图所示。
2、模型结构
在sample中有很多对应的案例,包括训练检测。直接在里面拷贝修改。
如图直接运行tongue.py训练检测,详见代码https://github.com/wliuxing123/Mask-RCNN_lx。
参考:https://blog.csdn.net/qq_36810544/article/details/83582397