Linux14.04+cuda7.5+anaconda2+opencv3.0+caffe

来源:互联网 发布:8051单片机引脚映射 编辑:程序博客网 时间:2024/04/29 21:12

现在安装caffe的博客着实很多,但是很多都是复制转载别人的,根本不管对错,有些没写清楚自己的环境,博主自己通过安装,一步一步记下自己的步骤,写下自己踩过的坑,希望大家借鉴,也是给自己做个笔记。

首先介绍博主电脑硬件和所安装的软件版本,gpu是GTX980Ti,Linux版本是14.04(LTS)64位,python环境用的是anaconda2,cuda版本是7.5,OpenCV用的是3.0。

博主在安装之前的机器是纯干净的,啥玩意儿都没有,在安装环境之前,我把硬盘格式化了,所以是特别干净的盘,电脑只有bios,这是我安装的一个前提吧。

一、安装ubuntu

下载ubuntu

这里写图片描述

制作ubuntu的系统u盘(博主是在win7的环境中制作的系统盘),博主用的是UltraISO(这个好早就下了,所以忘了是哪下的了,网上有很多的,自己百度谷歌雅虎一下吧)

这里写图片描述

1)博主这个包,解压出来是个exe,可以直接运行,运行界面如下,博主很穷所以选择试用
这里写图片描述
2)进入如下界面,选择 文件-打开
这里写图片描述
3)然后找到你刚刚下载的ubuntu的iso文件(ubuntu-14.04-desktop-amd64.iso),为什么要打码,嘿嘿,你猜
这里写图片描述
4)可以看见软件打开了iso镜像文件
这里写图片描述
5)选择 启动-写入硬盘映像
这里写图片描述
6)插入你的u盘,然后更改如下参数(选择USB-HDD,选择便捷启动中的syslinux,最后点击是)操作如下
这里写图片描述
这里写图片描述
这里写图片描述
7)最后选择写入,等待u盘做好
这里写图片描述

将制作好的系统盘插入即将安装系统的电脑,开机,一直按f2(装系统的电脑也就是要安装ubuntu的电脑主板是华硕的),进入bios后,选择u盘启动,然后可以进入u盘系统盘,选择install ubuntu,之后按照提示安装即可。

安装好后拔出u盘,重启电脑

二、安装cuda

简单来说,按照官方文档安装即可,请认真阅读,不要侥幸的跳过任何一个步骤任何一个错误,我开始用的是.run文件装的,不是很顺手,不方便,还要另装显卡驱动,容易出错,后来同学之前踩过这个坑,建议用deb安装,很方便,换成.deb方式安装的,三条语句搞定。
官方安装文档网址

