项目源码:https://github.com/facebookresearch/maskrcnn-benchmark
包含faster rcnn、mask rcnn等等框架的库,非常强大。自己摸索了一下如何用自己定义的Dataset来训练。
参考资料:
介绍了一些核心py文件的作用:https://hellozhaozheng.github.io/z_post/PyTorch-MaskrcnnBenchmark-tools/
使用pytorch mask-rcnn进行目标检测/分割训练 - Slumbers的文章 - 知乎 https://zhuanlan.zhihu.com/p/57603975(最后介绍了关于重设学习率的问题)
使用自己的数据集训练maskrcnn需要做的几步(持续更新ing)
1. 配置环境:
参考项目里面给的步骤安装一下必要的包,具体步骤见下图:
https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/INSTALL.md
配置一下很重要,据网上资料说如果maskrcnn_benchmark文件夹里的内容有更改的话还需要重新生成一下!
配置好了之后可以检测测试一下,直接写在项目第一级文件夹里就可以,例如下面demo.py(demo文件夹里需要加一个__init__.py文件,内容为空就可以,不然会报错):
from maskrcnn_benchmark.config import cfg
from demo.predictor import COCODemo
from skimage import io
import cv2
config_file = "./configs/caffe2/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 = io.imread('test.jpg')
predictions = coco_demo.run_on_opencv_image(image)
cv2.imshow("detect",predictions)
cv2.waitKey(0)
cv2.destroyAllWindows()
成功的话就可以看到网络输出的结果啦!
2. 写自己的Dataset
这里项目里面也给出了示例,形式类似下面这种:
from maskrcnn_benchmark.structures.bounding_box import BoxList
class MyDataset(object):
def __init__(self, ...):
# as you would do normally
def __getitem__(self, idx):
# load the image as a PIL Image
image = ...
# load the bounding boxes as a list of list of boxes
# in this case, for illustrative purposes, we use
# x1, y1, x2, y2 order.
boxes = [[0, 0, 10, 10], [10, 20, 50, 50]]
# and labels
labels =