自己练手整理的!!!Caffe编译(CPU-only)+matlab接口实现,部分错误信息及解决方法

环境:VMware14、Ubuntu14.04、Matlab2014A。

我用的是虚拟机,所以只能使用CPU。

Caffe的安装和编译,大家可以参考我前面的文章(传送门),因为要使用Matlab,所以在Makefile.config文件中需要做些更改,我的文件内容如下(我改动的部分):

# CPU-only switch (uncomment to build without GPU support).
 CPU_ONLY := 1

# This is required only if you will compile the matlab interface.
# MATLAB directory should contain the mex binary in /bin.
 MATLAB_DIR := /usr/local/MATLAB/R2014a
# MATLAB_DIR := /Applications/MATLAB_R2012b.app

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial

更改完以后,在Caffe根目录下,重新编译下Caffe,命令如下:

$ sudo make clean
$ sudo make all -j4
$ sudo make test
$ sudo make runtest

这一步一般是没有问题的,Caffe编译成功时,make runtest的结果如下图(末尾截图):

继续在Caffe根目录下执行以下命令:

$ sudo make matcaffe

这一步一般不会出现什么问题,不过会有警告信息,GCC版本的问题,不过没有关系,警告不是error,不用管它。有些人可能会提示一大串错误,看的整个人立马懵逼,错误信息如下:

MEX matlab/+caffe/private/caffe_.cpp
使用 'g++' 编译。
警告: 您使用的 gcc 版本为 '4.8.4'。不支持该版本的 gcc。MEX 当前支持的版本为 '4.7.x'。有关当前支持的编译器列表,请参阅: http://www.mathworks.com/support/compilers/current_release。
警告: 您使用的 gcc 版本为 '4.8.4-2ubuntu1~14.04.4)'。不支持该版本的 gcc。MEX 当前支持的版本为 '4.7.x'。有关当前支持的编译器列表,请参阅: http://www.mathworks.com/support/compilers/current_release。
/opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp: In function ‘void delete_solver(int, mxArray**, int, const mxArray**)’:
/opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp:208:3: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]
   }), solvers_.end());
   ^
/opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp:208:4: error: no matching function for call to ‘remove_if(std::vector<boost::shared_ptr<caffe::Solver<float> > >::iterator, std::vector<boost::shared_ptr<caffe::Solver<float> > >::iterator, delete_solver(int, mxArray**, int, const mxArray**)::__lambda0)’
   }), solvers_.end());
    ^
/opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp:208:4: note: candidate is:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from ./include/caffe/blob.hpp:4,
                 from ./include/caffe/caffe.hpp:7,
                 from /opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp:18:
/usr/include/c++/4.8/bits/stl_algo.h:1134:5: note: template<class _FIter, class _Predicate> _FIter std::remove_if(_FIter, _FIter, _Predicate)
     remove_if(_ForwardIterator __first, _ForwardIterator __last,
     ^
/usr/include/c++/4.8/bits/stl_algo.h:1134:5: note:   template argument deduction/substitution failed:
/opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp: In substitution of ‘template<class _FIter, class _Predicate> _FIter std::remove_if(_FIter, _FIter, _Predicate) [with _FIter = __gnu_cxx::__normal_iterator<boost::shared_ptr<caffe::Solver<float> >*, std::vector<boost::shared_ptr<caffe::Solver<float> > > >; _Predicate = delete_solver(int, mxArray**, int, const mxArray**)::__lambda0]’:
/opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp:208:4:   required from here
/opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp:208:4: error: template argument for ‘template<class _FIter, class _Predicate> _FIter std::remove_if(_FIter, _FIter, _Predicate)’ uses local type ‘delete_solver(int, mxArray**, int, const mxArray**)::__lambda0’
   }), solvers_.end());
    ^
/opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp:208:4: error:   trying to instantiate ‘template<class _FIter, class _Predicate> _FIter std::remove_if(_FIter, _FIter, _Predicate)’
/opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp: In function ‘void delete_net(int, mxArray**, int, const mxArray**)’:
/opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp:293:3: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]
   }), nets_.end());
   ^
/opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp:293:4: error: no matching function for call to ‘remove_if(std::vector<boost::shared_ptr<caffe::Net<float> > >::iterator, std::vector<boost::shared_ptr<caffe::Net<float> > >::iterator, delete_net(int, mxArray**, int, const mxArray**)::__lambda1)’
   }), nets_.end());
    ^
/opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp:293:4: note: candidate is:
In file included from /usr/include/c++/4.8/algorithm:62:0,
                 from ./include/caffe/blob.hpp:4,
                 from ./include/caffe/caffe.hpp:7,
                 from /opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp:18:
/usr/include/c++/4.8/bits/stl_algo.h:1134:5: note: template<class _FIter, class _Predicate> _FIter std::remove_if(_FIter, _FIter, _Predicate)
     remove_if(_ForwardIterator __first, _ForwardIterator __last,
     ^
/usr/include/c++/4.8/bits/stl_algo.h:1134:5: note:   template argument deduction/substitution failed:
/opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp: In substitution of ‘template<class _FIter, class _Predicate> _FIter std::remove_if(_FIter, _FIter, _Predicate) [with _FIter = __gnu_cxx::__normal_iterator<boost::shared_ptr<caffe::Net<float> >*, std::vector<boost::shared_ptr<caffe::Net<float> > > >; _Predicate = delete_net(int, mxArray**, int, const mxArray**)::__lambda1]’:
/opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp:293:4:   required from here
/opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp:293:4: error: template argument for ‘template<class _FIter, class _Predicate> _FIter std::remove_if(_FIter, _FIter, _Predicate)’ uses local type ‘delete_net(int, mxArray**, int, const mxArray**)::__lambda1’
   }), nets_.end());
    ^
/opt/MSRNet-master/caffe-master/matlab/+caffe/private/caffe_.cpp:293:4: error:   trying to instantiate ‘template<class _FIter, class _Predicate> _FIter std::remove_if(_FIter, _FIter, _Predicate)’

make: *** [matlab/+caffe/private/caffe_.mexa64] 错误 255

 

遇到这种情况,就在caffe根目录下使用命令:sudo gedit Makefile打开Makefile文件,注意不是Makefile.config。在410行添加如下信息:

 

# Automatic dependency generation (nvcc is handled separately)
CXXFLAGS += -MMD -MP
CXXFLAGS += -std=c++11 #把这一行加进去,上面两行是已经存在的

然后在重新执行:sudo make matcaffe,问题就解决了。

同样是在Caffe根目录下,继续执行make mattest,命令如下:

$ sudo make mattest

这一步,大部分人会报错,我们根据报错的步骤,一个个解决。

错误1:详细信息看下图

出现这个错误的原因就是因为在编译caffe时使用的是系统的libstdc++.so.6,但是matlab使用的是自带的,解决方法就是在matlab中建一个软链接指向系统这个文件的位置,命令如下:

$ sudo ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6 /usr/local/MATLAB/R2014a/sys/os/glnxa64/libstdc++.so.6

错误2:解决错误1,重新执行make mattest,可能会报一以下错误:

由于caffe编译时使用的opencv和matlab使用的不一样,所以我们要做出如下更改:

$ cd /usr/local/MATLAB/R2014a/bin/glnxa64
$ sudo ln -s /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4 libopencv_core.so.2.4
$ sudo ln -s /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4 libopencv_highgui.so.2.4
$ sudo ln -s /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4 libopencv_imgproc.so.2.4

更改完以后,再执行make mattest,就会成功了。但是如果你使用的是Matlab2016b,你可能会遇到以下的错误:

这个错误,我没能在Matlab2016b中解决,事实上,刚开始我使用的环境是:Ubuntu16.04+Matlab2016b,费了很大劲,克服重重困难,解决了上面的两个错误,最后他报了这个,我彻底没招,然后我继续看官方安装文档,发现它支持的版本是Matlab2015以下的,所以我试试看的态度安装了Ubuntu14.04+Matlab2014a,然后把之前的流程重新做了一遍,最后编译成功。如果你也和我遇上了同样的问题,又不知道如何解决的时候,你可以像我一样使用更低版本的Matlab,看来有时候太新也不是一件好事。

自己安装编译caffe+matcaffe花了很多时间,前后有十天左右,因为Linux自己之前从没有接触过,复制文件或者对文件重命名的命令都不知道,分享自己安装编译过程遇到的常见问题,希望能帮到你。还会有一些其他的问题,大多数是因为你的依赖库没有装好,安装caffe之前,一定要确保自己的caffe依赖库是全部安装完毕的,避免出现错误以后,影响自己的心情。

©️2020 CSDN 皮肤主题: 深蓝海洋 设计师:CSDN官方博客 返回首页