test_image_classification_train.py 6.6 KB
Newer Older
1
import numpy as np
Q
Qiao Longfei 已提交
2
import paddle.v2 as paddle
Q
Qiao Longfei 已提交
3
import paddle.v2.fluid.core as core
Q
Qiao Longfei 已提交
4
import paddle.v2.fluid.framework as framework
Q
Qiao Longfei 已提交
5 6
import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.nets as nets
F
fengjiayi 已提交
7
import paddle.v2.fluid.evaluator as evaluator
8
from paddle.v2.fluid.io import get_inference_program
Q
Qiao Longfei 已提交
9 10
from paddle.v2.fluid.executor import Executor
from paddle.v2.fluid.initializer import XavierInitializer
Q
Qiao Longfei 已提交
11
from paddle.v2.fluid.optimizer import AdamOptimizer
Q
Qiao Longfei 已提交
12 13


14
def resnet_cifar10(input, depth=32):
Q
Qiao Longfei 已提交
15
    def conv_bn_layer(input, ch_out, filter_size, stride, padding, act='relu'):
Q
Qiao Longfei 已提交
16 17 18 19 20 21 22
        tmp = layers.conv2d(
            input=input,
            filter_size=filter_size,
            num_filters=ch_out,
            stride=stride,
            padding=padding,
            act=None,
23
            bias_attr=False)
Q
Qiao Longfei 已提交
24
        return layers.batch_norm(input=tmp, act=act)
Q
Qiao Longfei 已提交
25 26 27 28 29 30 31 32

    def shortcut(input, ch_in, ch_out, stride, program, init_program):
        if ch_in != ch_out:
            return conv_bn_layer(input, ch_out, 1, stride, 0, None, program,
                                 init_program)
        else:
            return input

Q
Qiao Longfei 已提交
33 34 35
    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)
36
        short = shortcut(input, ch_in, ch_out, stride)
Q
Qiao Longfei 已提交
37
        return layers.elementwise_add(x=tmp, y=short, act='relu')
Q
Qiao Longfei 已提交
38

39 40
    def layer_warp(block_func, input, ch_in, ch_out, count, stride):
        tmp = block_func(input, ch_in, ch_out, stride)
Q
Qiao Longfei 已提交
41
        for i in range(1, count):
42
            tmp = block_func(tmp, ch_out, ch_out, 1)
Q
Qiao Longfei 已提交
43 44 45 46 47
        return tmp

    assert (depth - 2) % 6 == 0
    n = (depth - 2) / 6
    conv1 = conv_bn_layer(
Q
Qiao Longfei 已提交
48 49 50 51
        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)
Q
Qiao Longfei 已提交
52
    pool = layers.pool2d(
Q
Qiao Longfei 已提交
53
        input=res3, pool_size=8, pool_type='avg', pool_stride=1)
Q
Qiao Longfei 已提交
54 55 56
    return pool


57
def vgg16_bn_drop(input):
Q
Qiao Longfei 已提交
58
    def conv_block(input, num_filter, groups, dropouts):
Q
Qiao Longfei 已提交
59 60 61 62 63 64 65 66 67
        return nets.img_conv_group(
            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,
68
            pool_type='max')
Q
Qiao Longfei 已提交
69

70 71 72 73 74
    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 已提交
75

Q
Qiao Longfei 已提交
76
    drop = layers.dropout(x=conv5, dropout_prob=0.5)
Q
Qiao Longfei 已提交
77 78 79
    fc1 = layers.fc(input=drop,
                    size=512,
                    act=None,
80
                    param_attr={"initializer": XavierInitializer()})
Q
Qiao Longfei 已提交
81 82 83
    reshape1 = layers.reshape(x=fc1, shape=list(fc1.shape + (1, 1)))
    bn = layers.batch_norm(input=reshape1, act='relu')
    drop2 = layers.dropout(x=bn, dropout_prob=0.5)
Q
Qiao Longfei 已提交
84 85 86
    fc2 = layers.fc(input=drop2,
                    size=512,
                    act=None,
87
                    param_attr={"initializer": XavierInitializer()})
Q
Qiao Longfei 已提交
88 89 90 91 92 93
    return fc2


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

F
fengjiayi 已提交
94 95
images = layers.data(name='pixel', shape=data_shape, dtype='float32')
label = layers.data(name='label', shape=[1], dtype='int64')
Q
Qiao Longfei 已提交
96 97 98

