Just 做 IT

求知若饥 虚心若愚 안년하세요 (•‾̑⌣‾̑•)

ubuntu12.04 + caffe + cuda 6.5 + opencv 2.3.1 + Intel MKL

2 years ago 0

因为没有N卡,所以没有省略安装n卡驱动步骤.

1:确保系统干净

2:软件下载地址
cuda6.5:http://pan.baidu.com/s/1kTkjfYR

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

opencv 2.3.1

Intel MKL http://pan.baidu.com/s/1gd3Ue1H

3:先安装一些基本的库


sudo apt-get install build-essential
sudo apt-get install python-dev
sudo apt-get install python-numpy
sudo apt-get install python-pip

On Ubuntu, most of the dependencies can be installed with

sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev

安装一些ubuntu12.04 apt-get 不能安装的库

# glog
wget https://google-glog.googlecode.com/files/glog-0.3.3.tar.gz
tar zxvf glog-0.3.3.tar.gz
cd glog-0.3.3
./configure
make && make install
# gflags
wget https://github.com/schuhschuh/gflags/archive/master.zip
unzip master.zip
cd gflags-master
mkdir build && cd build
export CXXFLAGS="-fPIC" && cmake .. && make VERBOSE=1
make && make install
# lmdb
git clone git://gitorious.org/mdb/mdb.git
cd mdb/libraries/liblmdb
make && make install

4: 安装cuda6.5
创建一个临时解压的文件夹


mkdir cuda6.5

./cuda_6.5.14_linux_64.run --extract=/home/long/Download/cuda6.5 注:(刚刚创建文件的绝对路径)

cd cuda6.5
chmod +x *.run
这儿有三个文件 例子(没安装)、安装包、N卡驱动(没安装)
sudo ./cuda-linux64-rel-6.5.14-18749181.run

添加环境变量

安装完成后需要在/etc/profile中添加环境变量, 在文件最后添加:

PATH=/usr/local/cuda-6.5/bin:$PATH
export PATH
保存后, 执行下列命令, 使环境变量立即生效

source /etc/profile
3.1.2 添加lib库路径

在 /etc/ld.so.conf.d/加入文件 cuda.conf, 内容如下

/usr/local/cuda-6.5/lib64
执行下列命令使之立刻生效

sudo ldconfig

5:安装Intel_MKL
(如果没有可以安装OpenBLAS代替)解压安装包,下面有一个install_GUI.sh文件, 执行该文件,会出现图形安装界面,根据说明一步一步执行即可。
$注意: 安装完成后需要添加library路径


sudo gedit /etc/ld.so.conf.d/intel_mkl.conf
在文件中添加内容
/opt/intel/lib
/opt/intel/mkl/lib/intel64
注意把路径替换成自己的安装路径。 编辑完后执行
sudo ldconfig

6. 安装OpenCV2.3.1
这个地方遇到一个很奇怪的问题,找不到cv.h文件,当然还有其他的
最后找到一个解决方案:
sudo apt-get install libopencv-dev
这个命令上面已经执行了,但是缺少头文件,也就是在/usr/include/下面没有opencv文件夹。
解决方法:下载opencv2.3.1 的源码,解压后,把include下的opencv文件夹直接复制到 系统/usr/include/下。 在编译以后使用到opencv的地方就可以通过了


7. 安装caffe

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


终于完成了所有环境的配置,可以愉快的编译Caffe了! 进入caffe根目录, 首先复制一份Makefile.config

cp Makefile.config.example Makefile.config
然后修改里面的内容,主要需要修改的参数包括

CPU_ONLY 是否只使用CPU模式,没有GPU没安装CUDA的同学可以打开这个选项

BLAS (使用intel mkl还是OpenBLAS)

MATLAB_DIR 如果需要使用MATLAB wrapper的同学需要指定matlab的安装路径, 如我的路径为 /usr/local/MATLAB/R2013b (注意该目录下需要包含bin文件夹,bin文件夹里应该包含mex二进制程序)

DEBUG 是否使用debug模式,打开此选项则可以在eclipse或者NSight中debug程序

例如:


## 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

# 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 (up to CUDA 5.5 compatible).
# For the latest architecture, you need to install CUDA >= 6.0 and uncomment
# the *_50 lines below.
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 OpenBlas
#BLAS := atlas
BLAS := mkl
BLAS_INCLUDE := /opt/intel/mkl/include
BLAS_LIB := /opt/intel/mkl/lib/intel64

# 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

# 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:
# PYTHON_INCLUDE := $(HOME)/anaconda/include \
		# $(HOME)/anaconda/include/python2.7 \
		# $(HOME)/anaconda/lib/python2.7/site-packages/numpy/core/include

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

# Whatever else you find you need goes here.
INCLUDE_DIRS := $(PYTHON_INCLUDE) /usr/local/include
LIBRARY_DIRS := $(PYTHON_LIB) /usr/local/lib /usr/lib

BUILD_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

完成设置后, 开始编译,caffe 不需要安装 放到哪库就生成在什么地方。直接引用就行


make all -j4 # -j4 是指使用几个线程来同时编译, 可以加快速度, j后面的数字可以根据CPU core的个数来决定
make test
make runtest

OVER! @#@
8: 顺便记下我编译 使用使用 caffe和opencv的 程序,编译时所编写的脚本


g++  SECTION.cpp SECTION.h Run.cpp Run.h Form.cpp Form.h main.cpp -o main \
    /home/long/Downloads/caffe/caffe/.build_release/lib/libcaffe.a  \
    -fPIC -DNDEBUG -O2 -DCPU_ONLY -DUSE_MKL \
    `pkg-config --cflags opencv` -I/usr/include/python2.7 \
    -I/usr/lib/python2.7/dist-packages/numpy/core/include \
    -I/usr/local/include \
    -I/home/long/Downloads/caffe/caffe/.build_release/src \
    -I/home/long/Downloads/caffe/caffe/src \
    -I/home/long/Downloads/caffe/caffe/include \
    -I/opt/intel/mkl/include \
    -Wall -Wno-sign-compare \
    -L/usr/lib \
    -L/usr/local/lib \
    -L/usr/lib \
    -L/opt/intel/mkl/lib/intel64 `pkg-config --libs opencv` \
    -lglog -lgflags -lprotobuf -lleveldb -lsnappy -llmdb -lboost_system -lhdf5_hl -lhdf5 -lopencv_core -lopencv_highgui -lopencv_imgproc -lpthread -lboost_thread -lmkl_rt

Write a Comment