32bit/64bit-Ubuntu14.04+anaconda+CPU版本的Caffe安装教程
来源:互联网 发布:淘宝网dnf游戏币 编辑:程序博客网 时间:2024/05/16 13:07
【前言】
关于深度学习,我想朋友们多有了解,这里就不介绍了。关于深度学习的框架来说,有最近比较流行的Tensorflow,它是由大牛贾扬清2015年联合谷歌团队开发的新型框架,安装比较简单,不过可惜了,由于小编的电脑是32位操作系统,对于Tensorflow来说,就有点无能为力了。所以,最终还是决定用经典框架——Caffe搭建。 废话少说,下面就开始我的艰难搭建之旅吧,在这之前还是要特别感谢实验室的吴学长,小大牛一个,反应灵敏,思路清晰,有很强的知识底蕴,小生佩服啊!
本文主要参考资料,来源于http://coldmooon.github.io/2015/08/03/caffe_install/,从零安装Caffe和http://www.cnblogs.com/platero/p/3993877.html
一,安装开发所依赖的安装包
打开Ubuntu的终端,输入命令。
sudo apt-get install build-essentialsudo apt-get install vim cmake gitsudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev</span>
二,升级g++。
终端输入命令
g++ --version</span>查看g++版本,由于编译需要至少4.8版本的g++,一般来说,自带版本应该时4.8。如果想要升级g++,这里也提供以下下载方式,并且设置默认编译的g++为最新版本。
sudo add-apt-repository ppa:ubuntu-toolchain-r/test #用来添加库信息的帮助脚本。sudo apt-get update #更新sudo apt-get install gcc-4.9 sudo apt-get install g++-4.9 #更新g++,gccsudo ln -sf /usr/bin/gcc-4.9 /usr/bin/gcc #在调用的gcc库上做一个与最新版本的软连接,更改g++的默认版本。sudo ln -sf /usr/bin/gcc-ar-4.9 /usr/bin/gcc-arsudo ln -sf /usr/bin/gcc-ranlib-4.9 /usr/bin/gcc-ranlib
三:安装anaconda,建议安装。
anaconda面集成了很多包,如ipython,mkl,numpy等都预装了, 省去了很多麻烦。如果有 edu 邮箱的话,还可以获得 anaconda accelerate
,在矩阵运算的时候,可以启用并行计算,速度快很多,这里需要注意加速版本,前提也是需要64位,可怜的32位,无形中增加了不少限制。
1,首先是去anoconda官网下载,需要所需版本。官网如下:https://www.continuum.io/downloads,这是软件安装的官方教程,提供了简单明了的安装方式。https://docs.continuum.io/anaconda/install
2,打开终端,输入
bash ~/Downloads/Anaconda3-4.0.0-Linux-x86_64.sh #注意这里的名字要换成自己对应版本的sh。安装过程中记得一直选择默认的就行,傻瓜式安装。
3,是否在bashrc中添加路径,这里选择“yes”就可以,否则后面报错的话,你需要将以下代码天价到你的bash shell环境变量中,记得
cd ~ #转到主目录,准备执行shell,注意其他目录不行。vim .bashrc
export PATH="/home/username/anaconda/bin:$PATH" #最下面一行添加就行,:wq保存退出。记得对应更改自己的用户名和anaconda名字,我的后面加了一个2.
source .bashrc #更新
关掉终端,完成安装。
四,安装OpenCV2.4。(必做)
1,去官网下载源码,保存在默认位置。官网链接: http://opencv.org/downloads.html。建议下载2.4.12稳定版本,由于新版3.0还有些文件缺失,有时候用起来反而不方便。
2,添加必要的库。
打开终端,添加代码。
sudo apt-get install build-essential libgtk2.0-dev libjpeg-dev libtiff4-dev libjasper-dev libopenexr-dev cmake python-dev python-numpy python-tk libtbb-dev libeigen2-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev libqt4-dev libqt4-opengl-dev sphinx-common texlive-latex-extra libv4l-dev libdc1394-22-dev libavcodec-dev libavformat-dev libswscale-dev3,解压,使用unzip或者tar命令,解压后,将执行目录chang directory your 解压目录。
mkdir buildcd buildcmake ..make #编译sudo make install #安装小编提示,这里花费时间较长,大约20+分钟,你可以愉快的看下《欢乐喜剧人》了。
五,安装MKL(可选)和atlas(必选),主要是为了编译时用的。
1 atlas
sudo apt-get install libatlas-base-dev #安装atlas简单易行。2 ,mkl
1 下载MKL,这里小编给大家推荐了学生版本,免费下载。https://software.intel.com/en-us/qualify-for-free-software/student。
2 ,解压安装包,cd命令,转到该目录下,有个readme的文件,里面提供了多种安装方式,其中一个比较简单。
下面有一个install_GUI.sh文件,会出现图形安装界面,根据说明一步一步执行即可。
3,添加路径。
注意: 安装完成后需要添加library路径, 创建/etc/ld.so.conf.d/intel_mkl.conf文件, 在文件中添加内容
sudo vim /etc/ld.so.conf.d/intel_mkl.conf
/opt/intel/lib
/opt/intel/mkl/lib/intel64 #注意这里把对应的文件名换成自己的。
sudo ldconfig #更新路径。
六 安装其他的依赖库
sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler
sudo apt-get install libgflags-dev libgoogle-glog-dev liblmdb-dev
sudo apt-get install python-dev python-pip
打开新的终端, 用which python
和which pip
确定使用的是anaconda提供的python环境,然后进入caffe_root/python
, 执行下列命令,
for req in $(cat requirements.txt); do pip install $req; done #python运行需要的库。
七:编译Caffe
前期的工作做完了,终于等到这令人愉快的一步了。1 下载caffe
cd ~git clone https://github.com/BVLC/caffe.git2,更新安装依赖。
cd caffecat python/requirements.txt | xargs -L 1 sudo pip install3,修改Makefile,这是非常重要的一步。
cp Makefile.config.example Makefile.configvi Makefile.config #我个人比较喜欢gedit Makefile.config,用起来方便。接下来,我就把修改的片段进行粘贴注解。主要采用的还是集成工具anaconda,atlas,CPU版本的。
style="font-size:14px;"># CPU-only switch (uncomment to build without GPU support). CPU_ONLY := 1 #一定需要打开。# Uncomment if you're using OpenCV 3# OPENCV_VERSION := 3 #用的是2.4版本不需要打开。# 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++ #已经更新到至少4.8,选择默认的就好。# CUDA directory contains bin/ and lib/ directories that we need. #这是GPU用到的,注释掉就可以。#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 lines for compatibility. #这些部分全部注释,和我们cpu家族没有关系。#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_50,code=compute_50# BLAS choice:# atlas for ATLAS (default)# mkl for MKL# open for OpenBlasBLAS := atlas #如果安装mkl并且设置好路径的可以试一下mkl,不过一般会出错,找不到lmkl文件,建议采用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. #python很流行,matlab暂时没安装,也就用不到了。# 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.#注释掉,我们采用的时anaconda里面的python安装包。# 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 \ #因为下载的时候采用的时2.7版本的,所以这里打开2.7版本的python。 $(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的默认版本,自然需要更改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 #python路径已经设置好,所以这里打开就行。LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/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# 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 := 1BUILD_DIR := build #编译分配用到的路径,打开即可。DISTRIBUTE_DIR := distribute# Uncomment for debugging. Does not work on OSX due to https://github.com/BVLC/caffe/issues/171# DEBUG := 1 #挑错模式,喜欢debug的朋友不妨试试。4,接下来,我们就可以愉快的进行编译了。
make pycaffe -jX #为了提高编译速度,这里的x设成自己的cpu核数make all -jXmake test -jX
make runtest -jX #注意,这里也可直接运行,make runtest小编在编译时主要遇到两类问题。
1,在make all时发现找不到hdf5的库。
这里提供两种解决方案。
【方案一】
安装过 anaconda
的话,那 libhdf5-serial-dev
可以不装。如果编译时提示找不到 hdf5
的库。就把 anaconda/lib
加到 ld.so.conf
中去。
sudo vim /etc/ld.so.conf
#添加一行,用户名改为你自己的: /home/your_username/anaconda/lib #:wq 关闭并保存文件。$ sudo ldconfig【方案二】
在同一个目录下,用cp命令复制一份 /libhdf5_hl.so.7 文件,记得尾缀改成你所缺少的。比如...10.
用cp命令复制一份usr/lib/i386-linux-gnu/libhdf5.so.7 还是 libhdf5.so.7 。
之后还是要建立与之前的软连接,已我的...10为例。
代码如下。
cd /usr/lib/i386-linux-gnucp libhdf5_hl.so.7 libhdf5_hl.so.10cp libhdf5.so.7 libhdf5.so.10sudo ln -sv libhdf5_hl.so.10 /usr/lib/ libhdf5_hl.so.10 #注意改掉不同的数字。sudo ln -sv libhdf5.so.10 /usr/lib/ libhdf5.so.10sudo ldconfig<span style="font-size: 14px;
#由于32位的操作系统,lmdb数据块没有足够的大,需要更改两个地方。
F0213 10:25:10.921640 9387 db.hpp:109] Check failed: mdb_status == 0 (-30792 vs. 0) MDB_MAP_FULL: Environment mapsize limit reached
需要更改的两个地方,前面时具体的文件位置,直接打开文件更改就行:
home/yourname/ caffe/src/caffe/util/db_lmdb.cpp:const size_t LMDB_MAP_SIZE = 536870912; // 1 TB 1099511627776/4
home/yourname/ caffe/examples/mnist/convert_mnist_data.cpp: CHECK_EQ(mdb_env_set_mapsize(mdb_env, 536870912), MDB_SUCCESS) // 1TB 1099511627776/4
哈哈,之后就可以看见一系列的”ok“样式了,小编心里很高兴。
八,输出caffe,
这是一个另外一个比较重要的步骤,也是多亏了学长给解决的。这个问题一些教程没有提到,小编这里详细说一下,因为上面我们已经设置了python和caffe的接口,也就是pycaffe,caffe编译成功与否,预示着在python下,能不能输出caffe,我们很多人用的就是python接口,这里输出成功才标志着我们真正的成功。不过小编告诉你,这里也不是太难了。
注意一下,如果之前的Makefileconfig你用的时系统自带的python版本的话,我相信,只要前面的make runtest成功了,这是输出python绝对不是问题。
但是事情呢,往往没有那么简单,因为之前我们采用的是anaconda版本的python2.7,这里我们需要把bashrc更新一下,只要添加caffe下的python路径即可。
具体操作如下:
cd ~ vim .bashrc #最后面进行添加。
# added by Anaconda2 4.0.0 installerexport PATH="/home/tom/anaconda2/bin:$PATH"#这里时安装anaconda时系统默认存在的,前面小编提到过的。#set PYTHONPATH 设置caffe下的路径。否则caffe也找不到。export PYTHONPATH="/home/tom/caffe/python:$PYTHONPATH"
执行以下代码,然后就可以见到令你期待已久的结果了。
cd ~pythonimport caffe
学长和我用的时一样的配置,但是他的也有问题,输出不了caffe,后来他在caffe下,加入了sys的目录,最终输出成功,具体见代码。
import sys>>> sys.path.append('/usr/lib/python2.7/dist-packages/')>>> import caffe
【总结】
讲解到此结束,总结一下呢,我是刚接触linux系统,对系统很陌生,有很多的指令,不是很明白,让我突然接触caffe框架的安装,还是用了不少时间的,在此期间吴学长的帮助毋庸置疑啊,再次谢谢了呢,O(∩_∩)O哈哈~期间对于congfig,添加路径,bashrc添加路径和etc/profile 添加路径还是有些许的迷茫,可能是跟库文件的搜索调用顺序有很多的关系吧,对于linux系统下,软件的安装也是多渠道的,deb一种,源码一种,sudo apt-get install一种等等。还有期间,我用的chrome搜索真的特别给力,不过得加强小编的英语学习啊,总之这几天还是收获不小的。
一些问题,能自己解决就自己解决,不能解决的再和他人商量,这是学长给我的建议,也是送给亲爱的读者啊!
- 32bit/64bit-Ubuntu14.04+anaconda+CPU版本的Caffe安装教程
- 32bit/64bit-Ubuntu14.04+anaconda+CPU版本的Caffe安装教程
- Caffe + Ubuntu14.04 64bit(位)+ Cuda6.5/Cuda7.0 安装配置教程
- ubuntu14.04 安装caffe步骤 CPU版本
- 虚拟机Ubuntu14.04+caffe+CPU安装教程
- caffe+Ubuntu14.0.4 64bit 环境配置说明(无CUDA,caffe在CPU下运行)
- caffe+Ubuntu14.0.4 64bit 环境配置说明(无CUDA,caffe在CPU下运行)
- 环境配置说明(无CUDA,caffe在CPU下运行)caffe+Ubuntu14.0.4 64bit
- Ubuntu14.04 64bit安装Android Studio
- ubuntu14.04 64bit安装skype4.3
- Ubuntu14.04(64bit)安装YouCompleteMe
- Ubuntu14.04(64bit)安装Dropbox
- Ubuntu 14.04 64bit 安装Caffe(GPU版本)
- 32bit的交叉编译器在ubuntu14.04 64bit下运行
- Anaconda 下安装opencv [win10 64bit]
- 环境配置说明(无CUDA,caffe在CPU下运行)caffe+Ubuntu14.0.4 64bit---(吐血推荐)
- 64bit CPU 知识 (IA32,IA64,EM64T,AMD64)-用于oracle、mysql安装版本的选择
- ubuntu14.04安装Anaconda、opencv3.1、caffe
- 使用Itext-Flying Saucer将Html转PDF的font-family问题
- 提供给开发者 10 款最好的 Python IDE
- vs2013编译 protoBuffer编译出现的问题
- Mysql中的存储过程详解
- UIButton的基本用法和事件
- 32bit/64bit-Ubuntu14.04+anaconda+CPU版本的Caffe安装教程
- java中hashCode()与equals()详解
- 极限安全新闻播报(04.08)
- ceph 对象存储网关radosgw和S3接口详细安装配置测试文档
- 引入cocoaPods后找不到头文件,实际头文件就包含在Pods下
- parfor, callback, real-time data reading
- linux日志文件相关
- linux开机默认进入命令界面
- Servlet 中文乱码问题及解决方案剖析