# Add neural network config
# option 1. resnet
99
# net = resnet_cifar10(images, 32)
Q
Qiao Longfei 已提交
100
# option 2. vgg
101
net = vgg16_bn_drop(images)
Q
Qiao Longfei 已提交
102 103 104

# print(program)

105 106 107
predict = layers.fc(input=net, size=classdim, act='softmax')
cost = layers.cross_entropy(input=predict, label=label)
avg_cost = layers.mean(x=cost)
Q
Qiao Longfei 已提交
108

Q
Qiao Longfei 已提交
109 110
# optimizer = SGDOptimizer(learning_rate=0.001)
optimizer = AdamOptimizer(learning_rate=0.001)
111
opts = optimizer.minimize(avg_cost)
Q
Qiao Longfei 已提交
112

F
fengjiayi 已提交
113 114
accuracy, acc_out = evaluator.accuracy(input=predict, label=label)

Q
Qiao Longfei 已提交
115 116 117 118 119
BATCH_SIZE = 128
PASS_NUM = 1

train_reader = paddle.batch(
    paddle.reader.shuffle(
120
        paddle.dataset.cifar.train10(), buf_size=BATCH_SIZE * 10),
Q
Qiao Longfei 已提交
121 122
    batch_size=BATCH_SIZE)

123 124
test_reader = paddle.batch(paddle.dataset.cifar.test10(), batch_size=BATCH_SIZE)

Q
Qiao Longfei 已提交
125 126 127
place = core.CPUPlace()
exe = Executor(place)

128
exe.run(framework.default_startup_program())
Q
Qiao Longfei 已提交
129 130 131

for pass_id in range(PASS_NUM):
    batch_id = 0
F
fengjiayi 已提交
132
    accuracy.reset(exe)
Q
Qiao Longfei 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146
    for data in train_reader():
        img_data = np.array(map(lambda x: x[0].reshape(data_shape),
                                data)).astype("float32")
        y_data = np.array(map(lambda x: x[1], data)).astype("int64")
        batch_size = 1
        for i in y_data.shape:
            batch_size = batch_size * i
        y_data = y_data.reshape([batch_size, 1])

        tensor_img = core.LoDTensor()
        tensor_y = core.LoDTensor()
        tensor_img.set(img_data, place)
        tensor_y.set(y_data, place)

147
        outs = exe.run(framework.default_main_program(),
Q
Qiao Longfei 已提交
148 149
                       feed={"pixel": tensor_img,
                             "label": tensor_y},
F
fengjiayi 已提交
150
                       fetch_list=[avg_cost, acc_out])
Q
Qiao Longfei 已提交
151 152

        loss = np.array(outs[0])
153
        acc = np.array(outs[1])
F
fengjiayi 已提交
154
        pass_acc = accuracy.eval(exe)
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

        batch_id = batch_id + 1

        test_accuracy, test_acc_out = evaluator.accuracy(
            input=predict, label=label)

        test_target = [avg_cost, test_acc_out] + test_accuracy.states().values()
        inference_program = get_inference_program(test_target)

        test_accuracy.reset(exe)

        for data in test_reader():
            x_data = np.array(map(lambda x: x[0].reshape(data_shape),
                                  data)).astype("float32")
            y_data = np.array(map(lambda x: x[1], data)).astype("int64")
            y_data = np.expand_dims(y_data, axis=1)

            tensor_x = core.LoDTensor()
            tensor_x.set(x_data, place)

            tensor_y = core.LoDTensor()
            tensor_y.set(y_data, place)

            outs = exe.run(inference_program,
                           feed={'pixel': tensor_x,
                                 'label': tensor_y},
                           fetch_list=[avg_cost, test_acc_out])
            out = np.array(outs[0])
            acc = np.array(outs[1])

        test_pass_acc = test_accuracy.eval(exe)

Q
Qiao Longfei 已提交
187
        print("pass_id:" + str(pass_id) + " batch_id:" + str(batch_id) +
F
fengjiayi 已提交
188
              " loss:" + str(loss) + " acc:" + str(acc) + " pass_acc:" + str(
189
                  pass_acc) + " test_pass_acc:" + str(test_pass_acc))
Q
Qiao Longfei 已提交
190 191 192 193 194

        if batch_id > 1:
            # this model is slow, so if we can train two mini batch, we think it works properly.
            exit(0)
exit(1)