一、 创建虚拟环境
这里创建python3.7环境
二、安装torch及cudatoolkit
进入torch官网,找到需要的torch及cuda(torch官网已经搭配并打包一起,后续不用设置环境变量,激活环境直接用即可)
找到以前版本,这里安装torch1.8.0和cuda11.1版本(后续安装mmcv需要对应)
复制这条命令,cmd中激活刚才的虚拟环境(cmd中输入activate mmdetection),输入下面代码进行安装conda install pytorch==1.8.0 torchvision==0.9.0 torchaudio==0.8.0 cudatoolkit=11.1 -c pytorch -c conda-forge
安装完成后输入conda list可以看到安装成功
三、安装mmcv
这里需要注意,需要将mmcv安装在对应的torch中,打开mmcv开源链接GitHub - open-mmlab/mmcv: OpenMMLab Computer Vision Foundation,找到torch对应的mmcv命令
打开复制命令,并删除=={mmcv_version},在虚拟环境中运行安装,
这里安装命令为:
pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu111/torch1.8.0/index.html
安装成功后如下:
四、安装MMdetection
打开mmdetection开源链接https://github.com/open-mmlab/mmdetection下载project
五、安装其他依赖
下载解压后,cmd到mmdetection-master目录下激活虚拟环境并输入下列命令:
pip install -r requirements/build.txt
python setup.py develop
安装成功如下:
六、测试安装是否成功
pycharm打开mmdetection-master工程,
然后加载解释器,
在mmdetection-master文件夹下创建checkpoints文件夹,下载预训练权重文件faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth,放在checkpoints文件夹里面,权重链接:
在mmdetection-master文件夹下创建一个py文件,粘贴如下代码并运行此文件:
from mmdet.apis import init_detector, inference_detector
config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
# download the checkpoint from model zoo and put it in `checkpoints/`
# url: https://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'
# init a detector
model = init_detector(config_file, checkpoint_file, device=device)
# inference the demo image
result = inference_detector(model, 'demo/demo.jpg')
model.show_result('demo/demo.jpg', result, out_file='initial_demo.jpg')
运行成功: