Ubuntu14.04配置cuda-convnet

转载请注明:http://blog.csdn.net/stdcoutzyx/article/details/39722999

在上一个链接中,我配置了cuda,有强大的GPU,自然不能暴殄天物,让资源白白空暇着,所以配置一下卷积神经网络跑一下程序喽。至于卷积神经网络的原理,容后再写。打算先写库的使用方法,再写原理,以行动带动对理论的追求。

话不多说,步入正题。

1. 预说明

关于cuda-convnet,起源于一篇经典论文①,论文中针对ILSVRC-2010的数据进行实验,然后发布了事实上验使用的代码,链接为②。可是,事实往往跟论文是有差距的,链接②中的代码根本不能重现论文中的结果。在下不才,在使用这个链接的库非常久之后才发现的,认为非常坑,希望后来者慎之。

之所以说它坑,首先,论文中提到特性中,multi-GPUdropout就没有实现,并且也没有给出论文中8层卷积神经网络的配置文件。总之不能直接拿来用,须要自己探索。

尽管如此,但有总比没有好,毕竟这个库实现的卷积神经网络封装的非常好,论文中的大神的贡献非我等小菜所能企及的。给大神点32个赞。

本文仅仅对cuda-convnetcuda-convnet2的配置进行说明,论文中的作者还发布了其它版本号的库,尚未用到,故且按下不提。

2. Cuda-convnet配置

2.1. 源代码下载

參考链接②,先将源代码下载下来。

svn checkout http://cuda-convnet.googlecode.com/svn/trunk/ cuda-convnet-read-only

取出的版本号是562

2.2. 安装必要的库

然后,安装必须的库,我使用的是ubuntu系统。所以命令为

sudo apt-get install python-dev python-numpy python-magic python-matplotlib libatlas-base-dev

当然,还要确认你安装了cuda,我安装的是cuda6.5,在/usr/local/文件夹下,例如以下所看到的:

$ ls /usr/local
bin  cuda  cuda-6.5  etc  games  include  lib  man  sbin  share  src

2.3. 更改build.sh

进入到刚才下载的cuda-convnet-read-only文件夹,更改build.sh文件里的配置路径。例如以下所看到的:

# CUDA toolkit installation directory.
export CUDA_INSTALL_PATH=/usr/local/cuda
 
# CUDA SDK installation directory.
export CUDA_SDK_PATH=/usr/local/cuda-6.5/samples/common/inc
 
# Python include directory. This should contain the file Python.h, among others.
export PYTHON_INCLUDE_PATH=/usr/include/python2.7
 
# Numpy include directory. This should contain the file arrayobject.h, among others.
export NUMPY_INCLUDE_PATH=/usr/lib/python2.7/dist-packages/numpy/core/include/numpy
 
# ATLAS library directory. This should contain the file libcblas.so, among others.
export ATLAS_LIB_PATH=/usr/lib/atlas-base
 
make $*


依照官网的教程,配置完build.sh后就能够进行编译了。可是会错误发生,还须要改例如以下几个地方才干够。

2.4. 头文件加入

直接编译会发生找不到cutil_inline.h头文件的错误。分析原因可能是原来有这个头文件,后来这个头文件的功能被实现到其它头文件里去了。

include子目录下田间cutil_inline.h文件,并输入内容。

#include "helper_cuda.h"
#define cutilCheckMsg(a) getLastCudaError(a)
#define cutGetMaxGflopsDeviceId() gpuGetMaxGflopsDeviceId()
#define MIN(a,b) (a) < (b) ? (a) : (b)

2.5. MakeFile文件更改

MakeFile3行,原文例如以下:

INCLUDES :=  -I$(PYTHON_INCLUDE_PATH) -I$(NUMPY_INCLUDE_PATH) -I./include -I./include/common -I./include/cudaconv2 -I./include/nvmatrix

加入cuda的路径后例如以下:

INCLUDES :=  -I$(PYTHON_INCLUDE_PATH) -I$(NUMPY_INCLUDE_PATH) -I$(CUDA_SDK_PATH) -I./include -I./include/common -I./include/cudaconv2 -I./include/nvmatrix

保存之。

2.6. 最后的库链接错误

做完上述修改后,能够编译了,但到最后会发生一个库链接的错误,不用管,直接将那个库凝视掉。

common-gcc-cuda-4.0.mk文件的332行。直接用#号凝视。

# LIB += -lcutil_$(LIB_ARCH) $(LIBSUFFIX) -lshrutil_$(LIB_ARCH) $(LIBSUFFIX)

至此,就能够完毕cuda-convnet的编译了。

3. Cuda-convnet2配置

顾名思义,这是cuda-convnet2.0版本号,支持多GPU执行。

3.1. 源代码下载

git clone https://code.google.com/p/cuda-convnet2/

3.2. 必要的库

sudo apt-get install python-dev python-numpy python-scipy python-magic python-matplotlib libatlas-base-dev libjpeg-dev libopencv-dev

3.3. 配置

我仅仅能说,这个版本号的比上个版本号人性化多了,这个版本号的build.sh直接如此。

# CUDA toolkit installation directory.
export CUDA_INSTALL_PATH=/usr/local/cuda
 
# Python include directory. This should contain the file Python.h, among others.
export PYTHON_INCLUDE_PATH=/usr/include/python2.7
 
# Numpy include directory. This should contain the file arrayobject.h, among others.
export NUMPY_INCLUDE_PATH=/usr/lib/python2.7/dist-packages/numpy/core/include/numpy/
 
# ATLAS library directory. This should contain the file libcblas.so, among others.
export ATLAS_LIB_PATH=/usr/lib/atlas-base
 
# You don't have to change these:
export LD_LIBRARY_PATH=$CUDA_INSTALL_PATH/lib64:$LD_LIBRARY_PATH
export CUDA_SDK_PATH=$CUDA_INSTALL_PATH/samples
export PATH=$PATH:$CUDA_INSTALL_PATH/bin

假设是在ubuntu下的话,这样就直接已经基本把路径都配置对了。

3.4. 链接错误

可是直接编译的话还是会遇到错误,例如以下所看到的:

cd ./bin/ && g++  -O3   -DNUMPY_INTERFACE -shared -Wl,-no-undefined -o libutilpy.so src/matrix.o -L/usr/lib/atlas-base -latlas -lcblas -lpython2.7
/usr/bin/ld: cannot find -latlas
/usr/bin/ld: cannot find -lcblas
collect2: error: ld returned 1 exit status

主要是由于atlas库中没有libatlas.solibctlas.so。查看atlas的文件夹发现结构如此:

:/usr/lib/atlas-base$ ls -l
总用量 4292
drwxr-xr-x 2 root root    4096  9月 22 11:41 atlas
lrwxrwxrwx 1 root root      15  2月  4  2014 libatlas.so.3 -> libatlas.so.3.0
-rw-r--r-- 1 root root 3746968  2月  4  2014 libatlas.so.3.0
lrwxrwxrwx 1 root root      15  2月  4  2014 libcblas.so.3 -> libcblas.so.3.0
-rw-r--r-- 1 root root  135376  2月  4  2014 libcblas.so.3.0
lrwxrwxrwx 1 root root      17  2月  4  2014 libf77blas.so.3 -> libf77blas.so.3.0
-rw-r--r-- 1 root root  131000  2月  4  2014 libf77blas.so.3.0
lrwxrwxrwx 1 root root      22  2月  4  2014 liblapack_atlas.so.3 -> liblapack_atlas.so.3.0
-rw-r--r-- 1 root root  369472  2月  4  2014 liblapack_atlas.so.3.0

加入两个软链接,运行命令:

sudo ln -s libatlas.so.3.0 libatlas.so
sudo ln -s libcblas.so.3.0 libcblas.so

文件夹结构变为如此:

/usr/lib/atlas-base$ ls -l
总用量 4292
drwxr-xr-x 2 root root    4096  9月 22 11:41 atlas
lrwxrwxrwx 1 root root      15 10月  1 22:35 libatlas.so -> libatlas.so.3.0
lrwxrwxrwx 1 root root      15  2月  4  2014 libatlas.so.3 -> libatlas.so.3.0
-rw-r--r-- 1 root root 3746968  2月  4  2014 libatlas.so.3.0
lrwxrwxrwx 1 root root      15 10月  1 22:36 libcblas.so -> libcblas.so.3.0
lrwxrwxrwx 1 root root      15  2月  4  2014 libcblas.so.3 -> libcblas.so.3.0
-rw-r--r-- 1 root root  135376  2月  4  2014 libcblas.so.3.0
lrwxrwxrwx 1 root root      17  2月  4  2014 libf77blas.so.3 -> libf77blas.so.3.0
-rw-r--r-- 1 root root  131000  2月  4  2014 libf77blas.so.3.0
lrwxrwxrwx 1 root root      22  2月  4  2014 liblapack_atlas.so.3 -> liblapack_atlas.so.3.0
-rw-r--r-- 1 root root  369472  2月  4  2014 liblapack_atlas.so.3.0

然后就能够正常编译了。

參考文献

① ImageNet Classification with Deep Convolutional Neural Networks

② https://code.google.com/p/cuda-convnet

③ https://code.google.com/p/cuda-convnet2

原文地址:https://www.cnblogs.com/zfyouxi/p/4020561.html