Ubuntu 安装 Caffe

文章目录

  • Ubuntu 安装 Caffe
    • 官方安装说明(新装机用)
    • 测试通过版本组合
    • 安装依赖
    • (预检查)卸载 apt 安装在默认路径上的 OpenCV
    • 修改配置
      • Quick Start With my Makefile.config
        • Anaconda with python3
        • default python2.7
    • 安装
    • 测试
    • 安装 pycaffe
    • 测试
    • 添加新的 caffe layers(可选)
      • 添加完新的 caffe layer 之后
      • 生成 caffe.pb.h
    • 尽情享用吧~

Ubuntu 安装 Caffe

官方安装说明(新装机用)

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

(预检查)卸载 apt 安装在默认路径上的 OpenCV

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

Quick Start With my Makefile.config

Anaconda with python3

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 ?= @

default python2.7

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)

安装 pycaffe

make pycaffe
make pytest
gedit ~/.bashrc

添加

export PYTHONPATH=${PYTHONPATH}:/python

测试

打开 python,输入

import caffe
print(caffe.__version__)

输出

1.0.0

添加新的 caffe layers(可选)

添加完新的 caffe layer 之后

make clean
make all -j$(nproc)
make pycaffe

生成 caffe.pb.h

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/

尽情享用吧~

你可能感兴趣的:(install,linux)