Linux安装基于py35的caffe(CPU版,结合Anaconda)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/hanzy88/article/details/96015211

参考链接:默认安装python2.7(少坑版)

装了一遍linux下的caffe,又是一遍艰苦刨坑。
自己装在mac上练习的,至于为什么不在mac os上装(brew 更新后有些坑我实在刨不动)

安装Anaconda

1.考虑到python3.5版本的conda比较难找了,另外我也试着装了py35的anaconda3,可能中间配置了什么,反正一直编译不成功,所有这里可以直接安装最新版。然后配置新建python 3.5的环境:
conda create -n caffe python=3.5
caffe那个是环境名称,可以随便取,方便记就行。

安装相关组件

sudo apt-get install libprotobuf-dev 
sudo apt-get install libleveldb-dev
sudo apt-get install libsnappy-dev 
sudo apt-get install libopencv-dev
sudo apt-get install libhdf5-serial-dev
sudo apt-get install protobuf-compiler
sudo apt-get install libgflags-dev
sudo apt-get install libgoogle-glog-dev
sudo apt-get install liblmdb-dev
sudo apt-get install libatlas-base-dev
sudo apt-get install --no-install-recommeds libboost-all-dev
sudo apt-get install python-numpy
sudo apt-get install git

相关组件没有的,跟着代码依次安装就行。

编译Caffe

  1. 先下载caffe
git clone git://github.com/BVLC/caffe.git

cd caffe进入目录。

  1. 生成Makefile.config文件,这里是将caffe目录下自带的Makefile.config.example文件复制一份并更名为Makefile.config,命令如下:
cp Makefile.config.example Makefile.config

因为我在编译的过程中遇到问题:

cannot find -lhdf5_hl

为了避免出现这个问题,所有可以先进入/usr/lib/x86_64-linux-gnu执行下面操作:

sudo ln -sf libhdf5_serial.so.10.1.0 libhdf5.so
sudo ln -sf libhdf5_serial_hl.so.10.0.2 libhdf5_hl.so

其中10.1.0是在我系统的版本,可以对照自己的文件进行更改,实在不行(如果出现问题,又有墙的话,推荐Bing国际版根据相应问题解决)

  1. 配置Makefile.config/Makefile两个文件

为了简单扼要,我先直接贴我的配置信息:

## 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 := 0
# USE_LEVELDB := 0
# USE_LMDB := 0
# This code is taken from https://github.com/sh1r0/caffe-android-lib
# USE_HDF5 := 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 := 3

# 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.
# 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

# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
# BLAS_INCLUDE := /path/to/your/blas
# BLAS_LIB := /path/to/your/blas

# 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_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)/anaconda3
# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
          $(ANACONDA_HOME)/include/python3.7m \
          $(ANACONDA_HOME)/lib/python3.7/site-packages/numpy/core/include

# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python-py35 python3.5m
PYTHON_INCLUDE := /home/user/anaconda3/envs/caffe/include/python3.5m \
                 /home/user/.local/lib/python3.5/site-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 := $(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

# 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

可以对照自己的配置文件改一下,注意不要用Anaconda去配置,我也不知道为什么一直编译不过(后面有部分可能没复制进去,默认值就行)。所以这里用刚开始配置的caffe的python3.5的环境去编译,这里有个./local/~/numpyt/core/…这个目录,刚开始我也没有,可以先激活python3.5环境source activate caffe,然后安装下numpy,之后找下相应的目录配置进去,我不知道用anaconda3下面的numpy目录是否影响,可以试下。有些文件要是找不到,可以用下面的命令:

find / -name filename 在所有目录下查找
find . -name filename 在当前目录下查找

接着配置Makefile文件:

PYTHON_LIBRARIES ?= boost_python-py35

如果编译有问题,可以试着配置下面命令:

LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
  1. 执行编译
sudo make all
sudo make test
sudo make runtest

上面每一个步骤错,都需要执行sudo make clean,然后解决问题后重新编译(可以不用sudo,有问题Bing一下)。

配置Python3.5接口

恭喜你,走到这一步。继续加油~

  1. 激活python3.5环境,并配置相应依赖组件
source(或者conda也可以) activate caffe

进入到caffe/python目录,执行:

sudo apt-get install gfortran
cd ~/caffe/python
for req in $(cat requirements.txt); do pip install $req; done

其实主要是第三个命令,如果中间因为故障没安装成功,可以多试几次,或者Bing一下。

  1. 将caffe根目录下的python文件夹加入到环境变量
sudo gedit ~/.bashrc
export PYTHONPATH=/home/user/caffe/python:$PYTHONPATH

添加到文件最后即可

sudo ldconfig

执行更新配置(有时更新不起作用时,可以重启电脑就会有效)。

  1. 编译
cd ~/caffe/
sudo make pycaffe
  1. 验证
    这个时候,确保激活了python3.5的环境,然后执行一下命令:
python
import caffe

如果没有报错,就大功告成了。
如果报错,那就说明“我们不一样”,试着Bing一下,看看国内外论坛有没有相似的问题试着解决,加油~

猜你喜欢

转载自blog.csdn.net/hanzy88/article/details/96015211