Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

fix bug in 03.image_classification #290

Merged
merged 2 commits into from
Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions 03.image_classification/README.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -483,20 +482,33 @@ 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
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)
# 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 = []
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
Expand Down
18 changes: 14 additions & 4 deletions 03.image_classification/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -475,20 +474,31 @@ Test with Pass 0, {'classification_error_evaluator': 0.885200023651123}

## 应用模型

可以使用训练好的模型对图片进行分类,下面程序展示了如何使用`paddle.infer`接口进行推断。
可以使用训练好的模型对图片进行分类,下面程序展示了如何使用`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 = 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 = []
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
Expand Down
20 changes: 16 additions & 4 deletions 03.image_classification/index.en.html
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -525,20 +524,33 @@

## 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
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)
# 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 = []
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
Expand Down
18 changes: 14 additions & 4 deletions 03.image_classification/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -517,20 +516,31 @@

## 应用模型

可以使用训练好的模型对图片进行分类,下面程序展示了如何使用`paddle.infer`接口进行推断。
可以使用训练好的模型对图片进行分类,下面程序展示了如何使用`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 = 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 = []
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
Expand Down
20 changes: 16 additions & 4 deletions 03.image_classification/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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})
Expand All @@ -98,13 +97,26 @@ 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)
# 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 = []
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
Expand Down