caffe python接口编译

来源:互联网 发布:等腰三角形面积算法 编辑:程序博客网 时间:2024/04/28 22:09

1、如果有需要用opnecv的话可以进行这一步。
在github上有大神已经将脚本写好地址:opencv脚本下载链接直接copy就好,安装起来麻烦。

解压:unzip /home/yyy/下载/Install-OpenCV-master.zip (yyy:)
进入解压文件夹执行脚本:sudo ./opencv2_4_9.shsudo bash opencv2_4_9.sh

2、下载caffe

下载地址:https://github.com/BVLC/caffe 解压

进入caffe的目录,修改 配置文件Makefile.config.example 的文件名改为Mikefile.config

可以直接改,也可以用命令改cp Mkefile.config.example Makefile.config
3、修改配置文件

# CPU-only switch (uncomment to build without GPU support). CPU_ONLY := 1# uncomment to disable IO dependencies and corresponding data layers USE_OPENCV := 1# Uncomment if you're using OpenCV 3 OPENCV_VERSION := 2.4.9# 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)/anaconda2 PYTHON_INCLUDE := $(ANACONDA_HOME)/include \         $(ANACONDA_HOME)/include/python2.7 \         $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include

我这个之编译了pycaffe,如果有需要编译matlab版本的可以直接修改matlab dir的路径:MATLAB_DIR :=这个maltab 的安装路径复制进去,添加路径到bin即可。
3,安装caffe依赖包

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5-serial-dev protobuf-compilersudo apt-get install --no-install-recommends libboost-all-devsudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-devsudo apt-get install libatlas-base-dev

4、先直接编译caffe

make all -j4make test -j4make runtest -j4

5、编译python接口
首先安装anaconda2,使用的是版本python2.7,可去官网直接下载。在安装即可。
编译python接口。由于已经修改过Mikefile的配置文件,直接进入caffe目录直接编译:make pycaffe matlab同理,由于ubuntu在使用gcc版本较高,编译使用matlab时编译不了,在安装matlab时需要进行gcc降级。
6、import caffe
首先打开anaconda自带的spyder,发现我的版本还是ubuntu自带的python,所以将anaconda的环境变量加入,打开gedit ~/.bashrc,在最后一行加入anaconda的环境:export PATH="/home/yyy/anaconda2/bin:$PATH"加入即可。更新环境变量source ~/.bashrc
进入caffe/python的目录下导入,如果没在python目录下出现如下结果

>>> import caffeTraceback (most recent call last):  File "<stdin>", line 1, in <module>ImportError: No module named caffe  

进入caffe的python目录

cd /home/cross_li/caffe-master/python/

又出现如下错误:ImportError: No module named google.protobuf.internal
那就安装protobuf:conda install protobuf

如果报这个错误请执行Error: Missing write permissions in: /home/cross_li/anaconda2

sudo chmod 777 -R anaconda2yyy:~$ conda install protobuf

最后没有报错即可。

原创粉丝点击