diff --git a/03.image_classification/README.en.md b/03.image_classification/README.en.md index 2c243c62737cb217ec00df7103ac9b7b378e08d5..d5e8b650199cc32bec817a884baed222d17115e3 100644 --- a/03.image_classification/README.en.md +++ b/03.image_classification/README.en.md @@ -169,6 +169,7 @@ We must import and initialize PaddlePaddle (enable/disable GPU, set the number o ```python import sys +import gzip import paddle.v2 as paddle from vgg import vgg_bn_drop from resnet import resnet_cifar10 @@ -437,6 +438,10 @@ def event_handler(event): sys.stdout.write('.') sys.stdout.flush() 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.cifar.test10(), batch_size=128), @@ -475,7 +480,29 @@ Figure 12. The error rate of VGG model on CIFAR10

-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 diff --git a/03.image_classification/README.md b/03.image_classification/README.md index 0eea28f906e1a63c1703c7d660e4bd9987df2a04..8f3a8d256173839adeec06125527a6b696b79974 100644 --- a/03.image_classification/README.md +++ b/03.image_classification/README.md @@ -156,6 +156,7 @@ Paddle API提供了自动加载cifar数据集模块 `paddle.dataset.cifar`。 ```python import sys +import gzip import paddle.v2 as paddle from vgg import vgg_bn_drop from resnet import resnet_cifar10 @@ -409,6 +410,7 @@ def event_handler_plot(event): cost_ploter.plot() step += 1 if isinstance(event, paddle.event.EndPass): + result = trainer.test( reader=paddle.batch( paddle.dataset.cifar.test10(), batch_size=128), @@ -429,6 +431,10 @@ def event_handler(event): sys.stdout.write('.') sys.stdout.flush() 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.cifar.test10(), batch_size=128), @@ -467,6 +473,28 @@ Test with Pass 0, {'classification_error_evaluator': 0.885200023651123} 图12. CIFAR10数据集上VGG模型的分类错误率

+## 应用模型 + +可以使用训练好的模型对图片进行分类,下面程序展示了如何使用`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] +``` + ## 总结 diff --git a/03.image_classification/index.en.html b/03.image_classification/index.en.html index c28836ac766f49924fa10b56045de17178cde3e1..37f1dc5f2392a931f0e5161926abe262f3fad7ee 100644 --- a/03.image_classification/index.en.html +++ b/03.image_classification/index.en.html @@ -211,6 +211,7 @@ We must import and initialize PaddlePaddle (enable/disable GPU, set the number o ```python import sys +import gzip import paddle.v2 as paddle from vgg import vgg_bn_drop from resnet import resnet_cifar10 @@ -479,6 +480,10 @@ def event_handler(event): sys.stdout.write('.') sys.stdout.flush() 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.cifar.test10(), batch_size=128), @@ -517,7 +522,29 @@ Figure 12. The error rate of VGG model on CIFAR10

-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 diff --git a/03.image_classification/index.html b/03.image_classification/index.html index 8678bde12918bbf847f810242041bd130be5cacb..4001d122d275657a201f47e70fb4923688e523e9 100644 --- a/03.image_classification/index.html +++ b/03.image_classification/index.html @@ -198,6 +198,7 @@ Paddle API提供了自动加载cifar数据集模块 `paddle.dataset.cifar`。 ```python import sys +import gzip import paddle.v2 as paddle from vgg import vgg_bn_drop from resnet import resnet_cifar10 @@ -451,6 +452,7 @@ def event_handler_plot(event): cost_ploter.plot() step += 1 if isinstance(event, paddle.event.EndPass): + result = trainer.test( reader=paddle.batch( paddle.dataset.cifar.test10(), batch_size=128), @@ -471,6 +473,10 @@ def event_handler(event): sys.stdout.write('.') sys.stdout.flush() 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.cifar.test10(), batch_size=128), @@ -509,6 +515,28 @@ Test with Pass 0, {'classification_error_evaluator': 0.885200023651123} 图12. CIFAR10数据集上VGG模型的分类错误率

+## 应用模型 + +可以使用训练好的模型对图片进行分类,下面程序展示了如何使用`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] +``` + ## 总结 diff --git a/03.image_classification/train.py b/03.image_classification/train.py index f8e18452cc08f174ef3c63026c880759734e42f4..e60b9bda3c7ec1eefb2cfb9798f72aa8fd5e2b93 100644 --- a/03.image_classification/train.py +++ b/03.image_classification/train.py @@ -13,6 +13,7 @@ # limitations under the License import sys +import gzip import paddle.v2 as paddle @@ -66,6 +67,10 @@ def main(): sys.stdout.write('.') sys.stdout.flush() 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.cifar.test10(), batch_size=128), @@ -81,11 +86,30 @@ def main(): paddle.reader.shuffle( paddle.dataset.cifar.train10(), buf_size=50000), batch_size=128), - num_passes=200, + num_passes=1, event_handler=event_handler, feeding={'image': 0, '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__': main()