硬件需求:
win10系统,pycharm,NIVIDA显卡以及下载好的Cuda
源码地址:https://github.com/facebookresearch/maskrcnn-benchmark
文章目录
前言
打开命令提示符,输入命令nvcc --version,查看显卡配置:
C:\WINDOWS\system32>nvcc --version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:04_Central_Daylight_Time_2018
Cuda compilation tools, release 10.0, V10.0.130
我的cuda安装的是10.0版本,在后续可以进行参考。
提示:以下是本篇文章正文内容,下面案例可供参考
一、创建虚拟环境
输入命令,新建一共名为maskrcnn_pytorch的虚拟环境
conda create -n maskrcnn_pytorch python=3.7 pip
出现如下提示,选择y
## Package Plan ##
environment location: D:\Anaconda\program\envs\maskrcnn_pytorch
added / updated specs:
- pip
- python=3.7
The following packages will be downloaded:
package | build
---------------------------|-----------------
certifi-2021.5.30 | py37haa95532_0 142 KB https://mirrors.ustc.edu.cn/anaconda/pkgs/main
pip-21.1.3 | py37haa95532_0 2.0 MB https://mirrors.ustc.edu.cn/anaconda/pkgs/main
python-3.7.11 | h6244533_0 17.4 MB https://mirrors.ustc.edu.cn/anaconda/pkgs/main
sqlite-3.36.0 | h2bbff1b_0 1.2 MB https://mirrors.ustc.edu.cn/anaconda/pkgs/main
------------------------------------------------------------
Total: 20.7 MB
The following NEW packages will be INSTALLED:
ca-certificates anaconda/pkgs/main/win-64::ca-certificates-2021.7.5-haa95532_1
certifi anaconda/pkgs/main/win-64::certifi-2021.5.30-py37haa95532_0
openssl anaconda/pkgs/main/win-64::openssl-1.1.1k-h2bbff1b_0
pip anaconda/pkgs/main/win-64::pip-21.1.3-py37haa95532_0
python anaconda/pkgs/main/win-64::python-3.7.11-h6244533_0
setuptools anaconda/pkgs/main/win-64::setuptools-52.0.0-py37haa95532_0
sqlite anaconda/pkgs/main/win-64::sqlite-3.36.0-h2bbff1b_0
vc anaconda/pkgs/main/win-64::vc-14.2-h21ff451_1
vs2015_runtime anaconda/pkgs/main/win-64::vs2015_runtime-14.27.29016-h5e58377_2
wheel anaconda/pkgs/main/noarch::wheel-0.36.2-pyhd3eb1b0_0
wincertstore anaconda/pkgs/main/win-64::wincertstore-0.2-py37_0
Proceed ([y]/n)?
如果使用国内镜像的话,大约30s安装完毕,显示
done
#
# To activate this environment, use
#
# $ conda activate maskrcnn_pytorch
#
# To deactivate an active environment, use
#
# $ conda deactivate
进入虚拟环境
conda activate maskrcnn_pytorch
出现以下的字样:
(maskrcnn_pytorch) C:\WINDOWS\system32>
虚拟环境创建完毕
二、安装对应库
1.pytorch
首先安装pytorch,pytorch需要与自己的cuda相对应,我的cuda是10.0版本,同时,Mask R-CNN环境要求pytorch在1.0以上,在pytorch官网找到对应的版本
官网地址:
https://pytorch.org/get-started/previous-versions/
我们选择在线安装
# CUDA 9.0
conda install pytorch==1.0.1 torchvision==0.2.2 cudatoolkit=9.0 -c pytorch
# CUDA 10.0
conda install pytorch==1.0.1 torchvision==0.2.2 cudatoolkit=10.0 -c pytorch
# CPU Only
conda install pytorch-cpu==1.0.1 torchvision-cpu==0.2.2 cpuonly -c pytorch
选择对应的命令,出现:
## Package Plan ##
environment location: D:\Anaconda\program\envs\maskrcnn_pytorch
added / updated specs:
- cudatoolkit=10.0
- pytorch==1.0.1
- torchvision==0.2.2
The following packages will be downloaded:
package | build
---------------------------|-----------------
cudatoolkit-10.0.130 | 0 371.0 MB https://mirrors.ustc.edu.cn/anaconda/pkgs/main
pytorch-1.0.1 |py3.7_cuda100_cudnn7_1 464.9 MB pytorch
torchvision-0.2.2 | py_3 44 KB pytorch
------------------------------------------------------------
Total: 835.9 MB
The following NEW packages will be INSTALLED:
blas anaconda/pkgs/main/win-64::blas-1.0-mkl
cffi anaconda/pkgs/main/win-64::cffi-1.14.6-py37h2bbff1b_0
cudatoolkit anaconda/pkgs/main/win-64::cudatoolkit-10.0.130-0
freetype anaconda/pkgs/main/win-64::freetype-2.10.4-hd328e21_0
intel-openmp anaconda/pkgs/main/win-64::intel-openmp-2021.3.0-haa95532_3372
jpeg anaconda/pkgs/main/win-64::jpeg-9b-hb83a4c4_2
libpng anaconda/pkgs/main/win-64::libpng-1.6.37-h2a8f88b_0
libtiff anaconda/pkgs/main/win-64::libtiff-4.2.0-hd0e1b90_0
lz4-c anaconda/pkgs/main/win-64::lz4-c-1.9.3-h2bbff1b_0
mkl anaconda/pkgs/main/win-64::mkl-2021.3.0-haa95532_524
mkl-service anaconda/pkgs/main/win-64::mkl-service-2.4.0-py37h2bbff1b_0
mkl_fft anaconda/pkgs/main/win-64::mkl_fft-1.3.0-py37h277e83a_2
mkl_random anaconda/pkgs/main/win-64::mkl_random-1.2.2-py37hf11a4ad_0
ninja anaconda/pkgs/main/win-64::ninja-1.10.2-h6d14046_1
numpy anaconda/pkgs/main/win-64::numpy-1.20.3-py37ha4e8547_0
numpy-base anaconda/pkgs/main/win-64::numpy-base-1.20.3-py37hc2deb75_0
olefile anaconda/pkgs/main/win-64::olefile-0.46-py37_0
pillow anaconda/pkgs/main/win-64::pillow-8.3.1-py37h4fa10fc_0
pycparser anaconda/pkgs/main/noarch::pycparser-2.20-py_2
pytorch pytorch/win-64::pytorch-1.0.1-py3.7_cuda100_cudnn7_1
six anaconda/pkgs/main/noarch::six-1.16.0-pyhd3eb1b0_0
tk anaconda/pkgs/main/win-64::tk-8.6.10-he774522_0
torchvision pytorch/noarch::torchvision-0.2.2-py_3
xz anaconda/pkgs/main/win-64::xz-5.2.5-h62dcd97_0
zlib anaconda/pkgs/main/win-64::zlib-1.2.11-h62dcd97_4
zstd anaconda/pkgs/main/win-64::zstd-1.4.9-h19a0ad4_0
Proceed ([y]/n)?
选择y,这一过程大约等待5至10分钟,pytorch安装完毕。
2.ipython等其余安装包
安装ipython
conda install ipython pip
选择y
## Package Plan ##
environment location: D:\Anaconda\program\envs\maskrcnn_pytorch
added / updated specs:
- ipython
- pip
The following NEW packages will be INSTALLED:
backcall anaconda/pkgs/main/noarch::backcall-0.2.0-pyhd3eb1b0_0
colorama anaconda/pkgs/main/noarch::colorama-0.4.4-pyhd3eb1b0_0
decorator anaconda/pkgs/main/noarch::decorator-5.0.9-pyhd3eb1b0_0
ipython anaconda/pkgs/main/win-64::ipython-7.22.0-py37hd4e2768_0
ipython_genutils anaconda/pkgs/main/noarch::ipython_genutils-0.2.0-pyhd3eb1b0_1
jedi anaconda/pkgs/main/win-64::jedi-0.17.0-py37_0
parso anaconda/pkgs/main/noarch::parso-0.8.2-pyhd3eb1b0_0
pickleshare anaconda/pkgs/main/noarch::pickleshare-0.7.5-pyhd3eb1b0_1003
prompt-toolkit anaconda/pkgs/main/noarch::prompt-toolkit-3.0.17-pyh06a4308_0
pygments anaconda/pkgs/main/noarch::pygments-2.9.0-pyhd3eb1b0_0
traitlets anaconda/pkgs/main/noarch::traitlets-5.0.5-pyhd3eb1b0_0
wcwidth anaconda/pkgs/main/noarch::wcwidth-0.2.5-py_0
Proceed ([y]/n)?
根据requestment.txt安装其余的库
pip install ninja yacs cython matplotlib tqdm opencv-python
结果显示
Collecting ninja
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/2c/f1/30316c8ec18ef9837c1c957379110eef28f9d9d08837bd6643066bdcc329/ninja-1.10.2-py2.py3-none-win_amd64.whl (837 kB)
Collecting yacs
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/38/4f/fe9a4d472aa867878ce3bb7efb16654c5d63672b86dc0e6e953a67018433/yacs-0.1.8-py3-none-any.whl (14 kB)
Collecting cython
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/4f/1c/d5b7b91f99044bf84da41059912f2a38ce8db9c9b65bfa50e3cf8afa07c5/Cython-0.29.24-cp37-cp37m-win_amd64.whl (1.6 MB)
Requirement already satisfied: matplotlib in c:\users\guojinyue\appdata\roaming\python\python37\site-packages (3.0.2)
Collecting tqdm
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/0b/e8/d6f4db0886dbba2fc87b5314f2d5127acdc782e4b51e6f86972a2e45ffd6/tqdm-4.62.0-py2.py3-none-any.whl (76 kB)
Requirement already satisfied: opencv-python in c:\users\guojinyue\appdata\roaming\python\python37\site-packages (4.3.0.36)
Collecting PyYAML
Using cached https://pypi.tuna.tsinghua.edu.cn/packages/66/7a/9f3a49dea61e98ac8bd3b6b619da4fcb59053232a6e11e9e865dab7c16e0/PyYAML-5.4.1-cp37-cp37m-win_amd64.whl (210 kB)
Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in c:\users\guojinyue\appdata\roaming\python\python37\site-packages (from matplotlib) (2.4.7)
Requirement already satisfied: kiwisolver>=1.0.1 in c:\users\guojinyue\appdata\roaming\python\python37\site-packages (from matplotlib) (1.2.0)
Requirement already satisfied: cycler>=0.10 in c:\users\guojinyue\appdata\roaming\python\python37\site-packages (from matplotlib) (0.10.0)
Requirement already satisfied: numpy>=1.10.0 in c:\users\guojinyue\appdata\roaming\python\python37\site-packages (from matplotlib) (1.17.0+mkl)
Requirement already satisfied: python-dateutil>=2.1 in c:\users\guojinyue\appdata\roaming\python\python37\site-packages (from matplotlib) (2.8.1)
Requirement already satisfied: colorama; platform_system == "Windows" in c:\users\guojinyue\appdata\roaming\python\python37\site-packages (from tqdm) (0.4.3)
Requirement already satisfied: six in c:\users\guojinyue\appdata\roaming\python\python37\site-packages (from cycler>=0.10->matplotlib) (1.15.0)
Installing collected packages: ninja, PyYAML, yacs, cython, tqdm
Successfully installed PyYAML-5.4.1 cython-0.29.24 ninja-1.10.2 tqdm-4.62.0 yacs-0.1.8
pip库安装成功。
如果在安装中出现:
WARNING conda.gateways.disk.delete:unlink_or_rename_to_trash(140): Could not remove or rename D:\Anaconda\program\pkgs\jedi-0.17.0-py37_0.tar.bz2. Please remove this file manually (you may need to reboot to free file handles)
WARNING conda.gateways.disk.delete:unlink_or_rename_to_trash(140): Could not remove or rename D:\Anaconda\program\pkgs\jedi-0.17.0-py37_0\Lib\site-packages\jedi\api\__init__.py. Please remove this file manually (you may need to reboot to free file handles)
这是anaconda3文件夹权限问题,我的anaconda3文件夹安装在D盘,可能安装在默认C盘路径就不会出现问题。
根据提示,手动删除路径上的对应文件,重新输入指令,即可安装成功
3.检验环境
在虚拟环境中进入python,进行环境验证
>>> import torch
>>> torch.__version__
'1.0.1'
>>> torch.cuda.is_available()
True
>>> a=torch.rand(1,2)
>>> b=a.cuda()
>>> print(a,b)
tensor([[0.5375, 0.1968]]) tensor([[0.5375, 0.1968]], device='cuda:0')
>>>
安装完毕
三、安装cocoapi
教程地址:https://github.com/philferriere/cocoapi
点击网址,打开readme.md,根据链接找到Visual C++ 2015
点击默认安装即可
在线安装后就能安装pycocotool了
pip install git+https://github.com/philferriere/cocoapi.git#subdirectory=PythonAPI
如果pip安装可能会出现问题,这里提供另外一种方法,直接去https://github.com/philferriere/cocoapi下载项目,然后在anaconda的激活环境中,我是根据这个方法进行安装的,进到cocoapi\PythonAPI目录:
(maskrcnn_pytorch) C:\WINDOWS\system32>d:
(maskrcnn_pytorch) D:\>cd D:\ChromeCoreDownloads\cocoapi-master\cocoapi-master\PythonAPI
(maskrcnn_pytorch) D:\ChromeCoreDownloads\cocoapi-master\cocoapi-master\PythonAPI>
执行下列命令:
# install pycocotools locally
python setup.py build_ext --inplace
# install pycocotools to the Python site-packages
python setup.py build_ext install
注意,#为注释,可不用写在命令行中,执行完毕后完成安装
在D:\ChromeCoreDownloads\cocoapi-master\cocoapi-master\PythonAPI\pycocotools中,可以看见增加文件:
安装完成之后python命令进入python环境,能运行from pycocotools.coco import COCO和import pycocotools._mask as _mask即大功告成。
(maskrcnn_pytorch) C:\WINDOWS\system32>python
Python 3.7.11 (default, Jul 27 2021, 09:42:29) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from pycocotools.coco import COCO
>>> import pycocotools._mask as _mask
>>>
PS:如果有github项目中有pycocotools文件夹则import pycocotools的时候会优先import当前目录下的文件,如果在命令行中import没有问题在jupyter notebook上发现“No module named ‘pycocotools._mask”等问题,则只需把上文生成的pycocotools文件夹替换掉当前github项目目录下的pycocotools文件夹即可
四、安装深度学习加速库apex
克隆项目:https://github.com/NVIDIA/apex
cd到该项目文件夹,执行指令:
(maskrcnn_pytorch) C:\WINDOWS\system32>d:
(maskrcnn_pytorch) D:\>cd D:\ChromeCoreDownloads\apex-master\apex-master
(maskrcnn_pytorch) D:\ChromeCoreDownloads\apex-master\apex-master>
输入指令
pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" ./
此时会有很多的信息跳出,这里不再记录,安装完毕后,进入python,输入
from apex import amp
具体的步骤可以参考这篇博客https://blog.csdn.net/qq_34914551/article/details/103203862
最后,再安装apex的相关库,再当前文件夹下运行
python setup.py install
出现
Installed d:\anaconda\program\envs\maskrcnn_pytorch\lib\site-packages\apex-0.1-py3.7.egg
Processing dependencies for apex==0.1
Finished processing dependencies for apex==0.1
完成安装
五、安装maskrcnn benchmark
cd 到下载好的maskrcnn-benchmark 目录下
python setup.py build develop
完成安装
六、配置pycharm
打开pycharm,打开程序
配置解释器,点击File->setting->project,可以看到当前的环境:
点击右上角的齿轮,选择Add…,出现如下界面:
选择Existing Enviroment,找到本地虚拟环境的安装地址
我的是D:\Anaconda\program\envs\maskrcnn_pytorch\python.exe
点击OK,发现已经设置成功。
至此pycharm环境配置完毕。
七、配置jupyter notebook
anaconda会自带jupyter notebook,但是notebook默认运行的是base环境,因此当我们创建其他虚拟环境时启动notebook需要另行设置。
要想在jupyter中使用刚刚创建的虚拟环境,需要进入刚才的虚拟环境中安装ipykernel:
pip install ipykernel
conda install jupyter
conda install nb_conda
接着修改配置文件(最重要的一步),进入Anaconda安装目录:我的目录为D:\Anaconda\program\share\jupyter\kernels,在里面新建一个文件夹,文件夹命名为虚拟环境的名字maskrcnn_pytorch,找到虚拟环境的对应位置,我的是D:\Anaconda\program\envs\maskrcnn_pytorch\share\jupyter\kernels\python3,将python3文件夹里的三个文件全部复制到新文件夹去
打开kernel.json并对"display_name"进行修改:
{
"argv": [
"D:/Anaconda/program/envs/maskrcnn_pytorch\\python.exe",#这是虚拟环境的目录
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "maskrcnn_pytorch",# 这是在jupyter里看见的虚拟环境的名字,可以和创建的虚拟环境名称保持一致
"language": "python"
}
在左下角Terminal中输入:
Jupyter-notebook
同时,浏览器中也会打开Jupyter-notebook
打开demo文件夹中的Mask_R-CNN_demo.ipynb
选择建立的虚拟环境即可。
其余的方法也可以直接通过
conda install nb_conda
详细请看:https://www.jianshu.com/p/afea092dda1d
总结
Mask R-CNN的环境配置比较复杂,建议从头建立一个虚拟环境,方便以后运用在自己的数据集上。
相关参考:https://blog.csdn.net/qq_38732601/article/details/106450549?ops_request_misc=&request_id=&biz_id=102&utm_term=maskrcnn%20%E7%8E%AF%E5%A2%83%20torch&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-1-.first_rank_v2_pc_rank_v29&spm=1018.2226.3001.4187