首先安装caffe
安装的过程主要参考了 赵永科的书深度学习21天实战Caffe中的内容。
主要步骤如下:
sudo apt-get install git
sudo apt-get install libprotobuf-dev libleveldb-dev libopencv-dev libsnappy-dev
sudo apt-get install libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install python-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
下载caffe源码:
git clone https://github.com/bvlc/caffe.git
cd caffe/
修改文件名:
mv Makefile.config.example Makefile.config
对文件Makefile.config进行修改,一般在第8行
vim Makefile.config
将CPU_ONLY前的“#”去掉,表示caffe编译时仅支持CPU
编译
make -j
j 代表 使用多线程进行编译,利用所有可以用的CPU,加速编译速度
编译报错整理:
PROTOC src/caffe/proto/caffe.proto
CXX src/caffe/common.cpp
...
CXX ...
...
CXX examples/mnist/convert_mnist_data.cpp
src/caffe/layers/hdf5_output_layer.cpp:3:18: fatal error: hdf5.h: No such file or directory
compilation terminated.
Makefile:581: recipe for target '.build_release/src/caffe/layers/hdf5_output_layer.o' failed
make: *** [.build_release/src/caffe/layers/hdf5_output_layer.o] Error 1
make: *** Waiting for unfinished jobs....
src/caffe/net.cpp:8:18: fatal error: hdf5.h: No such file or directory
compilation terminated.
Makefile:581: recipe for target '.build_release/src/caffe/net.o' failed
make: *** [.build_release/src/caffe/net.o] Error 1
src/caffe/layers/hdf5_data_layer.cpp:13:18: fatal error: hdf5.h: No such file or directory
compilation terminated.
Makefile:581: recipe for target '.build_release/src/caffe/layers/hdf5_data_layer.o' failed
make: *** [.build_release/src/caffe/layers/hdf5_data_layer.o] Error 1
In file included from src/caffe/util/hdf5.cpp:1:0:
./include/caffe/util/hdf5.hpp:6:18: fatal error: hdf5.h: No such file or directory
compilation terminated.
Makefile:581: recipe for target '.build_release/src/caffe/util/hdf5.o' failed
make: *** [.build_release/src/caffe/util/hdf5.o] Error 1
In file included from src/caffe/solver.cpp:8:0:
./include/caffe/util/hdf5.hpp:6:18: fatal error: hdf5.h: No such file or directory
compilation terminated.
In file included from src/caffe/solvers/sgd_solver.cpp:5:0:
./include/caffe/util/hdf5.hpp:6:18: fatal error: hdf5.h: No such file or directory
compilation terminated.
Makefile:581: recipe for target '.build_release/src/caffe/solver.o' failed
make: *** [.build_release/src/caffe/solver.o] Error 1
Makefile:581: recipe for target '.build_release/src/caffe/solvers/sgd_solver.o' failed
make: *** [.build_release/src/caffe/solvers/sgd_solver.o] Error 1
好像出了不少的问题,首先我们看到前几个error,说的其实是一个问题
* error: hdf5.h: No such file or directory *
找不到hdf5.h这个头文件,此时我们可以通过修改Makefile.config文件来解决这个问题:
添加/usr/include/hdf5/serial/ 到 INCLUDE_DIRS
原:
# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
改:
# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/
此时再进行编译:
lixin@lixin-ThinkPad-E470:~/caffe$ make -j
CXX src/caffe/solver.cpp
CXX src/caffe/solvers/sgd_solver.cpp
CXX src/caffe/net.cpp
CXX src/caffe/layers/hdf5_output_layer.cpp
CXX src/caffe/layers/hdf5_data_layer.cpp
CXX src/caffe/util/hdf5.cpp
AR -o .build_release/lib/libcaffe.a
LD -o .build_release/lib/libcaffe.so.1.0.0
/usr/bin/ld: cannot find -lhdf5_hl
/usr/bin/ld: cannot find -lhdf5
collect2: error: ld returned 1 exit status
Makefile:572: recipe for target '.build_release/lib/libcaffe.so.1.0.0' failed
make: *** [.build_release/lib/libcaffe.so.1.0.0] Error 1
说找不到* lhdf5_hl , lhdf5 *文件
此时我们需修改Makefile文件,把 hdf5_hl 和hdf5修改hdf5_serial_hl 和 hdf5_serial
vim Makefile
原:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
改:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
再重新编译:
LD -o .build_release/lib/libcaffe.so.1.0.0
CXX/LD -o .build_release/tools/train_net.bin
CXX/LD -o .build_release/tools/extract_features.bin
CXX/LD -o .build_release/tools/device_query.bin
CXX/LD -o .build_release/tools/finetune_net.bin
CXX/LD -o .build_release/tools/upgrade_net_proto_binary.bin
CXX/LD -o .build_release/tools/compute_image_mean.bin
CXX/LD -o .build_release/tools/upgrade_net_proto_text.bin
CXX/LD -o .build_release/tools/caffe.bin
CXX/LD -o .build_release/examples/siamese/convert_mnist_siamese_data.bin
CXX/LD -o .build_release/tools/upgrade_solver_proto_text.bin
CXX/LD -o .build_release/tools/convert_imageset.bin
CXX/LD -o .build_release/tools/net_speed_benchmark.bin
CXX/LD -o .build_release/examples/cifar10/convert_cifar_data.bin
CXX/LD -o .build_release/tools/test_net.bin
CXX/LD -o .build_release/examples/mnist/convert_mnist_data.bin
CXX/LD -o .build_release/examples/cpp_classification/classification.bin
完美~
参考:
1.http://blog.csdn.net/xue_wenyuan/article/details/52037121
2.http://www.tuxi.com.cn/viewcsdn-39205-392051023.html