Caffe环境安装

2019-05-23 14:35:58 浏览数 (1)

1. CUDA的安装

前面已经装过了,可以查看一下

$ cat /usr/local/cuda/version.txt CUDA Version 8.0.44

确认是否安装成功:

$ cd ~/NVIDIA_CUDA-8.0_Samples/bin/x86_64/linux/release/ $ ./deviceQuery

如果显示显卡信息,说明安装成功。

2. 安装cuDNN

前面已经安装过了。

3. 安装BLAS(基本线性代数子库)

Caffe支持的有三种:MKL,AtLas,OpenBlas。 OpenBlas是完全免费的,所以这里就安装它了:

$ sudo apt-get install openblas-dev

错误:

代码语言:javascript复制
E: 无法定位软件包 openblas-dev

解决:

$ apt-cache search openblas libblas-test - Basic Linear Algebra Subroutines 3, testing programs libopenblas-base - Optimized BLAS (linear algebra) library (shared library) libopenblas-dev - Optimized BLAS (linear algebra) library (development files) $ sudo apt-get install libopenblas-dev

注意:安装后要修改Caffe中的Makefile.config文件的BLAS选项为open,才能使其生效。

4. 安装OpenCV

前面已经安装过了,现在查看是否安装成功:

代码语言:javascript复制
 $ python
Python 2.7.14 |Anaconda, Inc.| (default, Oct 16 2017, 17:29:19) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.2.0'
>>> 

5. 安装caffe的python环境(Anaconda)

前面已经安装过了,在终端输入“python”, python 的版本信息,但是后面带了 anaconda 的标识,这就说明Anaconda 安装成功了

代码语言:javascript复制
$ python
Python 2.7.14 |Anaconda, Inc.| (default, Oct 16 2017, 17:29:19) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 

6. 下载caffe源码包

cd ~/download $ git clone https://github.com/BVLC/caffe.git $ cd ~/download $ cp -r ./caffe ~/

7.安装caffe的python依赖库

cd ~/caffe/python $ for req in $(cat requirements.txt); do pip install $req; done

如果提示权限不够,换root用户执行。

8. 编译caffe

8.1 配置文件修改

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

8.1.1Makefile.config文件修改如下:

(1)第21行

代码语言:javascript复制
 # OPENCV_VERSION := 3

改为:

代码语言:javascript复制
 OPENCV_VERSION := 3

(2)第69行

代码语言:javascript复制
PYTHON_INCLUDE := /usr/include/python2.7 
                  /usr/lib/python2.7/dist-packages/numpy/core/include

后面添加:

代码语言:javascript复制
 /home/fc/anaconda2/lib/python2.7/site-packages/numpy/core/include

(3)第97行将# Whatever else you find you need goes here.下面的

代码语言:javascript复制
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

修改为:

代码语言:javascript复制
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

8.1.2修改 makefile 文件

打开 Makefile 文件,在 415 行,将:

代码语言:javascript复制
NVCCFLAGS  =-ccbin=$(CXX) -Xcompiler-fPIC $(COMMON_FLAGS)

替换为:

代码语言:javascript复制
NVCCFLAGS  = -D_FORCE_INLINES -ccbin=$(CXX) -Xcompiler -fPIC $(COMMON_FLAGS)

8.2 编译

$ make all -j4 $ make test -j4 $ make runtest -j4

9. pycaffe配置

9.1 安装依赖

$ sudo apt-get install python-numpy python-scipy python-matplotlib python-sklearn python-skimage python-h5py python-protobuf python-leveldb python-networkx python-nose python-pandas python-gflags ipython cython $ sudo apt-get install protobuf-c-compiler protobuf-compiler

9.2 依赖库编译

$ cd ~/caffe $ make pycaffe

10 配置环境变量,以便 python 调用

$sudo vim ~/.bashrc

加入

代码语言:javascript复制
export PYTHONPATH=/home/fc/caffe/python:$PYTHONPATH

$ source ~/.bashrc

11.测试

代码语言:javascript复制
$ python
Python 2.7.14 |Anaconda, Inc.| (default, Oct 16 2017, 17:29:19) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import caffe
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/fc/caffe/python/caffe/__init__.py", line 1, in <module>
    from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver, AdamSolver, NCCL, Timer
  File "/home/fc/caffe/python/caffe/pycaffe.py", line 15, in <module>
    import caffe.io
  File "/home/fc/caffe/python/caffe/io.py", line 8, in <module>
    from caffe.proto import caffe_pb2
  File "/home/fc/caffe/python/caffe/proto/caffe_pb2.py", line 6, in <module>
    from google.protobuf.internal import enum_type_wrapper
ImportError: No module named google.protobuf.internal
>>> 

问题:

代码语言:javascript复制
ImportError: No module named google.protobuf.internal

解决:这说明没有添加google.protobuf.的位置

a.临时性添加

代码语言:javascript复制
$ python
Python 2.7.14 |Anaconda, Inc.| (default, Oct 16 2017, 17:29:19) 
[GCC 7.2.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path.append('/usr/lib/python2.7/dist-packages/')
>>> import caffe
>>> 

这样有个问题是,当关闭shell后,就会失效

b. 永久性添加

shell 下,

$ cd /usr/lib/python2.7/dist-packages

然后建立一个.pth的文件,例如我们建立一个mypth.pth

$ sudo echo mypth.pth

然后我们打开这个文件进行编辑

$ sudo vim mypth.pth

然后就会看到我们建立的文件打开了,在文件里添加我们要添加的模块路径,例如我的是 /home/fc/caffe/python/,保存退出

另外:这个方法可以试试 http://blog.csdn.net/wuzuyu365/article/details/52431062

$ sudo chmod 777 -R ~/anaconda2 $ pip install protobuf

0 人点赞