虚拟机中ubuntu17.04+python3.6+anaconda3配置caffe(CPU)

本文记录了新手在Ubuntu17.04虚拟机中安装配置Caffe的过程,尽管官方教程针对的是17.04,但作者最终参照16.04和14.04的教程成功安装。首先,文章介绍了安装Python3.6的步骤,包括从官网下载并进行相应操作。此外,还提到了如何在需要时切换回Python2。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

新手第一次配置caffe,加上课业繁重每天几乎只有中午饭后睡前和晚上下课以后有时间搞一点点,最后找了个周六自己花了一天时间安好,希望能记录下来。

首先是安装ubuntu17.04虚拟机,这个不必多说。官网上我看到了caffe对ubuntu17.04的安装教程,可能是因为我还没参透吧,毕竟ubuntu接触了时间不长,最后还是找了16.04和14.04的教程最终安装完成。

下面开始caffe安装啦。

1.安装python3.6

从官网上下载,我的是3.6.3。附官网链接点击打开链接 下载文件Python3.6.3.tar.xz

  
tar xJf Python-3.6.1.tar.xz cd Python-3.6.1 ./configure make /*这步如果需要sudo,请使用sudo -H命令,即sudo -H make install,避免pip等模块安装失败。 错误示例(pip安装失败):The directory '/home/ls/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. */ make install
作者:Shawpo 链接:http://www.jianshu.com/p/2fd6383ec010 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
  
tar xJf Python-3.6.1.tar.xz cd Python-3.6.1 ./configure make /*这步如果需要sudo,请使用sudo -H命令,即sudo -H make install,避免pip等模块安装失败。 错误示例(pip安装失败):The directory '/home/ls/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. */ make install
作者:Shawpo 链接:http://www.jianshu.com/p/2fd6383ec010 來源:简书 著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
tar xJf Python-3.6.1.tar.xz
cd Python-3.6.1
./configure --enable-shared  //解决共享库libpython3.6m.so.1.0问题,后期遇到重新编译会很麻烦
make
make install
cp libpython3.6m.so.1.0 /usr/lib

*****************************************************************************************

若遇到错误提示发现使用zlib库出错

sudo apt-get install libbz2-dev libgdbm-dev liblzma-dev libreadline-dev libsqlite3-dev libssl-dev tcl-dev tk-dev dpkg-dev

*****************************************************************************************

2.配置anaconda3

官网下载点击打开链接

下载完之后是后缀.sh的文件,终端输入

bash Anaconda3-5.0.0.1-Linux-x86_64.sh

3.切换默认python版本

终端键入python查看版本,如果默认不是python3

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

如果要切换到Python2,执行:

sudo update-alternatives --config python


正式开始安装caffe:

4.安装依赖库

sudo apt-get install libprotobuf-dev   
sudo apt-get install libleveldb-dev   
sudo apt-get install libsnappy-dev   
sudo apt-get install libopencv-dev   
sudo apt-get install libhdf5-serial-dev   
sudo apt-get install protobuf-compiler  
sudo apt-get install --no-install-recommends libboost-all-dev

接下来是BLAS:

sudo apt-get install libatlas-base-dev  

建立pycaffe接口:

sudo apt-get install python-dev 

以及一些依赖库:

sudo apt-get install libgflags-dev  
sudo apt-get install libgoogle-glog-dev   
sudo apt-get install liblmdb-dev  


5.下载caffe源码:

首先安装git

sudo apt-get install git

然后下载caffe源码:

git clone https://github.com/BVLC/caffe.git

之后:

cd caffe/python
sudo apt-get install python-pip  
for req in $(cat requirements.txt); do pip install $req; done 

6.编译caffe源码:

cp Makefile.config.example Makefile.config  
文本编辑器打开Makefile.config对文件做相应的修改:共四处


# CPU-only switch (uncomment to build without GPU support).
CPU_ONLY := 1           

这里取消注释


# 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 /usr/include/hdf5/serial
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib /usr/lib/x86_64-linux-gnu/hdf5/serial
这里修改为如图

# 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.6m
PYTHON_INCLUDE := /usr/include/python3.6m \
                /usr/lib/python3.6/dist-packages/numpy/core/include

默认为python2的路径,把python2的路径注释掉,把python3的路径取消注释,注意anaconda的路径需要修改。


7.开始编译:

make pycaffe
make all
make test
make runtest
如果有错误,先make clean再重新编译。

*****************************************************************************************

错误信息没有pyconfig.h文件

export CPLUS_INCLUDE_PATH=/usr/include/python2.7
注意每次clean之后都要重来一次


错误信息fatal error: numpy/arrayobject.h没有那个文件或目录:

sudo apt-get install python-numpy

错误信息/usr/bin/ld:cannot find -lboost_python3

cd /usr/lib/x86_64-linux-gnu/
sudo ln -s libboost_python-py35.so libboost_python3.so

错误信息dpkg:处理软件包xxx(--config)时出错:

sudo mv /var/lib/dpkg/info /var/lib/dpkg/info_old

sudo mkdir /var/lib/dpkg/info

sudo apt-get update

sudo apt-get -f install

sudo mv /var/lib/dpkg/info/* /var/lib/dpkg/info_old

sudo rm -rf /var/lib/dpkg/info

sudo mv /var/lib/dpkg/info_old /var/lib/dpkg/info  



*****************************************************************************************

最后

python
import caffe


结束。


喝一杯咖啡真难啊。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值