ubuntu16.04安装detectron教程
系统环境要求:
- NVIDIA GPU, Linux, Python2
- Caffe2, 部分Python包, COCO API
1. python2.7
Detectron必须在python2环境,python3不支持, 推荐使用conda 创建一个新的环境python27,并且切换到新环境
conda create -n python27 python=2.7
conda activate python27
以下所有操作都在python2.7下执行
2. 安装 CUDA 8 + CuDNN 7 + NCCL
2.1 CUDA8安装
安装过程略
验证安装:nvcc –version
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2016 NVIDIA Corporation
Built on Tue_Jan_10_13:22:03_CST_2017
Cuda compilation tools, release 8.0, V8.0.61
2.2 从Debian文件安装CuDNN 7
下载地址:https://developer.nvidia.com/cudnn
# 1.Install the runtime library, for example:
sudo dpkg -i libcudnn7_7.0.3.11-1+cuda9.0_amd64.deb
# 2.Install the developer library, for example:
sudo dpkg -i libcudnn7-devel_7.0.3.11-1+cuda9.0_amd64.deb
# 3.Install the code samples and the cuDNN Library User Guide, for example:
sudo dpkg -i libcudnn7-doc_7.0.3.11-1+cuda9.0_amd64.deb
2.3 测试CuDNN 7
样例在/usr/src/cudnn_samples_v7路径下
1.Copy the cuDNN sample to a writable path.
$cp -r /usr/src/cudnn_samples_v7/ $HOME
2.Go to the writable path.
$ cd $HOME/cudnn_samples_v7/mnistCUDNN
3.Compile the mnistCUDNN sample.
$make clean && make
4.Run the mnistCUDNN sample.
$ ./mnistCUDNN
显示如下,表示安装成功: Test passed!
2.4 cuDNN从v6升级到v7
cuDNN v7可以与之前版本的cuDNN共存,例如v5或v6。 cuDNN v7 can coexist with previous versions of cuDNN, such as v5 or v6.
2.5 NCCL安装
下载安装包并安装
sudo dpkg -i nccl-repo-ubuntu1604-2.2.13-ga-cuda8.0_1-1_amd64.deb
3. 安装Caffe2
官网:https://caffe2.ai/docs/getting-started.html?platform=ubuntu&configuration=prebuilt
3.1 conda安装Caffe2
使用Anaconda在GPU + CUDA8 + CuDNN7环境下
conda install pytorch-nightly cuda80 -c pytorch
3.2 验证Caffe2
检查Caffe2的GPU依赖是否正确,下面命令输出的GPU卡的数量必须要大于0 ,否则不能使用Detectron
# To check if Caffe2 build was successful
python -c 'from caffe2.python import core' 2>/dev/null && echo "Success" || echo "Failure"
# To check if Caffe2 GPU build was successful
# This must print a number > 0 in order to use Detectron
python -c 'from caffe2.python import workspace; print(workspace.NumCudaDevices())'
验证过程显示Failure,terminal重新输入 python -c ‘from caffe2.python import core’,可以显示错误信息
错误1: No module named google.protobuf.internal 解决方法:pip install protobuf
错误2: no moudle named past.builtins 解决方法:pip install future
4. 安装COCO API
# COCOAPI=/path/to/clone/cocoapi
git clone https://github.com/cocodataset/cocoapi.git $COCOAPI
cd $COCOAPI/PythonAPI
# Install into global site-packages
make install
# Alternatively, if you do not have permissions or prefer
# not to install the COCO API into global site-packages
python setup.py install --user
在make install 如出现错误error: pycocotools/_mask.c: No such file or directory:
解决方法:pip install cython
5. 安装Detectron
5.1 安装
官网教程:https://github.com/facebookresearch/Detectron/blob/master/INSTALL.md
Clone the Detectron repository:
# DETECTRON=/path/to/clone/detectron
git clone https://github.com/facebookresearch/detectron $DETECTRON
Install Python dependencies:
pip install -r $DETECTRON/requirements.txt
Set up Python modules:
cd $DETECTRON && make
Check that Detectron tests pass (e.g. for SpatialNarrowAsOp test):
python $DETECTRON/detectron/tests/test_spatial_narrow_as_op.py
5.2 运行Detectron
官方网址: https://github.com/facebookresearch/Detectron/blob/master/GETTING_STARTED.md
可以使用tools目录下内置的infer_simple.py 来使用预训练的模型来预测实际的照片,infer_simple.py里面调用的是detectron封装的vis_utils.vis_one_image API。
python tools/infer_simple.py \
--cfg configs/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml \
--output-dir /tmp/detectron-visualizations \
--image-ext jpg \
--wts https://dl.fbaipublicfiles.com/detectron/35861858/12_2017_baselines/e2e_mask_rcnn_R-101-FPN_2x.yaml.02_32_51.SgT4y1cO/output/train/coco_2014_train:coco_2014_valminusminival/generalized_rcnn/model_final.pkl \
demo
最终,检测结果就以pdf的格式输出到了/tmp/detectron-visualizations目录下