这里提供以下windows下CPU版本环境配置的教程:CPU-mmdetection
注:Ubuntu系统下的环境配置大同小异,甚至会简单点
pip3 install torch==1.9.1+cu111 torchvision==0.10.1+cu111 torchaudio===0.9.1 -f https://download.pytorch.org/whl/torch_stable.html
可能会出现pip失败的情况,大概率是网络不稳定造成的,
pytorch下载
如果上面因为网络原因pip失败,可以去上面链接下载对应的库包,本文的库链接如下:
下载下来执行:
package_location:下载包的绝对地址
pip3 install {package_location}
上述CUDA TOOlkit默认安装完毕之后,将cuDNN解压出来文件夹里的许多文件复制到如下目录即可:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1
open-mmlab-mmdetection开源库
将上述库clone下来
MMDetection中文安装指南
pip install mmcv-full=={1.3.9} -f https://download.openmmlab.com/mmcv/dist/cu111/torch1.9.1/index.html
下载上述代码指定的权重:
faster-rcnn权重下载链接
from mmdet.apis import init_detector, inference_detector
config_file = '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)
# 推理演示图像
inference_detector(model, 'demo/demo.jpg')
复制上述代码到工程根目录运行,如果程序exit code为0,则环境配置完成
或者运行如下代码:
from mmdet.apis import init_detector, inference_detector
import mmcv
# 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 = './demo/demo.jpg' # or img = mmcv.imread(img), which will only load it once
result = inference_detector(model, img)
# visualize the results in a new window
model.show_result(img, result)
# or save the visualization results to image files
model.show_result(img, result, out_file='result.jpg')