环境 TX2+JetPack3.0
1 安装相关依赖库
sudo apt-get install libatlas-base-dev libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler libboost-all-dev libgflags2 libgflags-dev libgoogle-glog-dev liblmdb-dev libyaml-dev
sudo apt-get install python-numpy python-setuptools python-pip cython python-opencv python-skimage python-protobuf
sudo pip install easydict PyYAML
2 下载源码
git clone --recursive https://github.com/rbgirshick/py-faster-rcnn.git
参考其他博客中所说,由于原版py-faster-rcnn依赖的caffe比较老,不支持cudnnv5,需要更新下对应的源码。我是直接利用最新版caffe源码中的文档替换原faster rcnn中的文件:
用最新caffe源码的以下文件替换掉faster rcnn 的对应文件
include/caffe/layers/cudnn_relu_layer.hpp,src/caffe/layers/cudnn_relu_layer.cpp, src/caffe/layers/cudnn_relu_layer.cu
include/caffe/layers/cudnn_sigmoid_layer.hpp,src/caffe/layers/cudnn_sigmoid_layer.cpp,src/caffe/layers/cudnn_sigmoid_layer.cu
include/caffe/layers/cudnn_tanh_layer.hpp,src/caffe/layers/cudnn_tanh_layer.cpp, src/caffe/layers/cudnn_tanh_layer.cu
用caffe源码中的这个文件替换掉faster rcnn 对应文件
include/caffe/util/cudnn.hpp
将 faster rcnn 中的 src/caffe/layers/cudnn_conv_layer.cu 文件中的所有
cudnnConvolutionBackwardData_v3 函数名替换为 cudnnConvolutionBackwardData
cudnnConvolutionBackwardFilter_v3函数名替换为 cudnnConvolutionBackwardFilter
3 修改配置文件
cp Makefile.config.example Makefile.config
vim Makefile.config #修改配置文件
USE_CUDNN := 1
CUDA_DIR := /usr/local/cuda-8.0
WITH_PYTHON_LAYER := 1
INCLUDE_DIRS :=$(PYTHON_INCLUDE)/usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB)/usr/local/lib/usr/lib /usr/lib/aarch64-linux-gnu/hdf5/serial
为了匹配cuda8.0的计算能力,请把Makefile.config中CUDA_ARCH中的前两行去掉,如下图所示(保留也行,编译的时候会弹出警告)在Makefile文件中,把 hdf5_hl 和hdf5修改为hdf5_serial_hl 和 hdf5_serial,也就是把下面第一行代码改为第二行代码。
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
4 编译
cd py-faster-rcnn/lib
make
cd py-faster-rcnn/caffe-fast-rcnn
make -j16
make pycaffe
7 下载模型
网上给的教程是通过源码中所带的.sh文件下载,步骤如下:
cd py-faster-rcnn
./data/scripts/fetch_faster_rcnn_models.sh
不过为了方便,我是直接从百度云上下载的模型。链接如下:
链接: https://pan.baidu.com/s/1eSwBXjO 密码: dcr6
下载后直接解压到data文件夹下即可。
8 测试运行demo
cd py-faster-rcnn/tools
python demo --gpu 0
默认使用的是VGG16模型,速度在1s左右每张,结果如图
9 遇到的问题
我是按照网上的教程一步一步来的,可能因为之前编译过caffe的原因,所以这次入的坑还比较少。
(1)在lib文件夹下make时,
Traceback (most recent call last):
File "setup.py", line 58, in
CUDA = locate_cuda()
File "setup.py", line 55, in locate_cuda
raise EnvironmentError('The CUDA %s path could not be located in %s' % (k, v))
EnvironmentError: The CUDA lib path could not be located in /usr/local/cuda-8.0/lib
解决办法:
gedit setup.py
将 cudaconfig
=
{
'home'
:home,
'nvcc'
:nvcc,
'include'
: pjoin(home,
'include'
),
'lib'
: pjoin(home,
'lib'
)}
cudaconfig = {'home':home, 'nvcc':nvcc,
'include': pjoin(home, 'include'),
'lib': pjoin(home, 'lib64')}
(2)错误:aarch64-linux-gnu-gcc: error: utils/bbox.c: No such file or directory
aarch64-linux-gnu-gcc: fatal error: no input files
解决办法:手动生成bbox
cd py-faster-rcnn/lib/utils
cython bbox.pyx
aarch64-linux-gnu-gcc: error: nms/cpu_nms.c: No such file or directory
aarch64-linux-gnu-gcc: fatal error: no input files
compilation terminated.
error: command ‘x86_64-linux-gnu-gcc’ failed with exit status 4
make: * [all] Error 1
解决办法:手动生成cpu_nms
cd py-faster-rcnn/lib/nms
cython cpu_nms.pyx
aarch64-linux-gnu-gcc: error: nms/gpu_nms.c: No such file or directory
aarch64-linux-gnu-gcc: fatal error: no input files
compilation terminated.
error: command ‘x86_64-linux-gnu-gcc’ failed with exit status 4
make: * [all] Error 1
解决办法:手动生成gpu_nms
cd py-faster-rcnn/lib/nms
cython gpu_nms.pyx
Traceback (most recent call last):
File “./demo.py”, line 18, in module
from fast_rcnn.test import im_detect
File “/home/nvidia/py-faster-rcnn/tools/../lib/fast_rcnn/test.py”, line 17, in module
from fast_rcnn.nms_wrapper import nms
File “/home/nvidia/py-faster-rcnn/tools/../lib/fast_rcnn/nms_wrapper.py”, line 9, in module
from nms.gpu_nms import gpu_nms
ImportError: /home/nvidia/py-faster-rcnn/tools/../lib/nms/gpu_nms.so: undefined symbol: _nms
解决办法:
1)编辑setup.py
cd py-faster-rcnn/lib
vim setup.py
2)将gpu_nms.pyx改为gpu_nms.cpp
#before
Extension('nms.gpu_nms',
['nms/nms_kernel.cu', 'nms/gpu_nms.pyx'],
...
#after
Extension('nms.gpu_nms',
['nms/nms_kernel.cu', 'nms/gpu_nms.cpp'],
...
3)修改gpu_nms.c文件后缀为.cpp
cd py-faster-rcnn/lib/nms
mv gpu_nms.c gpu_nms.cpp
rm gpu_nms.so
4)cuowutishi:
CXX/LD -o .build_release/tools/convert_imageset.bin
.build_release/lib/libcaffe.so: undefined reference to cv::imread(cv::String const&, int)’
.build_release/lib/libcaffe.so: undefined reference tocv::imencode(cv::String const&, cv::_InputArray const&, std::vector >&, std::vector > const&)’
.build_release/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::_InputArray const&, int)’
collect2: error: ld returned 1 exit status
make: * [.build_release/tools/convert_imageset.bin] Error 1
opencv_imgcodecs
链接的问题,比较有效的解决方案是,把opencv需要的lib添加到
Makefile
文件中,找到
LIBRARIES
(在
PYTHON_LIBRARIES := boost_python python2.7
前一行)并修改为:
LIBRARIES += glog gflags protobuf leveldb snappy \
lmdb boost_system hdf5_hl hdf5 m \
opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
参考:http://blog.csdn.net/jiajunlee/article/details/50373815
http://blog.csdn.net/jiongnima/article/details/70040262
http://blog.csdn.net/chenjiehua123456789/article/details/61919374
http://blog.csdn.net/sinat_31802439/article/details/52604972