Ubuntu16.04下安装Caffe

1.在安装caffe之前先安装一下依赖库:

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
sudo apt-get install libopenblas-dev liblapack-dev libatlas-base-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev

2.到Github直接下载Caffe,地址:https://github.com/BVLC/caffe

3.将caffe-master放在根目录home下,解压:

unzip caffe-master.zip

4.将解压后的文件夹的名字改为caffe,将Makefile.config.example的内容复制到Makefile.config:

cd caffe
cp Makefile.config.example Makefile.config 

5.打开Makefile.config文件:

gedit Makefile.config

6.更改Makefile.config文件中的配置:

  • 若使用cudnn:
将:
#USE_CUDNN := 1 
改为:
USE_CUDNN := 1 
  • 若使用的opencv版本是3的:
将:
#OPENCV_VERSION := 3  
改为:
OPENCV_VERSION := 3  
  • 若使用python来编写layer:
将:
#WITH_PYTHON_LAYER := 1   
改为:
WITH_PYTHON_LAYER := 1  
  • 将 # Whatever else you find you need goes here. 下面的
将:
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     
  • 在CUDA_ARCH := 后前两行即有*_20和*_21的两行前面添加#:
# For CUDA >= 9.0, comment the *_20 and *_21 lines for compatibility.
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
  • 若你用的是anaconda,则还需要修改:
# NOTE: this is required only if you will compile the python interface.
 
# We need to be able to find Python.h and numpy/arrayobject.h.
 
#PYTHON_INCLUDE := /usr/include/python2.7 \
		
	 #/usr/lib/python2.7/dist-packages/numpy/core/include
 
# Anaconda Python distribution is quite popular. Include path:
 
# Verify anaconda location, sometimes it's in root.
 
ANACONDA_HOME := $(HOME)/anaconda
 
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
		
	 $(ANACONDA_HOME)/include/python2.7 \
		
	 $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
 

7.修改Makefile文件:

  • 第一个修改位置:
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

8.编译与测试:

在caffe文件夹下,执行命令:

make all -j12
make test -j12

如果成功的话会显示:

