糟心的caffe+ matlab编译路程

Posted captain-dl

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了糟心的caffe+ matlab编译路程相关的知识,希望对你有一定的参考价值。

配置:Ubuntu16.04+MatlabR2016b+cuda8.0+cudnn5.1+caffe

配置caffe真的不是很容易,特别是对初次接触Linux的同学,各种报错(ノ_;\( `ロ′),搞了好几天才解决

caffe安装可能出现的问题

可能会出现的问题

问题1."libcudart.so.8.0 cannot open shared object file: No such file or directory"
解决方法:
解决办法是将一些文件复制到/usr/local/lib文件夹下:
注意自己CUDA的版本号!

sudo cp /usr/local/cuda-8.0/lib64/libcudart.so.8.0 /usr/local/lib/libcudart.so.8.0 && sudo ldconfig
sudo cp /usr/local/cuda-8.0/lib64/libcublas.so.8.0 /usr/local/lib/libcublas.so.8.0 && sudo ldconfig
sudo cp /usr/local/cuda-8.0/lib64/libcurand.so.8.0 /usr/local/lib/libcurand.so.8.0 && sudo ldconfig

问题2."libcudnn.so.5 cannot open shared object file: No such file or directory"
解决方法:
解决办法是将一些文件复制到/usr/local/lib文件夹下
注意自己CUDA的版本号!

sudo cp /usr/local/cuda-8.0/lib64/libcudnn.so /usr/local/lib/libcudnn.so && sudo ldconfig
sudo cp /usr/local/cuda-8.0/lib64/libcudnn.so.5 /usr/local/lib/libcudnn.so.5 && sudo ldconfig
sudo cp /usr/local/cuda-8.0/lib64/libcudnn.so.5.1.5 /usr/local/lib/libcudnn.so.5.1.5 && sudo ldconfig

问题3."OSError: libcudnn.so.7.0: cannot open shared object file: No such file or directory错误"
解决方法:

#因为cuda的路径可能设置错了

sudo ldconfig /usr/local/cuda/lib64

问题4.linux下Matcaffe调用及库链接问题的解决(mattest不通过)
解决方法:

编译make matcaffe后,执行make mattest后,往往出现“Invalid MEX-file"问题,其原因是MATLAB和linux的库冲突,解决的方法是用linux的库(在编译caffe之前大家的opencv等库肯定也早已装好了)

大部分的解决方法是通过export LD_LIBRARY_PATH和 LD_PRELOAD来链接,但是效果不好。最后发现,只有直接去MATLAB下面删除库并重新链接到x86_64-linux-gnu的方法是最好的。具体方法如下:

1.不需要降级gcc和g++,就用linux的自带版本,否则caffe编译不一定通过。我的是14.04的5.4(千万不要先用5去编译caffe再降级用4.4编译matcaffe)

2.不要去用改LIBRARY_PATH的方法,因为很可能不成功,尤其是有倒霉催的anaconda的情况下。

3.找到你的linux库的位置(一般是/usr/lib/x86_64-linux-gnu/)以及MATLAB库的位置(默认是/usr/local/MATLAB/R2014a/sys/os/glnxa64/)。然后写个sh执行下列操作


rm -rf /usr/local/MATLAB/R2014a/sys/os/glnxa64/libstdc++.so.6
ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 /usr/local/MATLAB/R2014a/sys/os/glnxa64/libstdc++.so.6
rm -rf /usr/local/MATLAB/R2014a/bin/glnxa64/libopencv_core.so.2.4
ln -s /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.9  /usr/local/MATLAB/R2014a/bin/glnxa64/libopencv_core.so.2.4
rm -rf /usr/local/MATLAB/R2014a/bin/glnxa64/libopencv_imgproc.so.2.4
sudo ln -s /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4.9  /usr/local/MATLAB/R2017a/bin/glnxa64/libopencv_imgproc.so.2.4
rm -rf /usr/local/MATLAB/R2014a/bin/glnxa64/libopencv_highgui.so.2.4
sudo ln -s /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.9  /usr/local/MATLAB/R2017a/bin/glnxa64/libopencv_highgui.so.2.4
rm -rf /usr/local/MATLAB/R2014a/bin/glnxa64/libfreetype.so.6
sudo ln -s /usr/lib/x86_64-linux-gnu/libfreetype.so.6  /usr/local/MATLAB/R2017a/bin/glnxa64/libfreetype.so.6

问题5.Invalid MEX-file
‘/home/xw/caffeBuild/caffe-master/matlab/+caffe/private/caffe_.mexa64‘:
/home/xw/caffeBuild/caffe-master/matlab/+caffe/private/caffe_.mexa64: undefined
symbol:
_ZN2cv8imencodeERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_11_InputArrayERSt6vectorIhSaIhEERKSB_IiSaIiEE

Error in caffe.set_mode_cpu (line 5)
caffe_(‘set_mode_cpu‘);

Error in caffe.run_tests (line 6)
caffe.set_mode_cpu();
解决方法:

[email protected]:/matlab/r2016a/bin/glnxa64# mv libopencv_imgproc.so.2.4 libopencv_imgproc.so.2.4.bak
[email protected]:/matlab/r2016a/bin/glnxa64# mv libopencv_highgui.so.2.4 libopencv_highgui.so.2.4.bak
[email protected]:/matlab/r2016a/bin/glnxa64# mv libopencv_core.so.2.4 libopencv_core.so.2.4.bak

[email protected]:/matlab/r2016a/bin/glnxa64# ln /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.9 libopencv_core.so.2.4
[email protected]:/matlab/r2016a/bin/glnxa64# ln /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.9 libopencv_highgui.so.2.4
[email protected]:/matlab/r2016a/bin/glnxa64# ln /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4.9 libopencv_imgproc.so.2.4

export LD_LIBRARY_PATH=/usr/lib/x86_64-linux-gnu/:/usr/local/cuda-8.0/lib64
export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4:/usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4:/usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4:/usr/lib/x86_64-linux-gnu/libstdc++.so.6:/usr/lib/x86_64-linux-gnu/libfreetype.so.6

问题6.错误:undefined
symbol:
_ZN2cv8imencodeERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEERKNS_11_InputArrayERSt6vectorIhSaIhEERKSB_IiSaIiEE

解决方法:

[email protected]:/matlab/r2016a/bin/glnxa64# mv libopencv_imgproc.so.2.4 libopencv_imgproc.so.2.4.bak
[email protected]:/matlab/r2016a/bin/glnxa64# mv libopencv_highgui.so.2.4 libopencv_highgui.so.2.4.bak
[email protected]:/matlab/r2016a/bin/glnxa64# mv libopencv_core.so.2.4 libopencv_core.so.2.4.bak

[email protected]:/matlab/r2016a/bin/glnxa64# sudo ln -s /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4.9 libopencv_core.so.2.4
[email protected]:/matlab/r2016a/bin/glnxa64#sudo ln -s /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4.9 libopencv_highgui.so.2.4
[email protected]:/matlab/r2016a/bin/glnxa64#sudo  ln -s /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4.9 libopencv_imgproc.so.2.4

问题7.警告: 执行 ‘caffe.Solver‘ 类析构函数时,捕获到以下错误:
错误使用 caffe_
Usage: caffe_(‘delete_solver‘, hSolver)

出错 caffe.Solver/delete (line 40)
caffe_(‘delete_solver‘, self.hSolver_self);

出错 caffe.Solver (line 17)
function self = Solver(varargin)

出错 caffe.test.test_solver (line 22)
self.solver = caffe.Solver(solver_file);

出错 caffe.run_tests (line 14)
run(caffe.test.test_solver) ...

In caffe.Solver (line 17)
In caffe.test.test_solver (line 22)
In caffe.run_tests (line 14)
解决方法:

https://blog.csdn.net/xiaojiajia007/article/details/72850247

40行:
      if ~isempty(self.hNet_self)
        caffe_('delete_net', self.hNet_self);
      end

    if ~isempty(self.hNet_self)
        caffe_('delete_net', self.hNet_self);
    end

    if self.isvalid
        caffe_('delete_net', self.hNet_self);
    end

问题8.matlab测试
https://blog.csdn.net/weiqi_fan/article/details/71023222
解决方法:

设置GPU
gpu_id = 0
caffe.set_mode_gpu();
caffe.set_device(gpu_id);

问题9.matlab奔溃的问题
解决方法:

https://askubuntu.com/questions/758892/doesnt-matlab-work-on-ubuntu-16-04

问题10.更换caffe版本
解决方法:

https://www.codeleading.com/article/1186958985/

使用新版本的问题:
./include/caffe/util/cudnn.hpp
./include/caffe/layers/cudnn_conv_layer.hpp
./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_conv_layer.cpp
./src/caffe/layers/cudnn_conv_layer.cu
./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


保存原来的文件 mv cudnn.hpp cudnn.hpp.bak

layers:
 mv cudnn_conv_layer.hpp cudnn_conv_layer.hpp.bak
 mv cudnn_relu_layer.hpp cudnn_relu_layer.hpp.bak
 mv cudnn_sigmoid_layer.hpp cudnn_sigmoid_layer.hpp.bak
 mv cudnn_tanh_layer.hpp cudnn_tanh_layer.hpp.bak
 

src:
mv cudnn_conv_layer.cpp cudnn_conv_layer.cpp.bak
mv cudnn_conv_layer.cu cudnn_conv_layer.cu.bak

mv cudnn_relu_layer.cpp cudnn_relu_layer.cpp.bak
mv cudnn_relu_layer.cu cudnn_relu_layer.cu.bak

mv cudnn_sigmoid_layer.cpp cudnn_sigmoid_layer.cpp.bak
mv cudnn_sigmoid_layer.cu cudnn_sigmoid_layer.cu.bak

mv cudnn_tanh_layer.cpp cudnn_tanh_layer.cpp.bak
mv cudnn_tanh_layer.cu cudnn_tanh_layer.cu.bak


复制文件:     源文件:/home/a/public1/denglei_codeFile/caffe/  
                     目标文件夹:/home/a/public1/denglei_codeFile/caffe2v/caffe-rpnbf-cudnn5/

cp  /home/a/public1/denglei_codeFile/caffe/include/caffe/util/cudnn.hpp       /home/a/public1/denglei_codeFile/caffe2v/caffe-rpnbf-cudnn5/include/caffe/util/

cp  /home/a/public1/denglei_codeFile/caffe/include/caffe/layers/cudnn_conv_layer.hpp       /home/a/public1/denglei_codeFile/caffe2v/caffe-rpnbf-cudnn5/include/caffe/layers/
cp  /home/a/public1/denglei_codeFile/caffe/include/caffe/layers/cudnn_relu_layer.hpp      /home/a/public1/denglei_codeFile/caffe2v/caffe-rpnbf-cudnn5/include/caffe/layers/
cp  /home/a/public1/denglei_codeFile/caffe/include/caffe/layers/cudnn_sigmoid_layer.hpp       /home/a/public1/denglei_codeFile/caffe2v/caffe-rpnbf-cudnn5/include/caffe/layers/
cp  /home/a/public1/denglei_codeFile/caffe/include/caffe/layers/cudnn_tanh_layer.hpp       /home/a/public1/denglei_codeFile/caffe2v/caffe-rpnbf-cudnn5/include/caffe/layers/

cp  /home/a/public1/denglei_codeFile/caffe/src/caffe/layers/cudnn_conv_layer.cpp      /home/a/public1/denglei_codeFile/caffe2v/caffe-rpnbf-cudnn5/src/caffe/layers/
cp  /home/a/public1/denglei_codeFile/caffe/src/caffe/layers/cudnn_conv_layer.cu      /home/a/public1/denglei_codeFile/caffe2v/caffe-rpnbf-cudnn5/src/caffe/layers/
cp  /home/a/public1/denglei_codeFile/caffe/src/caffe/layers/cudnn_relu_layer.cpp      /home/a/public1/denglei_codeFile/caffe2v/caffe-rpnbf-cudnn5/src/caffe/layers/
cp  /home/a/public1/denglei_codeFile/caffe/src/caffe/layers/cudnn_relu_layer.cu      /home/a/public1/denglei_codeFile/caffe2v/caffe-rpnbf-cudnn5/src/caffe/layers/
cp  /home/a/public1/denglei_codeFile/caffe/src/caffe/layers/cudnn_sigmoid_layer.cpp      /home/a/public1/denglei_codeFile/caffe2v/caffe-rpnbf-cudnn5/src/caffe/layers/
cp  /home/a/public1/denglei_codeFile/caffe/src/caffe/layers/cudnn_sigmoid_layer.cu     /home/a/public1/denglei_codeFile/caffe2v/caffe-rpnbf-cudnn5/src/caffe/layers/
cp  /home/a/public1/denglei_codeFile/caffe/src/caffe/layers/cudnn_tanh_layer.cpp      /home/a/public1/denglei_codeFile/caffe2v/caffe-rpnbf-cudnn5/src/caffe/layers/
cp  /home/a/public1/denglei_codeFile/caffe/src/caffe/layers/cudnn_tanh_layer.cu      /home/a/public1/denglei_codeFile/caffe2v/caffe-rpnbf-cudnn5/src/caffe/layers/

参考链接:
很有用的博客
安装好caffe之后配置Matlab的接口

MatCaffe用法总结
Ubuntu16.04 Caffe 安装步骤记录(超详尽)
caffe的Matlab接口的使用方法

以上是关于糟心的caffe+ matlab编译路程的主要内容,如果未能解决你的问题,请参考以下文章

windows配置caffe及matlab接口编译和调用(cpu和gpu)

ubuntu14.04&matlab2015b 测试caffe的Matlab接口

caffe 在window下编译(windows7, cuda8.0,matlab接口编译)

[Caffe学习日志] 在Ubuntu14.04上安装MATLAB2014a并编译其接口

(转)如何使用caffe的MATLAB接口

ubuntu14.04下安装cudnn5.1.3,opencv3.0,编译caffe及matlab和python接口过程记录