From 6a68dc07dce365abba2d1e43912119016be964eb Mon Sep 17 00:00:00 2001 From: liaogang Date: Mon, 17 Apr 2017 14:47:48 +0800 Subject: [PATCH] add infer and save models --- 02.recognize_digits/README.en.md | 33 ++++++++++++++++++++++++- 02.recognize_digits/README.md | 34 +++++++++++++++++++++++++- 02.recognize_digits/image/infer_3.png | Bin 0 -> 7346 bytes 02.recognize_digits/index.en.html | 33 ++++++++++++++++++++++++- 02.recognize_digits/index.html | 34 +++++++++++++++++++++++++- 02.recognize_digits/train.py | 26 +++++++++++++++++++- 6 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 02.recognize_digits/image/infer_3.png diff --git a/02.recognize_digits/README.en.md b/02.recognize_digits/README.en.md index 0dc9354..b01790a 100644 --- a/02.recognize_digits/README.en.md +++ b/02.recognize_digits/README.en.md @@ -131,6 +131,7 @@ PaddlePaddle provides a Python module, `paddle.dataset.mnist`, which downloads a A PaddlePaddle program starts from importing the API package: ```python +import gzip import paddle.v2 as paddle ``` @@ -249,6 +250,10 @@ def event_handler_plot(event): cost_ploter.plot() step += 1 if isinstance(event, paddle.event.EndPass): + # save parameters + with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f: + parameters.to_tar(f) + result = trainer.test(reader=paddle.batch( paddle.dataset.mnist.test(), batch_size=128)) cost_ploter.append(test_title, step, result.cost) @@ -265,6 +270,10 @@ def event_handler(event): print "Pass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics) if isinstance(event, paddle.event.EndPass): + # save parameters + with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f: + parameters.to_tar(f) + result = trainer.test(reader=paddle.batch( paddle.dataset.mnist.test(), batch_size=128)) print "Test with Pass %d, Cost %f, %s\n" % ( @@ -280,7 +289,7 @@ trainer.train( paddle.dataset.mnist.train(), buf_size=8192), batch_size=128), event_handler=event_handler_plot, - num_passes=100) + num_passes=5) ``` During training, `trainer.train` invokes `event_handler` for certain events. This gives us a chance to print the training progress. @@ -305,8 +314,30 @@ print 'The classification accuracy is %.2f%%' % (100 - float(best[2]) * 100) Usually, with MNIST data, the softmax regression model can get accuracy around 92.34%, MLP can get about 97.66%, and convolution network can get up to around 99.20%. Convolution layers have been widely considered a great invention for image processsing. +## Application + +After training is done, user can use the trained model to classify images. The following code shows how to inference MNIST images through `paddle.infer` interface. + +```python +from PIL import Image +import numpy as np +def load_image(file): + im = Image.open(file).convert('L') + im = im.resize((28, 28), Image.ANTIALIAS) + im = np.array(im).astype(np.float32).flatten() + im = im / 255.0 + return im +test_data = [] +test_data.append((load_image('image/infer_3.png'),)) + +probs = paddle.infer( + output_layer=predict, parameters=parameters, input=test_data) +lab = np.argsort(-probs) # probs and lab are the results of one batch data +print "Label of image/infer_3.png is: %d" % lab[0][0] +``` ## Conclusion + This tutorial describes a few basic Deep Learning models viz. Softmax regression, Multilayer Perceptron Network and Convolutional Neural Network. The subsequent tutorials will derive more sophisticated models from these. So it is crucial to understand these models for future learning. When our model evolved from a simple softmax regression to slightly complex Convolutional Neural Network, the recognition accuracy on the MNIST data set achieved large improvement in accuracy. This is due to the Convolutional layers' local connections and parameter sharing. While learning new models in the future, we encourage the readers to understand the key ideas that lead a new model to improve results of an old one. Moreover, this tutorial introduced the basic flow of PaddlePaddle model design, starting with a dataprovider, model layer construction, to final training and prediction. Readers can leverage the flow used in this MNIST handwritten digit classification example and experiment with different data and network architectures to train models for classification tasks of their choice. ## References diff --git a/02.recognize_digits/README.md b/02.recognize_digits/README.md index b9cac63..dada65b 100644 --- a/02.recognize_digits/README.md +++ b/02.recognize_digits/README.md @@ -132,6 +132,7 @@ PaddlePaddle在API中提供了自动加载[MNIST](http://yann.lecun.com/exdb/mni 首先,加载PaddlePaddle的V2 api包。 ```python +import gzip import paddle.v2 as paddle ``` 其次,定义三个不同的分类器: @@ -254,6 +255,10 @@ def event_handler_plot(event): cost_ploter.plot() step += 1 if isinstance(event, paddle.event.EndPass): + # save parameters + with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f: + parameters.to_tar(f) + result = trainer.test(reader=paddle.batch( paddle.dataset.mnist.test(), batch_size=128)) cost_ploter.append(test_title, step, result.cost) @@ -269,6 +274,10 @@ def event_handler(event): print "Pass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics) if isinstance(event, paddle.event.EndPass): + # save parameters + with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f: + parameters.to_tar(f) + result = trainer.test(reader=paddle.batch( paddle.dataset.mnist.test(), batch_size=128)) print "Test with Pass %d, Cost %f, %s\n" % ( @@ -284,7 +293,7 @@ trainer.train( paddle.dataset.mnist.train(), buf_size=8192), batch_size=128), event_handler=event_handler_plot, - num_passes=100) + num_passes=5) ``` 训练过程是完全自动的,event_handler里打印的日志类似如下所示: @@ -300,6 +309,29 @@ trainer.train( 训练之后,检查模型的预测准确度。用 MNIST 训练的时候,一般 softmax回归模型的分类准确率为约为 92.34%,多层感知器为97.66%,卷积神经网络可以达到 99.20%。 + +## 应用模型 + +可以使用训练好的模型对手写体数字图片进行分类,下面程序展示了如何使用paddle.infer接口进行推断。 + +```python +from PIL import Image +import numpy as np +def load_image(file): + im = Image.open(file).convert('L') + im = im.resize((28, 28), Image.ANTIALIAS) + im = np.array(im).astype(np.float32).flatten() + im = im / 255.0 + return im +test_data = [] +test_data.append((load_image('image/infer_3.png'),)) + +probs = paddle.infer( + output_layer=predict, parameters=parameters, input=test_data) +lab = np.argsort(-probs) # probs and lab are the results of one batch data +print "Label of image/infer_3.png is: %d" % lab[0][0] +``` + ## 总结 本教程的softmax回归、多层感知器和卷积神经网络是最基础的深度学习模型,后续章节中复杂的神经网络都是从它们衍生出来的,因此这几个模型对之后的学习大有裨益。同时,我们也观察到从最简单的softmax回归变换到稍复杂的卷积神经网络的时候,MNIST数据集上的识别准确率有了大幅度的提升,原因是卷积层具有局部连接和共享权重的特性。在之后学习新模型的时候,希望大家也要深入到新模型相比原模型带来效果提升的关键之处。此外,本教程还介绍了PaddlePaddle模型搭建的基本流程,从dataprovider的编写、网络层的构建,到最后的训练和预测。对这个流程熟悉以后,大家就可以用自己的数据,定义自己的网络模型,并完成自己的训练和预测任务了。 diff --git a/02.recognize_digits/image/infer_3.png b/02.recognize_digits/image/infer_3.png new file mode 100644 index 0000000000000000000000000000000000000000..030cd60d3b4af9aecd4941204da4ad15f6e1189f GIT binary patch literal 7346 zcmb_g2{={V*Wc%!JDBIWxP*w5A!Bh3nTZslLDxJF*N|jtB2tEW)k{TEl;M^+$q>L ze*k?5gcycZn0etTTZMt|e;3|IPRpkeJ95E2mV84xJ0vPltW=$l$%84;l@W9enc)g_wLmkdg< zA7hy>PR!{?(}y{AO$`j3tZl4}Of3wTiEt{C0|R|ATmbm_hfr*c^~Lu%IEoX7;G7(Q z9c%)C>=GQPXKii4h`D-Qj{m!v=~>PlbSf}n{Wkw+fZNqA*aecehVIU;!7hFfmjQqk zatWk_0Dxm@_aN#5MlTJFFn) zf%uUZWb_Na@(eYxg_z+#;Sy{BYY7AZzyMeBc5{f=LM#yKW2^TIE0ZY@E&F43c!ikm z{)L?>0XDzlW5WD>cQES9h;z!*&2$xCaSJxw1??~gOqjcuktxJ*rD4h+wU8Hn&SVAz*^D4{ltoTZ?B%AI1w;0S$CA-`aVmgA$FT*%9F z!L@+uhIsBafp$0+-QyO#hmp^64z6z74Izd(pssHIw!d;i;p)<7#AJ+(3-n!H>(C#) z;^u42u){i|w}ZoWt&Z&oq3mYZVGYq?Z?YL60z7~lQ21q89bv!R;}6?iAOd^_{?I4*_c;CKUN;a1?Tf#y zD`ORa`*LsC@(3UB0nXru6~XFYw_+_|s|!^btAW*A9VgDYnkU{6za4LaH^OgNwE!2G zwJ%f(@cM6T=CIEVvZKJ(f0e_Zc(@O^cHA(o8#jpS19srua+F_8oxF}wRx|h=*%A!G zXe(85gBAM+JM4OxwJKI0s}1vqxnl*eB0vGV1v1gYk{~x#XkD!yBkR!Lx&5i-YTZ}; z7+H(|gX1qdha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s}{aUjy@`qyPW_ literal 0 HcmV?d00001 diff --git a/02.recognize_digits/index.en.html b/02.recognize_digits/index.en.html index 6298165..924810d 100644 --- a/02.recognize_digits/index.en.html +++ b/02.recognize_digits/index.en.html @@ -173,6 +173,7 @@ PaddlePaddle provides a Python module, `paddle.dataset.mnist`, which downloads a A PaddlePaddle program starts from importing the API package: ```python +import gzip import paddle.v2 as paddle ``` @@ -291,6 +292,10 @@ def event_handler_plot(event): cost_ploter.plot() step += 1 if isinstance(event, paddle.event.EndPass): + # save parameters + with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f: + parameters.to_tar(f) + result = trainer.test(reader=paddle.batch( paddle.dataset.mnist.test(), batch_size=128)) cost_ploter.append(test_title, step, result.cost) @@ -307,6 +312,10 @@ def event_handler(event): print "Pass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics) if isinstance(event, paddle.event.EndPass): + # save parameters + with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f: + parameters.to_tar(f) + result = trainer.test(reader=paddle.batch( paddle.dataset.mnist.test(), batch_size=128)) print "Test with Pass %d, Cost %f, %s\n" % ( @@ -322,7 +331,7 @@ trainer.train( paddle.dataset.mnist.train(), buf_size=8192), batch_size=128), event_handler=event_handler_plot, - num_passes=100) + num_passes=5) ``` During training, `trainer.train` invokes `event_handler` for certain events. This gives us a chance to print the training progress. @@ -347,8 +356,30 @@ print 'The classification accuracy is %.2f%%' % (100 - float(best[2]) * 100) Usually, with MNIST data, the softmax regression model can get accuracy around 92.34%, MLP can get about 97.66%, and convolution network can get up to around 99.20%. Convolution layers have been widely considered a great invention for image processsing. +## Application + +After training is done, user can use the trained model to classify images. The following code shows how to inference MNIST images through `paddle.infer` interface. + +```python +from PIL import Image +import numpy as np +def load_image(file): + im = Image.open(file).convert('L') + im = im.resize((28, 28), Image.ANTIALIAS) + im = np.array(im).astype(np.float32).flatten() + im = im / 255.0 + return im +test_data = [] +test_data.append((load_image('image/infer_3.png'),)) + +probs = paddle.infer( + output_layer=predict, parameters=parameters, input=test_data) +lab = np.argsort(-probs) # probs and lab are the results of one batch data +print "Label of image/infer_3.png is: %d" % lab[0][0] +``` ## Conclusion + This tutorial describes a few basic Deep Learning models viz. Softmax regression, Multilayer Perceptron Network and Convolutional Neural Network. The subsequent tutorials will derive more sophisticated models from these. So it is crucial to understand these models for future learning. When our model evolved from a simple softmax regression to slightly complex Convolutional Neural Network, the recognition accuracy on the MNIST data set achieved large improvement in accuracy. This is due to the Convolutional layers' local connections and parameter sharing. While learning new models in the future, we encourage the readers to understand the key ideas that lead a new model to improve results of an old one. Moreover, this tutorial introduced the basic flow of PaddlePaddle model design, starting with a dataprovider, model layer construction, to final training and prediction. Readers can leverage the flow used in this MNIST handwritten digit classification example and experiment with different data and network architectures to train models for classification tasks of their choice. ## References diff --git a/02.recognize_digits/index.html b/02.recognize_digits/index.html index 2568393..2717d93 100644 --- a/02.recognize_digits/index.html +++ b/02.recognize_digits/index.html @@ -174,6 +174,7 @@ PaddlePaddle在API中提供了自动加载[MNIST](http://yann.lecun.com/exdb/mni 首先,加载PaddlePaddle的V2 api包。 ```python +import gzip import paddle.v2 as paddle ``` 其次,定义三个不同的分类器: @@ -296,6 +297,10 @@ def event_handler_plot(event): cost_ploter.plot() step += 1 if isinstance(event, paddle.event.EndPass): + # save parameters + with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f: + parameters.to_tar(f) + result = trainer.test(reader=paddle.batch( paddle.dataset.mnist.test(), batch_size=128)) cost_ploter.append(test_title, step, result.cost) @@ -311,6 +316,10 @@ def event_handler(event): print "Pass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics) if isinstance(event, paddle.event.EndPass): + # save parameters + with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f: + parameters.to_tar(f) + result = trainer.test(reader=paddle.batch( paddle.dataset.mnist.test(), batch_size=128)) print "Test with Pass %d, Cost %f, %s\n" % ( @@ -326,7 +335,7 @@ trainer.train( paddle.dataset.mnist.train(), buf_size=8192), batch_size=128), event_handler=event_handler_plot, - num_passes=100) + num_passes=5) ``` 训练过程是完全自动的,event_handler里打印的日志类似如下所示: @@ -342,6 +351,29 @@ trainer.train( 训练之后,检查模型的预测准确度。用 MNIST 训练的时候,一般 softmax回归模型的分类准确率为约为 92.34%,多层感知器为97.66%,卷积神经网络可以达到 99.20%。 + +## 应用模型 + +可以使用训练好的模型对手写体数字图片进行分类,下面程序展示了如何使用paddle.infer接口进行推断。 + +```python +from PIL import Image +import numpy as np +def load_image(file): + im = Image.open(file).convert('L') + im = im.resize((28, 28), Image.ANTIALIAS) + im = np.array(im).astype(np.float32).flatten() + im = im / 255.0 + return im +test_data = [] +test_data.append((load_image('image/infer_3.png'),)) + +probs = paddle.infer( + output_layer=predict, parameters=parameters, input=test_data) +lab = np.argsort(-probs) # probs and lab are the results of one batch data +print "Label of image/infer_3.png is: %d" % lab[0][0] +``` + ## 总结 本教程的softmax回归、多层感知器和卷积神经网络是最基础的深度学习模型,后续章节中复杂的神经网络都是从它们衍生出来的,因此这几个模型对之后的学习大有裨益。同时,我们也观察到从最简单的softmax回归变换到稍复杂的卷积神经网络的时候,MNIST数据集上的识别准确率有了大幅度的提升,原因是卷积层具有局部连接和共享权重的特性。在之后学习新模型的时候,希望大家也要深入到新模型相比原模型带来效果提升的关键之处。此外,本教程还介绍了PaddlePaddle模型搭建的基本流程,从dataprovider的编写、网络层的构建,到最后的训练和预测。对这个流程熟悉以后,大家就可以用自己的数据,定义自己的网络模型,并完成自己的训练和预测任务了。 diff --git a/02.recognize_digits/train.py b/02.recognize_digits/train.py index 6a11839..7dfd9f3 100644 --- a/02.recognize_digits/train.py +++ b/02.recognize_digits/train.py @@ -1,3 +1,6 @@ +import gzip +from PIL import Image +import numpy as np import paddle.v2 as paddle @@ -79,6 +82,10 @@ def event_handler(event): print "Pass %d, Batch %d, Cost %f, %s" % ( event.pass_id, event.batch_id, event.cost, event.metrics) if isinstance(event, paddle.event.EndPass): + # save parameters + with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f: + parameters.to_tar(f) + result = trainer.test(reader=paddle.batch( paddle.dataset.mnist.test(), batch_size=128)) print "Test with Pass %d, Cost %f, %s\n" % (event.pass_id, result.cost, @@ -92,9 +99,26 @@ trainer.train( paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=8192), batch_size=128), event_handler=event_handler, - num_passes=100) + num_passes=1) # find the best pass best = sorted(lists, key=lambda list: float(list[1]))[0] print 'Best pass is %s, testing Avgcost is %s' % (best[0], best[1]) print 'The classification accuracy is %.2f%%' % (100 - float(best[2]) * 100) + + +def load_image(file): + im = Image.open(file).convert('L') + im = im.resize((28, 28), Image.ANTIALIAS) + im = np.array(im).astype(np.float32).flatten() + im = im / 255.0 + return im + + +test_data = [] +test_data.append((load_image('image/infer_3.png'), )) + +probs = paddle.infer( + output_layer=predict, parameters=parameters, input=test_data) +lab = np.argsort(-probs) # probs and lab are the results of one batch data +print "Label of image/infer_3.png is: %d" % lab[0][0] -- GitLab