Caffe的安装与安装常见错误

1、Caffe的安装
安装依赖项
$ sudo apt-get install -y libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev
$ sudo apt-get install -y libgflags-dev libgoogle-glog-dev liblmdb-dev
$ sudo apt-get install -y python3-numpy python3-scipy python3-matplotlib python3-sklearn python3-skimage python3-h5py python3-protobuf python3-leveldb python3-networkx python3-nose python3-pandas python3-gflags Cython ipython

如果安装过程中出现无法定位软件包
$ sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
$ sudo apt-get update
$ git clone git://github.com/BVLC/caffe.git

$ cd caffe
$ cp Makefile.config.example Makefile.config

修改 Makefile.config
$ sudo vim Makefile.config
去掉CPU_ONLY :=1的注释
去掉OPENCV_VERSION:=3的注释
注释掉CUDA有关的行:
#CUDA_DIR := /usr/local/cuda
#CUDA_DIR := /usr
#CUDA_ARCH := …
#TEST_GPUID := 0

注释掉Python2的路径,去掉Python3的注释,修改为
PYTHON_LIBRARIES :=boost_python3 python3.6m
PYTHON_INCLUDE := /usr/include/python3.6m /usr/lib/python3.6/dist-packages/numpy/core/include

去掉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 /usr/lib/x86_64-linux-gnu/hdf5/serial

去掉TEST_GPUID := 0的注释

修改 caffe/examples/cpp_classification/classification.cpp文件
加入
#include
#include

修改完成后编译
$ sudo make all –j4
$ sudo make test
$ sudo make runtest
$ sudo make pycaffe
编译完成后测试是否可用
$ cd python
$ python
#如果出现
Python 3.6.8 (default, Jun 22 2015, 17:58:13)
[GCC 4.8.2] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.

import caffe

出现错误需要重新编译,重新编译命令如下:
$ sudo make clean
$ sudo make all –j4
$ sudo make test
$ sudo make runtest
$ sudo make pycaffe

2、Caffe安装的常见错误

出现问题:
ImportError: cannot import name ‘_validate_lengths’
解决方案:
$ sudo vim /usr/lib/python3/dist-package/skimage/util/arraycrop.py
修改如下:
import numpy as np
from distutils.version import LooseVersion as Version
old_numpy=Version(np.Version) < Version(‘1.16’)
if old_numpy:
from numpy.lib.arraypad import _validate_lengths
else:
from numpy.lib.arraypad import _as_pairs

出现问题:
编译caffe时提示错误/usr/bin/ld:cannot find –lhdf5_hl
解决方案:
更改Makefile:在Makefile中作如下更改:
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial

出现问题:
Cannot find –lpython3.5m
解决方案:
可能是因为Makefile.config中python3的注释打开了,在编译时不用打开注释,直接修改python2的内容为python3即可。

出现问题:
如果不报错就是python的接口配置成功,如果:
XXXX@amax:~/caffe$ python
Python 3.6.3 |Anaconda, Inc.| (default, Oct 13 2017, 12:02:49)
[GCC 7.2.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.

import caffe
Failed to include caffe_pb2, things might go wrong!
Traceback (most recent call last):
File “”, line 1, in
File “/home/gaoshuai/caffe/python/caffe/init.py”, line 4, in
from .proto.caffe_pb2 import TRAIN, TEST
File “/home/gaoshuai/caffe/python/caffe/proto/caffe_pb2.py”, line 6, in
from google.protobuf.internal import enum_type_wrapper
ModuleNotFoundError: No module named ‘google’

解决方案:
需要在python内重装protobuf,输入如下命令:
$ sudo pip3 install protobuf

出现问题:
安装Caffe框架时, make -j8; make pycaffe都正常。 测试python接口是否可用时出现了错误:

import caffe
Traceback (most recent call last):
File “”, line 1, in
File “/home/jcole/Git/caffe/python/caffe/init.py”, line 1, in
from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
File “/home/jcole/Git/caffe/python/caffe/pycaffe.py”, line 13, in
from ._caffe import Net, SGDSolver, NesterovSolver, AdaGradSolver,
ImportError: /home/jcole/Git/caffe/python/caffe/_caffe.so: undefined symbol: _ZN5boost6python6detail11init_moduleER11P

解决方案:
libboost_python.so的版本不匹配,默认python版本为3.5,而boost_python为2.7。修改Makefile.config文件,将PYTHON_LIBRARIES := boost_python3 python3.5m注释去掉,并修改为PYTHON_LIBRARIES := boost_python3-py35 python3.5m,重新编译即可。注:python3.5m一定要带上,否则会出现“未定义引用”的错误。

出现问题:
在python里面import caffe时报错:
ImportError: No module named caffe
解决方案:
要把路径添加一下:
export PYTHONPATH=/your caffe home/python:$PYTHONPATH

出现问题:
make all时出现
CXX/LD -o .build_release/tools/upgrade_net_proto_binary.bin
.build_release/lib/libcaffe.so: undefined reference to cv::imread(cv::String const&, int)’ .build_release/lib/libcaffe.so: undefined reference tocv::imencode(cv::String const&, cv::_InputArray const&, std::vector&, std::vector const&)’
.build_release/lib/libcaffe.so: undefined reference to `cv::imdecode(cv::_InputArray const&, int)’
collect2: error: ld returned 1 exit status
Makefile:635: recipe for target ‘.build_release/tools/upgrade_net_proto_binary.bin’ failed
make: *** [.build_release/tools/upgrade_net_proto_binary.bin] Error 1
解决方案:
在Makefile.config文件中添加如下命令行
LIBRARIES += glog gflags protobuf leveldb snappy
lmdb boost_system hdf5_hl hdf5 m
opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs

出现问题:
make pycaffe时出现
CXX/LD -o python/caffe/_caffe.so python/caffe/_caffe.cpp
python/caffe/_caffe.cpp:10:10: fatal error: numpy/arrayobject.h: No such file or directory
#include
^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Makefile:517: recipe for target ‘python/caffe/_caffe.so’ failed
make: *** [python/caffe/_caffe.so] Error 1
解决方案:
下载numpy,命令如下
sudo apt-get install python-numpy

出现问题:
fatal error:hdf5.h:没有那个文件或目录
解决方案:
在Makefile.config文件的第85行,添加/usr/include/hdf5/serial/ 到 INCLUDE_DIRS,也就是把下面第一行代码改为第二行代码。
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /usr/include/hdf5/serial/
在Makefile文件的第173行,把 hdf5_hl 和hdf5修改为hdf5_serial_hl 和 hdf5_serial,也就是把下面第一行代码改为第二行代码。
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_hl hdf5
LIBRARIES += glog gflags protobuf boost_system boost_filesystem m hdf5_serial_hl hdf5_serial

可能解决方案:
修改 Makefile
$ gedit Makefile
opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs
加入 opencv_imgcodecs

以上为本人在安装Caffe时的真实踩坑经历,送给同样喜欢深度学习的童鞋,避免踩坑,如需转载,请注明出处。

你可能感兴趣的:(python,caffe,linux)