- The script `plotcurve.py` requires the python module of `matplotlib`, so if it fails, maybe you need to install `matplotlib`.
After training finishes, the training and testing error curve will be saved to `plot.png` using `plotcurve.py` script. An example of the plot is shown below:
After training finishes, the training and testing error curves will be saved to `plot.png` using `plotcurve.py` script. An example of the plot is shown below:
<center>![Training and testing curves.](./plot.png)</center>
[ImageNet](http://www.image-net.org/) is a popular dataset for generic object classification. This tutorial provided convolutional neural network(CNN) models for ImageNet.
[ImageNet](http://www.image-net.org/) is a popular dataset for generic object classification. This tutorial provides convolutional neural network(CNN) models for ImageNet.
## ResNet Introduction
...
...
@@ -48,11 +48,11 @@ We present three ResNet models, which are converted from the models provided by
## ResNet Model
See ```demo/model_zoo/resnet/resnet.py```. This confgiure contains network of 50, 101 and 152 layers. You can specify layer number by adding argument like this ```--config_args=layer_num=50``` in command line arguments.
See ```demo/model_zoo/resnet/resnet.py```. This config contains network of 50, 101 and 152 layers. You can specify layer number by adding argument like ```--config_args=layer_num=50``` in command line arguments.
### Network Visualization
You can get a diagram of ResNet network by running the following command. The script generates dot file and then converts dot file to PNG file, which uses installed draw_dot tool in our server. If you can not access the server, just install graphviz to convert dot file.
You can get a diagram of ResNet network by running the following commands. The script generates dot file and then converts dot file to PNG file, which uses installed draw_dot tool in our server. If you can not access the server, just install graphviz to convert dot file.
```
cd demo/model_zoo/resnet
...
...
@@ -190,8 +190,7 @@ Second, specify layers to extract features in `Outputs()` of `resnet.py`. For ex
<spanid="image-classification-tutorial"></span><h1>Image Classification Tutorial<aclass="headerlink"href="#image-classification-tutorial"title="Permalink to this headline">¶</a></h1>
<p>This tutorial will guide you through training a convolutional neural network to classify objects using the CIFAR-10 image classification dataset.
As shown in the following figure, the convolutional neural network can recognize the main object in images, and output the classification result.</p>
<p>We have prepared a script to download and process CIFAR-10 dataset. The script will download CIFAR-10 dataset from the official dataset.
...
...
@@ -112,7 +113,7 @@ sh download_cifar.sh
<p>It has two directories:<codeclass="docutils literal"><spanclass="pre">train</span></code> and <codeclass="docutils literal"><spanclass="pre">test</span></code>. These two directories contain training data and testing data of CIFAR-10, respectively. Each of these two folders contains 10 sub-folders, ranging from <codeclass="docutils literal"><spanclass="pre">airplane</span></code> to <codeclass="docutils literal"><spanclass="pre">truck</span></code>. Each sub-folder contains images with the corresponding label. After the images are organized into this structure, we are ready to train an image classification model.</p>
</div>
<divclass="section"id="preprocess">
<spanid="preprocess"></span><h1>Preprocess<aclass="headerlink"href="#preprocess"title="Permalink to this headline">¶</a></h1>
<spanid="preprocess"></span><h2>Preprocess<aclass="headerlink"href="#preprocess"title="Permalink to this headline">¶</a></h2>
<p>After the data has been downloaded, it needs to be pre-processed into the Paddle format. We can run the following command for preprocessing.</p>
<spanid="model-training"></span><h1>Model Training<aclass="headerlink"href="#model-training"title="Permalink to this headline">¶</a></h1>
<spanid="model-training"></span><h2>Model Training<aclass="headerlink"href="#model-training"title="Permalink to this headline">¶</a></h2>
<p>We need to create a model config file before training the model. An example of the config file (vgg_16_cifar.py) is listed below. <strong>Note</strong>, it is slightly different from the <codeclass="docutils literal"><spanclass="pre">vgg_16_cifar.py</span></code> which also applies to the prediction.</p>
<li><codeclass="docutils literal"><spanclass="pre">./demo/image_classification/vgg_16_cifar.py</span></code> is the network and data configuration file. The meaning of the other flags can be found in the documentation of the command line flags.</li>
<li>The script <codeclass="docutils literal"><spanclass="pre">plotcurve.py</span></code> requires the python module of <codeclass="docutils literal"><spanclass="pre">matplotlib</span></code>, so if it fails, maybe you need to install <codeclass="docutils literal"><spanclass="pre">matplotlib</span></code>.</li>
</ul>
<p>After training finishes, the training and testing error curve will be saved to <codeclass="docutils literal"><spanclass="pre">plot.png</span></code> using <codeclass="docutils literal"><spanclass="pre">plotcurve.py</span></code> script. An example of the plot is shown below:</p>
<p>After training finishes, the training and testing error curves will be saved to <codeclass="docutils literal"><spanclass="pre">plot.png</span></code> using <codeclass="docutils literal"><spanclass="pre">plotcurve.py</span></code> script. An example of the plot is shown below:</p>
<p><center><imgalt="Training and testing curves."src="../../_images/plot.png"/></center></p>
</div>
<divclass="section"id="prediction">
<spanid="prediction"></span><h1>Prediction<aclass="headerlink"href="#prediction"title="Permalink to this headline">¶</a></h1>
<spanid="prediction"></span><h2>Prediction<aclass="headerlink"href="#prediction"title="Permalink to this headline">¶</a></h2>
<p>After we train the model, the model file as well as the model parameters are stored in path <codeclass="docutils literal"><spanclass="pre">./cifar_vgg_model/pass-%05d</span></code>. For example, the model of the 300-th pass is stored at <codeclass="docutils literal"><spanclass="pre">./cifar_vgg_model/pass-00299</span></code>.</p>
<p>To make a prediction for an image, one can run <codeclass="docutils literal"><spanclass="pre">predict.sh</span></code> as follows. The script will output the label of the classfiication.</p>
<spanid="exercise"></span><h1>Exercise<aclass="headerlink"href="#exercise"title="Permalink to this headline">¶</a></h1>
<spanid="exercise"></span><h2>Exercise<aclass="headerlink"href="#exercise"title="Permalink to this headline">¶</a></h2>
<p>Train a image classification of birds using VGG model and CUB-200 dataset. The birds dataset can be downloaded here. It contains an image dataset with photos of 200 bird species (mostly North American).</p>
<spanid="convolutional-neural-network"></span><h2>Convolutional Neural Network<aclass="headerlink"href="#convolutional-neural-network"title="Permalink to this headline">¶</a></h2>
<spanid="convolutional-neural-network"></span><h3>Convolutional Neural Network<aclass="headerlink"href="#convolutional-neural-network"title="Permalink to this headline">¶</a></h3>
<p>A Convolutional Neural Network is a feedforward neural network that uses convolution layers. It is very suitable for building neural networks that process and understand images. A standard convolutional neural network is shown below:</p>
<p>Convolutional Neural Network achieves amazing performance for image classification because it exploits two important characteristics of images: <em>local correlation</em> and <em>spatial invariance</em>. By iteratively applying convolution and max-pooing operations, convolutional neural network can well represent these two characteristics of images.</p>
<p>For more details of how to define layers and their connections, please refer to the documentation of layers.</p>
<spanid="model-zoo-imagenet"></span><h1>Model Zoo - ImageNet<aclass="headerlink"href="#model-zoo-imagenet"title="Permalink to this headline">¶</a></h1>
<p><aclass="reference external"href="http://www.image-net.org/">ImageNet</a> is a popular dataset for generic object classification. This tutorial provided convolutional neural network(CNN) models for ImageNet.</p>
<p><aclass="reference external"href="http://www.image-net.org/">ImageNet</a> is a popular dataset for generic object classification. This tutorial provides convolutional neural network(CNN) models for ImageNet.</p>
<divclass="section"id="resnet-introduction">
<spanid="resnet-introduction"></span><h2>ResNet Introduction<aclass="headerlink"href="#resnet-introduction"title="Permalink to this headline">¶</a></h2>
<p>ResNets from paper <aclass="reference external"href="http://arxiv.org/abs/1512.03385">Deep Residual Learning for Image Recognition</a> won the 1st place on the ILSVRC 2015 classification task. They present residual learning framework to ease the training of networks that are substantially deeper than those used previously. The residual connections are shown in following figure. The left building block is used in network of 34 layers and the right bottleneck building block is used in network of 50, 101, 152 layers .</p>
...
...
@@ -97,10 +97,10 @@
<br></div>
<divclass="section"id="resnet-model">
<spanid="resnet-model"></span><h2>ResNet Model<aclass="headerlink"href="#resnet-model"title="Permalink to this headline">¶</a></h2>
<p>See <codeclass="docutils literal"><spanclass="pre">demo/model_zoo/resnet/resnet.py</span></code>. This confgiure contains network of 50, 101 and 152 layers. You can specify layer number by adding argument like this<codeclass="docutils literal"><spanclass="pre">--config_args=layer_num=50</span></code> in command line arguments.</p>
<p>See <codeclass="docutils literal"><spanclass="pre">demo/model_zoo/resnet/resnet.py</span></code>. This config contains network of 50, 101 and 152 layers. You can specify layer number by adding argument like<codeclass="docutils literal"><spanclass="pre">--config_args=layer_num=50</span></code> in command line arguments.</p>
<divclass="section"id="network-visualization">
<spanid="network-visualization"></span><h3>Network Visualization<aclass="headerlink"href="#network-visualization"title="Permalink to this headline">¶</a></h3>
<p>You can get a diagram of ResNet network by running the following command. The script generates dot file and then converts dot file to PNG file, which uses installed draw_dot tool in our server. If you can not access the server, just install graphviz to convert dot file.</p>
<p>You can get a diagram of ResNet network by running the following commands. The script generates dot file and then converts dot file to PNG file, which uses installed draw_dot tool in our server. If you can not access the server, just install graphviz to convert dot file.</p>
<p>Third, specify model path and output directory in <codeclass="docutils literal"><spanclass="pre">extract_fea_c++.sh</span></code>, and then run following commands</p>
<p>Third, specify model path and output directory in <codeclass="docutils literal"><spanclass="pre">extract_fea_c++.sh</span></code>, and then run the following commands.</p>
<spanclass="k">def</span><spanclass="nf">process</span><spanclass="p">(</span><spanclass="n">settings</span><spanclass="p">,</span><spanclass="n">filename</span><spanclass="p">):</span><spanclass="c1"># settings is not used currently.</span>
<spanclass="n">f</span><spanclass="o">=</span><spanclass="nb">open</span><spanclass="p">(</span><spanclass="n">filename</span><spanclass="p">,</span><spanclass="s1">'r'</span><spanclass="p">)</span><spanclass="c1"># open one of training file</span>
<spanclass="k">for</span><spanclass="n">line</span><spanclass="ow">in</span><spanclass="n">f</span><spanclass="p">:</span><spanclass="c1"># read each line</span>
It sets some properties to DataProvider, and constructs a real PaddlePaddle
DataProvider from a very sample user implemented python function. It does not
matter if you are not familiar with <aclass="reference external"href="http://www.learnpython.org/en/Decorators">Decorator</a>. You can keep it sample by
DataProvider from a very simple user implemented python function. It does not
matter if you are not familiar with <aclass="reference external"href="http://www.learnpython.org/en/Decorators">Decorator</a>. You can keep it simple by
just taking <codeclass="code docutils literal"><spanclass="pre">@provider</span></code> as a fixed mark above the provider function you
implemented.</p>
<p><aclass="reference internal"href="#input-types">input_types</a> defines the data format that a DataProvider returns.
...
...
@@ -105,9 +132,9 @@ scalar, whose value ranges from 0 to 9.
document of <aclass="reference internal"href="#input-types">input_types</a> for more details.</p>
<p>The process method is the core part to construct a real DataProvider in
PaddlePaddle. It implements how to open the text file, how to read one sample
from the original text file, converted them into <aclass="reference internal"href="#input-types">input_types</a>, and give them
from the original text file, convert them into <aclass="reference internal"href="#input-types">input_types</a>, and give them
back to PaddlePaddle process at line 23.
Note that data yields by the process function must follow a same order that
Note that data yielded by the process function must follow the same order that
<aclass="reference internal"href="#input-types">input_types</a> are defined.</p>
<p>With the help of PyDataProvider2, user can focus on how to generate ONE traning
sample by using keywords <codeclass="code docutils literal"><spanclass="pre">yield</span></code>.
...
...
@@ -202,7 +229,7 @@ negative sentiment (marked by 0 and 1 respectively).</p>
<p>This data provider for sequential model is a little bit complex than that
<p>This data provider for sequential model is a little more complex than that
for MINST dataset.
A new initialization method is introduced here.
The method <codeclass="code docutils literal"><spanclass="pre">on_init</span></code> is configured to DataProvider by <codeclass="code docutils literal"><spanclass="pre">@provider</span></code>‘s
...
...
@@ -363,7 +390,7 @@ parameters which your init_hook does not use.</p>
<divclass="section"id="cache">
<h3>cache<aclass="headerlink"href="#cache"title="Permalink to this headline">¶</a></h3>
<p>DataProvider provides two simple cache strategy. They are
* CacheType.NO_CACHE means do not cache any data, then data is read runtime by</p>
* CacheType.NO_CACHE means do not cache any data, then data is read at runtime by</p>
<blockquote>
<div>the user implemented python module every pass.</div></blockquote>
<spanid="logistic-regression"></span><h3>逻辑回归模型(Logistic Regression)<aclass="headerlink"href="#logistic-regression"title="Permalink to this headline">¶</a></h3>
<spanid="optimization-algorithm"></span><h2>优化算法(Optimization Algorithm)<aclass="headerlink"href="#optimization-algorithm"title="Permalink to this headline">¶</a></h2>