https://caffe.berkeleyvision.org/install_apt.html
Ubuntu 20.04
CUDA 10.0.130
CUDNN 7.6.5
python 3.8.5 & 2.7.18 (in Anaconda3)
caffe (官方)
all-in-one
sudo apt install --no-install-recommends libboost-all-dev
sudo apt install cmake git unzip libgflags-dev libgoogle-glog-dev libprotobuf-dev libleveldb-dev liblmdb-dev libsnappy-dev libhdf5-serial-dev protobuf-compiler libatlas-base-dev libopenblas-dev liblapack-dev the python3-dev python3-skimage graphviz python-protobuf
pip install --upgrade pip
pip install numpy pydot protobuf scikit-image
上面的拆分
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install cmake git unzip
sudo apt-get install libgflags-dev libgoogle-glog-dev
sudo apt-get install libprotobuf-dev libleveldb-dev liblmdb-dev
sudo apt-get install libsnappy-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install libatlas-base-dev libopenblas-dev liblapack-dev
sudo apt-get install the python3-dev python3-skimage
sudo apt-get install graphviz python-protobuf
pip install numpy pydot protobuf scikit-image
sudo apt purge libopencv*
运行
git clone https://github.com/BVLC/caffe.git
cp Makefile.config.example Makefile.config
gedit Makefile.config
下面的所有操作,若使用系统默认 python2.7,不使用 anaconda,则跳过所有带有 anaconda 字段的修改
在最开头添加
LINKFLAGS := -Wl,-rpath,/home//anaconda3/envs/py/lib
在下面内容中寻找并修改为
USE_CUDNN := 1
OPENCV_VERSION := 3
WITH_PYTHON_LAYER := 1
USE_PKG_CONFIG := 1
OpenCV version <= 3.*
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial /include
OpenCV version = 4.*
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial /include/opencv4
修改
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial /lib
ANACONDA_HOME := $(HOME)/
PYTHON_INCLUDE := $(ANACONDA_HOME)/envs//include \
$(ANACONDA_HOME)/envs//include/python<...> \
$(ANACONDA_HOME)/envs//lib/python<...>/site-packages/numpy/core/include
若使用系统自带 python2.7,则不需要进行下述修改
查看对应文件名
/usr/lib/x86_64-linux-gnu/libboost_python-py.so
根据上面的文件名修改
PYTHON_LIBRARIES := boost_python-py python<...>
PYTHON_LIB := /home//anaconda3/envs//lib
LINKFLAGS := -Wl,-rpath,/home/chli/anaconda3/envs/py38/lib
USE_CUDNN := 1
OPENCV_VERSION := 3
CUDA_DIR := /usr/local/cuda
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 := atlas
ANACONDA_HOME := $(HOME)/anaconda3
PYTHON_INCLUDE := $(ANACONDA_HOME)/envs/py38/include \
$(ANACONDA_HOME)/envs/py38/include/python3.8 \
$(ANACONDA_HOME)/envs/py38/lib/python3.8/site-packages/numpy/core/include
PYTHON_LIBRARIES := boost_python3 python3.8
PYTHON_LIB := /home/chli/anaconda3/envs/py38/lib
WITH_PYTHON_LAYER := 1
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial /home/chli/OpenCV/OpenCV-3.4.0/opencv-3.4.0/build/installed/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial /home/chli/OpenCV/OpenCV-3.4.0/opencv-3.4.0/build/installed/lib
USE_PKG_CONFIG := 1
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
TEST_GPUID := 0
Q ?= @
USE_CUDNN := 1
OPENCV_VERSION := 3
CUDA_DIR := /usr/local/cuda
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 := atlas
PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/lib/python2.7/dist-packages/numpy/core/include
PYTHON_LIB := /usr/lib
WITH_PYTHON_LAYER := 1
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial /home/chli/OpenCV/opencv-3.4.0/build/installed/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu /usr/lib/x86_64-linux-gnu/hdf5/serial /home/chli/OpenCV/opencv-3.4.0/build/installed/lib
USE_PKG_CONFIG := 1
BUILD_DIR := build
DISTRIBUTE_DIR := distribute
TEST_GPUID := 0
Q ?= @
运行
gedit Makefile
修改
NVCCFLAGS += -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS) -D_FORCE_INLINES
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial
make all -j$(nproc)
make test -j$(nproc)
make runtest -j$(nproc)
make pycaffe
make pytest
gedit ~/.bashrc
添加
export PYTHONPATH=${PYTHONPATH}:/python
打开 python,输入
import caffe
print(caffe.__version__)
输出
1.0.0
make clean
make all -j$(nproc)
make pycaffe
protoc src/caffe/proto/caffe.proto --cpp_out=.
mkdir build/include
mkdir build/include/caffe
mkdir build/include/caffe/proto
mv src/caffe/proto/caffe.pb.h build/include/caffe/proto/