在本教程中,我将介绍如何在没有root特权的情况下安装Caffe。 我假设你已经安装了Anaconda和CUDA。
创造虚拟环境
conda create -n caffe
conda activate caffe
安装依赖
conda install boost openblas mkl mkl-include gflags glog lmdb leveldb h5py hdf5 scikit-image
conda install ffmpeg
conda install -c menpo opencv
编译安装Protocol Buffer (protobuf)
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.2.0/protobuf-cpp-3.2.0.tar.gz
tar -xf protobuf-cpp-3.2.0.tar.gz
cd protobuf-3.2.0
./configure --prefix=/path/to/install/protobuf-3.2.0 --enable-shared
make -j32
make install
设置环境变量
这里是我的路径,可以根据自己的安装路径进行设置
# CUDA-10.1
export PATH=/usr0/home/zhiqic/cuda/cuda-10.1/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr0/home/zhiqic/cuda/cuda-10.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
# caffe
export PATH=/usr0/home/zhiqic/anaconda3/envs/caffe/bin:$PATH
export LD_LIBRARY_PATH=/usr0/home/zhiqic/anaconda3/envs/caffe/lib:$LD_LIBRARY_PATH
# protobuf-3.2.0
export PATH=/usr0/home/zhiqic/env/protobuf/bin:$PATH
export LD_LIBRARY_PATH=/usr0/home/zhiqic/env/protobuf/lib:$LD_LIBRARY_PATH
设置完后使用source .bashrc更新环境变量。可以使用protoc --version查看protobuf是否安装成功。
编译安装caffe
在github上下载caffe:
git clone -b 1.0 --depth 1 https://github.com/BVLC/caffe.git
然后进入caffe文件将,复制编译配置文件:
cd caffe
cp Makefile.config.example Makefile.config
接下来更新配置文件:
USE_CUDNN := 1
OPENCV_VERSION := 3
CUDA_DIR := /path/to/cuda-9.0
CUDA_ARCH := \
-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 := open
ANACONDA_HOME := /path/to/anaconda3/envs/caffe/
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python3.6m \
$(ANACONDA_HOME)/lib/python3.6/site-packages/numpy/core/include
PYTHON_LIBRARIES := boost_python3 python3.6m
PYTHON_LIB := $(ANACONDA_HOME)/lib
WITH_PYTHON_LAYER := 1
INCLUDE_DIRS := /path/to/protobuf-3.2.0/include $(PYTHON_INCLUDE)
LIBRARY_DIRS := /path/to/protobuf-3.2.0/lib $(PYTHON_LIB)
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
TEST_GPUID := 0
Q ?= @
然后进行make编译安装:
make -j32
如果编译时出现错误 "cannot find -lboost_python3",可以将上面Makefile.config
配置文件中的 PYTHON_LIBRARIES := boost_python3 python3.6m改为PYTHON_LIBRARIES := boost_python36 python3.6m。继续编译,如果还是报错,可以参考https://github.com/BVLC/caffe/issues/4843的解决办法。
如果上面make成功,继续build pycaffe:
make pycaffe
到了这里就只需要最后一步:在.bashrc中更新环境变量并用source .bashrc更新。
export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH
上面都成功后,如果命令行输入python,在里面引入caffe,发现找不到google包,则可以使用conda安装protobuf包:
conda install protobuf
如果想验证caffe安装成功,可以参考这个网站caffe安装成功测试实例进行测试。