提交 fe1f9e99 编写于 作者: Q qingqing01 提交者: GitHub

Merge pull request #271 from qingqing01/image

model saving and inference for 03.image_classification
...@@ -169,6 +169,7 @@ We must import and initialize PaddlePaddle (enable/disable GPU, set the number o ...@@ -169,6 +169,7 @@ We must import and initialize PaddlePaddle (enable/disable GPU, set the number o
```python ```python
import sys import sys
import gzip
import paddle.v2 as paddle import paddle.v2 as paddle
from vgg import vgg_bn_drop from vgg import vgg_bn_drop
from resnet import resnet_cifar10 from resnet import resnet_cifar10
...@@ -437,6 +438,10 @@ def event_handler(event): ...@@ -437,6 +438,10 @@ def event_handler(event):
sys.stdout.write('.') sys.stdout.write('.')
sys.stdout.flush() sys.stdout.flush()
if isinstance(event, paddle.event.EndPass): 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( result = trainer.test(
reader=paddle.batch( reader=paddle.batch(
paddle.dataset.cifar.test10(), batch_size=128), paddle.dataset.cifar.test10(), batch_size=128),
...@@ -475,7 +480,29 @@ Figure 12. The error rate of VGG model on CIFAR10 ...@@ -475,7 +480,29 @@ Figure 12. The error rate of VGG model on CIFAR10
</p> </p>
After training is done, the model from each pass is saved in `output/pass-%05d`. For example, the model of Pass 300 is saved in `output/pass-00299`.
## Application
After training is done, users can use the trained model to classify images. The following code shows how to infer through `paddle.infer` interface.
```python
from PIL import Image
import numpy as np
def load_image(file):
im = Image.open(file)
im = im.resize((32, 32), Image.ANTIALIAS)
im = np.array(im).astype(np.float32).flatten()
im = im / 255.0
return im
test_data = []
test_data.append((load_image('image/dog.png'),))
probs = paddle.infer(
output_layer=out, parameters=parameters, input=test_data)
lab = np.argsort(-probs) # probs and lab are the results of one batch data
print "Label of image/dog.png is: %d" % lab[0][0]
```
## Conclusion ## Conclusion
......
...@@ -156,6 +156,7 @@ Paddle API提供了自动加载cifar数据集模块 `paddle.dataset.cifar`。 ...@@ -156,6 +156,7 @@ Paddle API提供了自动加载cifar数据集模块 `paddle.dataset.cifar`。
```python ```python
import sys import sys
import gzip
import paddle.v2 as paddle import paddle.v2 as paddle
from vgg import vgg_bn_drop from vgg import vgg_bn_drop
from resnet import resnet_cifar10 from resnet import resnet_cifar10
...@@ -409,6 +410,7 @@ def event_handler_plot(event): ...@@ -409,6 +410,7 @@ def event_handler_plot(event):
cost_ploter.plot() cost_ploter.plot()
step += 1 step += 1
if isinstance(event, paddle.event.EndPass): if isinstance(event, paddle.event.EndPass):
result = trainer.test( result = trainer.test(
reader=paddle.batch( reader=paddle.batch(
paddle.dataset.cifar.test10(), batch_size=128), paddle.dataset.cifar.test10(), batch_size=128),
...@@ -429,6 +431,10 @@ def event_handler(event): ...@@ -429,6 +431,10 @@ def event_handler(event):
sys.stdout.write('.') sys.stdout.write('.')
sys.stdout.flush() sys.stdout.flush()
if isinstance(event, paddle.event.EndPass): 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( result = trainer.test(
reader=paddle.batch( reader=paddle.batch(
paddle.dataset.cifar.test10(), batch_size=128), paddle.dataset.cifar.test10(), batch_size=128),
...@@ -467,6 +473,28 @@ Test with Pass 0, {'classification_error_evaluator': 0.885200023651123} ...@@ -467,6 +473,28 @@ Test with Pass 0, {'classification_error_evaluator': 0.885200023651123}
图12. CIFAR10数据集上VGG模型的分类错误率 图12. CIFAR10数据集上VGG模型的分类错误率
</p> </p>
## 应用模型
可以使用训练好的模型对图片进行分类,下面程序展示了如何使用`paddle.infer`接口进行推断。
```python
from PIL import Image
import numpy as np
def load_image(file):
im = Image.open(file)
im = im.resize((32, 32), Image.ANTIALIAS)
im = np.array(im).astype(np.float32).flatten()
im = im / 255.0
return im
test_data = []
test_data.append((load_image('image/dog.png'),))
probs = paddle.infer(
output_layer=out, parameters=parameters, input=test_data)
lab = np.argsort(-probs) # probs and lab are the results of one batch data
print "Label of image/dog.png is: %d" % lab[0][0]
```
## 总结 ## 总结
......
...@@ -211,6 +211,7 @@ We must import and initialize PaddlePaddle (enable/disable GPU, set the number o ...@@ -211,6 +211,7 @@ We must import and initialize PaddlePaddle (enable/disable GPU, set the number o
```python ```python
import sys import sys
import gzip
import paddle.v2 as paddle import paddle.v2 as paddle
from vgg import vgg_bn_drop from vgg import vgg_bn_drop
from resnet import resnet_cifar10 from resnet import resnet_cifar10
...@@ -479,6 +480,10 @@ def event_handler(event): ...@@ -479,6 +480,10 @@ def event_handler(event):
sys.stdout.write('.') sys.stdout.write('.')
sys.stdout.flush() sys.stdout.flush()
if isinstance(event, paddle.event.EndPass): 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( result = trainer.test(
reader=paddle.batch( reader=paddle.batch(
paddle.dataset.cifar.test10(), batch_size=128), paddle.dataset.cifar.test10(), batch_size=128),
...@@ -517,7 +522,29 @@ Figure 12. The error rate of VGG model on CIFAR10 ...@@ -517,7 +522,29 @@ Figure 12. The error rate of VGG model on CIFAR10
</p> </p>
After training is done, the model from each pass is saved in `output/pass-%05d`. For example, the model of Pass 300 is saved in `output/pass-00299`.
## Application
After training is done, users can use the trained model to classify images. The following code shows how to infer through `paddle.infer` interface.
```python
from PIL import Image
import numpy as np
def load_image(file):
im = Image.open(file)
im = im.resize((32, 32), Image.ANTIALIAS)
im = np.array(im).astype(np.float32).flatten()
im = im / 255.0
return im
test_data = []
test_data.append((load_image('image/dog.png'),))
probs = paddle.infer(
output_layer=out, parameters=parameters, input=test_data)
lab = np.argsort(-probs) # probs and lab are the results of one batch data
print "Label of image/dog.png is: %d" % lab[0][0]
```
## Conclusion ## Conclusion
......
...@@ -198,6 +198,7 @@ Paddle API提供了自动加载cifar数据集模块 `paddle.dataset.cifar`。 ...@@ -198,6 +198,7 @@ Paddle API提供了自动加载cifar数据集模块 `paddle.dataset.cifar`。
```python ```python
import sys import sys
import gzip
import paddle.v2 as paddle import paddle.v2 as paddle
from vgg import vgg_bn_drop from vgg import vgg_bn_drop
from resnet import resnet_cifar10 from resnet import resnet_cifar10
...@@ -451,6 +452,7 @@ def event_handler_plot(event): ...@@ -451,6 +452,7 @@ def event_handler_plot(event):
cost_ploter.plot() cost_ploter.plot()
step += 1 step += 1
if isinstance(event, paddle.event.EndPass): if isinstance(event, paddle.event.EndPass):
result = trainer.test( result = trainer.test(
reader=paddle.batch( reader=paddle.batch(
paddle.dataset.cifar.test10(), batch_size=128), paddle.dataset.cifar.test10(), batch_size=128),
...@@ -471,6 +473,10 @@ def event_handler(event): ...@@ -471,6 +473,10 @@ def event_handler(event):
sys.stdout.write('.') sys.stdout.write('.')
sys.stdout.flush() sys.stdout.flush()
if isinstance(event, paddle.event.EndPass): 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( result = trainer.test(
reader=paddle.batch( reader=paddle.batch(
paddle.dataset.cifar.test10(), batch_size=128), paddle.dataset.cifar.test10(), batch_size=128),
...@@ -509,6 +515,28 @@ Test with Pass 0, {'classification_error_evaluator': 0.885200023651123} ...@@ -509,6 +515,28 @@ Test with Pass 0, {'classification_error_evaluator': 0.885200023651123}
图12. CIFAR10数据集上VGG模型的分类错误率 图12. CIFAR10数据集上VGG模型的分类错误率
</p> </p>
## 应用模型
可以使用训练好的模型对图片进行分类,下面程序展示了如何使用`paddle.infer`接口进行推断。
```python
from PIL import Image
import numpy as np
def load_image(file):
im = Image.open(file)
im = im.resize((32, 32), Image.ANTIALIAS)
im = np.array(im).astype(np.float32).flatten()
im = im / 255.0
return im
test_data = []
test_data.append((load_image('image/dog.png'),))
probs = paddle.infer(
output_layer=out, parameters=parameters, input=test_data)
lab = np.argsort(-probs) # probs and lab are the results of one batch data
print "Label of image/dog.png is: %d" % lab[0][0]
```
## 总结 ## 总结
......
...@@ -13,6 +13,7 @@ ...@@ -13,6 +13,7 @@
# limitations under the License # limitations under the License
import sys import sys
import gzip
import paddle.v2 as paddle import paddle.v2 as paddle
...@@ -66,6 +67,10 @@ def main(): ...@@ -66,6 +67,10 @@ def main():
sys.stdout.write('.') sys.stdout.write('.')
sys.stdout.flush() sys.stdout.flush()
if isinstance(event, paddle.event.EndPass): 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( result = trainer.test(
reader=paddle.batch( reader=paddle.batch(
paddle.dataset.cifar.test10(), batch_size=128), paddle.dataset.cifar.test10(), batch_size=128),
...@@ -81,11 +86,30 @@ def main(): ...@@ -81,11 +86,30 @@ def main():
paddle.reader.shuffle( paddle.reader.shuffle(
paddle.dataset.cifar.train10(), buf_size=50000), paddle.dataset.cifar.train10(), buf_size=50000),
batch_size=128), batch_size=128),
num_passes=200, num_passes=1,
event_handler=event_handler, event_handler=event_handler,
feeding={'image': 0, feeding={'image': 0,
'label': 1}) 'label': 1})
# inference
from PIL import Image
import numpy as np
def load_image(file):
im = Image.open(file)
im = im.resize((32, 32), Image.ANTIALIAS)
im = np.array(im).astype(np.float32).flatten()
im = im / 255.0
return im
test_data = []
test_data.append((load_image('image/dog.png'), ))
probs = paddle.infer(
output_layer=out, parameters=parameters, input=test_data)
lab = np.argsort(-probs) # probs and lab are the results of one batch data
print "Label of image/dog.png is: %d" % lab[0][0]
if __name__ == '__main__': if __name__ == '__main__':
main() main()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册