深度学习之caffe入门——cifar10例程

来源:互联网 发布:知即墨下载 编辑:程序博客网 时间:2024/06/05 01:52

2017年3月11日23:55
cifar10是caffe自带的第二个比较有趣的例程。
首先贴出cifar官网,http://www.cs.toronto.edu/~kriz/cifar.html

简单地说,Cifar-10是由Hinton的两个大弟子Alex Krizhevsky和Ilya Sutskever收集的一个用于普通物体识别的数据集。

CIFAR-10(DataSet)这个数据集总共包含:60000张图片

1—图片尺寸:32pixel*32pixel

2—图片深度:三通道RGB的彩色图片

2—这60000张图片共分为10类,具体的分类如下图所示:

CIFAR-10数据集
60000张图片里面有:
1–50000张训练样本

2–10000张测试样本(验证Set)

3—记住:

1—CIFAR-10:是一个[普通物体]识别的数据集

2—因此,这个数据集和网络模型的最大特点就是:可以很容易的将[物体识别]迁移到其他普通的物体

3—而且可以将10分类问题扩展至100类物体的分类,甚至1000类和更多类的物体分类。

4—比如cifar-100,这个在之后会写到吧。

caffe的源文件夹内给出了很多readme文件,这是学习caffe框架最直接的、第一手的资料。相关的疑问比如怎样得到cifar数据集,怎样训练样本,输出日志都表示什么意思,readme file讲的很清楚。下面贴出readme文件的翻译,以便以后查阅。


title: CIFAR-10 tutorial
标题:CIFAR-10 教程
category: example
description: Train and test Caffe on CIFAR-10 data.
include_in_docs: true
priority:5


Alex’s CIFAR-10 tutorial, Caffe style

Alex Krizhevsky’s cuda-convnet details the model definitions, parameters, and training procedure(步骤、过程) for good performance on CIFAR-10. This example reproduces(重现) his results in Caffe.

/××××××××××××××××××××××××××××××××××
Alex Krizhevsky这位前辈用CUDA+卷积神经网络实现了对cifar-10数据集的识别,caffe所包含的这个example是对Alex的模型的一个重现。
××××××××××××××××××××××××××××××××××/

We will assume that you have Caffe successfully compiled. If not, please refer to the Installation page. In this tutorial, we will assume that your caffe installation is located at CAFFE_ROOT.

/××××××××××××××××××××××××××××××××××
我们假定你已经将caffe成功编译了,如果没有,请参见caffe安装文档。同时在这篇教程,我们假定你的caffe在如下路径中。
××××××××××××××××××××××××××××××××××/

We thank @chyojn for the pull request that defined the model schemas(模块) and solver configurations(配置).

This example is a work-in-progress(在制品). It would be nice to further explain details of the network and training choices and benchmark the full training.

Prepare the Dataset

获取数据集

You will first need to download and convert the data format from the CIFAR-10 website. To do this, simply run the following commands:

/××××××××××××××××××××××××××××××××××
首先你需要下载并转换数据的格式。数据可以在官网上下载,也可以在终端输入如下命令。
第一个命令是下载数据,第二个是生成caffe可以利用的数据格式。
××××××××××××××××××××××××××××××××××/

cd $CAFFE_ROOT./data/cifar10/get_cifar10.sh(下载很慢)./examples/cifar10/create_cifar10.sh

If it complains that wget or gunzip are not installed, you need to install them respectively.

/××××××××××××××××××××××××××××××××××
这个过程可能会报错,如果你的电脑没有安装wget和gunzip。
××××××××××××××××××××××××××××××××××/

After running the script there should be the dataset, ./cifar10-leveldb, and the data set image mean ./mean.binaryproto.

/××××××××××××××××××××××××××××××××××
运行完这两个脚本,会产生两个与数据集相关的文件。
××××××××××××××××××××××××××××××××××/

The Model

The CIFAR-10 model is a CNN that composes(组成) layers of convolution, pooling, rectified linear unit (ReLU) nonlinearities, and local contrast normalization with a linear classifier on top of it all. We have defined the model in the CAFFE_ROOT/examples/cifar10 directory’s cifar10_quick_train_test.prototxt.

/××××××××××××××××××××××××××××××××××
CIFAR-10是一个由卷积层、池化层、ReLU层等构成的卷积神经网络(CNN),顶层还有一个线性分类器,我们在cifar10_quick_train_test.ptrototxt 这个文件中定义了这个网络的参数。
××××××××××××××××××××××××××××××××××/

Training and Testing the “Quick” Model

训练与测试——quick model

Training the model is simple after you have written the network definition protobuf and solver protobuf files (refer to MNIST Tutorial). Simply run train_quick.sh, or the following command directly:

/××××××××××××××××××××××××××××××××××
快速训练只需要执行 train_quick.sh脚本就可以了。详细了解还可以参看mnist的readme手册
××××××××××××××××××××××××××××××××××/

cd $CAFFE_ROOT./examples/cifar10/train_quick.sh

train_quick.sh is a simple script, so have a look inside. The main tool for training is caffe with the train action, and the solver protobuf text file as its argument.

When you run the code, you will see a lot of messages flying by like this:

