diff --git a/03.image_classification/README.en.md b/03.image_classification/README.en.md index 93391af3e5999fa22e8cc55bf7e6f40d76874c21..07d5e319b0ce5c3cb38db18826d5efcba2603bfd 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 @@ -492,13 +491,26 @@ import os 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) + # 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 return im test_data = [] cur_dir = os.path.dirname(os.path.realpath(__file__)) test_data.append((load_image(cur_dir + '/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 96891b15fa702bf673b90667516bf2c93807c181..31d29d9bb23beaf2b1a383501dff31053cca1d9b 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 @@ -484,13 +483,24 @@ import os 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) + # 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 return im + test_data = [] cur_dir = os.path.dirname(os.path.realpath(__file__)) test_data.append((load_image(cur_dir + '/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 b737fb74f3e41c01d23b247a0170a1d2241d92fd..ad5b9053180f32c2901d9b28d6cd62149412c353 100644 --- a/03.image_classification/index.en.html +++ b/03.image_classification/index.en.html @@ -406,8 +406,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, @@ -525,7 +524,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 @@ -534,13 +533,26 @@ import os 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) + # 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 return im test_data = [] cur_dir = os.path.dirname(os.path.realpath(__file__)) test_data.append((load_image(cur_dir + '/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 25373fbdb682901987afd3e37d19900234acb4fe..1bb2b96102c306a0123641c8bab41a67dfad4d8d 100644 --- a/03.image_classification/index.html +++ b/03.image_classification/index.html @@ -398,8 +398,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, @@ -517,7 +516,7 @@ Test with Pass 0, {'classification_error_evaluator': 0.885200023651123} ## 应用模型 -可以使用训练好的模型对图片进行分类,下面程序展示了如何使用`paddle.infer`接口进行推断。 +可以使用训练好的模型对图片进行分类,下面程序展示了如何使用`paddle.infer`接口进行推断,可以打开注释,更改加载的模型。 ```python from PIL import Image @@ -526,13 +525,24 @@ import os 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) + # 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 return im + test_data = [] cur_dir = os.path.dirname(os.path.realpath(__file__)) test_data.append((load_image(cur_dir + '/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 9ec8237c074651c350bbc87cdfba8ab518fed30c..57ec9822cde7bf11a9ebfba5c4fe4ccc4dfde7ef 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 main(): 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}) @@ -99,7 +98,16 @@ def main(): 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) + # 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 return im @@ -107,6 +115,10 @@ def main(): cur_dir = os.path.dirname(os.path.realpath(__file__)) test_data.append((load_image(cur_dir + '/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