摘要: maskrcnn主要是做实例分割的,maskrcnn是在faster rcnn基础上改进的,原理可以参考我关于faster rcnn的原理解读FasterRcnn原理理解记录
1. 安装
直接参考官方项目facebookresearch/maskrcnn-benchmark
2. 官方demo测试
用官方例子和预训练模型进行测试。或者从百度网盘,密码:5qwy,下载预训练模型进行测试
from maskrcnn_benchmark.config import cfg
from predictor import COCODemo
config_file = "../configs/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml"
# update the config options with the config file
cfg.merge_from_file(config_file)
# manual override some options
cfg.merge_from_list(["MODEL.DEVICE", "cpu"])
coco_demo = COCODemo(
cfg,
min_image_size=800,
confidence_threshold=0.7,
)
# load image and then run prediction
image = ...
predictions = coco_demo.run_on_opencv_image(image)
3. 训练自己的数据集(实例分割)
网上很多例子,但是大部分没有提供数据集,这里我标注了14张数据,用于测试。下载链接,密码:5qwy.
操作流程具体参考这篇博文:
maskrcnn-benchmark训练自己数据集用于视觉分割
按照以上流程是没有问题的。
4. 测试自己的模型
预训练模型下载:链接,密码:5qwy
代码部分:
from maskrcnn_benchmark.config import cfg
from predictor import COCODemo
import cv2
import matplotlib.pyplot as plt
from PIL import Image
import requests
from io import BytesIO
import numpy as np
config_file = "./configs/caffe2/e2e_mask_rcnn_R_50_FPN_1x_caffe2.yaml"
config_file = "./configs/e2e_mask_rcnn_R_51_C4_1x_2.yaml"
# update the config options with the config file
cfg.merge_from_file(config_file)
# manual override some options
cfg.merge_from_list(["MODEL.DEVICE", "cpu"]) # 用CPU
cfg.merge_from_list(["MODEL.DEVICE", "cuda"]) # 用GPU
coco_demo = COCODemo(
cfg,
min_image_size=800,
confidence_threshold=0.7,
)
def imshow(img):
plt.imshow(img[:, :, [2, 1, 0]])
plt.axis("off")
plt.show()
# image = load("http://farm3.staticflickr.com/2469/3915380994_2e611b1779_z.jpg")
image = Image.open("/home/yangna/deepblue/OCR_D/maskrcnn-benchmark/datasets/coco2/train2014/000.jpg").convert("RGB")
image = np.array(image)[:, :, [2, 1, 0]]
imshow(image)
predictions = coco_demo.run_on_opencv_image(image)
imshow(predictions)
5. 思考
其实本身是想做旋转矩形的检测,考虑用mask回归的最大外接矩形。但是maskrcnn—benchmark本身没有提供旋转的数据增强。