这篇文章主要讲安装和编译caffe过程以及遇到的问题,还有解决方法。关于如何安装nvidia驱动,cuda,cudnn,以及opencv3 等网上已经有足够多的教程了,可以直接在网上找。
OS: Ubuntu16.04
python Version: python3
CUDA Version: 10.0
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
下载地址:git clone https://github.com/BVLC/caffe.git
cp Makefile.config.example Makefile.config
# USE_CUDNN := 1
# USE_OPENCV := 0
# OPENCV_VERSION := 3
# WITH_PYTHON_LAYER := 1
# 改为:
USE_CUDNN := 1
USE_OPENCV := 1
OPENCV_VERSION := 3
WITH_PYTHON_LAYER := 1
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib
改为:
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 /usr/lib/x86_64-linux-gnu/hdf5/serial
使用python3.5版本,把python2.7代码加注释,python3.5前的注释去掉
#PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/lib/python2.7/dist-packages/numpy/core/include
PYTHON_INCLUDE := /usr/include/python3.5m \
/usr/lib/python3.5/dist-packages/numpy/core/include
NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
改为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m
改为:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
make all -j8
遇到错误:
1. 编译选项问题
/usr/include/c++/4.9/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
#error This file requires compiler and library support for the......
/usr/local/include/google/protobuf/arena.h:324:36: note: template argument deduction/substitution failed:
Makefile:600: recipe for target '.build_release/src/caffe/proto/caffe.pb.o' failed
make: *** [.build_release/src/caffe/proto/caffe.pb.o] Error 1
解决方法:请看这位大神是如何操作的https://blog.csdn.net/pkwcxy/article/details/82351669?utm_source=blogxgwz8
在Makefile文件中找到:
##############################
# Configure build
##############################找到:
# Linux
添加:CXXFLAGS += -std=c++11
安装上面的方法一开始没报错,但过了一段时间后还是报同样的错误,再进一步如下修改,打开Makefile文件,给CXXFLAGS、NVCCFLAGS、LINKFLAGS都使用-std=c++11标准编译:
CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
改为:
CXXFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) -std=c++11
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)
改为:
NVCCFLAGS += -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS) -std=c++11
LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)
改为:
LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS) -std=c++11
2. CUDA版本问题
重新编译还是有错误:
NVCC src/caffe/solvers/adagrad_solver.cu
nvcc fatal : Unsupported gpu architecture 'compute_20'
NVCC src/caffe/solvers/sgd_solver.cu
nvcc fatal : Unsupported gpu architecture 'compute_20'
Makefile:607: recipe for target '.build_release/cuda/src/caffe/solvers/rmsprop_solver.o' failed
原因是不支持GPU架构'compute_20',在Makefile.config里面的找到
CUDA_ARCH := -gencode arch=compute_20,code=sm_20
-gencode arch=compute_20,code=sm_21 \
-gencode arch=compute_30,code=sm_30 \
-gencode arch=compute_35,code=sm_35 \
-gencode arch=compute_50,code=sm_50 \
-gencode arch=compute_52,code=sm_52 \
-gencode arch=compute_60,code=sm_60 \
-gencode arch=compute_61,code=sm_61 \
-gencode arch=compute_61,code=compute_61
把前面compute_20相关的那2行删除
-gencode arch=compute_20,code=sm_20
-gencode arch=compute_20,code=sm_21
3. g++版本问题
.build_release/lib/libcaffe.so:对‘leveldb::DB::Open(leveldb::Options const&, std::string const&, leveldb::DB**)’未定义的引用
.build_release/lib/libcaffe.so:对‘google::base::CheckOpMessageBuilder::NewString()’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::Status::ToString() const’未定义的引用
collect2: error: ld returned 1 exit status
Makefile:637: recipe for target '.build_release/tools/upgrade_solver_proto_text.bin' failed
make: *** [.build_release/tools/upgrade_solver_proto_text.bin] Error 1
make: *** 正在等待未完成的任务....
......
这个g++版本问题,解决方法这里参考:https://blog.csdn.net/chenshuibiao/article/details/78734957
先查看g++版本:
gcc --version # 或 g++ --version
gcc (Ubuntu 4.9.3-13ubuntu2) 4.9.3
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
发现我的是4.9.3版本,需要升级gcc版本。查看我Ubuntu系统发现里面已经存在更高级的5版本,就不用再安装了,直接强制软链接一下即可:
sudo ln -s /usr/bin/gcc-5 /usr/bin/gcc -f
sudo ln -s /usr/bin/g++-5 /usr/bin/g++ -f
再次查看g++版本发现已经变成5.4.0了
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.11) 5.4.0 20160609
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
再次编译caffe源码,发现没问题了。
4. python版本问题
如果在Makefile.config文件里面Python相关的配置有问题,可能会遇到以下的问题:
CXX/LD -o .build_release/tools/upgrade_net_proto_text.bin
CXX/LD -o .build_release/examples/siamese/convert_mnist_siamese_data.bin
.build_release/lib/libcaffe.so:对‘leveldb::DB::Open(leveldb::Options const&, std::string const&, leveldb::DB**)’未定义的引用
.build_release/lib/libcaffe.so:对‘google::base::CheckOpMessageBuilder::NewString()’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::Status::ToString() const’未定义的引用
.build_release/tools/convert_imageset.o:在函数‘std::string* google::MakeCheckOpString(unsigned long const&, int const&, char const*)’中:
convert_imageset.cpp:(.text._ZN6google17MakeCheckOpStringImiEEPSsRKT_RKT0_PKc[_ZN6google17MakeCheckOpStringImiEEPSsRKT_RKT0_PKc]+0x50):对‘google::base::CheckOpMessageBuilder::NewString()’未定义的引用
.build_release/tools/convert_imageset.o:在函数‘main’中:
convert_imageset.cpp:(.text.startup+0x347):对‘google::SetUsageMessage(std::string const&)’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::DB::Open(leveldb::Options const&, std::string const&, leveldb::DB**)’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::Status::ToString() const’未定义的引用
.build_release/tools/caffe.o:在函数‘std::string* google::MakeCheckOpString(cudaError const&, cudaError const&, char const*)’中:
caffe.cpp:(.text._ZN6google17MakeCheckOpStringI9cudaErrorS1_EEPSsRKT_RKT0_PKc[_ZN6google17MakeCheckOpStringI9cudaErrorS1_EEPSsRKT_RKT0_PKc]+0x50):对‘google::base::CheckOpMessageBuilder::NewString()’未定义的引用
.build_release/tools/caffe.o:在函数‘std::string* google::MakeCheckOpString(unsigned long const&, int const&, char const*)’中:
caffe.cpp:(.text._ZN6google17MakeCheckOpStringImiEEPSsRKT_RKT0_PKc[_ZN6google17MakeCheckOpStringImiEEPSsRKT_RKT0_PKc]+0x50):对‘google::base::CheckOpMessageBuilder::NewString()’未定义的引用
.build_release/tools/caffe.o:在函数‘main’中:
caffe.cpp:(.text.startup+0x53):对‘google::SetVersionString(std::string const&)’未定义的引用
caffe.cpp:(.text.startup+0x86):对‘google::SetUsageMessage(std::string const&)’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::DB::Open(leveldb::Options const&, std::string const&, leveldb::DB**)’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::Status::ToString() const’未定义的引用
.build_release/tools/extract_features.o:在函数‘std::string* google::MakeCheckOpString(int const&, int const&, char const*)’中:
extract_features.cpp:(.text._ZN6google17MakeCheckOpStringIiiEEPSsRKT_RKT0_PKc[_ZN6google17MakeCheckOpStringIiiEEPSsRKT_RKT0_PKc]+0x50):对‘google::base::CheckOpMessageBuilder::NewString()’未定义的引用
.build_release/tools/extract_features.o:在函数‘std::string* google::MakeCheckOpString(unsigned long const&, unsigned long const&, char const*)’中:
extract_features.cpp:(.text._ZN6google17MakeCheckOpStringImmEEPSsRKT_RKT0_PKc[_ZN6google17MakeCheckOpStringImmEEPSsRKT_RKT0_PKc]+0x51):对‘google::base::CheckOpMessageBuilder::NewString()’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::DB::Open(leveldb::Options const&, std::string const&, leveldb::DB**)’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::Status::ToString() const’未定义的引用
.build_release/tools/compute_image_mean.o:在函数‘std::string* google::MakeCheckOpString(int const&, int const&, char const*)’中:
compute_image_mean.cpp:(.text._ZN6google17MakeCheckOpStringIiiEEPSsRKT_RKT0_PKc[_ZN6google17MakeCheckOpStringIiiEEPSsRKT_RKT0_PKc]+0x50):对‘google::base::CheckOpMessageBuilder::NewString()’未定义的引用
.build_release/tools/compute_image_mean.o:在函数‘std::string* google::MakeCheckOpString(unsigned long const&, int const&, char const*)’中:
compute_image_mean.cpp:(.text._ZN6google17MakeCheckOpStringImiEEPSsRKT_RKT0_PKc[_ZN6google17MakeCheckOpStringImiEEPSsRKT_RKT0_PKc]+0x50):对‘google::base::CheckOpMessageBuilder::NewString()’未定义的引用
.build_release/tools/compute_image_mean.o:在函数‘main’中:
compute_image_mean.cpp:(.text.startup+0x172):对‘google::SetUsageMessage(std::string const&)’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::DB::Open(leveldb::Options const&, std::string const&, leveldb::DB**)’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::Status::ToString() const’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::DB::Open(leveldb::Options const&, std::string const&, leveldb::DB**)’未定义的引用
.build_release/lib/libcaffe.so:对‘google::base::CheckOpMessageBuilder::NewString()’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::Status::ToString() const’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::DB::Open(leveldb::Options const&, std::string const&, leveldb::DB**)’未定义的引用
.build_release/lib/libcaffe.so:对‘google::base::CheckOpMessageBuilder::NewString()’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::Status::ToString() const’未定义的引用
.build_release/examples/siamese/convert_mnist_siamese_data.o:在函数‘convert_dataset(char const*, char const*, char const*)’中:
convert_mnist_siamese_data.cpp:(.text+0x4cf):对‘leveldb::DB::Open(leveldb::Options const&, std::string const&, leveldb::DB**)’未定义的引用
.build_release/examples/siamese/convert_mnist_siamese_data.o:在函数‘std::string* google::MakeCheckOpString(unsigned int const&, int const&, char const*)’中:
convert_mnist_siamese_data.cpp:(.text._ZN6google17MakeCheckOpStringIjiEEPSsRKT_RKT0_PKc[_ZN6google17MakeCheckOpStringIjiEEPSsRKT_RKT0_PKc]+0x50):对‘google::base::CheckOpMessageBuilder::NewString()’未定义的引用
.build_release/examples/siamese/convert_mnist_siamese_data.o:在函数‘std::string* google::MakeCheckOpString(unsigned int const&, unsigned int const&, char const*)’中:
convert_mnist_siamese_data.cpp:(.text._ZN6google17MakeCheckOpStringIjjEEPSsRKT_RKT0_PKc[_ZN6google17MakeCheckOpStringIjjEEPSsRKT_RKT0_PKc]+0x50):对‘google::base::CheckOpMessageBuilder::NewString()’未定义的引用
.build_release/lib/libcaffe.so:对‘leveldb::Status::ToString() const’未定义的引用
collect2: error: ld returned 1 exit status
collect2: error: ld returned 1 exit status
collect2: error: ld returned 1 exit status
collect2: error: ld returned 1 exit status
collect2: error: ld returned 1 exit status
collect2: error: ld returned 1 exit status
Makefile:637: recipe for target '.build_release/tools/compute_image_mean.bin' failed
collect2: error: ld returned 1 exit status
collect2: error: ld returned 1 exit status
make: *** [.build_release/tools/compute_image_mean.bin] Error 1
make: *** 正在等待未完成的任务....
Makefile:637: recipe for target '.build_release/tools/caffe.bin' failed
make: *** [.build_release/tools/caffe.bin] Error 1
Makefile:637: recipe for target '.build_release/tools/extract_features.bin' failed
make: *** [.build_release/tools/extract_features.bin] Error 1
Makefile:637: recipe for target '.build_release/tools/upgrade_solver_proto_text.bin' failed
make: *** [.build_release/tools/upgrade_solver_proto_text.bin] Error 1
Makefile:637: recipe for target '.build_release/tools/upgrade_net_proto_binary.bin' failed
make: *** [.build_release/tools/upgrade_net_proto_binary.bin] Error 1
Makefile:637: recipe for target '.build_release/tools/convert_imageset.bin' failed
make: *** [.build_release/tools/convert_imageset.bin] Error 1
Makefile:637: recipe for target '.build_release/tools/upgrade_net_proto_text.bin' failed
make: *** [.build_release/tools/upgrade_net_proto_text.bin] Error 1
Makefile:642: recipe for target '.build_release/examples/siamese/convert_mnist_siamese_data.bin' failed
make: *** [.build_release/examples/siamese/convert_mnist_siamese_data.bin] Error 1
经过自己研究发现,如果使用Python2版本就会有以上的问题,如果我改为 python3版本,即
PYTHON_INCLUDE := /usr/include/python3.5m \
/usr/lib/python3.5/dist-packages/numpy/core/include
就发现没有问题了。我系统里面两个版本都安装有的,可能是默认使用python3的问题吧。
5. 找不到 -lboost_python3
CXX examples/cifar10/convert_cifar_data.cpp
AR -o .build_release/lib/libcaffe.a
LD -o .build_release/lib/libcaffe.so.1.0.0
/usr/bin/ld: 找不到 -lboost_python3
collect2: error: ld returned 1 exit status
Makefile:585: recipe for target '.build_release/lib/libcaffe.so.1.0.0' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0] Error 1
解决方法:
在/usr/lib/x86_64-linux-gnu/ 下查看有没有libboost_python-py3.5.so,如果有链接一下就可以了。
sudo ln -s libboost_python-py35.so libboost_python3.so
如果你使用的是python3.6版本,发现在 /usr/lib/x86_64-linux-gnu/ 下没有libboost_python-py3.6.so 的,那你就的自己编译一个了,可以参考我的另一篇博文:https://blog.csdn.net/u012505617/article/details/88556621
参考:
https://blog.csdn.net/sinat_27240143/article/details/82701285
https://blog.csdn.net/fanhenghui/article/details/80092131