最近需要复现一篇基于mmdetection实现的实例分割模型《PolarMask》,之前博主在五月份的时候看的一篇论文《CenterPoint》也是基于mmdetection3d的。在前几天面试时,被质问“做检测的居然不用mmdetection???”,这些都揭示了mmdetection等一系列code base的便利之处,那就来学一下吧!
COCO数据集我这个实验室里没有学长学姐在玩,感受到了学部计算资源的匮乏。四张v100跑15个小时才能训完PolarMask,距离论文复现报告提交还有一个礼拜的时间,祝我好运吧。
环境配置
主要是参考官方文档,出问题了bing一下,看看大家是怎么解决的。
- mmdetection 官方文档https://mmdetection.readthedocs.io/en/latest/model_zoo.html
博主这里的配置是这样的:
driver CUDA
11.4 (为了用向日葵助手远程连接实验室电脑,把显示管理器从gnome换成lightdm了,结果驱动自己莫名其妙的升到了11.4)nvidia-driver version
470.57 (显卡驱动啦~)runtime CUDA
10.2 (这个是你自行安装的cuda,官方文档中说driver CUDA和runtime CUDA最好版本保持一致,上网查了一下,其实driverCUDA大于等于runtimeCUDA就可以啦,如果后期出什么问题了,我再来重装吧~)pytorch
1.9.0ubuntu
18.04gcc
7.5.0mmcv-full
1.3.8mmdetection
master 2.14.0(mmcv-full和你用的mmdetection版本要匹配,文档中都给出了~)python
3.7
其他的环境配置就不再赘述了,可以参考我之前的博客,把CUDA、cuDNN、pytorch配好了之后,完成mmcv-full和mmdetection的配置就可以了。
Demo inference
根据官方文档测试Faster RCNN,把模型的权重文件下载下来,放在mmdetection目录下的checkpoints文件夹内(没有的话自己新建一个)。
可以查看mmdet的api文档,在这里它的show_result函数变成了show_result_pyplot,博主查了一下文档,然后用新的接口就好啦。
mmdetection接口文档https://mmdetection.readthedocs.io/en/latest/api.html
from mmdet.apis import init_detector, inference_detector, show_result_pyplot
# Specify the path to model config and checkpoint file
config_file = './configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
checkpoint_file = './checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
# build the model from a config file and a checkpoint file
model = init_detector(config_file, checkpoint_file, device='cuda:0')
# test a single image and show the results
img = 'test.jpg' # or img = mmcv.imread(img), which will only load it once
result = inference_detector(model, img)
show_result_pyplot(model, img, result, score_thr=0.8)
# or save the visualization results to image files
# model.show_result(img, result, out_file='result.jpg')
阈值设了0.8~结果如下,强迫症看了都说好。