From 0087e3ccd42b08ebd2b50c99aa59359678b73cb9 Mon Sep 17 00:00:00 2001 From: qingqing01 Date: Tue, 18 Apr 2017 19:21:48 +0800 Subject: [PATCH 1/2] fix bug in 03.image_classification --- 03.image_classification/README.en.md | 14 ++++++++++---- 03.image_classification/README.md | 14 ++++++++++---- 03.image_classification/index.en.html | 14 ++++++++++---- 03.image_classification/index.html | 14 ++++++++++---- 03.image_classification/train.py | 14 ++++++++++---- 5 files changed, 50 insertions(+), 20 deletions(-) diff --git a/03.image_classification/README.en.md b/03.image_classification/README.en.md index d5e8b650..a39f9021 100644 --- a/03.image_classification/README.en.md +++ b/03.image_classification/README.en.md @@ -364,8 +364,7 @@ momentum_optimizer = paddle.optimizer.Momentum( learning_rate=0.1 / 128.0, learning_rate_decay_a=0.1, learning_rate_decay_b=50000 * 100, - learning_rate_schedule='discexp', - batch_size=128) + learning_rate_schedule='discexp') # Create trainer trainer = paddle.trainer.SGD(cost=cost, @@ -483,7 +482,7 @@ Figure 12. The error rate of VGG model on CIFAR10 ## 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. +After training is done, users can use the trained model to classify images. The following code shows how to infer through `paddle.infer` interface. You can remove the comments to change the model name. ```python from PIL import Image @@ -491,12 +490,19 @@ 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 = np.array(im).astype(np.float32) + im = im.transpose((2, 0, 1)) # CHW + im = im[(2, 1, 0),:,:] # BGR + im = im.flatten() im = im / 255.0 return im test_data = [] test_data.append((load_image('image/dog.png'),)) +# users can remove the comments and change the model name +# with gzip.open('params_pass_50.tar.gz', 'r') as f: +# parameters = paddle.parameters.Parameters.from_tar(f) + 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 diff --git a/03.image_classification/README.md b/03.image_classification/README.md index 8f3a8d25..d4769976 100644 --- a/03.image_classification/README.md +++ b/03.image_classification/README.md @@ -356,8 +356,7 @@ momentum_optimizer = paddle.optimizer.Momentum( learning_rate=0.1 / 128.0, learning_rate_decay_a=0.1, learning_rate_decay_b=50000 * 100, - learning_rate_schedule='discexp', - batch_size=128) + learning_rate_schedule='discexp') # Create trainer trainer = paddle.trainer.SGD(cost=cost, @@ -475,7 +474,7 @@ Test with Pass 0, {'classification_error_evaluator': 0.885200023651123} ## 应用模型 -可以使用训练好的模型对图片进行分类,下面程序展示了如何使用`paddle.infer`接口进行推断。 +可以使用训练好的模型对图片进行分类,下面程序展示了如何使用`paddle.infer`接口进行推断,可以打开注释,更改加载的模型。 ```python from PIL import Image @@ -483,12 +482,19 @@ 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 = np.array(im).astype(np.float32) + im = im.transpose((2, 0, 1)) # CHW + im = im[(2, 1, 0),:,:] # BGR + im = im.flatten() im = im / 255.0 return im + test_data = [] test_data.append((load_image('image/dog.png'),)) +# with gzip.open('params_pass_50.tar.gz', 'r') as f: +# parameters = paddle.parameters.Parameters.from_tar(f) + 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 diff --git a/03.image_classification/index.en.html b/03.image_classification/index.en.html index 37f1dc5f..92c5a531 100644 --- a/03.image_classification/index.en.html +++ b/03.image_classification/index.en.html @@ -406,8 +406,7 @@ learning_rate=0.1 / 128.0, learning_rate_decay_a=0.1, learning_rate_decay_b=50000 * 100, - learning_rate_schedule='discexp', - batch_size=128) + learning_rate_schedule='discexp') # Create trainer trainer = paddle.trainer.SGD(cost=cost, @@ -525,7 +524,7 @@ ## 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. +After training is done, users can use the trained model to classify images. The following code shows how to infer through `paddle.infer` interface. You can remove the comments to change the model name. ```python from PIL import Image @@ -533,12 +532,19 @@ def load_image(file): im = Image.open(file) im = im.resize((32, 32), Image.ANTIALIAS) - im = np.array(im).astype(np.float32).flatten() + im = np.array(im).astype(np.float32) + im = im.transpose((2, 0, 1)) # CHW + im = im[(2, 1, 0),:,:] # BGR + im = im.flatten() im = im / 255.0 return im test_data = [] test_data.append((load_image('image/dog.png'),)) +# users can remove the comments and change the model name +# with gzip.open('params_pass_50.tar.gz', 'r') as f: +# parameters = paddle.parameters.Parameters.from_tar(f) + 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 diff --git a/03.image_classification/index.html b/03.image_classification/index.html index 4001d122..4ab00b51 100644 --- a/03.image_classification/index.html +++ b/03.image_classification/index.html @@ -398,8 +398,7 @@ learning_rate=0.1 / 128.0, learning_rate_decay_a=0.1, learning_rate_decay_b=50000 * 100, - learning_rate_schedule='discexp', - batch_size=128) + learning_rate_schedule='discexp') # Create trainer trainer = paddle.trainer.SGD(cost=cost, @@ -517,7 +516,7 @@ ## 应用模型 -可以使用训练好的模型对图片进行分类,下面程序展示了如何使用`paddle.infer`接口进行推断。 +可以使用训练好的模型对图片进行分类,下面程序展示了如何使用`paddle.infer`接口进行推断,可以打开注释,更改加载的模型。 ```python from PIL import Image @@ -525,12 +524,19 @@ def load_image(file): im = Image.open(file) im = im.resize((32, 32), Image.ANTIALIAS) - im = np.array(im).astype(np.float32).flatten() + im = np.array(im).astype(np.float32) + im = im.transpose((2, 0, 1)) # CHW + im = im[(2, 1, 0),:,:] # BGR + im = im.flatten() im = im / 255.0 return im + test_data = [] test_data.append((load_image('image/dog.png'),)) +# with gzip.open('params_pass_50.tar.gz', 'r') as f: +# parameters = paddle.parameters.Parameters.from_tar(f) + 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 diff --git a/03.image_classification/train.py b/03.image_classification/train.py index e60b9bda..4e86b445 100644 --- a/03.image_classification/train.py +++ b/03.image_classification/train.py @@ -54,8 +54,7 @@ def main(): learning_rate=0.1 / 128.0, learning_rate_decay_a=0.1, learning_rate_decay_b=50000 * 100, - learning_rate_schedule='discexp', - batch_size=128) + learning_rate_schedule='discexp') # End batch and end pass event handler def event_handler(event): @@ -86,7 +85,7 @@ def event_handler(event): paddle.reader.shuffle( paddle.dataset.cifar.train10(), buf_size=50000), batch_size=128), - num_passes=1, + num_passes=200, event_handler=event_handler, feeding={'image': 0, 'label': 1}) @@ -98,13 +97,20 @@ def event_handler(event): def load_image(file): im = Image.open(file) im = im.resize((32, 32), Image.ANTIALIAS) - im = np.array(im).astype(np.float32).flatten() + im = np.array(im).astype(np.float32) + im = im.transpose((2, 0, 1)) # CHW + im = im[(2, 1, 0), :, :] # BGR + im = im.flatten() im = im / 255.0 return im test_data = [] test_data.append((load_image('image/dog.png'), )) + # users can remove the comments and change the model name + # with gzip.open('params_pass_50.tar.gz', 'r') as f: + # parameters = paddle.parameters.Parameters.from_tar(f) + 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 From 92d65ed48888137c7e7e470fac305c88fe679289 Mon Sep 17 00:00:00 2001 From: qingqing01 Date: Tue, 25 Apr 2017 15:05:34 +0800 Subject: [PATCH 2/2] follow comments --- 03.image_classification/README.en.md | 6 ++++++ 03.image_classification/README.md | 4 ++++ 03.image_classification/index.en.html | 6 ++++++ 03.image_classification/index.html | 4 ++++ 03.image_classification/train.py | 6 ++++++ 5 files changed, 26 insertions(+) diff --git a/03.image_classification/README.en.md b/03.image_classification/README.en.md index a39f9021..2ff186d8 100644 --- a/03.image_classification/README.en.md +++ b/03.image_classification/README.en.md @@ -491,7 +491,13 @@ def load_image(file): im = Image.open(file) im = im.resize((32, 32), Image.ANTIALIAS) im = np.array(im).astype(np.float32) + # The storage order of the loaded image is W(widht), + # H(height), C(channel). PaddlePaddle requires + # the CHW order, so transpose them. im = im.transpose((2, 0, 1)) # CHW + # In the training phase, the channel order of CIFAR + # image is B(Blue), G(green), R(Red). But PIL open + # image in RGB mode. It must swap the channel order. im = im[(2, 1, 0),:,:] # BGR im = im.flatten() im = im / 255.0 diff --git a/03.image_classification/README.md b/03.image_classification/README.md index d4769976..ccd0c350 100644 --- a/03.image_classification/README.md +++ b/03.image_classification/README.md @@ -483,7 +483,11 @@ def load_image(file): im = Image.open(file) im = im.resize((32, 32), Image.ANTIALIAS) im = np.array(im).astype(np.float32) + # PIL打开图片存储顺序为H(高度),W(宽度),C(通道)。 + # PaddlePaddle要求数据顺序为CHW,所以需要转换顺序。 im = im.transpose((2, 0, 1)) # CHW + # CIFAR训练图片通道顺序为B(蓝),G(绿),R(红), + # 而PIL打开图片默认通道顺序为RGB,因为需要交换通道。 im = im[(2, 1, 0),:,:] # BGR im = im.flatten() im = im / 255.0 diff --git a/03.image_classification/index.en.html b/03.image_classification/index.en.html index 92c5a531..f20658e7 100644 --- a/03.image_classification/index.en.html +++ b/03.image_classification/index.en.html @@ -533,7 +533,13 @@ im = Image.open(file) im = im.resize((32, 32), Image.ANTIALIAS) im = np.array(im).astype(np.float32) + # The storage order of the loaded image is W(widht), + # H(height), C(channel). PaddlePaddle requires + # the CHW order, so transpose them. im = im.transpose((2, 0, 1)) # CHW + # In the training phase, the channel order of CIFAR + # image is B(Blue), G(green), R(Red). But PIL open + # image in RGB mode. It must swap the channel order. im = im[(2, 1, 0),:,:] # BGR im = im.flatten() im = im / 255.0 diff --git a/03.image_classification/index.html b/03.image_classification/index.html index 4ab00b51..6344f774 100644 --- a/03.image_classification/index.html +++ b/03.image_classification/index.html @@ -525,7 +525,11 @@ im = Image.open(file) im = im.resize((32, 32), Image.ANTIALIAS) im = np.array(im).astype(np.float32) + # PIL打开图片存储顺序为H(高度),W(宽度),C(通道)。 + # PaddlePaddle要求数据顺序为CHW,所以需要转换顺序。 im = im.transpose((2, 0, 1)) # CHW + # CIFAR训练图片通道顺序为B(蓝),G(绿),R(红), + # 而PIL打开图片默认通道顺序为RGB,因为需要交换通道。 im = im[(2, 1, 0),:,:] # BGR im = im.flatten() im = im / 255.0 diff --git a/03.image_classification/train.py b/03.image_classification/train.py index 4e86b445..b2dc6c91 100644 --- a/03.image_classification/train.py +++ b/03.image_classification/train.py @@ -98,7 +98,13 @@ def load_image(file): im = Image.open(file) im = im.resize((32, 32), Image.ANTIALIAS) im = np.array(im).astype(np.float32) + # The storage order of the loaded image is W(widht), + # H(height), C(channel). PaddlePaddle requires + # the CHW order, so transpose them. im = im.transpose((2, 0, 1)) # CHW + # In the training phase, the channel order of CIFAR + # image is B(Blue), G(green), R(Red). But PIL open + # image in RGB mode. It must swap the channel order. im = im[(2, 1, 0), :, :] # BGR im = im.flatten() im = im / 255.0