mmdetection入门介绍-前言

mmdetection入门介绍

一、前沿

open-mmlab工程项目可以在github上下载,具体链接为mmdetection,下载完成后,就可以获得整个工程项目,这里需要说明一下,参考官方安装环境:

  • Linux (Windows is not officially supported)
  • Python 3.5+ (Python 2 is not supported)
  • PyTorch 1.1 or higher
  • CUDA 9.0 or higher
  • NCCL 2
  • GCC(G++) 4.9 or higher
  • mmcv

官方已经在一下系统测试过,暂时没有其他问题

  • OS: Ubuntu 16.04/18.04 and CentOS 7.2
  • CUDA: 9.0/9.2/10.0
  • NCCL:
  • 2.1.15/2.2.13/2.3.7/2.4.2
  • GCC(G++): 4.9/5.3/5.4/7.3

这里容易报错的就是CUDA,有的人下在的是最新版本的CUDA 10.1/10.2,然后一跑就报错,尽量在推荐的系统和软件上执行,减少没必要的麻烦,其中mmcv需要单独安装,可以简单的通过一个pip安装命令

pip install mmcv
或者
git clone https://github.com/open-mmlab/mmcv.git
cd mmcv
pip install -e .

其他依赖要求

  • mmcv>=0.2.10
  • numpy
  • matplotlib
  • six
  • terminaltables
  • pycocotools
  • torch>=1.1
  • torchvision
  • imagecorruptions
  • albumentations>=0.3.2

如果基本环境,比如Pytorch,torchvision,conda(可以选择不安装),python 3的环境装好了,就可以直接运行了

二、demo演示

这里官方给了一个demo.py,可以用来检测图片或者视频,具体内容如下

from mmdet.apis import init_detector, inference_detector, show_result
import mmcv

config_file = 'configs/faster_rcnn_r50_fpn_1x.py'
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.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 = 'pic/000177.jpg' # or img = mmcv.imread(img), which will only load it once
result = inference_detector(model, img)
# visualize the results in a new window
show_result(img, result, model.CLASSES)
# or save the visualization results to image files
#show_result(img, result, model.CLASSES, out_file='result.jpg')

# test a video and show the results
video = mmcv.VideoReader('a.mp4')
for frame in video:
result = inference_detector(model, frame)
show_result(frame, result, model.CLASSES, wait_time=1)

这里需要下载官方给的训练好的模型,然后根据你自己设定的路径,放在其中,上面中,需要新建一个checkpoints文件夹并把模型放在下面,具体下载的模型需要同config/下的配置文件里面相对应,这里下载的是faster_rcnn_r50_fpn_1x的模型,找到对应的configs/faster_rcnn_r50_fpn_1x.py文件,倒入配置就可以了,其他的模型,如果需要测试的话,官方是有给下载链接,具体在对应文件夹的docs/MODEL_ZOO.md文件里面。

然后mmdetection对于事实检测也是可以做到的,官方也有给测试代码,你也可以通过调用摄像头,或者外接摄像头,甚至一个简单的USB的一个摄像头,具体调用如下

python demo/webcam_demo.py ${CONFIG_FILE} ${CHECKPOINT_FILE} [--device ${GPU_ID}] [--camera-id ${CAMERA-ID}] [--score-thr ${SCORE_THR}]

具体的比如

python demo/webcam_demo.py configs/faster_rcnn_r50_fpn_1x.py checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth

数据集测试
mmdetection支持以下模式

  • 单GPU
  • 多GPU
  • 可视化检测结果

拿到训练模型之后就可以简单的测试一下,也可以拿自己训练好的模型拿来测试

# single-gpu testing
python tools/test.py ${CONFIG_FILE} ${CHECKPOINT_FILE} [--out ${RESULT_FILE}] [--eval ${EVAL_METRICS}] [--show]

# multi-gpu testing
./tools/dist_test.sh ${CONFIG_FILE} ${CHECKPOINT_FILE} ${GPU_NUM} [--out ${RESULT_FILE}] [--eval ${EVAL_METRICS}]

参数解释:

  • RESULT_FILE:输出结果的文件名采用pickle格式。如果未指定,结果将不会保存到文件中。
  • EVAL_METRICS:评估结果的选项。可使用的值包括:proposal_fast,proposal,bbox,segm,keypoints。
  • –show:如果初始化,检测结果将绘制在图像上并显示在新窗口中。它仅适用于单个GPU测试。请确保环境中可以使用GUI,否则您可能会遇到类似“无法连接到服务器”的错误。

参考样例
这里假设你已经下载好已经训练好的checkpoints文件并放在checkpoints/目录下
1.测试faster-rcnn并显示结果

python tools/test.py configs/faster_rcnn_r50_fpn_1x.py checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth --show

2.测试mask-rcnn并评估bbox和mask AP

python tools/test.py configs/mask_rcnn_r50_fpn_1x.py checkpoints/mask_rcnn_r50_fpn_1x_20181010-069fa190.pth --out results.pkl --eval bbox segm

3.测试8个GPU下的mask-rcnn并评估bbox和mask AP

./tools/dist_test.sh configs/mask_rcnn_r50_fpn_1x.py checkpoints/mask_rcnn_r50_fpn_1x_20181010-069fa190.pth 8 --out results.pkl --eval bbox segm

其他参考:
这里是mmdetection入门介绍 前言 部分
这里是mmdetection入门介绍 test.py解析 部分
这里是mmdetection入门介绍 train.py解析 部分
这里是mmdetection入门介绍 模型解析 部分

发布了80 篇原创文章 · 获赞 86 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/klaus_x/article/details/103831820