resnet.py 2.7 KB
Newer Older
L
liaogang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved
#
# 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
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# 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.

W
Wang,Jeff 已提交
15
import paddle.fluid as fluid
Y
Yu Yang 已提交
16 17 18 19 20 21 22 23 24 25 26
import sys

try:
    from paddle.fluid.contrib.trainer import *
    from paddle.fluid.contrib.inferencer import *
except ImportError:
    print(
        "In the fluid 1.0, the trainer and inferencer are moving to paddle.fluid.contrib",
        file=sys.stderr)
    from paddle.fluid.trainer import *
    from paddle.fluid.inferencer import *
L
liaogang 已提交
27 28 29 30 31 32 33 34 35

__all__ = ['resnet_cifar10']


def conv_bn_layer(input,
                  ch_out,
                  filter_size,
                  stride,
                  padding,
W
Wang,Jeff 已提交
36 37 38
                  act='relu',
                  bias_attr=False):
    tmp = fluid.layers.conv2d(
L
liaogang 已提交
39 40 41 42 43
        input=input,
        filter_size=filter_size,
        num_filters=ch_out,
        stride=stride,
        padding=padding,
W
Wang,Jeff 已提交
44 45 46
        act=None,
        bias_attr=bias_attr)
    return fluid.layers.batch_norm(input=tmp, act=act)
L
liaogang 已提交
47 48


W
Wang,Jeff 已提交
49
def shortcut(input, ch_in, ch_out, stride):
50
    if ch_in != ch_out:
W
Wang,Jeff 已提交
51
        return conv_bn_layer(input, ch_out, 1, stride, 0, None)
L
liaogang 已提交
52
    else:
W
Wang,Jeff 已提交
53
        return input
L
liaogang 已提交
54 55


W
Wang,Jeff 已提交
56 57 58 59 60
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, bias_attr=True)
    short = shortcut(input, ch_in, ch_out, stride)
    return fluid.layers.elementwise_add(x=tmp, y=short, act='relu')
L
liaogang 已提交
61 62


W
Wang,Jeff 已提交
63 64
def layer_warp(block_func, input, ch_in, ch_out, count, stride):
    tmp = block_func(input, ch_in, ch_out, stride)
L
liaogang 已提交
65
    for i in range(1, count):
66
        tmp = block_func(tmp, ch_out, ch_out, 1)
L
liaogang 已提交
67 68 69 70 71 72
    return tmp


def resnet_cifar10(ipt, depth=32):
    # depth should be one of 20, 32, 44, 56, 110, 1202
    assert (depth - 2) % 6 == 0
73
    n = (depth - 2) // 6
L
liaogang 已提交
74
    nStages = {16, 64, 128}
W
Wang,Jeff 已提交
75
    conv1 = conv_bn_layer(ipt, ch_out=16, filter_size=3, stride=1, padding=1)
76 77 78
    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)
W
Wang,Jeff 已提交
79 80 81 82
    pool = fluid.layers.pool2d(
        input=res3, pool_size=8, pool_type='avg', pool_stride=1)
    predict = fluid.layers.fc(input=pool, size=10, act='softmax')
    return predict