PROTOC src/caffe/proto/caffe.proto
NVCC src/caffe/util/math_functions.cu
NVCC src/caffe/util/im2col.cu
NVCC src/caffe/solvers/nesterov_solver.cu
NVCC src/caffe/solvers/sgd_solver.cu
NVCC src/caffe/solvers/adadelta_solver.cu
NVCC src/caffe/solvers/rmsprop_solver.cu
NVCC src/caffe/solvers/adagrad_solver.cu
NVCC src/caffe/layers/threshold_layer.cu
NVCC src/caffe/solvers/adam_solver.cu
NVCC src/caffe/layers/tanh_layer.cu
NVCC src/caffe/layers/pooling_layer.cu
NVCC src/caffe/layers/prelu_layer.cu
NVCC src/caffe/layers/cudnn_lrn_layer.cu
NVCC src/caffe/layers/cudnn_softmax_layer.cu
NVCC src/caffe/layers/sigmoid_layer.cu
NVCC src/caffe/layers/bias_layer.cu
NVCC src/caffe/layers/base_data_layer.cu
NVCC src/caffe/layers/recurrent_layer.cu
NVCC src/caffe/layers/batch_reindex_layer.cu
NVCC src/caffe/layers/absval_layer.cu
NVCC src/caffe/layers/deconv_layer.cu
NVCC src/caffe/layers/cudnn_deconv_layer.cu
NVCC src/caffe/layers/split_layer.cu
NVCC src/caffe/layers/cudnn_lcn_layer.cu
NVCC src/caffe/layers/crop_layer.cu
NVCC src/caffe/layers/eltwise_layer.cu
NVCC src/caffe/layers/bnll_layer.cu
NVCC src/caffe/layers/softmax_loss_layer.cu
NVCC src/caffe/layers/hdf5_data_layer.cu
NVCC src/caffe/layers/swish_layer.cu
NVCC src/caffe/layers/cudnn_tanh_layer.cu
NVCC src/caffe/layers/cudnn_relu_layer.cu
NVCC src/caffe/layers/log_layer.cu
NVCC src/caffe/layers/reduction_layer.cu
NVCC src/caffe/layers/lrn_layer.cu
NVCC src/caffe/layers/tile_layer.cu
NVCC src/caffe/layers/embed_layer.cu
NVCC src/caffe/layers/scale_layer.cu
NVCC src/caffe/layers/cudnn_conv_layer.cu
NVCC src/caffe/layers/euclidean_loss_layer.cu
NVCC src/caffe/layers/im2col_layer.cu
NVCC src/caffe/layers/lstm_unit_layer.cu
NVCC src/caffe/layers/mvn_layer.cu
NVCC src/caffe/layers/slice_layer.cu
NVCC src/caffe/layers/dropout_layer.cu
NVCC src/caffe/layers/conv_layer.cu
NVCC src/caffe/layers/sigmoid_cross_entropy_loss_layer.cu
NVCC src/caffe/layers/hdf5_output_layer.cu
NVCC src/caffe/layers/accuracy_layer.cu
NVCC src/caffe/layers/concat_layer.cu
NVCC src/caffe/layers/clip_layer.cu
NVCC src/caffe/layers/elu_layer.cu
NVCC src/caffe/layers/power_layer.cu
NVCC src/caffe/layers/silence_layer.cu
NVCC src/caffe/layers/cudnn_pooling_layer.cu
NVCC src/caffe/layers/batch_norm_layer.cu
NVCC src/caffe/layers/contrastive_loss_layer.cu
NVCC src/caffe/layers/cudnn_sigmoid_layer.cu
NVCC src/caffe/layers/softmax_layer.cu
NVCC src/caffe/layers/exp_layer.cu
NVCC src/caffe/layers/inner_product_layer.cu
NVCC src/caffe/layers/filter_layer.cu
NVCC src/caffe/layers/relu_layer.cu
CXX tools/convert_imageset.cpp
CXX tools/upgrade_net_proto_binary.cpp
CXX tools/upgrade_net_proto_text.cpp
CXX tools/compute_image_mean.cpp
CXX tools/extract_features.cpp
CXX tools/upgrade_solver_proto_text.cpp
CXX tools/caffe.cpp
CXX examples/mnist/convert_mnist_data.cpp
CXX examples/cifar10/convert_cifar_data.cpp
CXX examples/cpp_classification/classification.cpp
CXX examples/siamese/convert_mnist_siamese_data.cpp
CXX .build_release/src/caffe/proto/caffe.pb.cc
CXX src/caffe/util/blocking_queue.cpp
CXX src/caffe/util/io.cpp
CXX src/caffe/util/hdf5.cpp
CXX src/caffe/util/db.cpp
CXX src/caffe/util/im2col.cpp
CXX src/caffe/util/cudnn.cpp
CXX src/caffe/util/upgrade_proto.cpp
CXX src/caffe/util/insert_splits.cpp
CXX src/caffe/util/db_leveldb.cpp
CXX src/caffe/util/benchmark.cpp
CXX src/caffe/util/math_functions.cpp
CXX src/caffe/util/signal_handler.cpp
CXX src/caffe/util/db_lmdb.cpp
CXX src/caffe/parallel.cpp
CXX src/caffe/common.cpp
CXX src/caffe/solvers/sgd_solver.cpp
CXX src/caffe/solvers/adadelta_solver.cpp
CXX src/caffe/solvers/nesterov_solver.cpp
CXX src/caffe/solvers/adam_solver.cpp
CXX src/caffe/solvers/rmsprop_solver.cpp
CXX src/caffe/solvers/adagrad_solver.cpp
CXX src/caffe/net.cpp
CXX src/caffe/layer.cpp
CXX src/caffe/layer_factory.cpp
CXX src/caffe/syncedmem.cpp
CXX src/caffe/solver.cpp
CXX src/caffe/blob.cpp
CXX src/caffe/layers/recurrent_layer.cpp
CXX src/caffe/layers/input_layer.cpp
CXX src/caffe/layers/lrn_layer.cpp
CXX src/caffe/layers/scale_layer.cpp
CXX src/caffe/layers/spp_layer.cpp
CXX src/caffe/layers/cudnn_lcn_layer.cpp
CXX src/caffe/layers/reduction_layer.cpp
CXX src/caffe/layers/cudnn_conv_layer.cpp
CXX src/caffe/layers/lstm_unit_layer.cpp
CXX src/caffe/layers/cudnn_lrn_layer.cpp
CXX src/caffe/layers/memory_data_layer.cpp
CXX src/caffe/layers/power_layer.cpp
CXX src/caffe/layers/absval_layer.cpp
CXX src/caffe/layers/split_layer.cpp
CXX src/caffe/layers/cudnn_softmax_layer.cpp
CXX src/caffe/layers/hinge_loss_layer.cpp
CXX src/caffe/layers/cudnn_deconv_layer.cpp
CXX src/caffe/layers/batch_norm_layer.cpp
CXX src/caffe/layers/loss_layer.cpp
CXX src/caffe/layers/concat_layer.cpp
CXX src/caffe/layers/cudnn_tanh_layer.cpp
CXX src/caffe/layers/sigmoid_cross_entropy_loss_layer.cpp
CXX src/caffe/layers/exp_layer.cpp
CXX src/caffe/layers/cudnn_pooling_layer.cpp
CXX src/caffe/layers/tanh_layer.cpp
CXX src/caffe/layers/image_data_layer.cpp
CXX src/caffe/layers/argmax_layer.cpp
CXX src/caffe/layers/embed_layer.cpp
CXX src/caffe/layers/eltwise_layer.cpp
CXX src/caffe/layers/base_conv_layer.cpp
CXX src/caffe/layers/deconv_layer.cpp
CXX src/caffe/layers/pooling_layer.cpp
CXX src/caffe/layers/crop_layer.cpp
CXX src/caffe/layers/softmax_loss_layer.cpp
CXX src/caffe/layers/relu_layer.cpp
CXX src/caffe/layers/sigmoid_layer.cpp
CXX src/caffe/layers/cudnn_sigmoid_layer.cpp
CXX src/caffe/layers/bias_layer.cpp
CXX src/caffe/layers/dummy_data_layer.cpp
CXX src/caffe/layers/multinomial_logistic_loss_layer.cpp
CXX src/caffe/layers/dropout_layer.cpp
CXX src/caffe/layers/batch_reindex_layer.cpp
CXX src/caffe/layers/prelu_layer.cpp
CXX src/caffe/layers/lstm_layer.cpp
CXX src/caffe/layers/euclidean_loss_layer.cpp
CXX src/caffe/layers/hdf5_data_layer.cpp
CXX src/caffe/layers/infogain_loss_layer.cpp
CXX src/caffe/layers/softmax_layer.cpp
CXX src/caffe/layers/data_layer.cpp
CXX src/caffe/layers/neuron_layer.cpp
CXX src/caffe/layers/parameter_layer.cpp
CXX src/caffe/layers/swish_layer.cpp
CXX src/caffe/layers/accuracy_layer.cpp
CXX src/caffe/layers/im2col_layer.cpp
CXX src/caffe/layers/reshape_layer.cpp
CXX src/caffe/layers/tile_layer.cpp
CXX src/caffe/layers/slice_layer.cpp
CXX src/caffe/layers/base_data_layer.cpp
CXX src/caffe/layers/clip_layer.cpp
CXX src/caffe/layers/cudnn_relu_layer.cpp
CXX src/caffe/layers/hdf5_output_layer.cpp
CXX src/caffe/layers/conv_layer.cpp
CXX src/caffe/layers/rnn_layer.cpp
CXX src/caffe/layers/silence_layer.cpp
CXX src/caffe/layers/flatten_layer.cpp
CXX src/caffe/layers/threshold_layer.cpp
CXX src/caffe/layers/elu_layer.cpp
CXX src/caffe/layers/filter_layer.cpp
CXX src/caffe/layers/window_data_layer.cpp
CXX src/caffe/layers/bnll_layer.cpp
CXX src/caffe/layers/inner_product_layer.cpp
CXX src/caffe/layers/mvn_layer.cpp
CXX src/caffe/layers/log_layer.cpp
CXX src/caffe/layers/contrastive_loss_layer.cpp
CXX src/caffe/data_transformer.cpp
CXX src/caffe/internal_thread.cpp
AR -o .build_release/lib/libcaffe.a
LD -o .build_release/lib/libcaffe.so.1.0.0
CXX/LD -o .build_release/tools/convert_imageset.bin
CXX/LD -o .build_release/tools/upgrade_net_proto_binary.bin
CXX/LD -o .build_release/tools/upgrade_net_proto_text.bin
CXX/LD -o .build_release/tools/compute_image_mean.bin
CXX/LD -o .build_release/tools/extract_features.bin
CXX/LD -o .build_release/examples/siamese/convert_mnist_siamese_data.bin
CXX/LD -o .build_release/examples/mnist/convert_mnist_data.bin
CXX/LD -o .build_release/examples/cifar10/convert_cifar_data.bin
CXX/LD -o .build_release/tools/caffe.bin
CXX/LD -o .build_release/tools/upgrade_solver_proto_text.bin
CXX/LD -o .build_release/examples/cpp_classification/classification.bin

