提交 08e3e11e 编写于 作者: W Wang,Jeff

Make the inferencer part easier to read.

上级 106e6ae1
...@@ -340,7 +340,10 @@ def event_handler_plot(event): ...@@ -340,7 +340,10 @@ def event_handler_plot(event):
lists.append((event.epoch, avg_cost, acc)) lists.append((event.epoch, avg_cost, acc))
``` ```
#### Start training
Now that we setup the event_handler and the reader, we can start training the model. `feed_order` is used to map the data dict to the train_program Now that we setup the event_handler and the reader, we can start training the model. `feed_order` is used to map the data dict to the train_program
```python ```python
# Train the model now # Train the model now
trainer.train( trainer.train(
...@@ -381,6 +384,24 @@ Usually, with MNIST data, the softmax regression model achieves an accuracy arou ...@@ -381,6 +384,24 @@ Usually, with MNIST data, the softmax regression model achieves an accuracy arou
After training, users can use the trained model to classify images. The following code shows how to inference MNIST images through `fluid.Inferencer`. After training, users can use the trained model to classify images. The following code shows how to inference MNIST images through `fluid.Inferencer`.
### Create Inferencer
The `Inferencer` takes an `infer_func` and `param_path` to setup the network and the trained parameters.
We can simply plug-in the classifier defined earlier here.
```python
inferencer = fluid.Inferencer(
# infer_func=softmax_regression, # uncomment for softmax regression
# infer_func=multilayer_perceptron, # uncomment for MLP
infer_func=convolutional_neural_network, # uncomment for LeNet5
param_path=params_dirname,
place=place)
```
#### Generate input data for inferring
`infer_3.png` is an example image of the digit `3`. Turn it into an numpy array to match the data feeder format.
```python ```python
# Prepare the test image # Prepare the test image
import os import os
...@@ -395,15 +416,13 @@ def load_image(file): ...@@ -395,15 +416,13 @@ def load_image(file):
cur_dir = cur_dir = os.getcwd() cur_dir = cur_dir = os.getcwd()
img = load_image(cur_dir + '/image/infer_3.png') img = load_image(cur_dir + '/image/infer_3.png')
```
### Inference
inferencer = fluid.Inferencer( Now we are ready to do inference.
# infer_func=softmax_regression, # uncomment for softmax regression
# infer_func=multilayer_perceptron, # uncomment for MLP
infer_func=convolutional_neural_network, # uncomment for LeNet5
param_path=params_dirname,
place=place)
```python
results = inferencer.infer({'img': img}) results = inferencer.infer({'img': img})
lab = np.argsort(results) # probs and lab are the results of one batch data lab = np.argsort(results) # probs and lab are the results of one batch data
print "Label of image/infer_3.png is: %d" % lab[0][0][-1] print "Label of image/infer_3.png is: %d" % lab[0][0][-1]
......
...@@ -382,7 +382,10 @@ def event_handler_plot(event): ...@@ -382,7 +382,10 @@ def event_handler_plot(event):
lists.append((event.epoch, avg_cost, acc)) lists.append((event.epoch, avg_cost, acc))
``` ```
#### Start training
Now that we setup the event_handler and the reader, we can start training the model. `feed_order` is used to map the data dict to the train_program Now that we setup the event_handler and the reader, we can start training the model. `feed_order` is used to map the data dict to the train_program
```python ```python
# Train the model now # Train the model now
trainer.train( trainer.train(
...@@ -423,6 +426,24 @@ Usually, with MNIST data, the softmax regression model achieves an accuracy arou ...@@ -423,6 +426,24 @@ Usually, with MNIST data, the softmax regression model achieves an accuracy arou
After training, users can use the trained model to classify images. The following code shows how to inference MNIST images through `fluid.Inferencer`. After training, users can use the trained model to classify images. The following code shows how to inference MNIST images through `fluid.Inferencer`.
### Create Inferencer
The `Inferencer` takes an `infer_func` and `param_path` to setup the network and the trained parameters.
We can simply plug-in the classifier defined earlier here.
```python
inferencer = fluid.Inferencer(
# infer_func=softmax_regression, # uncomment for softmax regression
# infer_func=multilayer_perceptron, # uncomment for MLP
infer_func=convolutional_neural_network, # uncomment for LeNet5
param_path=params_dirname,
place=place)
```
#### Generate input data for inferring
`infer_3.png` is an example image of the digit `3`. Turn it into an numpy array to match the data feeder format.
```python ```python
# Prepare the test image # Prepare the test image
import os import os
...@@ -437,15 +458,13 @@ def load_image(file): ...@@ -437,15 +458,13 @@ def load_image(file):
cur_dir = cur_dir = os.getcwd() cur_dir = cur_dir = os.getcwd()
img = load_image(cur_dir + '/image/infer_3.png') img = load_image(cur_dir + '/image/infer_3.png')
```
### Inference
inferencer = fluid.Inferencer( Now we are ready to do inference.
# infer_func=softmax_regression, # uncomment for softmax regression
# infer_func=multilayer_perceptron, # uncomment for MLP
infer_func=convolutional_neural_network, # uncomment for LeNet5
param_path=params_dirname,
place=place)
```python
results = inferencer.infer({'img': img}) results = inferencer.infer({'img': img})
lab = np.argsort(results) # probs and lab are the results of one batch data lab = np.argsort(results) # probs and lab are the results of one batch data
print "Label of image/infer_3.png is: %d" % lab[0][0][-1] print "Label of image/infer_3.png is: %d" % lab[0][0][-1]
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册