https://blog.csdn.net/weixin_38125866/article/details/81951548
ubuntu16.04 安装caffe cpu版本 python3
sudo vim /etc/apt/sources.list
更换国内源,速度比较快。以下是阿里云源。
#deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted
deb-src http://archive.ubuntu.com/ubuntu xenial main restricted #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates universe
deb http://mirrors.aliyun.com/ubuntu/ xenial multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-updates multiverse
deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse #Added by software-properties
deb http://archive.canonical.com/ubuntu xenial partner
deb-src http://archive.canonical.com/ubuntu xenial partner
deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted
deb-src http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted multiverse universe #Added by software-properties
deb http://mirrors.aliyun.com/ubuntu/ xenial-security universe
deb http://mirrors.aliyun.com/ubuntu/ xenial-security multiverse
-
sudo apt update
,sudo apt upgrade
更新系统。 -
安装依赖文件。
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compiler
sudo apt-get install --no-install-recommends libboost-all-dev
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install libatlas-base-dev
- 1
- 2
- 3
- 4
-
git clone https://github.com/BVLC/caffe.git
下载caffe源码。 -
修改Makefile文件。
cd ~/caffe-master
cp Makefile.config.example Makefile.config
vim Makefile.config
- 1
- 2
- 3
删除 `#CPU_ONLY := 1` 的#,开启仅使用cpu。
以下两行带有hdf5的为新增加的内容,解决无法找到HD5的错误。
- 1
- 2
- 3
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/hdf5/serial
- 1
- 2
使用python3还需做如下修改,将PYTHON_INCLUDE python2的注释掉,取消python3的注释。
- 1
#python2
#PYTHON_INCLUDE := /usr/include/python2.7 \
/usr/lib/python2.7/dist-packages/numpy/core/include
#python3
PYTHON_LIBRARIES := boost_python3 python3.5m
PYTHON_INCLUDE := /usr/include/python3.5m \
/usr/lib/python3/dist-packages/numpy/core/include
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
解决`/usr/bin/ld: cannot find -lboost_python3` 错误
- 1
sudo ln -s /usr/lib/x86_64-linux-gnu/libboost_python-py35.so /usr/lib/x86_64-linux-gnu/libboost_python3.so
- 1
编译caffe,make默认单核运算,-j2表示双核,可加快速度,根据自己实际情况修改。
- 1
make all -j2
make test -j2
make runtest -j2
- 1
- 2
- 3
- 安装caffe python3接口。
sudo apt install python3-pip #安装pip工具。
sudo apt install python3-numpy #解决后面编译无法找到numpy错误。
sudo apt-get install gfortran #安装依赖文件
sudo pip3 install matplotlib#解决后面import caffe报错
- 1
- 2
- 3
- 4
cd ~/caffe-master
pip3 install -r python/requirements.txt
- 1
- 2
- 3
编译
- 1
make pycaffe -j2
- 1
打开python3终端测试:
cd ~/caffe-master/python/
python3
import caffe #不报错则安装成功.
- 增加环境变量
vim ~/.bashrc
添加一行 export PYTHONPATH=~/caffe-master/python:$PYTHONPATH