Ubuntu14.04+caffe+cuda 环境搭建以及MNIST数据集的训练与测试
一、ubuntu14.04的安装:
ubuntu的安装是一件十分简单的事情,这里给出一个参考教程:
二、cuda的安装:
1、首先下载nvidia cuda的仓库安装包(我的是ubuntu 14.04 64位,所以下载的是ubuntu14.04的安装包,如果你是32位的可以参看具体的地址,具体的地址是https://developer.nvidia.com/cuda-downloads)
wget http://developer.download.nvidia.com/compute/cuda/repos/ubuntu1404/x86_64/cuda-repo-ubuntu1404_6.5-14_amd64.deb
下载完成之后可以使用如下命令安装它,注意文件名修改为cuda-repo-ubuntu1404_6.5-14_amd64.deb:
sudo dpkg -i cuda-repo-__.deb
2、安装好仓库之后,就可以更新你的本地仓库。
sudo apt-get update
最后开始安装cuda以及显卡驱动(安装cuda的同时就会把显卡驱动也全部安装好,这个真的很方便。但是下载的时间有点长。)
sudo apt-get install cuda
3、安装完之后你需要设置环境变量:
$ export PATH=/usr/local/cuda-6.5/bin:$PATH$ export LD_LIBRARY_PATH=/usr/local/cuda-6.5/lib64:$LD_LIBRARY_PATH
4、设置完毕之后,你还可以选择是否安装cuda附带的示例代码(
$ cuda-install-samples-6.5.sh
cd ~/NVIDIA_CUDA-6.5_Samples
然后进入bin目录,并运行devicequery
cd ~/NVIDIA_CUDA-6.5_Samples/bin./ deviceQuery
如果出现下列显卡信息, 则驱动及显卡安装成功:
./deviceQuery Starting...CUDA Device Query (Runtime API) version (CUDARTstaticlinking)
Detected1CUDA Capable device(s)
Device0: "GeForce GTX 670"CUDA Driver Version/ Runtime Version 6.5 / 6.5CUDA Capability Major/Minor version number: 3.0Total amount ofglobal memory: 4095 MBytes (4294246400bytes)
(7) Multiprocessors, (192) CUDA Cores/MP: 1344CUDA Cores
GPU Clock rate: 1098 MHz (1.10GHz)
Memory Clock rate: 3105Mhz
Memory Bus Width: 256-bit
L2 Cache Size: 524288bytes
Maximum Texture Dimension Size (x,y,z) 1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)
Maximum Layered 1D Texture Size, (num) layers 1D=(16384), 2048layers
Maximum Layered 2D Texture Size, (num) layers 2D=(16384, 16384), 2048layers
Total amount ofconstant memory: 65536bytes
Total amount of shared memory per block: 49152bytes
Totalnumber of registers available per block: 65536Warp size: 32Maximumnumber of threads per multiprocessor: 2048Maximumnumber of threads per block: 1024
Max dimension size of a thread block (x,y,z): (1024, 1024, 64)Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535)
Maximum memory pitch: 2147483647bytes
Texture alignment: 512bytes
Concurrentcopy and kernel execution: Yes with 1 copyengine(s)
Runtime limit on kernels:Yes
Integrated GPU sharing Host Memory:No
Support host page-locked memory mapping:Yes
Alignment requirementfor Surfaces:Yes
Device has ECC support:Disabled
Device supports Unified Addressing (UVA):Yes
Device PCI Bus ID/ PCI location ID: 1 / 0Compute Mode:
< Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 6.5, CUDA Runtime Version = 6.5, NumDevs = 1, Device0 = GeForce GTX 670Result= PASS
具体的安装过程可以参考英文。
三、安装OpenCV:
这个尽量不要手动安装, Github上有人已经写好了完整的安装脚本:https://github.com/jayrambhia/Install-OpenCV下载该脚本,进入Install-OpenCV-master/Ubuntu/2.4 目录, 给所有shell脚本加上可执行权限
chmod +x *.sh
然后安装最新版本 (当前为2.4.9)
sudo ./opencv2_4_9.sh
脚本会自动安装依赖项,下载安装包,编译并安装OpenCV。整个过程大概半小时左右。
四、安装其他依赖项
sudo apt-get install build-essential libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libboost-all-dev libhdf5-serial-dev libgflags-dev libgoogle-glog-dev liblmdb-dev protobuf-compiler
五、下载caffe,关于MKL:
1、下载caffe
下载后直接解压解压在你的工程路径内,无需安装。
2、关于MKL
MKL是intel的收费数学计算库,获取是一件十分蛋疼的事情,如果你有时间,自己百度教程安装.MKL不是必须的,OpenBLAS和atlas都可以替代之,并且这两个库的安装和使用十分简单。自己百度教程。
安装ATLAS
sudo apt-get install libatlas-base-dev
六、安装caffe所需的python相关包:
首先安装pip和python-dev (系统默认有python环境的, 不过我们需要的使python-dev)
sudo apt-get install python-dev python-pip
然后cd到caffe所在路径执行如下命令安装编译caffe python wrapper 所需要的额外包
for req in $(cat requirements.txt); do sudo pip install $req; done
七、编译caffe:
进入caffe根目录, 首先复制一份Makefile.config
cp Makefile.config.example Makefile.config
然后修改里面的内容,主要需要修改的参数包括
CPU_ONLY 是否只使用CPU模式,没有GPU没安装CUDA的同学可以打开这个选项BLAS (使用intel mkl还是OpenBLAS)DEBUG 是否使用debug模式,打开此选项则可以在eclipse或者NSight中debug程序
完成设置后, 开始编译
make all -j4
make test
make runtest
-j4注意 -j4 是指使用几个线程来同时编译, 可以加快速度, j后面的数字可以根据CPU core的个数来决定。
编译pycaffe:
make pycaffe
然后基本就全部安装完拉.
八、MNIST数据集训练以及测试:
1、训练:
在Caffe安装目录之下,首先获得MNIST数据集:
cd data/mnist
sh get_mnist.sh
生成mnist-train-lmdb/ 和 mnist-test-lmdb/,把数据转化成lmdb格式(注意这里要从caffe根目录运行,不然会报错,不懂自己读一读creat_mnist.sh):
shexamples/mnist/create_mnist.sh
训练网络(同上,从caffe根目录运行):
sh examples/mnist/train_lenet.sh
下图(盗图):
2、测试:
当所有数据都训练好之后,接下来就是如何将模型应用到实际数据了:
./build/tools/caffe.bin test -model=examples/mnist/lenet_train_test.prototxt -weights=examples/mnist/lenet_iter_10000.caffemodel -gpu=0
如果没有GPU则使用
./build/tools/caffe.bin test -model=examples/mnist/lenet_train_test.prototxt -weights=examples/mnist/lenet_iter_10000.caffemodel
test:表示对训练好的模型进行Testing,而不是training。其他参数包括train, time, device_query。-model=XXX:指定模型prototxt文件,这是一个文本文件,详细描述了网络结构和数据集信息。结果如下: