远程服务器 ubuntu16.04 caffe+matlab r2014b+python+cpu配置(三)

Caffe配置过程中的一些问题

参考链接:
https://groups.google.com/forum/#!topic/caffe-users/x8Y26vf-or8
http://blog.csdn.net/striker_v/article/details/52890747
http://www.echojb.com/cuda/2017/05/05/375103.html
http://www.caffecn.cn/?/question/1113
https://blog.csdn.net/dym755833564/article/details/77965966

GCC版本太新

在make过程中可能 string.h ‘memcy’ was not declared in this scope的错误,这是因为ubuntu中gcc编译器版本太新,解决方法是打开Makefile文件,找到下面这行代码(大概在409行左右):

1
NVCCFLAGS += -ccbin=(CXX)−Xcompiler−fPIC(COMMON_FLAGS)

将其改为:

1
NVCCFLAGS += -D_FORCE_INLINES -ccbin=(CXX)−Xcompiler−fPIC(COMMON_FLAGS)

make matcaffe时报错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/home/zkk/caffe/matlab/+caffe/private/caffe_.cpp: In function ‘void delete_solver(int, mxArray**, int, const mxArray**)’:
/home/zkk/caffe/matlab/+caffe/private/caffe_.cpp:208:3: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]
/home/zkk/caffe/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**)::<lambda(const boost::shared_ptr<caffe::Solver<float> >&)>)’
/home/zkk/caffe/matlab/+caffe/private/caffe_.cpp:208:4: note: candidate is:
In file included from /usr/include/c++/4.7/algorithm:63:0,
from ./include/caffe/blob.hpp:4,
from ./include/caffe/caffe.hpp:7,
from /home/zkk/caffe/matlab/+caffe/private/caffe_.cpp:18:
/usr/include/c++/4.7/bits/stl_algo.h:1166:5: note: template<class _FIter, class _Predicate> _FIter std::remove_if(_FIter, _FIter, _Predicate)
/home/zkk/caffe/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**)::<lambda(const boost::shared_ptr<caffe::Solver<float> >&)>’
/home/zkk/caffe/matlab/+caffe/private/caffe_.cpp:208:4: error: trying to instantiate ‘template<class _FIter, class _Predicate> _FIter std::remove_if(_FIter, _FIter, _Predicate)’
/home/zkk/caffe/matlab/+caffe/private/caffe_.cpp: In function ‘void delete_net(int, mxArray**, int, const mxArray**)’:
/home/zkk/caffe/matlab/+caffe/private/caffe_.cpp:293:3: warning: lambda expressions only available with -std=c++11 or -std=gnu++11 [enabled by default]
/home/zkk/caffe/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**)::<lambda(const boost::shared_ptr<caffe::Net<float> >&)>)’
/home/zkk/caffe/matlab/+caffe/private/caffe_.cpp:293:4: note: candidate is:
In file included from /usr/include/c++/4.7/algorithm:63:0,
from ./include/caffe/blob.hpp:4,
from ./include/caffe/caffe.hpp:7,
from /home/zkk/caffe/matlab/+caffe/private/caffe_.cpp:18:
/usr/include/c++/4.7/bits/stl_algo.h:1166:5: note: template<class _FIter, class _Predicate> _FIter std::remove_if(_FIter, _FIter, _Predicate)
/home/zkk/caffe/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**)::<lambda(const boost::shared_ptr<caffe::Net<float> >&)>’
/home/zkk/caffe/matlab/+caffe/private/caffe_.cpp:293:4: error: trying to instantiate ‘template<class _FIter, class _Predicate> _FIter std::remove_if(_FIter, _FIter, _Predicate)’

Makefile:518: recipe for target 'matlab/+caffe/private/caffe_.mexa64' failed
make: *** [matlab/+caffe/private/caffe_.mexa64] Error 255

修改Makefile里面的CXXFLAGS,添加CXXFLAGS += -std=c++11.然后重新编译就可以了,即:

1
2
CXXFLAGS += -MMD -MP
CXXFLAGS += -std=c++11

注意: 是添加不是修改

make mattest时报错:

1
2
3
4
5
6
7
8
9
Invalid MEX-file 'caffe/matlab/+caffe/private/caffe_.mexa64':
caffe/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();

有些说法是要执行如下命令:

1
2
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
export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6

但是再次运行make mattest时会有新错误:

1
2
3
4
malloc: unknown:0: assertion botched
free: called with unallocated block argument
last command: (null)
Aborting...find: ‘bash’ terminated by signal 6

虽然最后还是能运行成功,但发现在运行bash指令时无法成功执行,纠结了好久发现是这里没有彻底解决问题,解决方法是:

将 libopencv_imgproc.so.2.4, libopencv_core.so.2.4, libopencv_highgui.so.2.4三个文件拷贝到/usr/local/MATLAB/R2016b/bin/glnxa64/中

1
2
3
4
5
6
7
8
$ sudo mv /usr/local/MATLAB/R2016b/bin/glnxa64/libopencv_imgproc.so.2.4 /usr/local/MATLAB/R2016b/bin/glnxa64/libopencv_imgproc.so.2.4.old
$ sudo cp /usr/lib/x86_64-linux-gnu/libopencv_imgproc.so.2.4 /usr/local/MATLAB/R2016b/bin/glnxa64/

$ sudo mv /usr/local/MATLAB/R2016b/bin/glnxa64/libopencv_core.so.2.4 /usr/local/MATLAB/R2016b/bin/glnxa64/libopencv_core.so.2.4.old
$ sudo cp /usr/lib/x86_64-linux-gnu/libopencv_core.so.2.4 /usr/local/MATLAB/R2016b/bin/glnxa64/

$ sudo mv /usr/local/MATLAB/R2016b/bin/glnxa64/libopencv_highgui.so.2.4 /usr/local/MATLAB/R2016b/bin/glnxa64/libopencv_highgui.so.2.4.old
$ sudo cp /usr/lib/x86_64-linux-gnu/libopencv_highgui.so.2.4 /usr/local/MATLAB/R2016b/bin/glnxa64/

再执行

1
export LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libstdc++.so.6

问题就解决啦,bash指令也没有问题啦

关于opencv

make all时报错,错误信息:

1
2
3
4
5
6
7
...
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 to cv::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
...
Makefile:518: recipe for target 'matlab/+caffe/private/caffe_.mexa64' failed
make: *** [matlab/+caffe/private/caffe_.mexa64] Error 255

首先,已经配置过了opencv,可以这样查询安装版本:

1
$ pkg-config --modversion opencv

所以出现上面的错误,应该是opencv_imgcodecs链接的问题,比较有效的解决方案是,把opencv需要的lib添加到Makefile文件中,找到LIBRARIES(在PYTHON_LIBRARIES := boost_python python2.7 前一行),并进行修改,向该句末尾添加:

1
opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

我的Makefile文件修改后为:

1
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5 opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs