温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Ubuntu14.04 Caffe如何安装

发布时间:2021-11-15 17:30:55 来源:亿速云 阅读:136 作者:小新 栏目:大数据

这篇文章将为大家详细讲解有关Ubuntu14.04 Caffe如何安装,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

Caffe简介

Caffe全称Convolutional Architecture for Fast Feature Embedding,是一个计算CNN相关算法的框架。就目前来说是deeplearning最流行的开源的框架,所以使用的人也是最多的。
caffe网址:http://caffe.berkeleyvision.org/
github网址:https://github.com/BVLC/caffe/

安装环境

系统:Ubuntu14.04 Desktop x64(建议使用刚安装好的系统,同时不要更新,不然会影响后面的某些环节)
显卡:支持CUDA的显卡,当然这个不是必须的

依赖程序

基本包

安装开发需要的一些基本包

sudo apt-get updatesudo apt-get install build-essentialsudo apt-get install gfortran ## python中安装scipy需要依赖gfortran

:Ubuntu默认环境虽然已经有gcc,但并没有提供C/C++的编译环境,单独安装编译环境比较复杂,所以提供了build-essential命令一键来构建所需的编译环境。

CUDA(可选)

cuda在caffe编译和使用中不是必须的,如果实在没有支持cuda的显卡或者只想尝试一下caffe,不需要用它来进行复杂的训练,不安装也可以。
安装链接:http://blog.csdn.net/honyniu/article/details/46387429

CuDNN(可选)

这个库是基于cuda,当然也是可选的。
安装链接:http://blog.csdn.net/honyniu/article/details/46388241

BLAS(必选)

这个库提供了caffe需要的一些基本的矩阵和向量库,这个是必选的。
安装链接:http://blog.csdn.net/honyniu/article/details/46388915

OpenCV(必选)

OpenCV提供了图像处理和计算机视觉的库,在caffe使用中是必须的,同时最好自己编译安装。
安装链接:http://blog.csdn.net/honyniu/article/details/46390097

其他依赖项

caffe需要一些特定的数据库的操作, 因此需要安装leveldb, lmdb, hdf5等库。 此外, caffe采用了google的一些库, 比如protobuf以及glog, 这些也需要安装,命令如下。

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler

Matlab和python

这两个不是必须的,但是caffe提供了bash、matlab和python的接口,同时caffe许多的可视化接口都是通过python实现的,推荐安装。
matlab在这里使用的是Matlab-R2014A,这里就不讲怎么安装得了,自己去百度教程,默认安装即可,安装在’/usr/local/MATLAB’目录下。
ubuntu默认安装的2.7的版本但是为了安装Python开发环境,方便今后编译其他扩展库,命令如下

sudo apt-get install python-dev

python是使用的虚拟环境,具体虚拟环境的构建见http://blog.csdn.net/honyniu/article/details/46382929
激活虚拟环境

source ENV/bin/activate

安装编译pycaffe的依赖包, 进入caffe安装目录中的python文件夹, 你会看到一个requirements.txt, 首先, 修改protobuf那一行为protobuf==2.5.0, 然后执行下面命令:

for req in $(cat requirements.txt); do pip install $req; done

这里我们没安装caffe推荐的anaconda包, 因为安装之后进不去桌面。而且anaconda包只是一个自带了requirements.txt中所有选项的python集合库, 将上面的库安装好之后, 不装anaconda没有任何影响。

配置编译

caffe程序

可以去上面的caffe的github网址把caffe最新代码下载下来,也可以使用git命令获取,如下

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

caffe配置

终于到了最后的一步了,如果安装好了上面的所有的caffe的依赖,那么下面就可以对caffe进行配置和编译了。
首先拷贝生成所需要的’Makefile.config’文件,caffe安装包下面给提供了example,注意直接拷贝下面的可能会报错,因为每一行命令的后面不能有空白的字符串,有可能会导致编译不通过。

cp Makefile.config.example Makefile.config

然后对该’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   #如果没有安装cudnn的注掉# CPU-only switch (uncomment to build without GPU support).# CPU_ONLY := 1   #如果没有安装cuda,不是要GPU的设置开启  # 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   #cuda默认安装目录(其实是软链接),一般不需要修改,但如果没有安装cuda需要注掉# On Ubuntu 14.04, if cuda tools are installed via# "sudo apt-get install nvidia-cuda-toolkit"># CUDA_DIR := /usr# CUDA architecture setting: going with all of them.# For CUDA < 6.0, comment the *_50 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_50,code=compute_50# BLAS choice:# atlas for ATLAS (default)# mkl for MKL# open for OpenBlasBLAS := mkl   #这里使用的是Intel MKL,如果使用的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 := /opt/intel/mkl/include   #如果是atlas,这两个路径要注掉BLAS_LIB := /opt/intel/mkl/lib/intel64# 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/R2014a   #Matlab的路径,没安装Matlab或者不需要编译该接口可以注掉# 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库的路径,使用的是虚拟环境capy这里就是一直提到的ENV,有时候可能不起作用,还是会报错,不知道为什么,那就设置下面的export,一般就没问题了。PYTHON_INCLUDE := /home/huayong/capy/include/python2.7 \
                /home/huayong/capy/lib/python2.7/site-packages/numpy/core/include#If it doesn't work above, you can try this below.export CPLUS_INCLUDE_PATH=/home/huayong/capy/include/python2.7:/home/huayong/capy/lib/python2.7/site-packages/numpy/core/include# Anaconda Python distribution is quite popular. Include path:# Verify anaconda location, sometimes it's in root.# ANACONDA_HOME := /home/wenzheng/anaconda# PYTHON_INCLUDE := $(ANACONDA_HOME)/include \# $(ANACONDA_HOME)/include/python2.7 \# $(ANACONDA_HOME)/lib/python2.7/site-packages/numpy/core/include \# We need to be able to find libpythonX.X.so or .dylib.PYTHON_LIB := /usr/lib/x86_64-linux-gnu# PYTHON_LIB := $(ANACONDA_HOME)/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# LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/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# 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 ?= @

caffe编译

基本的编译,j代表是多线程编译,一般前两个命令不会出错,最后一个可能会出错,但有时候也不影响使用。

make all -j8
make alltest -j8
make runtest

编译matlab和python接口,这个当然不是必须的,看你自己的需求。

make pycaffe
make matcaffe

关于“Ubuntu14.04 Caffe如何安装”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI