train.py 4.3 KB
Newer Older
1
import gzip
G
guosheng 已提交
2 3
import argparse

W
wwhu 已提交
4
import paddle.v2.dataset.flowers as flowers
5 6 7
import paddle.v2 as paddle
import reader
import vgg
W
wwhu 已提交
8 9 10
import resnet
import alexnet
import googlenet
11
import inception_v4
G
guosheng 已提交
12
import inception_resnet_v2
W
wangmeng28 已提交
13
import se_resnext
14

G
guosheng 已提交
15
DATA_DIM = 3 * 224 * 224  # Use 3 * 331 * 331 or 3 * 299 * 299 for Inception-ResNet-v2.
W
wwhu 已提交
16
CLASS_DIM = 102
17 18 19 20
BATCH_SIZE = 128


def main():
W
wwhu 已提交
21 22 23 24 25
    # parse the argument
    parser = argparse.ArgumentParser()
    parser.add_argument(
        'model',
        help='The model for image classification',
26 27
        choices=[
            'alexnet', 'vgg13', 'vgg16', 'vgg19', 'resnet', 'googlenet',
W
wangmeng28 已提交
28
            'inception-resnet-v2', 'inception_v4', 'se-resnext'
29
        ])
W
wwhu 已提交
30
    args = parser.parse_args()
31 32

    # PaddlePaddle init
W
wwhu 已提交
33
    paddle.init(use_gpu=True, trainer_count=1)
34 35 36 37 38

    image = paddle.layer.data(
        name="image", type=paddle.data_type.dense_vector(DATA_DIM))
    lbl = paddle.layer.data(
        name="label", type=paddle.data_type.integer_value(CLASS_DIM))
W
wwhu 已提交
39 40

    extra_layers = None
41
    learning_rate = 0.01
W
wwhu 已提交
42
    if args.model == 'alexnet':
W
wwhu 已提交
43
        out = alexnet.alexnet(image, class_dim=CLASS_DIM)
W
wwhu 已提交
44
    elif args.model == 'vgg13':
W
wwhu 已提交
45
        out = vgg.vgg13(image, class_dim=CLASS_DIM)
W
wwhu 已提交
46
    elif args.model == 'vgg16':
W
wwhu 已提交
47
        out = vgg.vgg16(image, class_dim=CLASS_DIM)
W
wwhu 已提交
48
    elif args.model == 'vgg19':
W
wwhu 已提交
49
        out = vgg.vgg19(image, class_dim=CLASS_DIM)
W
wwhu 已提交
50
    elif args.model == 'resnet':
W
wwhu 已提交
51
        out = resnet.resnet_imagenet(image, class_dim=CLASS_DIM)
52
        learning_rate = 0.1
W
wwhu 已提交
53
    elif args.model == 'googlenet':
W
wwhu 已提交
54
        out, out1, out2 = googlenet.googlenet(image, class_dim=CLASS_DIM)
W
wwhu 已提交
55 56 57 58 59 60 61
        loss1 = paddle.layer.cross_entropy_cost(
            input=out1, label=lbl, coeff=0.3)
        paddle.evaluator.classification_error(input=out1, label=lbl)
        loss2 = paddle.layer.cross_entropy_cost(
            input=out2, label=lbl, coeff=0.3)
        paddle.evaluator.classification_error(input=out2, label=lbl)
        extra_layers = [loss1, loss2]
G
guosheng 已提交
62 63 64 65
    elif args.model == 'inception-resnet-v2':
        assert DATA_DIM == 3 * 331 * 331 or DATA_DIM == 3 * 299 * 299
        out = inception_resnet_v2.inception_resnet_v2(
            image, class_dim=CLASS_DIM, dropout_rate=0.5, data_dim=DATA_DIM)
66 67
    elif args.model == 'inception_v4':
        out = inception_v4.inception_v4(image, class_dim=CLASS_DIM)
W
wangmeng28 已提交
68 69
    elif args.model == 'se-resnext':
        out = se_resnext.se_resnext50(image, class_dim=CLASS_DIM)
W
wwhu 已提交
70

71 72 73 74 75 76 77 78 79 80
    cost = paddle.layer.classification_cost(input=out, label=lbl)

    # Create parameters
    parameters = paddle.parameters.create(cost)

    # Create optimizer
    optimizer = paddle.optimizer.Momentum(
        momentum=0.9,
        regularization=paddle.optimizer.L2Regularization(rate=0.0005 *
                                                         BATCH_SIZE),
81
        learning_rate=learning_rate / BATCH_SIZE,
82 83 84 85 86
        learning_rate_decay_a=0.1,
        learning_rate_decay_b=128000 * 35,
        learning_rate_schedule="discexp", )

    train_reader = paddle.batch(
W
wwhu 已提交
87
        paddle.reader.shuffle(
W
wwhu 已提交
88 89
            flowers.train(),
            # To use other data, replace the above line with:
90
            # reader.train_reader('train.list'),
W
wwhu 已提交
91
            buf_size=1000),
92 93
        batch_size=BATCH_SIZE)
    test_reader = paddle.batch(
W
wwhu 已提交
94 95
        flowers.valid(),
        # To use other data, replace the above line with:
96
        # reader.test_reader('val.list'),
W
wwhu 已提交
97
        batch_size=BATCH_SIZE)
98

99 100 101 102 103 104 105
    # Create trainer
    trainer = paddle.trainer.SGD(
        cost=cost,
        parameters=parameters,
        update_equation=optimizer,
        extra_layers=extra_layers)

106 107 108 109 110 111 112 113
    # End batch and end pass event handler
    def event_handler(event):
        if isinstance(event, paddle.event.EndIteration):
            if event.batch_id % 1 == 0:
                print "\nPass %d, Batch %d, Cost %f, %s" % (
                    event.pass_id, event.batch_id, event.cost, event.metrics)
        if isinstance(event, paddle.event.EndPass):
            with gzip.open('params_pass_%d.tar.gz' % event.pass_id, 'w') as f:
114
                trainer.save_parameter_to_tar(f)
115 116 117 118 119 120 121 122 123

            result = trainer.test(reader=test_reader)
            print "\nTest with Pass %d, %s" % (event.pass_id, result.metrics)

    trainer.train(
        reader=train_reader, num_passes=200, event_handler=event_handler)


if __name__ == '__main__':
124
    main()