最近因之前的服务器上的caffe奔溃了,不得已重新安装这一古老的深度学习框架,之前也尝试了好几次,每次都失败,这次总算是成功了,因此及时地总结一下。
以下安装的caffe主要是针对之前虹膜分割和巩膜分割所需的caffe版本。之前已经出过一个安装指南,部分内容与之重叠。
其实整个caffe安装最困难的部分就是各个依赖软件的安装,因此我们只要把这部分安装好了就可以了。
注意:安装指南,请参考《深度学习:21天实战caffe》的第5天那一章,只要按照上面所说的,将所有依赖文件安装在一个目录下,即可。最后安装好的列表应该为:
最后,修改caffe目录下的Makefile.config,找到如下几行:
# Whatever else you find you need goes here.
INCLUDE_DIRS :=/home/caiyong.wang/bin/caffe_local_install/include $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS :=/home/caiyong.wang/bin/caffe_local_install/lib $(PYTHON_LIB) /usr/local/lib /usr/lib
完整的Makefile.config参考:
## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!
# cuDNN acceleration switch (uncomment to build with cuDNN).
USE_CUDNN := 1
# CPU-only switch (uncomment to build without GPU support).
# CPU_ONLY := 1
# uncomment to disable IO dependencies and corresponding data layers
USE_OPENCV := 1
# USE_LEVELDB := 0
# USE_LMDB := 0
# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
# You should not set this flag if you will be reading LMDBs with any
# possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1
# Uncomment if you're using OpenCV 3
OPENCV_VERSION := 2
# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++
# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda
# On Ubuntu 14.04, if cuda tools are installed via
# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
# CUDA_DIR := /usr
# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 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
# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := open
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
BLAS_INCLUDE := /opt/OpenBLAS/include
BLAS_LIB := /opt/OpenBLAS/lib
# Homebrew puts openblas in a directory that is not on the standard search path
# BLAS_INCLUDE := $(shell brew --prefix openblas)/include
# BLAS_LIB := $(shell brew --prefix openblas)/lib
# This is required only if you will compile the matlab interface.
# MATLAB directory should contain the mex binary in /bin.
# MATLAB_DIR := /usr/local/MATLAB/R2015a/
# MATLAB_DIR := /Applications/MATLAB_R2012b.app
# 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)/anaconda2
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python2.7 \
$(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
# Uncomment to use Python 3 (default is Python 2)
# PYTHON_LIBRARIES := boost_python3 python3.5m
# PYTHON_INCLUDE := /usr/include/python3.5m \
# /usr/lib/python3.5/dist-packages/numpy/core/include
# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/lib
# Homebrew installs numpy in a non standard path (keg only)
# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
# PYTHON_LIB += $(shell brew --prefix numpy)/lib
# Uncomment to support layers written in Python (will link against Python libs)
WITH_PYTHON_LAYER := 1
# Whatever else you find you need goes here.
INCLUDE_DIRS :=/home/caiyong.wang/bin/caffe_local_install/include $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS :=/home/caiyong.wang/bin/caffe_local_install/lib $(PYTHON_LIB) /usr/local/lib /usr/lib
# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
# INCLUDE_DIRS += $(shell brew --prefix)/include
# LIBRARY_DIRS += $(shell brew --prefix)/lib
# NCCL acceleration switch (uncomment to build with NCCL)
# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
# USE_NCCL := 1
# Uncomment to use `pkg-config` to specify OpenCV library paths.
# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
USE_PKG_CONFIG := 1
# N.B. both build and distribute dirs are cleared on `make clean`
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
# DEBUG := 1
# The ID of the GPU that 'make runtest' will use to run unit tests.
TEST_GPUID := 0
# enable pretty build (comment to see full commands)
Q ?= @
LINKFLAGS := -Wl,-rpath,$(ANACONDA_HOME)/lib
下面是一些细节:
1. 首先必须安装python2, 推荐直接安装anaconda2, 例如:https://repo.anaconda.com/archive/Anaconda2-5.0.0.1-Linux-x86_64.sh ,并添加路径到PATH环境变量。
2. opencv的安装
对于opencv,需要安装两个版本,一个是python的,一个是系统的。
对于python,可以安装
pip install opencv-python==4.2.0.34
直接安装pip install opencv-python,可能出现 “ModuleNotFoundError: No module named 'skbuild' #105”,因此推荐降级安装。
参考:https://github.com/openvinotoolkit/dlstreamer_gst/issues/105
对于系统的安装,方法为:
opencv一般安装opencv 2.4.9, 首先下载opencv-2.4.9.zip源码,并解压:
unzip opencv-2.4.9.zip
cd opencv-2.4.9/
mkdir build;
cd build/
cmake ..
ccmake ..
这里要用到cmake与ccmake,可以自己安装,参考: https://www.24kplus.com/linux/557.html
https://www.cnblogs.com/tosser/p/9905597.html
https://blog.csdn.net/sheqianweilong/article/details/102582668
注意编译过程中需要加入:
./bootstrap --prefix=/usr --datadir=share/cmake --docdir=doc/cmake --qt-gui && make // 参数--qt-gui是安装cmake-gui,不想安装可以省略
继续回到opencv的安装,注意在caffe中仅仅使用图像读取和缩放等在CPU上的简单模块,因此可以禁用大部分无关模块以节省编译时间。
然后进行编译安装,
make && make install
至此安装成功。安装成功后,可以将opencv涉及到的bin,lib,pkgconfig填到~/.bashrc,
export PATH=/home/caiyong.wang/bin/caffe_local_install/bin:$PATH
export LD_LIBRARY_PATH=/home/caiyong.wang/bin/caffe_local_install/lib/:$LD_LIBRARY_PATH
export PKG_CONFIG_PATH=/home/caiyong.wang/bin/caffe_local_install/lib/pkgconfig/:$PKG_CONFIG_PATH
然后source ~/.bashrc刷新。
opencv安装成功后,可以运行以下的程序进行测试,测试通过后,证明opencv安装成功,可以顺利安装caffe.
先编辑一个main函数,命名为main.cpp:
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(void)
{
printf("%s\r\n", CV_VERSION);
printf("%u.%u.%u\r\n", CV_MAJOR_VERSION, CV_MINOR_VERSION, CV_SUBMINOR_VERSION);
}
然后执行:
g++ -o main main.cpp `pkg-config opencv --cflags --libs`
最后执行: ./main
PS:对于pkgconfig的一些用法,如下:
[caiyong.wang@omnisky opencv_test]$ pkg-config --cflags opencv
-I/home/caiyong.wang/bin/caffe_local_install/include/opencv -I/home/caiyong.wang/bin/caffe_local_install/include
[caiyong.wang@omnisky opencv_test]$ pkg-config --libs opencv
/home/caiyong.wang/bin/caffe_local_install/lib/libopencv_calib3d.so /home/caiyong.wang/bin/caffe_local_install/lib/libopencv_core.so /home/caiyong.wang/bin/caffe_local_install/lib/libopencv_features2d.so /home/caiyong.wang/bin/caffe_local_install/lib/libopencv_flann.so /home/caiyong.wang/bin/caffe_local_install/lib/libopencv_highgui.so /home/caiyong.wang/bin/caffe_local_install/lib/libopencv_imgproc.so /home/caiyong.wang/bin/caffe_local_install/lib/libopencv_ml.so /home/caiyong.wang/bin/caffe_local_install/lib/libopencv_objdetect.so /home/caiyong.wang/bin/caffe_local_install/lib/libopencv_photo.so /home/caiyong.wang/bin/caffe_local_install/lib/libopencv_stitching.so /home/caiyong.wang/bin/caffe_local_install/lib/libopencv_ts.a -lrt -lpthread -lm -ldl
3. protobuf的安装
同上,也必须安装两个版本,一个是python,一个是系统。
python安装: 直接pip install protobuf或者conda install protobuf。
系统安装:参考:https://wangcaiyong.blog.csdn.net/article/details/78283336
参考:https://blog.csdn.net/dgyuanshaofeng/article/details/78151510