美文网首页
让Caffe支持Python3

让Caffe支持Python3

作者: 菜鸟瞎编 | 来源:发表于2019-02-21 17:29 被阅读0次

caffe 依赖boost库,boost默认为Python2,需要用python3环境编译boost库。

一、用Anaconda搭建Python3环境

1、安装Anaconda3

官网下载Anaconda安装包 Anaconda3-xxx.sh
安装Anaconda3 sh Anaconda3xxx.sh
在安装过程中选择自动将anaconda3/bin 写入 PATH变量
更新变量 source ~/.bashrc
如果安装过程中没有设置环境变量,这样设置:linux下的source命令(.命令)解释及应用

echo ". /home/app/anaconda3/etc/profile.d/conda.sh" >> $HOME/.bashrc

. 即是source, source 是 点命令
注意:这种方式export PATH=$PATH:$HOME/anaconda3/bin对新版本的Anaconda无效。

2、搭建Python3环境

conda create --name py365 python=3.6.5
设置环境变量(假设anaconda3安装在$HOME目录下,这里的环境变量是为了编译Caffe时使用)

export C_INCLUDE_PATH=$C_INCLUDE_PATH:$HOME/anaconda3/envs/py365/include:$HOME/anaconda3/envs/py365/include/python3.6m 
export C_PLUS_INCLUDE_PATH=$C_PLUS_INCLUDE_PATH:$HOME/anaconda3/envs/py365/include:$HOME/anaconda3/envs/py365/include/python3.6m 
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/anaconda3/envs/py365/lib

激活环境:

conda activate
conda activate py365

退出环境:

conda deactivate
conda deactivate

二、用python3编译boost

下载boost源码
激活py365环境
编译安装,这里安装到$HOME目录

./bootstrap.sh --with-libraries=python3.6
./b2
./b2 install --prefix=$HOME

设置环境变量

export C_INCLUDE_PATH=$C_INCLUDE_PATH:$HOME/boost/include
export C_PLUS_INCLUDE_PATH=$C_PLUS_INCLUDE_PATH:$HOME/boost/include
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/boost/lib

三、编译caffe

下载caffe源码,
修改Makefile.config中关于python的部分如下:

# 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)/anaconda3
# 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


# We need to be able to find libpythonX.X.so or .dylib.
# PYTHON_LIB := /usr/lib
# PYTHON_LIB := $(ANACONDA_HOME)/lib

PYTHON_LIBRARIES := boost_python3 python3.6m
PYTHON_INCLUDE := $(ANACONDA_HOME)/envs/py365/include/python3.6m \
                  $(ANACONDA_HOME)/envs/py365/lib/python3.6/site-packages/numpy/core/include
PYTHON_LIB := $(ANACONDA_HOME)/envs/py365/lib

其他修改参考:CentOS编译安装caffe
接下来即可编译安装

相关文章

网友评论

      本文标题:让Caffe支持Python3

      本文链接:https://www.haomeiwen.com/subject/vgufyqtx.html