test_image_classification_train.py 4.2 KB
Newer Older
1
from __future__ import print_function
2

Y
Yu Yang 已提交
3 4
import sys

Q
Qiao Longfei 已提交
5
import paddle.v2 as paddle
6
import paddle.v2.fluid as fluid
Q
Qiao Longfei 已提交
7 8


9
def resnet_cifar10(input, depth=32):
Q
Qiao Longfei 已提交
10
    def conv_bn_layer(input, ch_out, filter_size, stride, padding, act='relu'):
11
        tmp = fluid.layers.conv2d(
Q
Qiao Longfei 已提交
12 13 14 15 16 17
            input=input,
            filter_size=filter_size,
            num_filters=ch_out,
            stride=stride,
            padding=padding,
            act=None,
18
            bias_attr=False)
19
        return fluid.layers.batch_norm(input=tmp, act=act)
Q
Qiao Longfei 已提交
20

21
    def shortcut(input, ch_in, ch_out, stride):
Q
Qiao Longfei 已提交
22
        if ch_in != ch_out:
23
            return conv_bn_layer(input, ch_out, 1, stride, 0, None)
Q
Qiao Longfei 已提交
24 25 26
        else:
            return input

Q
Qiao Longfei 已提交
27 28 29
    def basicblock(input, ch_in, ch_out, stride):
        tmp = conv_bn_layer(input, ch_out, 3, stride, 1)
        tmp = conv_bn_layer(tmp, ch_out, 3, 1, 1, act=None)
30
        short = shortcut(input, ch_in, ch_out, stride)
31
        return fluid.layers.elementwise_add(x=tmp, y=short, act='relu')
Q
Qiao Longfei 已提交
32

33 34
    def layer_warp(block_func, input, ch_in, ch_out, count, stride):
        tmp = block_func(input, ch_in, ch_out, stride)
Q
Qiao Longfei 已提交
35
        for i in range(1, count):
36
            tmp = block_func(tmp, ch_out, ch_out, 1)
Q
Qiao Longfei 已提交
37 38 39 40 41
        return tmp

    assert (depth - 2) % 6 == 0
    n = (depth - 2) / 6
    conv1 = conv_bn_layer(
Q
Qiao Longfei 已提交
42 43 44 45
        input=input, ch_out=16, filter_size=3, stride=1, padding=1)
    res1 = layer_warp(basicblock, conv1, 16, 16, n, 1)
    res2 = layer_warp(basicblock, res1, 16, 32, n, 2)
    res3 = layer_warp(basicblock, res2, 32, 64, n, 2)
46
    pool = fluid.layers.pool2d(
Q
Qiao Longfei 已提交
47
        input=res3, pool_size=8, pool_type='avg', pool_stride=1)
Q
Qiao Longfei 已提交
48 49 50
    return pool


51
def vgg16_bn_drop(input):
Q
Qiao Longfei 已提交
52
    def conv_block(input, num_filter, groups, dropouts):
53
        return fluid.nets.img_conv_group(
Q
Qiao Longfei 已提交
54 55 56 57 58 59 60 61
            input=input,
            pool_size=2,
            pool_stride=2,
            conv_num_filter=[num_filter] * groups,
            conv_filter_size=3,
            conv_act='relu',
            conv_with_batchnorm=True,
            conv_batchnorm_drop_rate=dropouts,
62
            pool_type='max')
Q
Qiao Longfei 已提交
63

64 65 66 67 68
    conv1 = conv_block(input, 64, 2, [0.3, 0])
    conv2 = conv_block(conv1, 128, 2, [0.4, 0])
    conv3 = conv_block(conv2, 256, 3, [0.4, 0.4, 0])
    conv4 = conv_block(conv3, 512, 3, [0.4, 0.4, 0])
    conv5 = conv_block(conv4, 512, 3, [0.4, 0.4, 0])
Q
Qiao Longfei 已提交
69

70 71
    drop = fluid.layers.dropout(x=conv5, dropout_prob=0.5)
    fc1 = fluid.layers.fc(input=drop, size=512, act=None)
72
    bn = fluid.layers.batch_norm(input=fc1, act='relu')
73 74
    drop2 = fluid.layers.dropout(x=bn, dropout_prob=0.5)
    fc2 = fluid.layers.fc(input=drop2, size=512, act=None)
Q
Qiao Longfei 已提交
75 76 77 78 79 80
    return fc2


classdim = 10
data_shape = [3, 32, 32]

81 82
images = fluid.layers.data(name='pixel', shape=data_shape, dtype='float32')
label = fluid.layers.data(name='label', shape=[1], dtype='int64')
Q
Qiao Longfei 已提交
83

84 85 86 87 88 89 90 91 92 93 94 95
net_type = "vgg"
if len(sys.argv) >= 2:
    net_type = sys.argv[1]

if net_type == "vgg":
    print("train vgg net")
    net = vgg16_bn_drop(images)
elif net_type == "resnet":
    print("train resnet")
    net = resnet_cifar10(images, 32)
else:
    raise ValueError("%s network is not supported" % net_type)
Q
Qiao Longfei 已提交
96

97 98 99
predict = fluid.layers.fc(input=net, size=classdim, act='softmax')
cost = fluid.layers.cross_entropy(input=predict, label=label)
avg_cost = fluid.layers.mean(x=cost)
Q
Qiao Longfei 已提交
100

101
optimizer = fluid.optimizer.Adam(learning_rate=0.001)
102
opts = optimizer.minimize(avg_cost)
Q
Qiao Longfei 已提交
103

104
accuracy = fluid.evaluator.Accuracy(input=predict, label=label)
F
fengjiayi 已提交
105

Q
Qiao Longfei 已提交
106 107 108 109 110
BATCH_SIZE = 128
PASS_NUM = 1

train_reader = paddle.batch(
    paddle.reader.shuffle(
Y
Yu Yang 已提交
111
        paddle.dataset.cifar.train10(), buf_size=128 * 10),
Q
Qiao Longfei 已提交
112 113
    batch_size=BATCH_SIZE)

114 115
place = fluid.CPUPlace()
exe = fluid.Executor(place)
Y
Yu Yang 已提交
116
feeder = fluid.DataFeeder(place=place, feed_list=[images, label])
117
exe.run(fluid.default_startup_program())
Q
Qiao Longfei 已提交
118 119

for pass_id in range(PASS_NUM):
F
fengjiayi 已提交
120
    accuracy.reset(exe)
Q
Qiao Longfei 已提交
121
    for data in train_reader():
122
        loss, acc = exe.run(fluid.default_main_program(),
Y
Yu Yang 已提交
123
                            feed=feeder.feed(data),
124
                            fetch_list=[avg_cost] + accuracy.metrics)
F
fengjiayi 已提交
125
        pass_acc = accuracy.eval(exe)
126 127 128 129
        print("loss:" + str(loss) + " acc:" + str(acc) + " pass_acc:" + str(
            pass_acc))
        # this model is slow, so if we can train two mini batch, we think it works properly.
        exit(0)
Q
Qiao Longfei 已提交
130
exit(1)