MMDect 快速上手#

参考 安装教程 安装 mmdet

  1. 安装 Torch 相关包:

    conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
    
  2. 安装 mmdet

    pip install mmdet
    

下面验证安装。

首先,下载模型:

!wget http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
!mkdir checkpoints
!mv faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
--2022-06-10 12:16:34--  http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
Resolving download.openmmlab.com (download.openmmlab.com)... 47.97.233.89
Connecting to download.openmmlab.com (download.openmmlab.com)|47.97.233.89|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 167287506 (160M) [application/octet-stream]
Saving to: ‘faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth’

faster_rcnn_r50_fpn 100%[===================>] 159.54M  3.34MB/s    in 57s     

2022-06-10 12:17:30 (2.82 MB/s) - ‘faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth’ saved [167287506/167287506]

验证:

from mmdet.apis import init_detector, inference_detector

root = "/media/pc/data/4tb/lxw/books/mmdetection"
config_file = f"{root}/configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py"
# 从 model zoo 下载 checkpoint 并放在 `checkpoints/` 文件下
# 网址为: http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
checkpoint_file = "checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth"
device = "cuda:0"
# 初始化检测器
model = init_detector(config_file, checkpoint_file, device=device)
# 推理演示图像
result = inference_detector(model, f"{root}/demo/demo.jpg")
load checkpoint from local path: checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth