红联Linux门户
Linux帮助

Ubuntu 14.04配置cuda-convnet

发布时间:2014-10-13 09:25:00来源:linux网站作者:stdcoutzyx

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

话不多说,步入正题。


1. 预说明

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

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

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

本文只对cuda-convnet和cuda-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文件更改

MakeFile第3行,原文如下:

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-convnet的2.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.so和libctlas.so。查看atlas的目录发现结构如此:

:/usr/lib/atlas-base$ ls -l
总用量 4292
drwxr-xr-x 2 root root4096  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 root4096  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