1.创建新环境并激活
查看pytorch与Python对应版本关系:
conda create -n mmdetection python==3.8
activate mmdetection
2.查看cuda和cudnn版本号
在cmd中输入指令: cuda版本:10.1
nvcc --version
进入cuda安装目录查看include文件夹中的cudnn.h文件,cudnn版本:7.6.5
3.安装pytorch
到pytorch官网Previous PyTorch Versions | PyTorch寻找适合cuda版本的pytorch下载命令
安装的pytorch版本为:1.7.1,在激活的虚拟环境中输入下载命令
pip install torch==1.7.1+cu101 torchvision==0.8.2+cu101 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
4.安装 mmcv-full
参考链接:mmdetection/docs/zh_cn/get_started.md at master · open-mmlab/mmdetection · GitHub
pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/{cu_version}/{torch_version}/index.html
需要把命令行中的 {cu_version}
和 {torch_version}
替换成对应的版本。
本次安装命令为:
pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu101/torch1.7.1/index.html
未指定mmcv-full版本,默认安装最新版本,报错
尝试安装低版本,1.5.1,仍然报错
pip install mmcv-full==1.5.1 -f https://download.openmmlab.com/mmcv/dist/cu101/torch1.7.1/index.html
根据网上方法,下载Microsoft C++ Build Tools,未解决
尝试使用mim安装,安装成功,版本为1.7.1,为最新版本
pip install openmim
mim install mmcv-full
5.安装MMDetection
mmdetection和mmcv的版本要注意对应,查看版本关系:mmdetection/docs/zh_cn/get_started.md at master · open-mmlab/mmdetection · GitHub
因为安装的最新版本mmcv-full,直接安装最新版本的mmdetection即可,操作如下:
git clone https://github.com/open-mmlab/mmdetection.git
报错:因为没有安装git
解决办法:从git官网下载windows版本的git:http://git-scm.com/downloads,并进行安装,
然后将bin和git-core文件路径添加到环境变量,然后重启prompt,激活虚拟环境运行git代码
或者直接到github上下载mmdetection包:https://github.com/open-mmlab/mmdetection/tag
下一步:依次运行以下命令
cd mmdetection
pip install -r requirements/build.txt
pip install -v -e . # or "python setup.py develop"
又报错:
解决办法:(1)下载pycocotools压缩包,密码:i5d7:pycocotools2.0.2.rar_免费高速下载|百度网盘-分享无限制 (baidu.com)
解压后,将压缩包中的两个文件夹放置到当前环境的路径下的Lib文件夹中的site-packages路径下。
后续验证发现无法import pycocotools._mask,会报错No module named 'pycocotools._mask'
删掉了这两个文件。
(2)直接单独下载pycocotools,解决。
将下载的pycocotools包备份了一份,需要可下载后将压缩包中的两个文件夹放置到当前环境的路径下的Lib文件夹中的site-packages路径下。
链接:https://pan.baidu.com/s/14XqfFogeeohy-U5TBxhkRA?pwd=c4p2
提取码:c4p2
pip install pycocotools
注:在pip 之前,我根据报错给的网址,在Microsoft C++ 生成工具 - Visual Studio下载了miscrosoft C++生成工具
双击运行安装了vs2022,如果直接pip不成功可以尝试
安装完pycocotools再运行一遍代码,成功
6.验证mmdetection是否安装成功
(1)在mmdetection目录下新建一个checkpoints目录,下载FasterRcnn的权重到这个文件夹中,下载的地址是:http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
(2)在mmdetection目录下新建一个test.py文件,并运行(Python解释器选择建好的虚拟环境)
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')
运行报错:
解决:pip install mmengine
重新运行,又报错:
尝试更换mmcv版本,但是后续又会出现其他报错,然后还将VS2022给卸载了
pip install mmcv==2.0.0rc4 -f https://download.openmmlab.com/mmcv/dist/cu101/torch1.7/index.html
解决:猜测可能是最新版mmdetection版本不兼容,换回原来的mmcv版本,然后到github: https://github.com/open-mmlab/mmdetection/tag上下载mmdetection包(2.26.0版)并解压,然后cd到文件位置,重新运行一遍命令,之前安装了VS2022后下载的pycocotools没有被卸载,因此很顺利,未报错。
cd /d H:\installation\python_download\mmdetection-2.26.0
pip install -r requirements/build.txt
pip install -v -e .
重复检验步骤,未报错
检验方法二:在已激活的配置好mmdetection的虚拟环境中,先进入到mmdetection2.26.0目录下,再执行以下代码
python demo/image_demo.py demo/demo.jpg configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
结果如下:
2. mmdetection/docs/zh_cn/get_started.md at master · open-mmlab/mmdetection · GitHub