test_image_classification_train.py 5.9 KB
Newer Older
D
dzhwinter 已提交
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

15
from __future__ import print_function
16

Q
Qiao Longfei 已提交
17
import paddle.v2 as paddle
18
import paddle.v2.fluid as fluid
19 20
import unittest
import contextlib
Q
Qiao Longfei 已提交
21 22


23
def resnet_cifar10(input, depth=32):
Q
Qiao Longfei 已提交
24
    def conv_bn_layer(input, ch_out, filter_size, stride, padding, act='relu'):
25
        tmp = fluid.layers.conv2d(
Q
Qiao Longfei 已提交
26 27 28 29 30 31
            input=input,
            filter_size=filter_size,
            num_filters=ch_out,
            stride=stride,
            padding=padding,
            act=None,
32
            bias_attr=False)
33
        return fluid.layers.batch_norm(input=tmp, act=act)
Q
Qiao Longfei 已提交
34

35
    def shortcut(input, ch_in, ch_out, stride):
Q
Qiao Longfei 已提交
36
        if ch_in != ch_out:
37
            return conv_bn_layer(input, ch_out, 1, stride, 0, None)
Q
Qiao Longfei 已提交
38 39 40
        else:
            return input

Q
Qiao Longfei 已提交
41 42 43
    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)
44
        short = shortcut(input, ch_in, ch_out, stride)
45
        return fluid.layers.elementwise_add(x=tmp, y=short, act='relu')
Q
Qiao Longfei 已提交
46

47 48
    def layer_warp(block_func, input, ch_in, ch_out, count, stride):
        tmp = block_func(input, ch_in, ch_out, stride)
Q
Qiao Longfei 已提交
49
        for i in range(1, count):
50
            tmp = block_func(tmp, ch_out, ch_out, 1)
Q
Qiao Longfei 已提交
51 52 53 54 55
        return tmp

    assert (depth - 2) % 6 == 0
    n = (depth - 2) / 6
    conv1 = conv_bn_layer(
Q
Qiao Longfei 已提交
56 57 58 59
        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)
60
    pool = fluid.layers.pool2d(
Q
Qiao Longfei 已提交
61
        input=res3, pool_size=8, pool_type='avg', pool_stride=1)
Q
Qiao Longfei 已提交
62 63 64
    return pool


65
def vgg16_bn_drop(input):
Q
Qiao Longfei 已提交
66
    def conv_block(input, num_filter, groups, dropouts):
67
        return fluid.nets.img_conv_group(
Q
Qiao Longfei 已提交
68 69 70 71 72 73 74 75
            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,
76
            pool_type='max')
Q
Qiao Longfei 已提交
77

78 79 80 81 82
    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 已提交
83

84 85
    drop = fluid.layers.dropout(x=conv5, dropout_prob=0.5)
    fc1 = fluid.layers.fc(input=drop, size=512, act=None)
86
    bn = fluid.layers.batch_norm(input=fc1, act='relu')
87 88
    drop2 = fluid.layers.dropout(x=bn, dropout_prob=0.5)
    fc2 = fluid.layers.fc(input=drop2, size=512, act=None)
Q
Qiao Longfei 已提交
89 90 91
    return fc2


92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
def main(net_type, use_cuda):
    if use_cuda and not fluid.core.is_compiled_with_cuda():
        return

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

    images = fluid.layers.data(name='pixel', shape=data_shape, dtype='float32')
    label = fluid.layers.data(name='label', shape=[1], dtype='int64')

    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)

    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)

    optimizer = fluid.optimizer.Adam(learning_rate=0.001)
    optimizer.minimize(avg_cost)

    accuracy = fluid.evaluator.Accuracy(input=predict, label=label)

    BATCH_SIZE = 128
    PASS_NUM = 1

    train_reader = paddle.batch(
        paddle.reader.shuffle(
            paddle.dataset.cifar.train10(), buf_size=128 * 10),
        batch_size=BATCH_SIZE)

    place = fluid.CUDAPlace(0) if use_cuda else fluid.CPUPlace()
    exe = fluid.Executor(place)
    feeder = fluid.DataFeeder(place=place, feed_list=[images, label])
    exe.run(fluid.default_startup_program())

    loss = 0.0
    for pass_id in range(PASS_NUM):
        accuracy.reset(exe)
        for data in train_reader():
            loss, acc = exe.run(fluid.default_main_program(),
                                feed=feeder.feed(data),
                                fetch_list=[avg_cost] + accuracy.metrics)
            pass_acc = accuracy.eval(exe)
            print("loss:" + str(loss) + " acc:" + str(acc) + " pass_acc:" + str(
                pass_acc))
            return

    raise AssertionError(
        "Image classification loss is too large, {0:2.2}".format(loss))


class TestImageClassification(unittest.TestCase):
    def test_vgg_cuda(self):
        with self.scope_prog_guard():
            main('vgg', use_cuda=True)

    def test_resnet_cuda(self):
        with self.scope_prog_guard():
            main('resnet', use_cuda=True)

    def test_vgg_cpu(self):
        with self.scope_prog_guard():
            main('vgg', use_cuda=False)

    def test_resnet_cpu(self):
        with self.scope_prog_guard():
            main('resnet', use_cuda=False)

    @contextlib.contextmanager
    def scope_prog_guard(self):
        prog = fluid.Program()
        startup_prog = fluid.Program()
        scope = fluid.core.Scope()
        with fluid.scope_guard(scope):
            with fluid.program_guard(prog, startup_prog):
                yield


if __name__ == '__main__':
    unittest.main()