用Mask-RCNN训练自定义大小的数据集

         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[i].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[i],
                           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处理,代码可以参见之前的博客:

https://blog.csdn.net/yql_617540298/article/details/81154145

猜你喜欢

转载自blog.csdn.net/yql_617540298/article/details/81782685