没有问题的话,执行测试命令:

make runtest -j12

在一段时间内测试多项是否正确,如果都没有问题(一般是不可能滴),那么结果如下:

Ubuntu16.04下安装Caffe_第1张图片

 

编译pycaffe:

make pycaffe -j8

配置环境变量:

cd
gedit .bashrc

在末尾添加:

export PYTHONPATH=/home/xxx/caffe/python:$PYTHONPATH

其中/home/xxx/caffe请改成你的caffe地址
然后更新.bashrc:

source ~/.bashrc

9.出现的错误:

  • 错误1:
make: protoc: Command not found

解决方法:

sudo apt-get install protobuf-c-compiler protobuf-compiler
  • 错误2:
.build_release/tools/caffe: error while loading shared libraries: libcudart.so.9.0: cannot open shared object file: No such file or directory
Makefile:545: recipe for target 'runtest' failed

解决方法:

sudo cp /usr/local/cuda-9.0/lib64/libcudart.so.9.0 /usr/local/lib/libcudart.so.9.0 && sudo ldconfig
sudo cp /usr/local/cuda-9.0/lib64/libcublas.so.9.0 /usr/local/lib/libcublas.so.9.0 && sudo ldconfig
sudo cp /usr/local/cuda-9.0/lib64/libcurand.so.9.0 /usr/local/lib/libcurand.so.9.0 && sudo ldconfig
  • 错误3:
.build_release/tools/caffe: error while loading shared libraries: libcudnn.so.7: cannot open shared object file: No such file or directory
Makefile:545: recipe for target 'runtest' failed

解决方法:

sudo cp /usr/local/cuda-9.0/lib64/libcudnn.so.7 /usr/local/lib/libcudnn.so.7 && sudo ldconfig

 

完,祝你好运!

你可能感兴趣的:(Ubuntu16.04下安装Caffe)