maskrcnn-benchmark安装及常见错误
Mask rcnn环境配置
在安装好Anaconda之后可以配置Mask RCNN了。这里我用的是maskrcnn-benchmark,环境搭建相对简单。
1. 创建虚拟环境:
source activate conda create -n MaskRCNN python=3 conda activate MaskRCNN
完成后,可看到如下界面:
2. 安装各种依赖包:
conda install ipython pip install ninja yacs cython matplotlib tqdm opencv-python
3. 安装PyTorch
PyTorch官网,根据自己的电脑配置选择,会出现对应命令。
conda install pytorch torchvision cudatoolkit=10.0 -c pytorch
这里可以测试一下torchvision是否安装成功。如果没有安装成功,则需要下载源码编译安装(别着急,后面有写)。
:~$ python >>>>import torch >>>>import torchvision
4. 安装目录
export INSTALL_DIR=/data_1/software/pytorch/MaskRCNN //设置自己的安装目录 cd $INSTALL_DIR
5. 安装torchvision(若之前未安装成功,执行此步骤;否则跳过即可)
git clone https://github.com/pytorch/vision.git cd vision/ python setup.py install
6. 安装pycocotools
git clone https://github.com/cocodataset/cocoapi.git cd cocoapi/PythonAPI python setup.py build_ext install
7. 安装apex
cd $INSTALL_DIR git clone https://github.com/NVIDIA/apex.git cd apex python setup.py install --cuda_ext --cpp_ext
8. 安装maskrcnn-benchmark
cd $INSTALL_DIR git clone https://github.com/facebookresearch/maskrcnn-benchmark.git cd maskrcnn-benchmark ## the following will install the lib with ## symbolic links, so that you can modify ## the files if you want and won't need to ## re-build it python setup.py build develop
Bingo!^-^ !
错误集锦
问题1:
(MaskRCNN) em@B-GUWEIXIN:/data_1/software/pytorch/MaskRCNN/cocoapi/PythonAPI$ python setup.py build_ext install running build_ext building 'pycocotools._mask' extension gcc -pthread -B /home/em/.conda/envs/MaskRCNN/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/em/.conda/envs/MaskRCNN/lib/python3.6/site-packages/numpy/core/include -I../common -I/home/em/.conda/envs/MaskRCNN/include/python3.6m -c ../common/maskApi.c -o build/temp.linux-x86_64-3.6/../common/maskApi.o -Wno-cpp -Wno-unused-function -std=c99 ../common/maskApi.c: In function ‘rleToBbox’: ../common/maskApi.c:141:31: warning: ‘xp’ may be used uninitialized in this function [-Wmaybe-uninitialized] if(j%2==0) xp=x; else if(xp<x) { ys=0; ye=h-1; } ^ gcc -pthread -B /home/em/.conda/envs/MaskRCNN/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/em/.conda/envs/MaskRCNN/lib/python3.6/site-packages/numpy/core/include -I../common -I/home/em/.conda/envs/MaskRCNN/include/python3.6m -c pycocotools/_mask.c -o build/temp.linux-x86_64-3.6/pycocotools/_mask.o -Wno-cpp -Wno-unused-function -std=c99 gcc: error: pycocotools/_mask.c: 没有那个文件或目录 error: command 'gcc' failed with exit status 1
解决方法:
手动用cython生成_mask.c
cd $MaskRCNN_Root/cocoapi/PythonAPI/pycocotools cython _mask.pyx cd .. python setup.py build_ext install
问题2:
em@B-GUWEIXIN:/data_1/software/pytorch/MaskRCNN/apex$ python setup.py install --cuda_ext --cpp_ext torch.__version__ = 1.2.0.dev20190704 Traceback (most recent call last): File "setup.py", line 64, in <module> check_cuda_torch_binary_vs_bare_metal(torch.utils.cpp_extension.CUDA_HOME) File "setup.py", line 37, in check_cuda_torch_binary_vs_bare_metal raw_output = subprocess.check_output([cuda_dir + "/bin/nvcc", "-V"], universal_newlines=True) File "/home/em/.conda/envs/MaskRCNN/lib/python3.6/subprocess.py", line 336, in check_output **kwargs).stdout File "/home/em/.conda/envs/MaskRCNN/lib/python3.6/subprocess.py", line 403, in run with Popen(*popenargs, **kwargs) as process: File "/home/em/.conda/envs/MaskRCNN/lib/python3.6/subprocess.py", line 709, in __init__ restore_signals, start_new_session) File "/home/em/.conda/envs/MaskRCNN/lib/python3.6/subprocess.py", line 1344, in _execute_child raise child_exception_type(errno_num, err_msg, err_filename) FileNotFoundError: [Errno 2] No such file or directory: ':/usr/local/cuda-9.0/bin/nvcc': ':/usr/local/cuda-9.0/bin/nvcc'
提示找不到 nvcc, 但是输入 nvcc --version, 却可以正常显示.
解决方法:
修改环境变量配置
sudo ~/.bashrc export CUDA_HOME=$CUDA_HOME:/usr/local/cuda # 将上面的语句修改成: export CUDA_HOME=/usr/local/cuda:$CUDA_HOME source ~/.bashrc
打开新终端,在安装目录下执行重新安装
$ python setup.py install --cuda_ext --cpp_ext
问题3:
(MaskRCNN) em@B-GUWEIXIN:/data_1/software/pytorch/MaskRCNN/maskrcnn-benchmark/demo$ python webcam.py Traceback (most recent call last): File "webcam.py", line 6, in <module> from predictor import COCODemo File "/data_1/software/pytorch/MaskRCNN/maskrcnn-benchmark/demo/predictor.py", line 4, in <module> from torchvision import transforms as T File "/home/em/.conda/envs/MaskRCNN/lib/python3.6/site-packages/torchvision/__init__.py", line 1, in <module> from torchvision import models File "/home/em/.conda/envs/MaskRCNN/lib/python3.6/site-packages/torchvision/models/__init__.py", line 11, in <module> from . import detection File "/home/em/.conda/envs/MaskRCNN/lib/python3.6/site-packages/torchvision/models/detection/__init__.py", line 1, in <module> from .faster_rcnn import * File "/home/em/.conda/envs/MaskRCNN/lib/python3.6/site-packages/torchvision/models/detection/faster_rcnn.py", line 7, in <module> from torchvision.ops import misc as misc_nn_ops File "/home/em/.conda/envs/MaskRCNN/lib/python3.6/site-packages/torchvision/ops/__init__.py", line 1, in <module> from .boxes import nms, box_iou File "/home/em/.conda/envs/MaskRCNN/lib/python3.6/site-packages/torchvision/ops/boxes.py", line 2, in <module> from torchvision import _C ImportError: /home/em/.conda/envs/MaskRCNN/lib/python3.6/site-packages/torchvision/_C.cpython-36m-x86_64-linux-gnu.so: undefined symbol: _ZN2at7getTypeERKNS_6TensorE
解决方法:
这个问题就是torchvision没有安好,就通过源码安装就可以啦,详见上述mask rcnn环境配置的第五条。
问题4:
在vision目录下python->import torchvision没问题,换个目录就报错。
解决方法:
将vision下的torchvision和torchvision.egg-info文件夹 复制到/.conda/envs/MaskRCNN/lib/python3.6/site-packages/下面即可,删除原来的。