train.py 5.4 KB
Newer Older
J
JiabinYang 已提交
1
from __future__ import print_function
L
liaogang 已提交
2
import os
L
liaogang 已提交
3 4
from PIL import Image
import numpy as np
W
Wang,Jeff 已提交
5 6
import paddle
import paddle.fluid as fluid
L
Luo Tao 已提交
7

Y
yuyang 已提交
8 9 10 11 12 13 14 15 16 17
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 *

L
Luo Tao 已提交
18

W
Wang,Jeff 已提交
19 20
def softmax_regression():
    img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32')
W
Wang,Jeff 已提交
21
    predict = fluid.layers.fc(input=img, size=10, act='softmax')
L
Luo Tao 已提交
22 23 24
    return predict


W
Wang,Jeff 已提交
25 26 27 28 29 30
def multilayer_perceptron():
    img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32')
    # first fully-connected layer, using ReLu as its activation function
    hidden = fluid.layers.fc(input=img, size=128, act='relu')
    # second fully-connected layer, using ReLu as its activation function
    hidden = fluid.layers.fc(input=hidden, size=64, act='relu')
L
Luo Tao 已提交
31 32
    # The thrid fully-connected layer, note that the hidden size should be 10,
    # which is the number of unique digits
W
Wang,Jeff 已提交
33 34
    prediction = fluid.layers.fc(input=hidden, size=10, act='softmax')
    return prediction
L
Luo Tao 已提交
35 36


W
Wang,Jeff 已提交
37 38 39 40
def convolutional_neural_network():
    img = fluid.layers.data(name='img', shape=[1, 28, 28], dtype='float32')
    # first conv pool
    conv_pool_1 = fluid.nets.simple_img_conv_pool(
L
Luo Tao 已提交
41 42 43 44 45
        input=img,
        filter_size=5,
        num_filters=20,
        pool_size=2,
        pool_stride=2,
W
Wang,Jeff 已提交
46 47 48 49
        act="relu")
    conv_pool_1 = fluid.layers.batch_norm(conv_pool_1)
    # second conv pool
    conv_pool_2 = fluid.nets.simple_img_conv_pool(
L
Luo Tao 已提交
50 51 52 53 54
        input=conv_pool_1,
        filter_size=5,
        num_filters=50,
        pool_size=2,
        pool_stride=2,
W
Wang,Jeff 已提交
55 56 57 58
        act="relu")
    # output layer with softmax activation function. size = 10 since there are only 10 possible digits.
    prediction = fluid.layers.fc(input=conv_pool_2, size=10, act='softmax')
    return prediction
L
Luo Tao 已提交
59

Q
qijun 已提交
60

W
Wang,Jeff 已提交
61 62
def train_program():
    label = fluid.layers.data(name='label', shape=[1], dtype='int64')
Q
qijun 已提交
63 64

    # Here we can build the prediction network in different ways. Please
W
Wang,Jeff 已提交
65
    # predict = softmax_regression() # uncomment for Softmax
W
Wang,Jeff 已提交
66 67
    # predict = multilayer_perceptron() # uncomment for MLP
    predict = convolutional_neural_network()  # uncomment for LeNet5
Q
qijun 已提交
68

W
Wang,Jeff 已提交
69 70 71 72 73
    # Calculate the cost from the prediction and label.
    cost = fluid.layers.cross_entropy(input=predict, label=label)
    avg_cost = fluid.layers.mean(cost)
    acc = fluid.layers.accuracy(input=predict, label=label)
    return [avg_cost, acc]
Q
qijun 已提交
74 75


W
Wang,Jeff 已提交
76 77 78 79
def optimizer_program():
    return fluid.optimizer.Adam(learning_rate=0.001)


W
Wang,Jeff 已提交
80 81 82 83
def main():
    train_reader = paddle.batch(
        paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=500),
        batch_size=64)
Q
qijun 已提交
84

W
Wang,Jeff 已提交
85 86
    test_reader = paddle.batch(paddle.dataset.mnist.test(), batch_size=64)

W
Wang,Jeff 已提交
87
    use_cuda = False  # set to True if training with GPU
W
Wang,Jeff 已提交
88 89
    place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()

Y
yuyang 已提交
90
    trainer = Trainer(
W
Wang,Jeff 已提交
91
        train_func=train_program, place=place, optimizer_func=optimizer_program)
W
Wang,Jeff 已提交
92 93 94

    # Save the parameter into a directory. The Inferencer can load the parameters from it to do infer
    params_dirname = "recognize_digits_network.inference.model"
Q
qijun 已提交
95 96 97 98

    lists = []

    def event_handler(event):
Y
yuyang 已提交
99
        if isinstance(event, EndStepEvent):
W
Wang,Jeff 已提交
100 101 102
            if event.step % 100 == 0:
                # event.metrics maps with train program return arguments.
                # event.metrics[0] will yeild avg_cost and event.metrics[1] will yeild acc in this example.
J
JiabinYang 已提交
103 104
                print("Pass %d, Batch %d, Cost %f" % (event.step, event.epoch,
                                                      event.metrics[0]))
W
Wang,Jeff 已提交
105

Y
yuyang 已提交
106
        if isinstance(event, EndEpochEvent):
W
Wang,Jeff 已提交
107 108
            avg_cost, acc = trainer.test(
                reader=test_reader, feed_order=['img', 'label'])
L
liaogang 已提交
109

W
Wang,Jeff 已提交
110 111
            print("Test with Epoch %d, avg_cost: %s, acc: %s" %
                  (event.epoch, avg_cost, acc))
Q
qijun 已提交
112

W
Wang,Jeff 已提交
113 114 115 116 117
            # save parameters
            trainer.save_params(params_dirname)
            lists.append((event.epoch, avg_cost, acc))

    # Train the model now
Q
qijun 已提交
118
    trainer.train(
W
Wang,Jeff 已提交
119
        num_epochs=5,
Q
qijun 已提交
120
        event_handler=event_handler,
W
Wang,Jeff 已提交
121 122
        reader=train_reader,
        feed_order=['img', 'label'])
Q
qijun 已提交
123 124 125

    # find the best pass
    best = sorted(lists, key=lambda list: float(list[1]))[0]
J
JiabinYang 已提交
126 127
    print('Best pass is %s, testing Avgcost is %s' % (best[0], best[1]))
    print('The classification accuracy is %.2f%%' % (float(best[2]) * 100))
Q
qijun 已提交
128

L
liaogang 已提交
129 130 131
    def load_image(file):
        im = Image.open(file).convert('L')
        im = im.resize((28, 28), Image.ANTIALIAS)
W
Wang,Jeff 已提交
132
        im = np.array(im).reshape(1, 1, 28, 28).astype(np.float32)
A
alexqdh 已提交
133
        im = im / 255.0 * 2.0 - 1.0
L
liaogang 已提交
134 135
        return im

L
liaogang 已提交
136
    cur_dir = os.path.dirname(os.path.realpath(__file__))
W
Wang,Jeff 已提交
137
    img = load_image(cur_dir + '/image/infer_3.png')
Y
yuyang 已提交
138
    inferencer = Inferencer(
W
Wang,Jeff 已提交
139 140 141 142 143 144 145 146
        # 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)

    results = inferencer.infer({'img': img})
    lab = np.argsort(results)  # probs and lab are the results of one batch data
J
JiabinYang 已提交
147
    print("Inference result of image/infer_3.png is: %d" % lab[0][0][-1])
L
liaogang 已提交
148

Q
qijun 已提交
149

Q
qijun 已提交
150
if __name__ == '__main__':
Q
qijun 已提交
151
    main()