ubuntu16.04+anaconda2+caffe(cpu版)

一、安装Anaconda2

从官网下载所需的linux版本的可执行文件,执行如下命令:

cd downloadpath
bash Anaconda2-4.2.0-Linux-x86_64.sh
后面就根据提示执行就可以了,最后一步添加环境变量的部分选择yes,它会自动添加到用户环境变量文件~/.bashrc文件中.

安装完成后,为了防止编译caffe时文件冲突造成的一些错误,需替换掉anaconda包内的ibstdc++,libgomp这几个4.x版本的gcc编译动态库软连接:

rm -rf /home/your_path/anaconda2/lib/libstdc++.so
rm -rf /home/your_path/anaconda2/pkgs/libgcc-4.8.5-2/lib/libstdc++.so
rm -rf /home/your_path/anaconda2/lib/libstdc++.so.6
rm -rf /home/your_path/anaconda2/pkgs/libgcc-4.8.5-2/lib/libstdc++.so.6
ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 /home/your_path/anaconda2/lib/libstdc++.so.6
ln -s /home/your_path/anaconda2/lib/libstdc++.so.6 /home/your_path/anaconda2/lib/libstdc++.so
ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21 /home/your_path/anaconda2/pkgs/libgcc-4.8.5-2/lib/libstdc++.so.6
ln -s /home/your_path/anaconda2/pkgs/libgcc-4.8.5-2/lib/libstdc++.so.6 /home/your_path/anaconda2/pkgs/libgcc-4.8.5-2/lib/libstdc++.so
rm -rf /home/your_path/anaconda2/lib/libgomp.so
rm -rf /home/your_path/anaconda2/pkgs/libgcc-4.8.5-2/lib/libgomp.so
rm -rf /home/your_path/anaconda2/lib/libgomp.so.1
rm -rf /home/your_path/anaconda2/pkgs/libgcc-4.8.5-2/lib/libgomp.so.1
ln -s /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0 /home/your_path/anaconda2/lib/libgomp.so.1
ln -s /home/your_path/anaconda2/lib/libgomp.so.1 /home/your_path/anaconda2/lib/libgomp.so
ln -s /usr/lib/x86_64-linux-gnu/libgomp.so.1.0.0 /home/your_path/anaconda2/pkgs/libgcc-4.8.5-2/lib/libgomp.so.1
ln -s /home/your_path/anaconda2/pkgs/libgcc-4.8.5-2/lib/libgomp.so.1 /home/your_path/anaconda2/pkgs/libgcc-4.8.5-2/lib/libgomp.so
将your_path改成你自己的。

二、安装编译caffe

安装依赖项:

sudo apt-get install libprotobuf-dev protobuf-compiler libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev  
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
 sudo apt-get install -y libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler protobuf-c-compiler protobuf-compiler
从github上获取caffe:

git clone https://github.com/BVLC/caffe.git

下载完成之后,进入CAFFE文件夹, 进入里面的PYTHON文件夹,然后输入
for req in $(cat requirements.txt); do pip install $req; done
(PIP如果没有安装得先安装一下:sudo apt install python-pip)
进入caffe目录(caffe-master),并复制出所需的Makefile.config文件

sudo cp Makefile.config.example Makefile.config
修改Makefile.config文件(通过vim或是直接编辑)
# cuDNN acceleration switch (uncomment to build with cuDNN).
# USE_CUDNN := 1

# CPU-only switch (uncomment to build without GPU support).
CPU_ONLY := 1

# open for OpenBlas
BLAS := atlas
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.

ANACONDA_HOME := $(HOME)/anaconda
修改为:
ANACONDA_HOME := $(HOME)/anaconda2

# 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 \
#PYTHON_LIB := /usr/lib
PYTHON_LIB := $(ANACONDA_HOME)/lib

#WITH_PYTHON_LAYER := 1 
修改为 
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

编译caffe之前,添加环境路径:

sudo gedit ~/.bashrc
在最后加上

export PYTHONPATH=/home/***/caffe-master/python:$PYTHONPATH
export PATH="/home/***/anaconda2/bin:$PATH"
export LD_LIBRARY_PATH="/home/***/anaconda2/lib":$LD_LIBRARY_PATH

编译caffe

make all -j8
make test -j8
make runtest -j8

所有过程无报错,就可以说已经万事大吉啦



你可能感兴趣的:(环境搭建)