caffe利用anaconda配置python接口(cpu版可视化工具)

23 篇文章 0 订阅

参考博文:http://www.cnblogs.com/denny402/p/5679037.html

前沿

       何为可视化,请参考《21天实战caffe第16天》可视化的相关理论。caffe提供三种接口,分别为C++接口、python接口和matlab接口。C++接口不用我们自己配置,因为caffe的底层代码都是由C++编写的,而python接口和matlab接口作为可视化的工具需要我们自己配置。由于matlab文件太大,所以博主没有配置matlab接口,今天就来讲一讲博主配置python接口的一些心酸事并最终成功跳坑。

一、坑人的安装方法

        首先我们要知道的是 python环境不能单独配置,必须要先编译好caffe,才能编译python环境。如果读者连caffe都没有配置好那就不要玩接口了,先去把caffe配置好。相关配置方法可以参考博主博文,博文链接:点击打开链接

       参考博文中提到的第一种方法,先安装python和pip,然后再安装pyhon接口依赖库配置。一般ubuntu14.04系统自带的是python2.7,所以如果我们不要求更高的python版本就默认版的就可以了,如果需要可以自行搜索安装python3.4的方法。具体配置接口的方法与2.7版本类似。我们就以python2.7为例说明。

       如果选用安装python接口依赖库的方法来配置,我相信你们会被各种各样的问题给整哭的,按照参考博文的方法去配置。并不是方法本身有什么问题,解决的方法只有不停的重新安装,一次不成功马上再来一次。

二、利用anaconda安装

1、首先第一步我们来安装anaconda,其实如果我们学到这里了,我相信大多数人已经安装了anaconda。安装方法为:

      先到https://www.continuum.io/downloads 下载anaconda, 现在的版本有python2.7版本和python3.5版本,下载好对应版本、对应系统的anaconda,它实际上是一个sh脚本文件,大约280M左右。我下载的是linux版的python 2.7版本。

        下载成功后,在终端执行(2.7版本):

# bash Anaconda2-2.4.1-Linux-x86_64.sh

        或者3.5 版本:

# bash Anaconda3-2.4.1-Linux-x86_64.sh

        在安装的过程中,会问你安装路径,直接回车默认就可以了。有个地方问你是否将anaconda安装路径加入到环境变量(.bashrc)中,这个一定要输入yes

安装成功后,会有当前用户根目录下生成一个anaconda2的文件夹,里面就是安装好的内容。

        博主安装的为anaconda2。安装完成后在主文件夹下生成了一个文件为anaconda2。

2、我们来看看之前我们安装过的python依赖库,注意这个跟咱们之前配置caffe安装的那些依赖库关系不大。不要想着我以前装过了怎么还要装。

       首先我们要知道我们到底要安装那些依赖库。在caffe/python目录下我们可以找到一个文件为requirements.txt,里面都是我们需要安装的,后面的版本号我们可以不用去要求,管他,最新版的也可以的。

        在安装之前,我们先来查看一下我们都已经安装过了哪些。在主目录下命令:conda list将会把所有的以安装的显示出来。我们只要对着requirements.txt这个文件查找哪个还没有安装。

        这里有一个问题,可能会出现报错:conda:未找到命令

        解决方法:

        在你的主目录(自己的权限目录)下,使用下面的命令:

export PATH=~/anaconda2/bin:$PATH
conda --v 
        如果结果是你的anaconda2的版本号,那就成功了

        然后就可以使用conda命令了。

        将没有安装的库应用以下命令安装:

conda install ***
        例如博主经过conda list检查然后发现leveldb和protobuf没有安装,则安装:

conda install leveldb
conda install protobuf

       安装完成后,我们就可以编译配置了。

3、编译python接口     

      首先,将caffe根目录下的python文件夹加入到环境变量

      打开配置文件bashrc

# sudo vi ~/.bashrc
      在最后面加入

export PYTHONPATH=/home/xxx/caffe/python:$PYTHONPATH

      注意 /home/xxx/caffe/python 是我的路径,这个地方每个人都不同,需要修改

      保存退出,更新配置文件

# sudo ldconfig
       然后修改编译配置文件Makefile.config。在caffe目录下打开Makefile.config修改。注意,不是随便修改的,按照参考博文的说法直接给了结果,一开始是不知所云的,所以汇总一下需要修改的地方。汉字标注需要注意:

## Refer to http://caffe.berkeleyvision.org/installation.html
# Contributions simplifying and improving our build system are welcome!

