本文为第一次安装caffe,中间费尽曲折,故写下这篇博客让后人少走弯路,主要参考了以下链接:
博客:https://blog.csdn.net/muzilinxi90/article/details/53673184 muzilinxi90的博客
博客:https://www.linuxidc.com/Linux/2016-09/135034.htm 来源:Linux社区 作者:Linux
简书:https://www.jianshu.com/p/36badc9fbcc2
CAFFE官网安装指南页面: http://caffe.berkeleyvision.org/installation.html
系统:ubantu16.04通过修改默认python解释器(建立指向python3.5的软连接)的方法修改默认python版本,见
如果是Python2.x可以直接使用如下命令
sudo apt-get install pip
这样安装的是pip2不支持Python3.x,可以使用如下命令安装pip3
sudo apt-get install python3-pip
然后,我们还要升级里面的包,需要升级的包是matplotlib
。不然当你最后在python里面import caffe
时,会出现以下的问题:
File "/usr/local/lib/python3.4/dist-packages/matplotlib/pyplot.py", line 37, in
from matplotlib.figure import Figure, figaspect
File "/usr/local/lib/python3.4/dist-packages/matplotlib/figure.py", line 40, in
from matplotlib.axes import Axes, SubplotBase, subplot_class_factory
File "/usr/local/lib/python3.4/dist-packages/matplotlib/axes/__init__.py", line 4, in
from ._subplots import *
File "/usr/local/lib/python3.4/dist-packages/matplotlib/axes/_subplots.py", line 10, in
from matplotlib.axes._axes import Axes
File "/usr/local/lib/python3.4/dist-packages/matplotlib/axes/_axes.py", line 24, in
import matplotlib.dates as _ # <-registers a date unit converter
File "/usr/local/lib/python3.4/dist-packages/matplotlib/dates.py", line 125, in
from dateutil.rrule import (rrule, MO, TU, WE, TH, FR, SA, SU, YEARLY,
File "/usr/local/lib/python3.4/dist-packages/dateutil/rrule.py", line 55
raise ValueError, "Can't create weekday with n == 0"
^
SyntaxError: invalid syntax
# 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
这里去掉注释:
# Uncomment to use Python 3 (default is Python 2)
PYTHON_LIBRARIES := boost_python3 python3.5m
PYTHON_INCLUDE := /usr/include/python3.5m \
/usr/lib/python3.5/dist-packages/numpy/core/include
(python2的PYTHON_LIBRARIES在makefile里已有,故makefile.config中没有)
本人后文make pycaffe报错: python/caffe/_caffe.cpp:10:31: fatal error: numpy/arrayobject.h: 没有那个文件或目录
发现/usr/lib下没有numpy,使用如下命令安装numpy:
pip3 install numpy
将上面路径改为:
PYTHON_INCLUDE := /usr/include/python3.5m \
/home/jason/.local/lib/python3.5/site-packages/numpy/core/include
重新make pycaffe,报错: /usr/bin/ld: 找不到 -lboost_python3
(有人使用sudo apt-get install python-numpy安装numpy成功,路径应该有所差异,可能更好些
https://blog.csdn.net/shomy_liu/article/details/48543449)
去/usr/lib/x86_64-linux-gnu/文件夹下面去找找,你会找到属于python3的boost版本,可能是34,也可能是35。确定后,把makefile中的名称换成相应名称就可以了。3.5m的,我不了解为何要加m。修改后应该是:
PYTHON_LIBRARIES := boost_python-py35 python3.5m
之后make pycaffe成功。
注:本文未使用anaconda进行配置,是因为用anaconda安装了python3.5.2后,/usr/lib/x86_64-linux-gnu/文件夹内就只有python2对应的boost_python,没有python3对应的,python3对应的boost_python在anaconda中。报错:找不到××boost。修改如下的PYTHON_LIB也不成功,路径没错就是到不了:
# We need to be able to find libpythonX.X.so or .dylib.
PYTHON_LIB := /usr/lib
# PYTHON_LIB := $(ANACONDA_HOME)/lib
make runtest
export PYTHONPATH=/path/to/caffe/python:$PYTHONPATH
export PYTHONPATH=~/caffe/python:$PYTHONPATH
import caffe