【神经网络与深度学习】基于Windows+Caffe的Minst和CIFAR—10训练过程说明

来源:互联网 发布:算法导论第四章答案 编辑:程序博客网 时间:2024/05/16 07:56

Minst训练

我的路径:G:\Caffe\Caffe For Windows\examples\mnist 
对于新手来说,初步完成环境的配置后,一脸茫然。不知如何跑Demo,有么有!那么接下来的教程就是我们这些新手的福利了。 
第一步:如果前面的train_net.cpp编译通过了,那么这个就非常简单。Caffe训练和测试的数据都是需要leveldb格式的,niuzhiheng大牛已经给我们转好了MNIST的数据格式。如下图: 
这里写图片描述

第二步:如上图所示,文件夹下有个get_mnist_leveldb.bat ,双击就可以下载到MNIS的leveldb文件。不能翻墙的,就到:http://download.csdn.net/detail/u012878523/8140305 下载吧。下载完 解压到…\examples\mnist 文件夹下。网络参数可以再lenet_train.prototxt中修改。GPU和CPU的切换在lenet_solver.prototxt中,打开后代码如下所示:

# The training protocol buffer definitiontrain_net: "lenet_train.prototxt"# The testing protocol buffer definitiontest_net: "lenet_test.prototxt"# test_iter specifies how many forward passes the test should carry out.# In the case of MNIST, we have test batch size 100 and 100 test iterations,# covering the full 10,000 testing images.test_iter: 100# Carry out testing every 500 training iterations.test_interval: 500# The base learning rate, momentum and the weight decay of the Network.base_lr: 0.01momentum: 0.9weight_decay: 0.0005# The learning rate policylr_policy: "inv"gamma: 0.0001power: 0.75# Display every 100 iterationsdisplay: 100# The maximum number of iterationsmax_iter: 10000# snapshot intermediate resultssnapshot: 5000snapshot_prefix: "lenet"# solver mode: CPU or GPUsolver_mode: GPU    #两种方式二选一#solver_mode: CPU

第三步:双击train_lenet。bat就可以训练该网络。每500轮进行一次测试。Tset Score 0是正确率。Test Score 1是测试损失函数值。上两张训练图: 
这里写图片描述
这里写图片描述
然后准确率的变化: 
这里写图片描述
损失函数值的变化: 
这里写图片描述
每迭代100次输出一次训练比率lr和训练损失函数值loss,模型的参数存储在lenet_iter_10000.solverstate中,然后这个模型就可以应用在新数据上了。

至此,minst的训练demo到此结束。然后试试cifar-10!

CIFAR-10 训练

第一步:数据库的说明:CIFAR-10 
60000张 32X32 彩色图像 10类 
50000张训练 
10000张测试 
附上一个的deeplearning的常用数据集下载链接,赶紧默默的收藏吧: 
http://deeplearning.net/datasets/ 
这里写图片描述
这是binary格式的,所以我们要把它转换成leveldb格式。

第二步:部分直接复制别人的,因为没看到原作者是谁,所以就不上链接了。) 
/examples/cifar10文件夹中有一个 convert_cifar_data.cpp 
将他include到MainCaller.cpp中。如下: 
这里写图片描述
然后就编译,博主一次就通过了 ,在bin文件夹里出现convert_cifar_data.exe。 
接下来就可以进行格式转换。binary→leveldb。可以在bin文件夹下新建一个input文件夹。将cifar10.binary文件放在input文件夹中,这样转换时就不用写路径了。 
然后cmd进入bin文件夹,执行convert_cifar_data.exe后,在output文件夹下有cifar_train_leveldb和cifar_test_leveldb两个文件夹。里面是转化好的leveldb格式数据。

第三步:下面我们要求数据图像的均值编译../../tools/comput_image_mean.cpp 
这里写图片描述
编译成功后,没有出现comput_image_mean.exe。没关系,我们还有maincaller.exe,接下来求mean 
cmd进入bin,执行后,在bin文件夹下出现一个mean.binaryproto文件,这就是所需的均值文件。

第四步:训练cifar网络 
在…/examples/cifar10文件夹里已经有网络的配置文件,我们只需要将cifar_train_leveldb和cifar_test_leveldb两个文件夹还有mean.binaryproto文件拷到cifar0文件夹下。 
修改cifar10_quick_train.prototxt中的source: “cifar-train-leveldb” mean_file: “mean.binaryproto” 和cifar10_quick_test.prototxt中的source: “cifar-test-leveldb” mean_file: “mean.binaryproto”就可以了, 
后面再训练就类似于MNIST的训练。写一个train_quick.bat,内容如下: 
copy..//..//bin//MainCaller.exe..//..//bin//train_net.exeSETGLOG_logtostderr=1”../../bin/train_net.exe” cifar10_quick_solver.prototxt pause 
先编译一遍 train_net.cpp 
运行train_quick.bat,结果如下: 
这里写图片描述
备注: 
另外,更改cifar*solver.prototxt文件可以使用CPU训练, 
solver mode: GPU 
solver_mode: CPU 
可以看看CPU和GPU训练的差别。

0 0