现在网上安装caffe的版本基本上都是基于python 2.7的,本教程旨在用anaconda 3安装caffe cpu,其过程非常艰辛,连续弄了三天时间才终于有了一个完整教程。
我的系统环境:Ubuntu 16.04、Anaconda 3.5.2(也就是python 3.5.2)、低端笔记本、安装caffe cpu、在这之前安装了opencv 3.2.0
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
第四部分:也是最重要的部分——分享我解决错误的过程及解决方案
1、错误:按照上面的教程一点没变,老是说找不到Python.h
解决办法:网上查了各个版本的说法都没解决,后面仔细想了想,肯定是系统找不到自己的Python路径,我发现Python.h在我的anaconda文件中,但在配置文件Makefile.config中的anaconda环境的路径根本就不是"ANACONDA_HOME := $(HOME)/anaconda",所以系统根本找不到Python.h。所以我立即把ANACONDA_HOME改了,改成了我装anaconda时文件的位置,修改后的截图如下:
# Anaconda Python distribution is quite popular. Include path:
# Verify anaconda location, sometimes it's in root.
# ANACONDA_HOME := $(HOME)/anaconda
ANACONDA_HOME := $(HOME)/Downloads/yes
# 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
# PYTHON_INCLUDE := /usr/include/python3.5m \
# /usr/lib/python3.5/dist-packages/numpy/core/include
PYTHON_INCLUDE := $(ANACONDA_HOME)/include \
$(ANACONDA_HOME)/include/python3.5m \
$(ANACONDA_HOME)/lib/python3.5/site-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
2、错误:make pycaffe可以通过,但是make all时出现错误,具体错误我没有记录,但是好像是因为系统装了多个opencv的原因,不管怎么说,至少可以在python环境下import caffe了,不过import使又出现错误了:
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
Traceback (most recent call last):
File "
File "/home/.../Downloads/caffe-master/python/caffe/__init__.py", line 1, in
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
File "/home/.../Downloads/caffe-master/python/caffe/pycaffe.py", line 13, in
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, \
ImportError: /home/.../Downloads/caffe-master/python/caffe/../../build/lib/libcaffe.so.1.0.0-rc5: undefined symbol: _ZN2cv6imreadERKNS_6StringEi
make all时出现的错误:
.build_release/lib/libcaffe.so: undefined reference to `cv::imread(cv::String const&, int)'
.build_release/lib/libcaffe.so: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::vector
.build_release/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::_InputArray const&, int)'
collect2: error: ld returned 1 exit status
Makefile:625: recipe for target '.build_release/tools/upgrade_net_proto_text.bin' failed
make: *** [.build_release/tools/upgrade_net_proto_text.bin] Error 1
make: *** Waiting for unfinished jobs....
解决办法:也是,的确在这之前我分别在python 3.5和python 2.7下面装了opencv,而且该教程前面又装了libopencv-dev,所以我就怀疑是这个,但是我尝试着卸载了opencv还是import出现同样的错误,最后不断找解决方案,也尝试了他们的各种解决方法,都不知道make clean然后又make all了多少次,还是不管用。最后在网站https://github.com/BVLC/caffe/issues/2348#issuecomment-95156848看到有人说”Uncomment if you're using OpenCV 3”我顿时又想到了配置文件,于是我在配置文件中去掉 ”OPENCV_ VERSION := 3“,我又make clean,接着make all,奇迹终于出现了,上面的错误消失了,居然make all成功了,哈哈,太高兴了!!!
上面的两个问题花的时间最长,用了两天多的时间,下面的问题网上的解决办法都是正确的,花的时间少。
3、错误:现在可以import caffe了,但是又出现了以下错误:
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
Traceback (most recent call last):
File "
File "/home/.../Downloads/caffe-master/python/caffe/__init__.py", line 1, in
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
File "/home/.../Downloads/caffe-master/python/caffe/pycaffe.py", line 15, in
import caffe.io
File "/home/..../Downloads/caffe-master/python/caffe/io.py", line 2, in
import skimage.io
File "/home/.../Downloads/yes/lib/python3.5/site-packages/skimage/io/__init__.py", line 15, in
reset_plugins()
File "/home/.../Downloads/yes/lib/python3.5/site-packages/skimage/io/manage_plugins.py", line 93, in reset_plugins
_load_preferred_plugins()
File "/home/.../Downloads/yes/lib/python3.5/site-packages/skimage/io/manage_plugins.py", line 73, in _load_preferred_plugins
_set_plugin(p_type, preferred_plugins['all'])
File "/home/.../Downloads/yes/lib/python3.5/site-packages/skimage/io/manage_plugins.py", line 85, in _set_plugin
use_plugin(plugin, kind=plugin_type)
File "/home/.../Downloads/yes/lib/python3.5/site-packages/skimage/io/manage_plugins.py", line 255, in use_plugin
_load(name)
File "/home/.../Downloads/yes/lib/python3.5/site-packages/skimage/io/manage_plugins.py", line 299, in _load
fromlist=[modname])
File "/home/.../Downloads/yes/lib/python3.5/site-packages/skimage/io/_plugins/matplotlib_plugin.py", line 3, in
import matplotlib.pyplot as plt
File "/home/.../Downloads/yes/lib/python3.5/site-packages/matplotlib/pyplot.py", line 36, in
from matplotlib.figure import Figure, figaspect
File "/home/.../Downloads/yes/lib/python3.5/site-packages/matplotlib/figure.py", line 40, in
from matplotlib.axes import Axes, SubplotBase, subplot_class_factory
File "/home/..../Downloads/yes/lib/python3.5/site-packages/matplotlib/axes/__init__.py", line 4, in
from ._subplots import *
File "/home/.../Downloads/yes/lib/python3.5/site-packages/matplotlib/axes/_subplots.py", line 10, in
from matplotlib.axes._axes import Axes
File "/home/.../Downloads/yes/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 23, in
import matplotlib.dates as _ # <-registers a date unit converter
File "/home/.../Downloads/yes/lib/python3.5/site-packages/matplotlib/dates.py", line 126, in
from dateutil.rrule import (rrule, MO, TU, WE, TH, FR, SA, SU, YEARLY, MONTHLY, WEEKLY, DAILY, HOURLY, MINUTELY, SECONDLY)
File "/home/.../Downloads/yes/lib/python3.5/site-packages/dateutil/rrule.py", line 55
raise ValueError, "Can't create weekday with n == 0"
^
SyntaxError: invalid syntax
错误解决办法:从上面可以看出也就是在 import matplotlib.pyplot as plt 时出现错误,百度了以下,尝试了几种解决方案,原来是matplotlib版本太低了,需要升级,于是我pip install matplotlib --upgrade 了以下,果然解决了,但是又出现了下面的问题。
3、错误:Intel MKL FATAL ERROR: Cannot load libmkl_avx.so or libmkl_def.so.
Python 3.5.2 |Continuum Analytics, Inc.| (default, Jul 2 2016, 17:53:06)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
Intel MKL FATAL ERROR: Cannot load libmkl_avx.so or libmkl_def.so.
解决方案:继续百度了下,哈哈,原来是因为numpy版本过低导致,于是我 conda update numpy ,哈哈又成功了。。所以,我的anaconda里边的包必须得升级啊,里边的包都还是安装anaconda时系统自带的,太老了。
参考网站:
http://www.voidcn.com/blog/u012675539/article/p-5962112.html
http://www.linuxidc.com/Linux/2016-09/135034.htm
http://caffe.berkeleyvision.org/installation.html#compilation
https://github.com/BVLC/caffe/issues/2348#issuecomment-95156848
http://stackoverflow.com/questions/36659453/intel-mkl-fatal-error-cannot-load-libmkl-avx2-so-or-libmkl-def-so
http://blog.csdn.net/10km/article/details/65936155?locationNum=14&fps=1