未验证 提交 53b07468 编写于 作者: L lujun 提交者: GitHub

Merge pull request #646 from junjun315/03-stuff

update to low level api--03 image-classification
...@@ -169,15 +169,7 @@ import paddle.fluid as fluid ...@@ -169,15 +169,7 @@ import paddle.fluid as fluid
import numpy import numpy
import sys import sys
from __future__ import print_function from __future__ import print_function
try:
from paddle.fluid.contrib.trainer import *
from paddle.fluid.contrib.inferencer import *
except ImportError:
print(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib",
file=sys.stderr)
from paddle.fluid.trainer import *
from paddle.fluid.inferencer import *
``` ```
本教程中我们提供了VGG和ResNet两个模型的配置。 本教程中我们提供了VGG和ResNet两个模型的配置。
...@@ -348,19 +340,6 @@ def optimizer_program(): ...@@ -348,19 +340,6 @@ def optimizer_program():
## 训练模型 ## 训练模型
### Trainer 配置
现在,我们需要配置 `Trainer``Trainer` 需要接受训练程序 `train_program`, `place` 和优化器 `optimizer_func`
```python
use_cuda = False
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
trainer = Trainer(
train_func=train_program,
optimizer_func=optimizer_program,
place=place)
```
### Data Feeders 配置 ### Data Feeders 配置
`cifar.train10()` 每次产生一条样本,在完成shuffle和batch之后,作为训练的输入。 `cifar.train10()` 每次产生一条样本,在完成shuffle和batch之后,作为训练的输入。
...@@ -379,50 +358,104 @@ test_reader = paddle.batch( ...@@ -379,50 +358,104 @@ test_reader = paddle.batch(
paddle.dataset.cifar.test10(), batch_size=BATCH_SIZE) paddle.dataset.cifar.test10(), batch_size=BATCH_SIZE)
``` ```
### Event Handler ### Trainer 程序的实现
我们需要为训练过程制定一个main_program, 同样的,还需要为测试程序配置一个test_program。定义训练的 `place` ,并使用先前定义的优化器 `optimizer_func`
可以使用`event_handler`回调函数来观察训练过程,或进行测试等, 该回调函数是`trainer.train`函数里设定。
`event_handler` 用来在训练过程中输出文本日志
```python ```python
params_dirname = "image_classification_resnet.inference.model" use_cuda = False
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
# event handler to track training and testing process feed_order = ['pixel', 'label']
def event_handler(event):
if isinstance(event, EndStepEvent): main_program = fluid.default_main_program()
if event.step % 100 == 0: star_program = fluid.default_startup_program()
print("\nPass %d, Batch %d, Cost %f, Acc %f" %
(event.step, event.epoch, event.metrics[0], predict = inference_program()
event.metrics[1])) avg_cost, acc = train_program(predict)
else:
sys.stdout.write('.') # Test program
sys.stdout.flush() test_program = main_program.clone(for_test=True)
optimizer = optimizer_program()
optimizer.minimize(avg_cost)
exe = fluid.Executor(place)
EPOCH_NUM = 2
# For training test cost
def train_test(program, reader):
count = 0
feed_var_list = [
program.global_block().var(var_name) for var_name in feed_order
]
feeder_test = fluid.DataFeeder(
feed_list=feed_var_list, place=place)
test_exe = fluid.Executor(place)
accumulated = len([avg_cost, acc]) * [0]
for tid, test_data in enumerate(reader()):
avg_cost_np = test_exe.run(program=program,
feed=feeder_test.feed(test_data),
fetch_list=[avg_cost, acc])
accumulated = [x[0] + x[1][0] for x in zip(accumulated, avg_cost_np)]
count += 1
return [x / count for x in accumulated]
```
if isinstance(event, EndEpochEvent): ### 训练主循环以及过程输出
# Test against with the test dataset to get accuracy.
avg_cost, accuracy = trainer.test(
reader=test_reader, feed_order=['pixel', 'label'])
print('\nTest with Pass {0}, Loss {1:2.2}, Acc {2:2.2}'.format(event.epoch, avg_cost, accuracy)) 在接下来的主训练循环中,我们将通过输出来来观察训练过程,或进行测试等。
也可以使用`plot`, 利用回调数据来打点画图:
```python
params_dirname = "image_classification_resnet.inference.model"
from paddle.utils.plot import Ploter
train_prompt = "Train cost"
test_prompt = "Test cost"
plot_cost = Ploter(test_prompt,train_prompt)
# main train loop.
def train_loop():
feed_var_list_loop = [
main_program.global_block().var(var_name) for var_name in feed_order
]
feeder = fluid.DataFeeder(
feed_list=feed_var_list_loop, place=place)
exe.run(star_program)
step = 0
for pass_id in range(EPOCH_NUM):
for step_id, data_train in enumerate(train_reader()):
avg_loss_value = exe.run(main_program,
feed=feeder.feed(data_train),
fetch_list=[avg_cost, acc])
if step % 1 == 0:
plot_cost.append(train_prompt, step, avg_loss_value[0])
plot_cost.plot()
step += 1
avg_cost_test, accuracy_test = train_test(test_program,
reader=test_reader)
plot_cost.append(test_prompt, step, avg_cost_test)
# save parameters # save parameters
if params_dirname is not None: if params_dirname is not None:
trainer.save_params(params_dirname) fluid.io.save_inference_model(params_dirname, ["pixel"],
[predict], exe)
``` ```
### 训练 ### 训练
通过`trainer.train`函数训练: 通过`trainer_loop`函数训练, 这里我们只进行了2个Epoch, 一般我们在实际应用上会执行上百个以上Epoch
**注意:** CPU,每个 Epoch 将花费大约15~20分钟。这部分可能需要一段时间。请随意修改代码,在GPU上运行测试,以提高训练速度。 **注意:** CPU,每个 Epoch 将花费大约15~20分钟。这部分可能需要一段时间。请随意修改代码,在GPU上运行测试,以提高训练速度。
```python ```python
trainer.train( train_loop()
reader=train_reader,
num_epochs=2,
event_handler=event_handler,
feed_order=['pixel', 'label'])
``` ```
一轮训练log示例如下所示,经过1个pass, 训练集上平均 Accuracy 为0.59 ,测试集上平均 Accuracy 为0.6 。 一轮训练log示例如下所示,经过1个pass, 训练集上平均 Accuracy 为0.59 ,测试集上平均 Accuracy 为0.6 。
...@@ -448,23 +481,22 @@ Test with Pass 0, Loss 1.1, Acc 0.6 ...@@ -448,23 +481,22 @@ Test with Pass 0, Loss 1.1, Acc 0.6
## 应用模型 ## 应用模型
可以使用训练好的模型对图片进行分类,下面程序展示了如何使用 `fluid.contrib.inferencer.Inferencer` 接口进行推断,可以打开注释,更改加载的模型 可以使用训练好的模型对图片进行分类,下面程序展示了如何加载已经训练好的网络和参数进行推断
### 生成预测输入数据 ### 生成预测输入数据
`dog.png` is an example image of a dog. Turn it into an numpy array to match the data feeder format. `dog.png` 是一张小狗的图片. 我们将它转换成 `numpy` 数组以满足`feeder`的格式.
```python ```python
# Prepare testing data. # Prepare testing data.
from PIL import Image from PIL import Image
import numpy as np
import os import os
def load_image(file): def load_image(file):
im = Image.open(file) im = Image.open(file)
im = im.resize((32, 32), Image.ANTIALIAS) im = im.resize((32, 32), Image.ANTIALIAS)
im = np.array(im).astype(np.float32) im = numpy.array(im).astype(numpy.float32)
# The storage order of the loaded image is W(width), # The storage order of the loaded image is W(width),
# H(height), C(channel). PaddlePaddle requires # H(height), C(channel). PaddlePaddle requires
# the CHW order, so transpose them. # the CHW order, so transpose them.
...@@ -481,17 +513,48 @@ img = load_image(cur_dir + '/image/dog.png') ...@@ -481,17 +513,48 @@ img = load_image(cur_dir + '/image/dog.png')
### Inferencer 配置和预测 ### Inferencer 配置和预测
`Inferencer` 需要一个 `infer_func``param_path` 来设置网络和经过训练的参数。 与训练过程类似,inferencer需要构建相应的过程。我们从`params_dirname` 加载网络和经过训练的参数。
我们可以简单地插入前面定义的推理程序。 我们可以简单地插入前面定义的推理程序。
现在我们准备做预测。 现在我们准备做预测。
```python ```python
inferencer = Inferencer( place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
infer_func=inference_program, param_path=params_dirname, place=place) exe = fluid.Executor(place)
label_list = ["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"] inference_scope = fluid.core.Scope()
# inference
results = inferencer.infer({'pixel': img}) with fluid.scope_guard(inference_scope):
print("infer results: %s" % label_list[np.argmax(results[0])])
[inference_program, feed_target_names,
fetch_targets] = fluid.io.load_inference_model(params_dirname, exe)
# The input's dimension of conv should be 4-D or 5-D.
# Use inference_transpiler to speedup
inference_transpiler_program = inference_program.clone()
t = fluid.transpiler.InferenceTranspiler()
t.transpile(inference_transpiler_program, place)
# Construct feed as a dictionary of {feed_target_name: feed_target_data}
# and results will contain a list of data corresponding to fetch_targets.
results = exe.run(inference_program,
feed={feed_target_names[0]: img},
fetch_list=fetch_targets)
transpiler_results = exe.run(inference_transpiler_program,
feed={feed_target_names[0]: img},
fetch_list=fetch_targets)
assert len(results[0]) == len(transpiler_results[0])
for i in range(len(results[0])):
numpy.testing.assert_almost_equal(
results[0][i], transpiler_results[0][i], decimal=5)
# infer label
label_list = [
"airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse",
"ship", "truck"
]
print("infer results: %s" % label_list[numpy.argmax(results[0])])
``` ```
## 总结 ## 总结
......
...@@ -211,15 +211,7 @@ import paddle.fluid as fluid ...@@ -211,15 +211,7 @@ import paddle.fluid as fluid
import numpy import numpy
import sys import sys
from __future__ import print_function from __future__ import print_function
try:
from paddle.fluid.contrib.trainer import *
from paddle.fluid.contrib.inferencer import *
except ImportError:
print(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib",
file=sys.stderr)
from paddle.fluid.trainer import *
from paddle.fluid.inferencer import *
``` ```
本教程中我们提供了VGG和ResNet两个模型的配置。 本教程中我们提供了VGG和ResNet两个模型的配置。
...@@ -390,19 +382,6 @@ def optimizer_program(): ...@@ -390,19 +382,6 @@ def optimizer_program():
## 训练模型 ## 训练模型
### Trainer 配置
现在,我们需要配置 `Trainer`。`Trainer` 需要接受训练程序 `train_program`, `place` 和优化器 `optimizer_func`。
```python
use_cuda = False
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
trainer = Trainer(
train_func=train_program,
optimizer_func=optimizer_program,
place=place)
```
### Data Feeders 配置 ### Data Feeders 配置
`cifar.train10()` 每次产生一条样本,在完成shuffle和batch之后,作为训练的输入。 `cifar.train10()` 每次产生一条样本,在完成shuffle和batch之后,作为训练的输入。
...@@ -421,50 +400,104 @@ test_reader = paddle.batch( ...@@ -421,50 +400,104 @@ test_reader = paddle.batch(
paddle.dataset.cifar.test10(), batch_size=BATCH_SIZE) paddle.dataset.cifar.test10(), batch_size=BATCH_SIZE)
``` ```
### Event Handler ### Trainer 程序的实现
我们需要为训练过程制定一个main_program, 同样的,还需要为测试程序配置一个test_program。定义训练的 `place` ,并使用先前定义的优化器 `optimizer_func`。
可以使用`event_handler`回调函数来观察训练过程,或进行测试等, 该回调函数是`trainer.train`函数里设定。
`event_handler` 用来在训练过程中输出文本日志
```python ```python
params_dirname = "image_classification_resnet.inference.model" use_cuda = False
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
# event handler to track training and testing process feed_order = ['pixel', 'label']
def event_handler(event):
if isinstance(event, EndStepEvent): main_program = fluid.default_main_program()
if event.step % 100 == 0: star_program = fluid.default_startup_program()
print("\nPass %d, Batch %d, Cost %f, Acc %f" %
(event.step, event.epoch, event.metrics[0], predict = inference_program()
event.metrics[1])) avg_cost, acc = train_program(predict)
else:
sys.stdout.write('.') # Test program
sys.stdout.flush() test_program = main_program.clone(for_test=True)
optimizer = optimizer_program()
optimizer.minimize(avg_cost)
exe = fluid.Executor(place)
EPOCH_NUM = 2
# For training test cost
def train_test(program, reader):
count = 0
feed_var_list = [
program.global_block().var(var_name) for var_name in feed_order
]
feeder_test = fluid.DataFeeder(
feed_list=feed_var_list, place=place)
test_exe = fluid.Executor(place)
accumulated = len([avg_cost, acc]) * [0]
for tid, test_data in enumerate(reader()):
avg_cost_np = test_exe.run(program=program,
feed=feeder_test.feed(test_data),
fetch_list=[avg_cost, acc])
accumulated = [x[0] + x[1][0] for x in zip(accumulated, avg_cost_np)]
count += 1
return [x / count for x in accumulated]
```
if isinstance(event, EndEpochEvent): ### 训练主循环以及过程输出
# Test against with the test dataset to get accuracy.
avg_cost, accuracy = trainer.test(
reader=test_reader, feed_order=['pixel', 'label'])
print('\nTest with Pass {0}, Loss {1:2.2}, Acc {2:2.2}'.format(event.epoch, avg_cost, accuracy)) 在接下来的主训练循环中,我们将通过输出来来观察训练过程,或进行测试等。
也可以使用`plot`, 利用回调数据来打点画图:
```python
params_dirname = "image_classification_resnet.inference.model"
from paddle.utils.plot import Ploter
train_prompt = "Train cost"
test_prompt = "Test cost"
plot_cost = Ploter(test_prompt,train_prompt)
# main train loop.
def train_loop():
feed_var_list_loop = [
main_program.global_block().var(var_name) for var_name in feed_order
]
feeder = fluid.DataFeeder(
feed_list=feed_var_list_loop, place=place)
exe.run(star_program)
step = 0
for pass_id in range(EPOCH_NUM):
for step_id, data_train in enumerate(train_reader()):
avg_loss_value = exe.run(main_program,
feed=feeder.feed(data_train),
fetch_list=[avg_cost, acc])
if step % 1 == 0:
plot_cost.append(train_prompt, step, avg_loss_value[0])
plot_cost.plot()
step += 1
avg_cost_test, accuracy_test = train_test(test_program,
reader=test_reader)
plot_cost.append(test_prompt, step, avg_cost_test)
# save parameters # save parameters
if params_dirname is not None: if params_dirname is not None:
trainer.save_params(params_dirname) fluid.io.save_inference_model(params_dirname, ["pixel"],
[predict], exe)
``` ```
### 训练 ### 训练
通过`trainer.train`函数训练: 通过`trainer_loop`函数训练, 这里我们只进行了2个Epoch, 一般我们在实际应用上会执行上百个以上Epoch
**注意:** CPU,每个 Epoch 将花费大约15~20分钟。这部分可能需要一段时间。请随意修改代码,在GPU上运行测试,以提高训练速度。 **注意:** CPU,每个 Epoch 将花费大约15~20分钟。这部分可能需要一段时间。请随意修改代码,在GPU上运行测试,以提高训练速度。
```python ```python
trainer.train( train_loop()
reader=train_reader,
num_epochs=2,
event_handler=event_handler,
feed_order=['pixel', 'label'])
``` ```
一轮训练log示例如下所示,经过1个pass, 训练集上平均 Accuracy 为0.59 ,测试集上平均 Accuracy 为0.6 。 一轮训练log示例如下所示,经过1个pass, 训练集上平均 Accuracy 为0.59 ,测试集上平均 Accuracy 为0.6 。
...@@ -490,23 +523,22 @@ Test with Pass 0, Loss 1.1, Acc 0.6 ...@@ -490,23 +523,22 @@ Test with Pass 0, Loss 1.1, Acc 0.6
## 应用模型 ## 应用模型
可以使用训练好的模型对图片进行分类,下面程序展示了如何使用 `fluid.contrib.inferencer.Inferencer` 接口进行推断,可以打开注释,更改加载的模型 可以使用训练好的模型对图片进行分类,下面程序展示了如何加载已经训练好的网络和参数进行推断
### 生成预测输入数据 ### 生成预测输入数据
`dog.png` is an example image of a dog. Turn it into an numpy array to match the data feeder format. `dog.png` 是一张小狗的图片. 我们将它转换成 `numpy` 数组以满足`feeder`的格式.
```python ```python
# Prepare testing data. # Prepare testing data.
from PIL import Image from PIL import Image
import numpy as np
import os import os
def load_image(file): def load_image(file):
im = Image.open(file) im = Image.open(file)
im = im.resize((32, 32), Image.ANTIALIAS) im = im.resize((32, 32), Image.ANTIALIAS)
im = np.array(im).astype(np.float32) im = numpy.array(im).astype(numpy.float32)
# The storage order of the loaded image is W(width), # The storage order of the loaded image is W(width),
# H(height), C(channel). PaddlePaddle requires # H(height), C(channel). PaddlePaddle requires
# the CHW order, so transpose them. # the CHW order, so transpose them.
...@@ -523,17 +555,48 @@ img = load_image(cur_dir + '/image/dog.png') ...@@ -523,17 +555,48 @@ img = load_image(cur_dir + '/image/dog.png')
### Inferencer 配置和预测 ### Inferencer 配置和预测
`Inferencer` 需要一个 `infer_func` 和 `param_path` 来设置网络和经过训练的参数。 与训练过程类似,inferencer需要构建相应的过程。我们从`params_dirname` 加载网络和经过训练的参数。
我们可以简单地插入前面定义的推理程序。 我们可以简单地插入前面定义的推理程序。
现在我们准备做预测。 现在我们准备做预测。
```python ```python
inferencer = Inferencer( place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
infer_func=inference_program, param_path=params_dirname, place=place) exe = fluid.Executor(place)
label_list = ["airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", "ship", "truck"] inference_scope = fluid.core.Scope()
# inference
results = inferencer.infer({'pixel': img}) with fluid.scope_guard(inference_scope):
print("infer results: %s" % label_list[np.argmax(results[0])])
[inference_program, feed_target_names,
fetch_targets] = fluid.io.load_inference_model(params_dirname, exe)
# The input's dimension of conv should be 4-D or 5-D.
# Use inference_transpiler to speedup
inference_transpiler_program = inference_program.clone()
t = fluid.transpiler.InferenceTranspiler()
t.transpile(inference_transpiler_program, place)
# Construct feed as a dictionary of {feed_target_name: feed_target_data}
# and results will contain a list of data corresponding to fetch_targets.
results = exe.run(inference_program,
feed={feed_target_names[0]: img},
fetch_list=fetch_targets)
transpiler_results = exe.run(inference_transpiler_program,
feed={feed_target_names[0]: img},
fetch_list=fetch_targets)
assert len(results[0]) == len(transpiler_results[0])
for i in range(len(results[0])):
numpy.testing.assert_almost_equal(
results[0][i], transpiler_results[0][i], decimal=5)
# infer label
label_list = [
"airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse",
"ship", "truck"
]
print("infer results: %s" % label_list[numpy.argmax(results[0])])
``` ```
## 总结 ## 总结
......
...@@ -15,17 +15,6 @@ ...@@ -15,17 +15,6 @@
from __future__ import print_function from __future__ import print_function
import paddle.fluid as fluid import paddle.fluid as fluid
import sys
try:
from paddle.fluid.contrib.trainer import *
from paddle.fluid.contrib.inferencer import *
except ImportError:
print(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib",
file=sys.stderr)
from paddle.fluid.trainer import *
from paddle.fluid.inferencer import *
__all__ = ['resnet_cifar10'] __all__ = ['resnet_cifar10']
......
...@@ -14,21 +14,11 @@ ...@@ -14,21 +14,11 @@
from __future__ import print_function from __future__ import print_function
import os
import paddle import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import numpy import numpy
import sys import sys
try:
from paddle.fluid.contrib.trainer import *
from paddle.fluid.contrib.inferencer import *
except ImportError:
print(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib",
file=sys.stderr)
from paddle.fluid.trainer import *
from paddle.fluid.inferencer import *
from vgg import vgg_bn_drop from vgg import vgg_bn_drop
from resnet import resnet_cifar10 from resnet import resnet_cifar10
...@@ -43,8 +33,7 @@ def inference_network(): ...@@ -43,8 +33,7 @@ def inference_network():
return predict return predict
def train_network(): def train_network(predict):
predict = inference_network()
label = fluid.layers.data(name='label', shape=[1], dtype='int64') label = fluid.layers.data(name='label', shape=[1], dtype='int64')
cost = fluid.layers.cross_entropy(input=predict, label=label) cost = fluid.layers.cross_entropy(input=predict, label=label)
avg_cost = fluid.layers.mean(cost) avg_cost = fluid.layers.mean(cost)
...@@ -56,62 +45,101 @@ def optimizer_program(): ...@@ -56,62 +45,101 @@ def optimizer_program():
return fluid.optimizer.Adam(learning_rate=0.001) return fluid.optimizer.Adam(learning_rate=0.001)
def train(use_cuda, train_program, params_dirname): def train(use_cuda, params_dirname):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
BATCH_SIZE = 128 BATCH_SIZE = 128
EPOCH_NUM = 2
train_reader = paddle.batch( train_reader = paddle.batch(
paddle.reader.shuffle(paddle.dataset.cifar.train10(), buf_size=50000), paddle.reader.shuffle(
paddle.dataset.cifar.train10(), buf_size=128 * 100),
batch_size=BATCH_SIZE) batch_size=BATCH_SIZE)
test_reader = paddle.batch( test_reader = paddle.batch(
paddle.dataset.cifar.test10(), batch_size=BATCH_SIZE) paddle.dataset.cifar.test10(), batch_size=BATCH_SIZE)
def event_handler(event): feed_order = ['pixel', 'label']
if isinstance(event, EndStepEvent):
if event.step % 100 == 0:
print("\nPass %d, Batch %d, Cost %f, Acc %f" %
(event.step, event.epoch, event.metrics[0],
event.metrics[1]))
else:
sys.stdout.write('.')
sys.stdout.flush()
if isinstance(event, EndEpochEvent): main_program = fluid.default_main_program()
avg_cost, accuracy = trainer.test( star_program = fluid.default_startup_program()
reader=test_reader, feed_order=['pixel', 'label'])
predict = inference_network()
avg_cost, acc = train_network(predict)
# Test program
test_program = main_program.clone(for_test=True)
optimizer = optimizer_program()
optimizer.minimize(avg_cost)
exe = fluid.Executor(place)
EPOCH_NUM = 1
# For training test cost
def train_test(program, reader):
count = 0
feed_var_list = [
program.global_block().var(var_name) for var_name in feed_order
]
feeder_test = fluid.DataFeeder(feed_list=feed_var_list, place=place)
test_exe = fluid.Executor(place)
accumulated = len([avg_cost, acc]) * [0]
for tid, test_data in enumerate(reader()):
avg_cost_np = test_exe.run(
program=program,
feed=feeder_test.feed(test_data),
fetch_list=[avg_cost, acc])
accumulated = [
x[0] + x[1][0] for x in zip(accumulated, avg_cost_np)
]
count += 1
return [x / count for x in accumulated]
# main train loop.
def train_loop():
feed_var_list_loop = [
main_program.global_block().var(var_name) for var_name in feed_order
]
feeder = fluid.DataFeeder(feed_list=feed_var_list_loop, place=place)
exe.run(star_program)
step = 0
for pass_id in range(EPOCH_NUM):
for step_id, data_train in enumerate(train_reader()):
avg_loss_value = exe.run(
main_program,
feed=feeder.feed(data_train),
fetch_list=[avg_cost, acc])
if step_id % 100 == 0:
print("\nPass %d, Batch %d, Cost %f, Acc %f" % (
step_id, pass_id, avg_loss_value[0], avg_loss_value[1]))
else:
sys.stdout.write('.')
sys.stdout.flush()
step += 1
avg_cost_test, accuracy_test = train_test(
test_program, reader=test_reader)
print('\nTest with Pass {0}, Loss {1:2.2}, Acc {2:2.2}'.format( print('\nTest with Pass {0}, Loss {1:2.2}, Acc {2:2.2}'.format(
event.epoch, avg_cost, accuracy)) pass_id, avg_cost_test, accuracy_test))
if params_dirname is not None:
trainer.save_params(params_dirname)
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace() if params_dirname is not None:
trainer = Trainer( fluid.io.save_inference_model(params_dirname, ["pixel"],
train_func=train_program, optimizer_func=optimizer_program, place=place) [predict], exe)
trainer.train(
reader=train_reader,
num_epochs=EPOCH_NUM,
event_handler=event_handler,
feed_order=['pixel', 'label'])
train_loop()
def infer(use_cuda, inference_program, params_dirname=None):
place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
inferencer = Inferencer(
infer_func=inference_program, param_path=params_dirname, place=place)
# Prepare testing data. def infer(use_cuda, params_dirname=None):
from PIL import Image from PIL import Image
import numpy as np place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
import os exe = fluid.Executor(place)
inference_scope = fluid.core.Scope()
def load_image(file): def load_image(infer_file):
im = Image.open(file) im = Image.open(infer_file)
im = im.resize((32, 32), Image.ANTIALIAS) im = im.resize((32, 32), Image.ANTIALIAS)
im = np.array(im).astype(np.float32) im = numpy.array(im).astype(numpy.float32)
# The storage order of the loaded image is W(width), # The storage order of the loaded image is W(width),
# H(height), C(channel). PaddlePaddle requires # H(height), C(channel). PaddlePaddle requires
# the CHW order, so transpose them. # the CHW order, so transpose them.
...@@ -125,14 +153,44 @@ def infer(use_cuda, inference_program, params_dirname=None): ...@@ -125,14 +153,44 @@ def infer(use_cuda, inference_program, params_dirname=None):
cur_dir = os.path.dirname(os.path.realpath(__file__)) cur_dir = os.path.dirname(os.path.realpath(__file__))
img = load_image(cur_dir + '/image/dog.png') img = load_image(cur_dir + '/image/dog.png')
# inference with fluid.scope_guard(inference_scope):
results = inferencer.infer({'pixel': img}) # Use fluid.io.load_inference_model to obtain the inference program desc,
# the feed_target_names (the names of variables that will be feeded
label_list = [ # data using feed operators), and the fetch_targets (variables that
"airplane", "automobile", "bird", "cat", "deer", "dog", "frog", "horse", # we want to obtain data from using fetch operators).
"ship", "truck" [inference_program, feed_target_names,
] fetch_targets] = fluid.io.load_inference_model(params_dirname, exe)
print("infer results: %s" % label_list[np.argmax(results[0])])
# The input's dimension of conv should be 4-D or 5-D.
# Use inference_transpiler to speedup
inference_transpiler_program = inference_program.clone()
t = fluid.transpiler.InferenceTranspiler()
t.transpile(inference_transpiler_program, place)
# Construct feed as a dictionary of {feed_target_name: feed_target_data}
# and results will contain a list of data corresponding to fetch_targets.
results = exe.run(
inference_program,
feed={feed_target_names[0]: img},
fetch_list=fetch_targets)
transpiler_results = exe.run(
inference_transpiler_program,
feed={feed_target_names[0]: img},
fetch_list=fetch_targets)
assert len(results[0]) == len(transpiler_results[0])
for i in range(len(results[0])):
numpy.testing.assert_almost_equal(
results[0][i], transpiler_results[0][i], decimal=5)
# infer label
label_list = [
"airplane", "automobile", "bird", "cat", "deer", "dog", "frog",
"horse", "ship", "truck"
]
print("infer results: %s" % label_list[numpy.argmax(results[0])])
def main(use_cuda): def main(use_cuda):
...@@ -140,15 +198,9 @@ def main(use_cuda): ...@@ -140,15 +198,9 @@ def main(use_cuda):
return return
save_path = "image_classification_resnet.inference.model" save_path = "image_classification_resnet.inference.model"
train( train(use_cuda=use_cuda, params_dirname=save_path)
use_cuda=use_cuda,
train_program=train_network,
params_dirname=save_path)
infer( infer(use_cuda=use_cuda, params_dirname=save_path)
use_cuda=use_cuda,
inference_program=inference_network,
params_dirname=save_path)
if __name__ == '__main__': if __name__ == '__main__':
......
...@@ -14,21 +14,7 @@ ...@@ -14,21 +14,7 @@
from __future__ import print_function from __future__ import print_function
import paddle
import paddle.fluid as fluid import paddle.fluid as fluid
import sys
try:
from paddle.fluid.contrib.trainer import *
from paddle.fluid.contrib.inferencer import *
except ImportError:
print(
"In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib",
file=sys.stderr)
from paddle.fluid.trainer import *
from paddle.fluid.inferencer import *
__all__ = ['vgg_bn_drop']
def vgg_bn_drop(input): def vgg_bn_drop(input):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册