ubuntu 16.04 python3环境下安装仅cpu版本的caffe

1.安装依赖文件。

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

2.git clone https://github.com/BVLC/caffe.git 下载caffe源码。(个人认为用迅雷下载更快)

3.修改Makefile文件。

cd ~/caffe-master
cp Makefile.config.example Makefile.config
sudo gedit Makefile.config

删除 `#CPU_ONLY := 1` 的#,开启仅使用cpu。

以下两行带有hdf5的为新增加的内容,解决无法找到HD5的错误。

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

使用python3还需做如下修改,将PYTHON_INCLUDE python2的注释掉,取消python3的注释。
#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

4.解决`/usr/bin/ld: cannot find -lboost_python3` 错误
sudo ln -s /usr/lib/x86_64-linux-gnu/libboost_python-py35.so /usr/lib/x86_64-linux-gnu/libboost_python3.so
编译caffe,make默认单核运算,-j2表示双核,可加快速度,根据自己实际情况修改。
make all -j2
make test -j2
make runtest -j2

5.安装caffe python3接口。
sudo apt install python3-pip #安装pip工具。
sudo apt install python3-numpy==1.13.3  #解决后面编译无法找到numpy错误。
sudo apt-get install gfortran #安装依赖文件
sudo pip3 install matplotlib#解决后面import caffe报错

cd ~/caffe-master
pip3 install -r python/requirements.txt

6.编译
make pycaffe -j2

7.打开python3终端测试:

cd ~/caffe-master/python/
python3
import caffe #不报错则安装成功.

报错:ImportError: No module named ‘google‘

pip3 install google-cloud

pip3 install google-cloud-vision


8增加环境变量
sudo gedit ~/.bashrc
添加一行 export PYTHONPATH=~/caffe-master/python:$PYTHONPATH

参考:https://blog.csdn.net/weixin_38125866/article/details/81951548

相关推荐