截至目前(2018.6.10),以下方法亲测有效,完成caffe编译,pycaffe编译,python3成功import caffe无报错
配置:VirtualBox + Ubuntu16.04 LTS + python3.5(系统自带)+ caffe
在虚拟机上安装好Ubuntu16.04后点击屏幕上端的工具栏,点击设备-->安装增强功能...-->运行,会出现终端自动执行命令,按提示点击回车
共享粘贴板:
点击 设备-->共享粘贴板-->双向
共享文件:
点击 设备-->共享文件夹-->共享文件夹... 出现如下窗口
重启系统,此时会出现名字为“sf_{共享文件夹}”的文件夹,这就是和主机共享的文件夹,但Ubuntu对此没有访问权限,想获取权限的话需打开终端,输入:
sudo usermod -a -G vboxsf {yourusernanme}
重启即可获得访问共享文件的权限
Ubuntu16默认的python版本是2.7,如果想要把默认版本切换成3.5,做如下操作(如果不换默认版本,跳过这一步),打开终端输入:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150
即可完成python版本的切换,如需切换回2.7,可输入:
sudo update-alternatives --config python
按提示选择python版本
或者使用Ubuntu默认设置,即用2.7输入python,用3.5输入python3,本环境使用的是3.5,因此用python3
安装caffe:
首先安装依赖库:
sudo apt-get install libprotobuf-dev
sudo apt-get install libleveldb-dev
sudo apt-get install libsnappy-dev
sudo apt-get install libopencv-dev
sudo apt-get install libhdf5-serial-dev
sudo apt-get install protobuf-compiler
sudo apt-get install libatlas-base-dev
(sudo apt-get install libopenblas-dev)
sudo apt-get install --no-install-recommends libboost-all-dev
若中间出现“由于已经达到 MaxReports 限制,没有写入 apport 报告”的问题,解决方法如下:
cd /var/lib/dpkg
sudo mv info/ info-bak
sudo mkdir info
sudo apt-get update
sudo apt-get install -f
恢复info-bak:
sudo mv info/* info-bak/
sudo rm -rf info
sudo mv info-bak/ info
建立pycaffe接口:
sudo apt-get install python-dev
sudo apt-get install python3-dev
及相关依赖库
sudo apt-get install libgflags-dev
sudo apt-get install libgoogle-glog-dev
sudo apt-get install liblmdb-dev
下载caffe源码:
首先安装git
sudo apt-get install git
下载caffe源码:
git clone https://github.com/BVLC/caffe.git
安装pip3:
wget https://bootstrap.pypa.io/get-pip.py
sudo python3 get-pip.py
安装相关的Python包:
cd caffe/python
gedit requirements.txt
python-dateutil 版本改成 python-dateutil>=2.5
for req in $(cat requirements.txt); do sudo -H pip3 install $req; done
题外话,如果在anaconda3环境下使用conda若出现permission denided,解决方法如下:
sudo chown -R {username}:{username} /home/{username}/anaconda3
编译caffe源码:
cd ..
cp Makefile.config.example Makefile.config
修改Makefile.config文件内容:
gedit Makefile.config
修改内容如下:
取消 CPU_ONLY := 1 前的‘#’
取消 WITH_PYTHON_LAYER := 1 前的‘#’
修改:
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
根据你的python版本修改如下内容:
# NOTE: this is required only if you will compile the python interface.
# We need to be able to find Python.h and numpy/arrayobject.h.
#PYTHON_INCLUDE := /usr/include/python2.7 \
# /usr/lib/python2.7/dist-packages/numpy/core/include
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
# ANACONDA_HOME := $(HOME)/anaconda
# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
# $(ANACONDA_HOME)/include/python2.7 \
# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include
# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.5m (Ubuntu自带为python3.5,因此将这三行取消注释,将上面2.7的部分注释;如果用的是anaconda,则将2.7和3.5都注释,然后修改ANACONDA_HOME的路径为anaconda安装路径,并取消注释)
PYTHON_INCLUDE := /usr/include/python3.5m \
/usr/local/lib/python3.5/dist-packages/numpy/core/include
# We need to be able to find libpythonX.X.so or .dylib.
PYTHON_LIB := /usr/lib
# PYTHON_LIB := $(ANACONDA_HOME)/lib (使用anaconda时这里取消注释,并将上一句注释)
接下来编译caffe源码:
make pycaffe
如果遇到:
cannot find -lboost_python3
因为在配置文件的第 74 行中有 PYTHON_LIBRARIES := boost_python3 ,但是在系统中无法找到 boost_python3.lib 这个库文件。
解决方案如下:
检查是否有如下文件:
ls /usr/lib/x86_64-linux-gnu/libboost_python-py35.so
如果有,说明我们的系统中已经有了这种库文件,只是文件名不同。
cd /usr/lib/x86_64-linux-gnu/
sudo ln -s libboost_python-py35.so libboost_python3.so
遇到:
python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: No such file or directory
解决方法是:
gedit Makefile.config
PYTHON_INCLUDE := /usr/include/python3.5m \
/usr/lib/python3.5/dist-packages/numpy/core/include
修改为:
PYTHON_INCLUDE := /usr/include/python3.5m \
/usr/local/lib/python3.5/dist-packages/numpy/core/include
cd至caffe目录下,开始编译,中间如果报错,都要先make clean一遍再从头开始编译:
sudo make all
sudo make test
sudo make runtest
如果想在命令后加入 -jx (x为CPU数)加快编译速度,当sudo make runtest -j2时可能会报错:
BenchmarkTest/0.TestTimerMilliSeconds, where TypeParam = caffe::CPUDevice
或者
BenchmarkTest/1.TestTimerMilliSeconds, where TypeParam = caffe::CPUDevice
解决方法,在当前路径下:
export MKL_CBWR=AUTO
编译成功后,输入:
cd python (这里必须cd到/caffe/python下才能操作,如果想在任何路径下都能import caffe成功,则需添加环境变量,自行百度)
python3
import caffe
不报错即编译成功