您的位置:首页 > Web前端

在服务器上安装caffe环境(ubuntu14.04+非root权限+opencv3.3.1+anaconda3)

2018-03-13 15:36 3119 查看

已经装好的依赖

CUDA-8.0

anaconda3

安装cmake

在安装某一个依赖库的时候,出现服务器上cmake版本不够高的问题,于是在自己的用户上安装较新版本的cmake

从官网上下载对应版本的cmake: https://cmake.org/download/

解压
tar zxvf cmake.tar.gz
创建链接,这里把链接创建在自己有权限的目录下面。并且把这个目录要添加到.bashrc文件中的PATH中,并激活.bashrc文件
ln -s /home/user/cmake/bin/* /home/user/bin/
查看cmake版本号以验证是否安装成功
cmake --version


安装OpenCV3.3.1

opencv下载链接:https://opencv.org/

/home/user是用户的根目录

tar zxvf opencv-3.3.1.tar.gz
cd opencv-3.3.1
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=release -D CMAKE_INSTALL_PREFIX=/home/user/local ..
make install


在.bashrc中添加以下文件

# opencv,这个lib可能不该添加这里的
export LD_LIBRARY_PATH=/home/user/opencv-3.3.1/build/lib:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/home/user/local/lib/pkgconfig:$PKG_CONFIG_PATH


其中PKG_CONFIG_PATH里放的其实是opencv.pc所在的目录

安装各种依赖库

服务器上基本有了这些安装环境,我也不能确定编译的时候是用的哪边的环境,在用户根目录的.bashrc文件里放入path和librarypath。在安装完每个包后,查看一下path路径下的文件,可能需要在makefile里添加上/home/user/local/include/glog类似的问件才能够保证正确

export PATH=/home/user/local/include:$PATH
export LD_LIBRARY_PATH=/home/user/local/lib:$LD_LIBRARY_PATH


记得每次放完要激活文件

source ./bashrc


安装protobuf

这个包的安装我是使用的anaconda

conda install protobuf


然后把/home/user/anaconda3/pkgs/libprotobuf-3.4.0-0/lib添加到makefile的LIBRARY_DIRS里。这一步后来出错了,所以也把这个路径删掉了

安装snappy

下载
git clone https://github.com/google/snappy cd snappy
mkdir build
cmake ../
make


安装leveldb

git clone https://github.com/google/leveldb.git cd leveldb
make


../leveldb/include/leveldb把这个添加到makefile的include里

把../leveldb/out-shared和../leveldb/out-static添加到makefile的path里

安装gflag

git clone https://github.com/gflags/gflags mkdir build
cd build
cmake -D CMAKE_INSTALL_PREFIX=/home/user/local -D BUILD_gflags_LIBS=ON -D BUILD_gflags_nothreads_LIBS=ON -D GFLAGS_NAMESPACE=ON ..
make install


安装glog

这里我一直装不上,是先conda install gflags才装好make成功的

git clone https://github.com/google/glog mkdir build
cd build
cmake -D CMAKE_INSTALL_PREFIX=/home/user/local ..
make install


caffe安装

git clone https://github.com/BVLC/caffe cd caffe
make all -j16
make test -j16
make runtest -j16
make pycaffe -j16


把caffe路径添加到~/.bashrc中,source ~./bashrc

#Caffe Root
export CAFFE_ROOT="/home/user/caffe"
export PYTHONPATH="/home/user/caffe/python:$PYTHONPATH"


打开python,import caffe不出错则安装成功

出现问题

/home/user/anaconda3/lib/libopencv_imgcodecs.so: undefined reference to `jpeg_stdio_src@LIBJPEG_9.0'
/home/user/anaconda3/lib/libopencv_imgcodecs.so: undefined reference to `jpeg_finish_compress@LIBJPEG_9.0'
/home/user/anaconda3/lib/libopencv_imgcodecs.so: undefined reference tocollect2: error: ld returned 1 exit status
`png_set_filter@PNG16_0'
/home/user/anaconda3/lib/libopencv_imgcodecs.so: undefined reference to `png_set_rgb_to_gray@PNG16_0'
/home/user/anaconda3make: /lib/libopencv_imgcodecs.so: undefined *** [.build_release/tools/upgrade_net_proto_text.bin] Error 1reference
to `jpeg_calc_output_dimensions@LIBJPEG_9.0'
/home/user/anaconda3/lib/libopencv_imgcodecs.so: undefined reference to `jpeg_std_error@LIBJPEG_9.0'
/home/user/anaconda3/lib/libopencv_imgcodecs.so: undefined reference to `png_init_io@PNG16_0'
/home/user/anaconda3/lib/libopencv_imgcodecs.so: undefined reference to `jpeg_default_qtables@LIBJPEG_9.0'


在~/.bashrc中添加:

export LD_LIBRARY_PATH=/home/user/anaconda3/lib:$LD_LIBRARY_PATH


CXX/LD -o .build_release/examples/cpp_classification/classification.bin
.build_release/lib/libcaffe.so: undefined reference to `leveldb::DB::Open(leveldb::Options const&, std::string const&, leveldb::DB**)'
.build_release/lib/libcaffe.so: undefined reference to `google::base::CheckOpMessageBuilder::NewString()'
.build_release/lib/libcaffe.so: undefined reference to `leveldb::Status::ToString() const'
collect2: error: ld returned 1 exit status
make: *** [.build_release/tools/upgrade_solver_proto_text.bin] Error 1
make: *** Waiting for unfinished jobs....
.build_release/lib/libcaffe.so: undefined reference to `leveldb::DB::Open(leveldb::Options const&, std::string const&, leveldb::DB**)'


找到anaconda里存放libpng库的文件夹/home/user/anaconda3/pkgs/libpng-1.6.34-hb9fc6fc_0/lib,把目录加进caffe makefile的LIBRARY_DIRS里

出现opencv2和3冲突的问题


这里服务器自带的是opencv2的版本,最后我屏蔽掉了makefile里的系统lib和path就正常了

[libprotobuf FATAL /tmp/build/80754af9/opencv_1512491966560/work/3rdparty/protobuf/src/google/protobuf/stubs/common.cc:67] This program requires version 3.4.0 of the Protocol Buffer runtime library, but the installed version is 3.1.0.  Please update your library.  If you compiled the program yourself, make sure that your headers are from the same version of Protocol Buffers as your link-time library.  (Version verification failed in "google/protobuf/any.pb.cc".)
terminate called after throwing an instance of 'google::protobuf::FatalException'
what():  This program requires version 3.4.0 of the Protocol Buffer runtime library, but the installed version is 3.1.0.  Please update your library.  If you compiled the program yourself, make sure that your headers are from the same version of Protocol Buffers as your link-time library.  (Version verification failed in "google/protobuf/any.pb.cc".)
make: *** [runtest] Aborted (core dumped)


这个问题似乎是因为编译的protobuf和python里的protobuf版本不一样导致的。虽然提示写的是更新,但是根据在网上搜索的结果。好像更新不能解决问题,而是要把版本降到3.1.0.

这里选择下载对应版本的protobuf试图解决,从这个网站上也可以找到其他版本的protobuf

wget https://github.com/google/protobuf/archive/v3.1.0.tar.gz cd protobuf-3.1.0
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/home/user/local -D protobuf_BUILD_TESTS=OFF -D CFLAGS="-fPIC" ../cmake
make install


这里不加-D protobuf_BUILD_TESTS=OFF会报错,错误如下。

Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.8") CMake Error at tests.cmake:2 (message): Cannot find gmock directory. Call Stack (most recent call first): CMakeLists.txt:172 (include) -- Configuring incomplete, errors occurred!


最后在~/.bashrc里加上,source ~./bashrc,并把anaconda里的prorotobuf和libprotobuf卸载

export PATH=/home/user/local/include/google/protobuf/:$PATH


但是这么做了以后make all出现了一个新问题

/usr/bin/ld: /home/user/local/lib/libprotobuf.a(arenastring.cc.o): relocation R_X86_64_32 against `__gxx_personality_v0' can not be used when making a shared object; recompile with -fPIC
/home/user/local/lib/libprotobuf.a: error adding symbols: Bad value
collect2: error: ld returned 1 exit status
make: *** [.build_release/lib/libcaffe.so.1.0.0] Error 1


于是准备重新用conda装

conda install -c msarahan libprotobuf=3.1.0
conda install -c msarahan protobuf=3.1.0


.build_release/tools/caffe
caffe: command line brew
usage: caffe <command> <args>

commands:
train           train or finetune a model
test            score a model
device_query    show GPU diagnostic information
time            benchmark model execution time

Flags from tools/caffe.cpp:
-gpu (Optional; run in GPU mode on given device IDs separated by ','.Use
'-gpu all' to run on all available GPUs. The effective training batch
size is multiplied by the number of devices.) type: string default: ""
-iterations (The number of iterations to run.) type: int32 default: 50
-level (Optional; network level.) type: int32 default: 0
-model (The model definition protocol buffer text file.) type: string
default: ""
-phase (Optional; network phase (TRAIN or TEST). Only used for 'time'.)
type: string default: ""
-sighup_effect (Optional; action to take when a SIGHUP signal is received:
snapshot, stop or none.) type: string default: "snapshot"
-sigint_effect (Optional; action to take when a SIGINT signal is received:
snapshot, stop or none.) type: string default: "stop"
-snapshot (Optional; the snapshot solver state to resume training.)
type: string default: ""
-solver (The solver definition protocol buffer text file.) type: string
default: ""
-stage (Optional; network stages (not to be confused with phase), separated
by ','.) type: string default: ""
-weights (Optional; the pretrained weights to initialize finetuning,
separated by ','. Cannot be set simultaneously with snapshot.)
type: string default: ""
*** Error in `.build_release/tools/caffe': free(): invalid pointer: 0x00002b04f3b1e8e0 ***
*** Aborted at 1520918736 (unix time) try "date -d @1520918736" if you are using GNU date ***
PC: @     0x2b04f50e0c37 gsignal
*** SIGABRT (@0x3fa0000740a) received by PID 29706 (TID 0x2b04f2267ec0) from PID 29706; stack trace: ***
@     0x2b04f50e0cb0 (unknown)
@     0x2b04f50e0c37 gsignal
@     0x2b04f50e4028 abort
@     0x2b04f511d2a4 (unknown)
@     0x2b04f512982e (unknown)
@     0x2b04f4494ca5 std::string::~string()
@     0x2b05008ce8bc google::protobuf::util::Status::~Status()
@     0x2b04f50e653a __cxa_finalize
@     0x2b05008c9aa6 (unknown)
make: *** [runtest] Aborted (core dumped)


这里怀疑因为多gpu引起的。但import caffe成功了也没能解决这个问题。

./include/caffe/util/cudnn.hpp:8:34: fatal error: caffe/proto/caffe.pb.h: No such file or directory
#include "caffe/proto/caffe.pb.h"


使用protoc去生成这些文件

# In the directory you installed Caffe to
protoc src/caffe/proto/caffe.proto --cpp_out=.
mkdir include/caffe/proto
mv src/caffe/proto/caffe.pb.h include/caffe/proto


import caffe的时候出现

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/caffe/python/caffe/__init__.py", line 1, in <module>
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
File "/home/user/caffe/python/caffe/pycaffe.py", line 13, in <module>
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
ImportError: /home/user/caffe/python/caffe/../../build/lib/libcaffe.so.1.0.0: undefined symbol: _ZN7leveldb2DB4OpenERKNS_7OptionsERKSsPPS0_


找了很久找不到,看到错误里有leveldb,于是把anaconda里的leveldb卸载掉,就正常了。

In file included from ./include/caffe/common.hpp:6:0,
from ./include/caffe/blob.hpp:8,
from ./include/caffe/caffe.hpp:7,
from tools/upgrade_net_proto_text.cpp:10:
./include/caffe/net.hpp: In member function ‘const std::vector<caffe::Blob<Dtype>*>& caffe::Net<Dtype>::ForwardPrefilled(Dtype*)’:
/home/user/anaconda3/include/glog/logging.h:943:30: warning: typedef ‘INVALID_REQUESTED_LOG_SEVERITY’ locally defined but not used [-Wunused-local-typedefs]
INVALID_REQUESTED_LOG_SEVERITY);           \
^
/home/user/anaconda3/include/glog/logging.h:938:73: note: in definition of macro ‘GOOGLE_GLOG_COMPILE_ASSERT’
typedef google::glog_internal_namespace_::CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
^
./include/caffe/net.hpp:41:5: note: in expansion of macro ‘LOG_EVERY_N’
LOG_EVERY_N(WARNING, 1000) << "DEPRECATED: ForwardPrefilled() "
^
CXX tools/compute_image_mean.cpp
CXX tools/convert_imageset.cpp
In file included from ./include/caffe/common.hpp:6:0,
from ./include/caffe/blob.hpp:8,
from ./include/caffe/caffe.hpp:7,
from tools/upgrade_solver_proto_text.cpp:10:
./include/caffe/net.hpp: In member function ‘const std::vector<caffe::Blob<Dtype>*>& caffe::Net<Dtype>::ForwardPrefilled(Dtype*)’:
/home/user/anaconda3/include/glog/logging.h:943:30: warning: typedef ‘INVALID_REQUESTED_LOG_SEVERITY’ locally defined but not used [-Wunused-local-typedefs]
INVALID_REQUESTED_LOG_SEVERITY);           \
^
/home/user/anaconda3/include/glog/logging.h:938:73: note: in definition of macro ‘GOOGLE_GLOG_COMPILE_ASSERT’
typedef google::glog_internal_namespace_::CompileAssert<(bool(expr))> msg[bool(expr) ? 1 : -1]
^
./include/caffe/net.hpp:41:5: note: in expansion of macro ‘LOG_EVERY_N’
LOG_EVERY_N(WARNING, 1000) << "DEPRECATED: ForwardPrefilled() "


我把anaconda里的glog卸载就没有出现这个了

感想

真的是天坑,各种冲突,前前后后花了我快一个星期的时间。这篇文章也不见得能帮大家解决什么问题,因为有的问题尝试了多种解决方法,不一定记录下来的就是正确的。仅仅当作一篇写给自己的记录。

参考网站

[1]Caffe安装:http://kingofprank.com/2017/11/12/anaconda+TensorFlow+caffe/

[2][caffe笔记001]:caffe依赖库安装(非root):http://blog.csdn.net/happyflyy/article/details/53431772
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: