当前环境版本:
- Python 3.8.6 (anaconda 虚拟环境)
- PyTorch 1.8.1
- CUDA 11.1
- VS 2019 (需要在path设置环境变量)
- MMCV 1.3.5
- mmdetection 2.13.0
- mmsegmentation 0.13.0
- git (需要用到时,通过anaconda 安装即可)
以管理员身份打开anaconda powershell,否者有一些命令可能无法执行!!
环境搭建:
1.安装VS2019 社区版 (具体步骤跳过,记得安装c++环境)
2.安装anaconda (具体步骤跳过,傻瓜式安装即可,最好别设置系统路径,否则容易与系统python路径冲突)
3.安装cuda
下载cuda, cuda下载地址
根据自己的配置选择合适的型号
4.配置cudnn
下载cudnn,下载网址
注意:下载过程需要登陆,没有注册的话注册一个即可
将下载文件解压,将cuda文件夹里卖弄的文件夹直接覆盖到cuda安装目录下即可,一般默认路径类似于:
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.1
5. 安装pytorch
从开始菜单以管理员打开Anaconda powershell ,首先设置虚拟环境并激活,之后安装的python软件都是通过此窗口进行:
conda create -n open-mmlab python=3.8 -y
conda activate open-mmlab
然后根据官方指导,选择自己的配置
输入命令安装:
conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c pytorch -c conda-forge
安装 mmcv
安装新的mmcv(1.3.5),以下直接摘抄自官方教程:
你需要知道如何设置环境变量,接下来的过程强烈需要此技能
设置python环境
-
Prepare MMCV source code
git clone https://github.com/open-mmlab/mmcv.git cd mmcv
-
Install required Python packages
pip3 install -r requirements.txt
构建和安装 MMCV
通用步骤
-
Set up MSVC compiler
将
C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.27.29110\bin\Hostx86\x64
添加到环境变量PATH
, 这样cl.exe
可以在控制台中所有路径中被调用。(base) PS C:\Users\xxx> cl Microsoft (R) C/C++ Optimizing Compiler Version 19.27.29111 for x64 Copyright (C) Microsoft Corporation. All rights reserved. usage: cl [ option... ] filename... [ / link linkoption... ]
为了兼容性, 我们使用 x86架构宿主的 x64目标编译. 注意在路径中选择
Hostx86\x64
.
Option 3: Build MMCV (full version with CUDA)
-
通过
ls env:
命令,确定CUDA_PATH
orCUDA_HOME
已经在envs
中, 输出应如下:
一般来说cuda安装时,路径已经被添加到系统。 如果没有, 或者有多个不同版本cuda, 需要在控制台中制定
CUDA_HOME
路径如下所示(根据需要使用的版本):$env:CUDA_HOME = "C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.2" # OR $env:CUDA_HOME = $env:CUDA_PATH_V10_2 # if CUDA_PATH_V10_2 is in envs:
-
设置 CUDA 架构
# Suppose you are using 1660s, which is of capability 7.5 $env:TORCH_CUDA_ARCH_LIST="7.5"
根据此文章查看自己的显卡算力,我查到的是7.5,我的这个数值在官网上查不到。
- 进行安装,步骤如下,依次输入如下指令
$env:MMCV_WITH_OPS = 1
$env:MAX_JOBS = 8 # based on available number of CPU cores and amount of memory
# build
python setup.py build_ext # if success, cl will be launched to compile ops
# install
python setup.py develop
# check
pip list
如果采用
注意: 如果使用 PyTorch 1.6.0, 可能会遇到一些错误this issue. 需要根据 this pull request 修改pytorch的安装源码.
由于我的pytorch是1.8.1的版本,并未遇到此问题。安装完成后在安装列表里可以看到mmcv-full
安装mmdetection
1.准备和安装
这里当前安装的版本为2.13.0,参考教程
由于前面安装mmcv已经将大部分组件安装完成,然后直接安装mmdetection即可:
cd ..
git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
pip install -r requirements/build.txt
pip install -v -e . # or "python setup.py develop"
执行以上语句,很容易就安装完成了
2.验证
通过以下代码验证:
from mmdet.apis import init_detector, inference_detector, show_result_pyplot
config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
# download the checkpoint from model zoo and put it in `checkpoints/`
# url: 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'
# init a detector
model = init_detector(config_file, checkpoint_file, device=device)
# inference the demo image
result = inference_detector(model, 'demo/demo.jpg')
# show the results
show_result_pyplot(model, 'demo/demo.jpg', result)
首先,下载预训练模型到checkpoints。运行结果如下:
注意: 在我在另一台电脑上运行此测试代码的时候出现了这个问题:
该问题是因为多重引用libiomp5md.dll,我通过everything软件查找 libiomp5md.dll的位置,将pytorch中的libiomp5md.dll删除了,然后就可以正常运行了,不知道后面是否还会出现其他问题,因此可以先将该文件备份一下。
安装mmsegmentation
此过程完全参考官方指导:
首先可以看到,我们采用的当前最新版本0.13.0,需要mmcv-full>=1.3.1, 1.4版本当前还没出。
因为在安装mmcv的时候环境都已经铺垫好了,我们可以直接进入mmsegmentation的安装。
1.mmsegmentation安装:
cd ..
git clone https://github.com/open-mmlab/mmsegmentation.git
cd mmsegmentation
pip install -e . # or "python setup.py develop"
2.验证
很快就安装好了,然后我们来测试一下官方指导中Verification的代码是否可以运行:
首先下载预训练模型,放在项目下的checkpoints目录下,然后执行代码:
from mmseg.apis import inference_segmentor, init_segmentor
import mmcv
config_file = 'configs/pspnet/pspnet_r50-d8_512x1024_40k_cityscapes.py'
checkpoint_file = 'checkpoints/pspnet_r50-d8_512x1024_40k_cityscapes_20200605_003338-2966598c.pth'
# build the model from a config file and a checkpoint file
model = init_segmentor(config_file, checkpoint_file, device='cuda:0')
# test a single image and show the results
img = 'demo/demo.png' # or img = mmcv.imread(img), which will only load it once
result = inference_segmentor(model, img)
# visualize the results in a new window
model.show_result(img, result, show=True)
# or save the visualization results to image files
# you can change the opacity of the painted segmentation map in (0, 1].
model.show_result(img, result, out_file='result.jpg', opacity=0.5)