# cuDNN acceleration switch (uncomment to build with cuDNN).
# USE_CUDNN := 1   #参考博文是gpu版本的,所以这里没有屏蔽,而我们cpu版本的则需要屏蔽

# CPU-only switch (uncomment to build without GPU support).
	 CPU_ONLY := 1    #cpu版本的不需要屏蔽

# uncomment to disable IO dependencies and corresponding data layers
# USE_OPENCV := 0
# USE_LEVELDB := 0
# USE_LMDB := 0

# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)
#	You should not set this flag if you will be reading LMDBs with any
#	possibility of simultaneous read and write
# ALLOW_LMDB_NOLOCK := 1

# Uncomment if you're using OpenCV 3
# OPENCV_VERSION := 3

# To customize your choice of compiler, uncomment and set the following.
# N.B. the default for Linux is g++ and the default for OSX is clang++
# CUSTOM_CXX := g++

# CUDA directory contains bin/ and lib/ directories that we need.
CUDA_DIR := /usr/local/cuda
# On Ubuntu 14.04, if cuda tools are installed via
# "sudo apt-get install nvidia-cuda-toolkit" then use this instead:
# CUDA_DIR := /usr

# CUDA architecture setting: going with all of them.
# For CUDA < 6.0, comment the *_50 through *_61 lines for compatibility.
# For CUDA < 8.0, comment the *_60 and *_61 lines for compatibility.
CUDA_ARCH := -gencode arch=compute_20,code=sm_20 \
		-gencode arch=compute_20,code=sm_21 \
		-gencode arch=compute_30,code=sm_30 \
		-gencode arch=compute_35,code=sm_35 \
		-gencode arch=compute_50,code=sm_50 \
		-gencode arch=compute_52,code=sm_52 \
		-gencode arch=compute_60,code=sm_60 \
		-gencode arch=compute_61,code=sm_61 \
		-gencode arch=compute_61,code=compute_61

# BLAS choice:
# atlas for ATLAS (default)
# mkl for MKL
# open for OpenBlas
BLAS := atlas
# Custom (MKL/ATLAS/OpenBLAS) include and lib directories.
# Leave commented to accept the defaults for your choice of BLAS
# (which should work)!
# BLAS_INCLUDE := /path/to/your/blas
# BLAS_LIB := /path/to/your/blas

# Homebrew puts openblas in a directory that is not on the standard search path
# BLAS_INCLUDE := $(shell brew --prefix openblas)/include
# BLAS_LIB := $(shell brew --prefix openblas)/lib

# This is required only if you will compile the matlab interface.
# MATLAB directory should contain the mex binary in /bin.
# MATLAB_DIR := /usr/local
# MATLAB_DIR := /Applications/MATLAB_R2012b.app

# 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     #此处anaconda路径原始文件是屏蔽的,现在我们把它改过来下面的也是这样,这是基于python2.7版本需要修改的地方
 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是3.5版本的那么就需要把这里的屏蔽去掉
# PYTHON_INCLUDE := /usr/include/python3.5m \
#                 /usr/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

# Homebrew installs numpy in a non standard path (keg only)
# PYTHON_INCLUDE += $(dir $(shell python -c 'import numpy.core; print(numpy.core.__file__)'))/include
# PYTHON_LIB += $(shell brew --prefix numpy)/lib

# Uncomment to support layers written in Python (will link against Python libs)
 WITH_PYTHON_LAYER := 1

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include /home/xn/local_install/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /home/xn/local_install/lib

# If Homebrew is installed at a non standard location (for example your home directory) and you use it for general dependencies
# INCLUDE_DIRS += $(shell brew --prefix)/include
# LIBRARY_DIRS += $(shell brew --prefix)/lib

# NCCL acceleration switch (uncomment to build with NCCL)
# https://github.com/NVIDIA/nccl (last tested version: v1.2.3-1+cuda8.0)
# USE_NCCL := 1

# Uncomment to use `pkg-config` to specify OpenCV library paths.
# (Usually not necessary -- OpenCV libraries are normally installed in one of the above $LIBRARY_DIRS.)
# USE_PKG_CONFIG := 1

# N.B. both build and distribute dirs are cleared on `make clean`
BUILD_DIR := build
DISTRIBUTE_DIR := distribute

# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171
# DEBUG := 1

# The ID of the GPU that 'make runtest' will use to run unit tests.
TEST_GPUID := 0

