Ubuntu16.04安装caffe 框架

时间:2022-10-07 06:28:28

1 添加必要的库

cython,python-opencv,easydict

sudoaptgetinstallpythonpip sudo pip install cython
sudoaptgetinstallpythonopencv sudo pip install easydict

2 下载源代码 py-faster-rcnn

cd /software/caffe git clone –recursive https://github.com/rbgirshick/py-faster-rcnn.git
$ cp -r py-faster-rcnn/ ~/

注意,–recursive 一定要有,否则 py-faster-rcnn 目录下没有 caffe-fast-rcnn 文件夹。

3 修改~/py-faster-rcnn/caffe-fast-rcnn目录下的Makefile和Makefile.config

直接把之前 caffe 目录下的 Makefile 和 Makefile.config 复制到 caffe-fast-rcnn 下,

cp /caffe/Makefile /pyfasterrcnn/caffefastrcnn/ cp ~/caffe/Makefile.config ~/py-faster-rcnn/caffe-fast-rcnn/

4 编译~/py-faster-rcnn下的lib文件

4.1 修改py-faster-rcnn/lib/setup.py文件

第135行

  'nvcc': ['-arch=sm_35',

改为:自己显卡的计算能力

 'nvcc': ['-arch=sm_20',

(不改好像也可以)

4.2 编译 lib文件

cd /pyfasterrcnn/lib make

5 编译py-faster-rcnn下的caffe-fast-rcnn

cd /pyfasterrcnn/caffefastrcnn/ make

此时会报错,原因在于 faster rcnn 采用的是 cudnn_v4,而我们安装的是 cudnn_v6,解决方法

如下:
(1) 用最新 caffe 源码的以下文件替换掉 faster rcnn 的对应文件
include/caffe/layers/cudnn_relu_layer.hpp,
include/caffe/layers/cudnn_sigmoid_layer.hpp,
include/caffe/layers/cudnn_tanh_layer.hpp,
src/caffe/layers/cudnn_relu_layer.cpp,
src/caffe/layers/cudnn_relu_layer.cu
src/caffe/layers/cudnn_sigmoid_layer.cpp,
src/caffe/layers/cudnn_sigmoid_layer.cu
src/caffe/layers/cudnn_tanh_layer.cpp,
src/caffe/layers/cudnn_tanh_layer.cu

cp /caffe/include/caffe/layers/cudnnrelulayer.hpp /pyfasterrcnn/caffefastrcnn/include/caffe/layers cp ~/caffe/include/caffe/layers/cudnn_sigmoid_layer.hpp ~/py-faster-rcnn/caffe-fast-rcnn/include/caffe/layers
cp /caffe/include/caffe/layers/cudnntanhlayer.hpp /pyfasterrcnn/caffefastrcnn/include/caffe/layers cp ~/caffe/src/caffe/layers/cudnn_relu_layer.cpp ~/py-faster-rcnn/caffe-fast-rcnn/src/caffe/layers
cp /caffe/src/caffe/layers/cudnnrelulayer.cu /pyfasterrcnn/caffefastrcnn/src/caffe/layers cp ~/caffe/src/caffe/layers/cudnn_sigmoid_layer.cpp ~/py-faster-rcnn/caffe-fast-rcnn/src/caffe/layers
cp /caffe/src/caffe/layers/cudnntanhlayer.cpp /pyfasterrcnn/caffefastrcnn/src/caffe/layers cp ~/caffe/src/caffe/layers/cudnn_tanh_layer.cu ~/py-faster-rcnn/caffe-fast-rcnn/src/caffe/layers

(2) 用 caffe 源码中的这个文件替换掉 faster rcnn 对应文件
include/caffe/util/cudnn.hpp

$ cp ~/caffe/include/caffe/util/cudnn.hpp ~/py-faster-rcnn/caffe-fast-rcnn/include/caffe/util

(3) 将 faster rcnn 中的 src/caffe/layers/cudnn_conv_layer.cu 文件中的所有
cudnnConvolutionBackwardData_v3 函数名替换为
cudnnConvolutionBackwardData
cudnnConvolutionBackwardFilter_v3 函数名替换为
cudnnConvolutionBackwardFilter

vim ~/py-faster-rcnn/caffe-fast-rcnn/src/caffe/layers/cudnn_conv_layer.cu

运行指令clean,再编译

cd /pyfasterrcnn/caffefastrcnn/ make clean
makej4 make pycaffe

出现警告:

nvcc warning : The 'compute_20', 'sm_20', and 'sm_21' architectures are deprecated, and may be removed in a future release (Use -Wno-deprecated-gpu-targets to suppress warning).

暂时忽略。

6 下载faster_rcnn_models.tgz放在~/py-faster-rcnn/data并解压

编译好之后,需要下载一个 faster_rcnn_models.tgz 压缩包

cd /pyfasterrcnn ./data/scripts/fetch_faster_rcnn_models.sh

注意:最好再windows上下载。用命令下载,容易失败,所有就换到windows下载,再复制到相应目录解压

下载地址:http://www.cs.berkeley.edu/%7Erbg/faster-rcnn-data/faster_rcnn_models.tgz
或者
https://dl.dropboxusercontent.com/s/o6ii098bu51d139/faster_rcnn_models.tgz?dl=0
https://mm.ww.rrjs.pw/*下载即可

下载好后,将压缩包放在 py-faster-rcnn/data/下解压即可

tarzxvffasterrcnnmodels.tgzC /pyfasterrcnn/data cd ~/py-faster-rcnn/tools/

7 运行~/py-faster-rcnn/tools下的demo.py

cd /pyfasterrcnn/tools ./demo.py

问题:

$ ./demo.py
Traceback (most recent call last): File "./demo.py", line 17, in <module> from fast_rcnn.config import cfg File "/home/fc/py-faster-rcnn/tools/../lib/fast_rcnn/config.py", line 23, in <module> from easydict import EasyDict as edict ImportError: No module named easydict

解决:

$ sudo pip install easydict

其实easydict前面已经安装过了,所有再执行这个命令还是不能解决问题。不断尝试后发现,切换到root用户就没有这个问题了。

但出现新的问题

 File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 42, in <module>
    raise ImportError, str(msg) + ', please install the python-tk package'
ImportError: No module named _tkinter, please install the python-tk package

这个问题到比较容易,缺python-tk那就安装:

$ sudo apt-get install python-tk

新问题:

Loaded network /home/fc/py-faster-rcnn/data/faster_rcnn_models/VGG16_faster_rcnn_final.caffemodel
F0105 20:51:57.321182 12180 relu_layer.cu:26] Check failed: error == cudaSuccess (9 vs. 0) invalid configuration argument *** Check failure stack trace: *** 已放弃 (核心已转储)

解决1:
网上(https://www.zhihu.com/question/41493725)说这个原因可能显存不足 ,可以先使用小点的模型比如ZF来进行检测,执行demo.py时加参数–net zf,或者直接修改demo.py里面的代码将默认加载的model改为zf.如果想修改batch_size进行训练,可以在faster-rcnn-root/lib/fast_rcnn/config.py中进行修改。
所以执行:

$ ./demo.py --net zf

还是,不过“9 vs. 0”变成“8 vs. 0”

Loaded network /home/fc/py-faster-rcnn/data/faster_rcnn_models/ZF_faster_rcnn_final.caffemodel
F0105 20:57:31.931417 12305 roi_pooling_layer.cu:91] Check failed: error == cudaSuccess (8 vs. 0)  invalid device function *** Check failure stack trace: *** 已放弃 (核心已转储)

解决2:
还有地方(http://blog.csdn.net/u013078356/article/details/51009470)说,是因为显卡的计算能力不同而导致的,修改 ~/py-faster-rcnn/lib//setup.py 的第135行,将arch改为与你显卡相匹配的数值,(比如我的GTX 760,计算能力是3.0,就将sm_35改成了sm_30,前面我已经改过了)然后删除 py-faster-rcnn/lib/utils/bbox.c,py-faster-rcnn/lib/nms/cpu_nms.c ,py-faster-rcnn/lib/nms/gpu_nms.cpp 重新编译即可

$ mv ~/py-faster-rcnn/lib/utils/bbox.c ~/py-faster-rcnn/lib/utils/bbox.c_rmbyfc
$ mv ~/py-faster-rcnn/lib/nms/cpu_nms.c ~/py-faster-rcnn/lib/nms/cpu_nms.c_rmbyfc
$ mv ~/py-faster-rcnn/lib/nms/gpu_nms.cpp ~/py-faster-rcnn/lib/nms/gpu_nms.cpp_rmbyfc

重新编译~/py-faster-rcnn/lib/里的文件

#./demo.py --net zf

终于成功了。

运行成功后,会有如下显示
Ubuntu16.04安装caffe 框架

Ubuntu16.04安装caffe 框架

Ubuntu16.04安装caffe 框架

Ubuntu16.04安装caffe 框架

Ubuntu16.04安装caffe 框架

Ubuntu16.04安装caffe 框架

Ubuntu16.04安装caffe 框架

Ubuntu16.04安装caffe 框架

Ubuntu16.04安装caffe 框架

Ubuntu16.04安装caffe 框架

Ubuntu16.04安装caffe 框架

Ubuntu16.04安装caffe 框架