I0317 21:52:48.945710 2008298256 net.cpp:74] Creating Layer conv1I0317 21:52:48.945716 2008298256 net.cpp:84] conv1 <- dataI0317 21:52:48.945725 2008298256 net.cpp:110] conv1 -> conv1I0317 21:52:49.298691 2008298256 net.cpp:125] Top shape: 100 32 32 32 (3276800)I0317 21:52:49.298719 2008298256 net.cpp:151] conv1 needs backward computation.

These messages tell you the details about each layer, its connections and its output shape, which may be helpful in debugging. After the initialization, the training will start:

/××××××××××××××××××××××××××××××××××
这些信息讲的是每一层网络的细节,连接,输出形态。初始化之后,正式的学习将会开始。
××××××××××××××××××××××××××××××××××/

I0317 21:52:49.309370 2008298256 net.cpp:166] Network initialization done.I0317 21:52:49.309376 2008298256 net.cpp:167] Memory required for Data 23790808I0317 21:52:49.309422 2008298256 solver.cpp:36] Solver scaffolding done.I0317 21:52:49.309447 2008298256 solver.cpp:47] Solving CIFAR10_quick_train

Based on the solver setting, we will print the training loss function every 100 iterations, and test the network every 500 iterations. You will see messages like this:

/××××××××××××××××××××××××××××××××××
每一百次迭代,打印损失函数,每五百次迭代测试网络的预测精度,具体格式如下:
××××××××××××××××××××××××××××××××××/

I0317 21:53:12.179772 2008298256 solver.cpp:208] Iteration 100, lr = 0.001I0317 21:53:12.185698 2008298256 solver.cpp:65] Iteration 100, loss = 1.73643...I0317 21:54:41.150030 2008298256 solver.cpp:87] Iteration 500, Testing netI0317 21:54:47.129461 2008298256 solver.cpp:114] Test score #0: 0.5504I0317 21:54:47.129500 2008298256 solver.cpp:114] Test score #1: 1.27805

For each training iteration, lr is the learning rate of that iteration, and loss is the training function. For the output of the testing phase, score 0 is the accuracy, and score 1 is the testing loss function.

/××××××××××××××××××××××××××××××××××
每次迭代,lr表示的是学习率,loss表示损失函数。在测试阶段,accuracy是预测精度,testing loss function是测试集的损失函数。
××××××××××××××××××××××××××××××××××/

And after making yourself a cup of coffee, you are done!

/××××××××××××××××××××××××××××××××××
如果你选择用CPU训练这个网络,将会变得非常地耗时间。train_full完整训练花了我十几个小时的时间,train_quick倒是还可以接受。
××××××××××××××××××××××××××××××××××/

I0317 22:12:19.666914 2008298256 solver.cpp:87] Iteration 5000, Testing netI0317 22:12:25.580330 2008298256 solver.cpp:114] Test score #0: 0.7533I0317 22:12:25.580379 2008298256 solver.cpp:114] Test score #1: 0.739837I0317 22:12:25.587262 2008298256 solver.cpp:130] Snapshotting to cifar10_quick_iter_5000I0317 22:12:25.590215 2008298256 solver.cpp:137] Snapshotting solver state to cifar10_quick_iter_5000.solverstateI0317 22:12:25.592813 2008298256 solver.cpp:81] Optimization Done.

Our model achieved ~75% test accuracy. The model parameters are stored in binary protobuf format in

/××××××××××××××××××××××××××××××××××
模型学出来的参数放在一个二进制格式的文件里,如下:
××××××××××××××××××××××××××××××××××/

cifar10_quick_iter_5000

which is ready-to-deploy in CPU or GPU mode! Refer to the CAFFE_ROOT/examples/cifar10/cifar10_quick.prototxt for the deployment model definition that can be called on new data.

Why train on a GPU?为什么要用GPU来训练?

CIFAR-10, while still small, has enough data to make GPU training attractive.
/××××××××××××××××××××××××××××××××××
cifar数据集尽管很小,但也足够让你的显卡忙一阵子了。
最后讲讲怎么切换CPU和GPU的工作模式。
其实很简单。先找到 /home/caffe/examples/cifar10/的路径。
在这个文件夹下,你应该能找到这样五个文件:
cifar10_quick_solver.prototxt
cifar10_quick_solver_lr1.prototxt
cifar10_full_solver.prototxt
cifar10_full_solver_lr1.prototxt
cifar10_full_solver_lr2.prototxt
如果你仅仅想要运行一下quick train,那只需要将前两个文件的最后一行改成CPU就可以了。如果想要full_train ,需要改动后三个文件的最后一行。如下图所示:
××××××××××××××××××××××××××××××××××/
这五个prototxt文件都大同小异,只需要关注最后一行就可以了,当然你也可以尝试更改上面的网络参数,看看是否有新的发现!

To compare CPU vs. GPU training speed, simply change one line in all the cifar*solver.prototxt:

# solver mode: CPU or GPUsolver_mode: CPU

and you will be using CPU for training.

最后顺便说一句,卷积神经网络学习出来的权重信息都存放在h5文件中如下图所示,上面选中的16个文件都是train_full训练出来的结果,下面四个是train_quick 训练的结果。full的效果要好用一些,准确率82%左右,quick的准确率只有约75%.

最后要感谢

http://blog.csdn.net/maweifei/article/details/52981425这位前辈。这篇博客参考了他的一些内容

0 0
原创粉丝点击