[下载cuda网址](https://developer.nvidia.com/cuda-toolkit-archive)

这里写图片描述
这里写图片描述

下载完后运行md5sum和下面网页中的checksums对比看看下载是否正确完成

这里写图片描述

接下来三条语句搞定,不过建议还是认真看看文档,就当学习一下英文了

这里写图片描述

在设置环境变量的时候要注意,官方文件写在终端输入

export PATH=/usr/local/cuda-7.5/bin:$PATHexport LD_LIBRARY_PATH=/usr/local/cuda-7.5/lib64:$LD_LIBRARY_PATH

这样做,当你关闭终端的时候变量就没了。
本人在/etc/profile里末尾添加上面两句,添加完了回到终端source /etc/profile,这样就不用重启就能改变变量配置了。

接下来是检查是否安装cuda成功,cuda-install-samples-7.5.sh ./ ,表示在当前文件夹下安装samples,然后进入NVIDIA_CUDA-7.5_Samples下make

make成功后cd到NVIDIA_CUDA-7.5_Samples/bin/x86_64/linux/release下,运行./deviceQuery看是否安装成功

运行./bandwidthTest看显卡和cuda有没有连接成功

三、安装anaconda2

安装anaconda,从官网下载anaconda2,我下的是Anaconda2-4.2.0-Linux-x86_64.sh,然后cd到包含该文件的路径下,运行bash Anaconda2-4.2.0-Linux-x86_64.sh,一路enter,第一个问题Do you approve the license terms?选yes,然后问你安装路径,我和[4]一样安装在/usr/local/anaconda2下,之后问你是否添加anaconda路径,选yes,,它自动将anaconda2的bin路径添加到~/.bashrc,然后就安装好了。

在终端输入python命令,如果没报错,并且显示出如下界面,说明anaconda2安装完毕。
这里写图片描述

编译caffe时会提示找不到hdf5相关的动态库

默认在usr/lib/x86_64-linux-gnu下找,可以创建anaconda2中的库的软连接,在终端运行如下命令:

sudo ln -s /usr/local/anaconda2/lib/libhdf5_hl.so.10  /usr/lib/x86_64-linux-gnu/libhdf5_hl.so.10sudo ln -s /usr/local/anaconda2/lib/libhdf5.so.10  /usr/lib/x86_64-linux-gnu/libhdf5.so.10

四、安装OpenCV3

OpenCV3,从官网下载opencv3网址,方法看opencv3的installation in linux,在downloads界面的右上角

这里写图片描述
这里写图片描述

博主最近发现一个很严重的问题,如果python用的是anaconda的话,按照上述操作编译会有问题,会有没有cv2.so库的情况出现,所以cmake的命令要有所改变,我的命令如下:

cmake -D CMAKE_BUILD_TYPE=RELEASE \      -D INSTALL_C_EXAMPLES=OFF \      -D INSTALL_PYTHON_EXAMPLES=ON \      -D CMAKE_INSTALL_PREFIX=/usr/local \      -D PYTHON_INCLUDE_DIR=/usr/local/anaconda2/include/python2.7/ \      -D PYTHON_INCLUDE_DIR2=/usr/local/anaconda2/include/python2.7/ \      -D PYTHON_LIBRARY=/usr/local/anaconda2/lib/libpython2.7.so \      -D PYTHON_LIBRARY2=/usr/local/anaconda2/lib/libpython2.7.so \      -D PYTHON_PACKAGES_PATH=/usr/local/anaconda2/lib/python2.7/site-packages/ \      -D BUILD_EXAMPLES=ON \      -D BUILD_NEW_PYTHON_SUPPORT=ON \      -D BUILD_opencv_python3=OFF \      -D BUILD_opencv_python2=ON ..

如果你不知道你anaconda装的路径,可以在python环境中执行:

import syssys.prefix

编译出来的cv2.so在文件夹/usr/local/lib/python2.7/site-packages下面,通过使用命令:

sudo ln -s /usr/local/lib/python2.7/site-packages/cv2.so  /usr/local/anaconda2/lib/python2.7/site-packages/

在anaconda安装文件夹下建立cv2.so的软连接,

然后在python下执行import cv2,就不会报ImportError: No module named cv2这样的错误了。

五、接下来是编译caffe的过程

官网安装方法,因为官网不是用的anaconda2和opencv3,所以安装方法有点不一样。

下载其他依赖包

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

下载caffe

github中下载caffe的网址

官网编译方法如下

编译caffe官网网址,下面是根据自己的环境更改caffe文件夹下的makefile.config文件。

根据我现有的环境(linux14.04 + anaconda2 + cuda7.5 + opencv3.0),我的Makefile如下:

## 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# CPU-only switch (uncomment to build without GPU support).# CPU_ONLY := 1# uncomment to disable IO dependencies and corresponding data layers# USE_OPENCV := 0# USE_LEVELDB := 0# USE_LMDB := 0# uncomment to allow MDB_NOLOCK when reading LMDB files (only if necessary)#       You should not set this flag if you will be reading LMDBs with any#       possibility of simultaneous read and write# ALLOW_LMDB_NOLOCK := 1# Uncomment if you're using OpenCV 3OPENCV_VERSION := 3# 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# 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.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# 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.# 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 := /usr/local/anaconda2PYTHON_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/libPYTHON_LIB := $(ANACONDA_HOME)/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/includeLIBRARY_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 := 1# N.B. both build and distribute dirs are cleared on `make clean`BUILD_DIR := buildDISTRIBUTE_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是否正确编译:

make all  #make -j8可以加快编译速度make testmake runtest

出现如下错误,可能是caffe makefile找不到python路径,该配置文件中的anaconda路径不对,检查makefile.config文件中的ANACONDA_HOME := /usr/local/anaconda2语句
这里写图片描述

如果没有报错,caffe就安装好了,但是此时python还不能import caffe,要使用caffe的python的接口,还需要执行下面语句:

make pycaffe

在终端输入python进入python运行环境,输入import caffe,会出现下列错误:
这里写图片描述

在python环境中,输入下列命令:

import syssys.path.append('/home/hpc/caffe/python')    #添加caffe根目录下的python路径,按照自己的路径进行更改import caffe

出现下面界面,说明能成功使用caffe的python接口了。
这里写图片描述

但是在你添加完caffe的python路径后,导入caffe还是会报错,错误是ImportError: No module named google.protobuf.internal,说明没有按照protobuf,但是之前按照caffe之前是安装过的,caffe也编译完了,还有一个原因就是机器里有两个python环境,一个是系统自带的,另一个就是你anaconda的环境:

pip install protobuf/home/username/anaconda2/bin/pip install protobuf    #这是我的anaconda安装路径,路径改成你自己的

最后在python环境中,输入下列命令:

import syssys.path.append('/home/hpc/caffe/python')    #添加caffe根目录下的python路径,按照自己的路径进行更改import caffe

应该没有问题了,界面如下
这里写图片描述

如果是写.py文件,可以在.py文件中添加下列语句:

CAFFE_ROOT = '/home/hpc/caffe'sys.path.append(CAFFE_ROOT+'/python')  # Add caffe python path so we can import it

一些指令:

pkg-config --modversion opencv    #查看opencv的安装版本protoc --version    #查看protobuf安装版本
1 0
原创粉丝点击