用Mask-RCNN训练自定义大小的数据集
一、目前情况
用Mask-RCNN训练自己的数据集时,需要制定图片的长度和宽度,即
IMAGE_MIN_DIM = 448
IMAGE_MAX_DIM = 640
而在Mask_RCNN/mrcnn目录下model.py文件中第1815行到1819行代码
h, w = config.IMAGE_SHAPE[:2]
if h / 2**6 != int(h / 2**6) or w / 2**6 != int(w / 2**6):
raise Exception("Image size must be dividable by 2 at least 6 times "
"to avoid fractions when downscaling and upscaling."
"For example, use 256, 320, 384, 448, 512, ... etc. ")
需要将图像处理成指定长宽比例的图像然后才可以用于训练,并且训练集中的图像需要长度和宽度都需一致。
若训练集中的图像有长度和宽度不同时则不能训练,这样极不方便。
二、更改Mask-RCNN代码
在训练数据集的代码train_shapes.ipynb中,在load_shapes()中添加更改代码:
for i in range(count):
# 获取图片宽和高
filestr = imglist.split(".")[0]
mask_path = mask_floder + "/" + filestr + ".png"
yaml_path = dataset_root_path + "total/" + filestr + "_json/info.yaml"
print(dataset_root_path + "total/" + filestr + "_json/img.png")
cv_img = cv2.imread(dataset_root_path + "total/" + filestr + "_json/img.png")
self.add_image("shapes", image_id=i, path=img_floder + "/" + imglist,
width=cv_img.shape[1], height=cv_img.shape[0],
mask_path=mask_path, yaml_path=yaml_path)
在代码中
width=cv_img.shape[1], height=cv_img.shape[0]
便是自动获取图像的长度和宽度。
三、处理图像
在train_shapes.ipynb代码中可以自动获取图片的长度和宽度,但是若需要将长度和宽度大小不一致的图像分别规范到一样大小,则可用python处理,代码可以参见之前的博客:
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)