train.py 4.3 KB
Newer Older
L
liaogang 已提交
1
import os
L
liaogang 已提交
2 3
from PIL import Image
import numpy as np
L
Luo Tao 已提交
4 5
import paddle.v2 as paddle

D
dzhwinter 已提交
6
with_gpu = os.getenv('WITH_GPU', '0') != '0'
L
Luo Tao 已提交
7

H
Helin Wang 已提交
8

L
Luo Tao 已提交
9
def softmax_regression(img):
10 11
    predict = paddle.layer.fc(
        input=img, size=10, act=paddle.activation.Softmax())
L
Luo Tao 已提交
12 13 14 15 16 17 18
    return predict


def multilayer_perceptron(img):
    # The first fully-connected layer
    hidden1 = paddle.layer.fc(input=img, size=128, act=paddle.activation.Relu())
    # The second fully-connected layer and the according activation function
19 20
    hidden2 = paddle.layer.fc(
        input=hidden1, size=64, act=paddle.activation.Relu())
L
Luo Tao 已提交
21 22
    # The thrid fully-connected layer, note that the hidden size should be 10,
    # which is the number of unique digits
23 24
    predict = paddle.layer.fc(
        input=hidden2, size=10, act=paddle.activation.Softmax())
L
Luo Tao 已提交
25 26 27 28 29 30 31 32 33 34 35 36
    return predict


def convolutional_neural_network(img):
    # first conv layer
    conv_pool_1 = paddle.networks.simple_img_conv_pool(
        input=img,
        filter_size=5,
        num_filters=20,
        num_channel=1,
        pool_size=2,
        pool_stride=2,
L
liaogang 已提交
37
        act=paddle.activation.Relu())
L
Luo Tao 已提交
38 39 40 41 42 43 44 45
    # second conv layer
    conv_pool_2 = paddle.networks.simple_img_conv_pool(
        input=conv_pool_1,
        filter_size=5,
        num_filters=50,
        num_channel=20,
        pool_size=2,
        pool_stride=2,
L
liaogang 已提交
46 47
        act=paddle.activation.Relu())
    # fully-connected layer
48
    predict = paddle.layer.fc(
L
liaogang 已提交
49
        input=conv_pool_2, size=10, act=paddle.activation.Softmax())
L
Luo Tao 已提交
50 51 52
    return predict


Q
qijun 已提交
53
def main():
D
dzhwinter 已提交
54
    paddle.init(use_gpu=with_gpu, trainer_count=1)
Q
qijun 已提交
55 56 57 58

    # define network topology
    images = paddle.layer.data(
        name='pixel', type=paddle.data_type.dense_vector(784))
Q
qijun 已提交
59 60
    label = paddle.layer.data(
        name='label', type=paddle.data_type.integer_value(10))
Q
qijun 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

    # Here we can build the prediction network in different ways. Please
    # choose one by uncomment corresponding line.
    # predict = softmax_regression(images)
    # predict = multilayer_perceptron(images)
    predict = convolutional_neural_network(images)

    cost = paddle.layer.classification_cost(input=predict, label=label)

    parameters = paddle.parameters.create(cost)

    optimizer = paddle.optimizer.Momentum(
        learning_rate=0.1 / 128.0,
        momentum=0.9,
        regularization=paddle.optimizer.L2Regularization(rate=0.0005 * 128))

    trainer = paddle.trainer.SGD(
        cost=cost, parameters=parameters, update_equation=optimizer)

    lists = []

    def event_handler(event):
        if isinstance(event, paddle.event.EndIteration):
            if event.batch_id % 100 == 0:
                print "Pass %d, Batch %d, Cost %f, %s" % (
                    event.pass_id, event.batch_id, event.cost, event.metrics)
        if isinstance(event, paddle.event.EndPass):
L
liaogang 已提交
88
            # save parameters
89
            with open('params_pass_%d.tar' % event.pass_id, 'w') as f:
90
                trainer.save_parameter_to_tar(f)
L
liaogang 已提交
91

Q
qijun 已提交
92 93
            result = trainer.test(reader=paddle.batch(
                paddle.dataset.mnist.test(), batch_size=128))
Q
qijun 已提交
94 95
            print "Test with Pass %d, Cost %f, %s\n" % (
                event.pass_id, result.cost, result.metrics)
Q
qijun 已提交
96
            lists.append((event.pass_id, result.cost,
Q
qijun 已提交
97
                          result.metrics['classification_error_evaluator']))
Q
qijun 已提交
98 99 100 101 102 103

    trainer.train(
        reader=paddle.batch(
            paddle.reader.shuffle(paddle.dataset.mnist.train(), buf_size=8192),
            batch_size=128),
        event_handler=event_handler,
L
liaogang 已提交
104
        num_passes=5)
Q
qijun 已提交
105 106 107 108 109 110

    # find the best pass
    best = sorted(lists, key=lambda list: float(list[1]))[0]
    print 'Best pass is %s, testing Avgcost is %s' % (best[0], best[1])
    print 'The classification accuracy is %.2f%%' % (100 - float(best[2]) * 100)

L
liaogang 已提交
111 112 113 114 115 116 117 118
    def load_image(file):
        im = Image.open(file).convert('L')
        im = im.resize((28, 28), Image.ANTIALIAS)
        im = np.array(im).astype(np.float32).flatten()
        im = im / 255.0
        return im

    test_data = []
L
liaogang 已提交
119 120
    cur_dir = os.path.dirname(os.path.realpath(__file__))
    test_data.append((load_image(cur_dir + '/image/infer_3.png'), ))
L
liaogang 已提交
121 122 123 124 125 126

    probs = paddle.infer(
        output_layer=predict, parameters=parameters, input=test_data)
    lab = np.argsort(-probs)  # probs and lab are the results of one batch data
    print "Label of image/infer_3.png is: %d" % lab[0][0]

Q
qijun 已提交
127

Q
qijun 已提交
128
if __name__ == '__main__':
Q
qijun 已提交
129
    main()