# enable pretty build (comment to see full commands)
Q ?= @

       至此Makefile.config文件修改并保存完成。下面就是真的编译了。

      编译之前最好先clean一下可以减少出错几率:

sudo make clean
      在caffe路径下编译:

sudo make pycaffe
       编译成功后,不能重复编译,否则会提示 Nothing to be done for "pycaffe"的错误。下图是编译成功的界面。


      

       防止其它意外的错误,最好还编译一下:

# sudo make test -j8
# sudo make runtest -j8
(1)也许你在编译runtest的时候,会报这样的错误:

.build_release/test/test_all.testbin: error while loading shared libraries: libhdf5.so.10: cannot open shared object file: No such file or directory

       这是因为 libhdf5.so的版本问题,你可以进入/usr/lib/x86_64-linux-gnu看一下,你的libhdf5.so.x中的那个x是多少,比如我的是libhdf5.so.7

       因此可以执行下面几行代码解决:

# cd /usr/lib/x86_64-linux-gnu
# sudo ln -s libhdf5.so.7 libhdf5.so.10
# sudo ln -s libhdf5_hl.so.7 libhdf5_hl.so.10
# sudo ldconfig

 (2)也可能这样报错: .build_release/tools/caffe: error while loading shared libraries: libboost_system.so.1.58.0: cannot open shared object file: No such file or directory

原因与上面类似在usr/lib/x86_64-linux-gnu下查看到我的版本是libboost_system.so.1.54.0,所以用下面的命令解决后重新runtest。

# cd /usr/lib/x86_64-linux-gnu
# sudo ln -s libboost_system.so.1.54.0  libboost_system.so.1.58.0
# sudo ln -s _libboost_system_hl.so.1.54.0  libboost_system_hl.so.1.58.0
# sudo ldconfig
(3)之后还可能出现这样的报错:.build_release/tools/caffe: error while loading shared libraries: libboost_thread.so.1.58.0: cannot open shared object file: No such file or directory
原因与上面类似,解决方法:

# cd /usr/lib/x86_64-linux-gnu
# sudo ln -s libboost_thread.so.1.54.0  libboost_thread.so.1.58.0
# sudo ln -s _libboost_thread_hl.so.1.54.0  libboost_thread_hl.so.1.58.0
# sudo ldconfig

完成这些只有,再次runtest发现可以运行了。

最终查看python接口是否编译成功:
进入python环境,进行import操作

# python
>>> import caffe
博主最后这里出现一个问题,配置不成功。出现报错无法识别caffe。搜索了解决方案但不可以解决。不知到是哪里除了问题,唯一有可能的就是
# sudo make runtest -j8
出了问题,然后博主重新执行此命令,make完成后发现成功了。至此python环境接口配置完成。
3、安装jupyter
       安装了python还不行,还得安装一下ipython,后者更加方便快捷,更有自动补全功能。而ipython notebook是ipython的最好展现方式。最新的版本改名为jupyter notebook,我们先来安装一下。(如果安装了anaconda, jupyter notebook就已经自动装好,不需要再安装)

# sudo pip install jupyter

       这里博主又出现了报错,一堆红色,如下图。

      根据之前的经验,应该是不可以用sudo权限运行。采用如下命令:

pip install jupyter
       成功!!!!

也有可能不成功,因为本来是配好了,结果博主隔了两天重新启动jupter无法打开了。不知哪里出现问题。

根据以上红色的问题发现还是pip的问题,其实就是pip版本太老的原因,博主先卸载原先pip,命令如下:

sudo apt-get remove python-pip

然后利用install安装新下载的pip,不要用apt-get安装。

 wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.6.tar.gz"

tar -xzvf pip-1.5.6.tar.gz

cd pip-1.5.6

python setup.py install

然后再次利用pip安装看看能不能成功,如果继续报错,则利用如下命令更新pip:

sudo easy_install --upgrade pip
然后再次安装jupyter应该就差不多可以成功了。
安装成功后,运行notebook

# jupyter notebook

就会在浏览器中打开notebook,  点击右上角的New-python2, 就可以新建一个网页一样的文件,扩展名为ipynb。在这个网页上,我们就可以像在命令行下面一样运行python代码了。输入代码后,按shift+enter运行,更多的快捷键,可点击上方的help-Keyboard shortcuts查看,或者先按esc退出编辑状态,再按h键查看。

至此,全部完成。如有意见或建议,请留言!

如要退出jupyter终端环境,按下ctrl+c然后选择y即可。


  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值