xception.py 11.4 KB
Newer Older
W
WuHaobo 已提交
1
import paddle
littletomatodonkey's avatar
littletomatodonkey 已提交
2 3 4
from paddle import ParamAttr
import paddle.nn as nn
import paddle.nn.functional as F
5 6
from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
littletomatodonkey's avatar
littletomatodonkey 已提交
7
from paddle.nn.initializer import Uniform
8
import math
C
cuicheng01 已提交
9
import sys
W
WuHaobo 已提交
10

11
__all__ = ['Xception41', 'Xception65', 'Xception71']
W
WuHaobo 已提交
12 13


littletomatodonkey's avatar
littletomatodonkey 已提交
14
class ConvBNLayer(nn.Layer):
15 16 17 18 19 20 21 22 23
    def __init__(self,
                 num_channels,
                 num_filters,
                 filter_size,
                 stride=1,
                 groups=1,
                 act=None,
                 name=None):
        super(ConvBNLayer, self).__init__()
W
WuHaobo 已提交
24

25
        self._conv = Conv2D(
littletomatodonkey's avatar
littletomatodonkey 已提交
26 27 28
            in_channels=num_channels,
            out_channels=num_filters,
            kernel_size=filter_size,
29 30 31
            stride=stride,
            padding=(filter_size - 1) // 2,
            groups=groups,
littletomatodonkey's avatar
littletomatodonkey 已提交
32
            weight_attr=ParamAttr(name=name + "_weights"),
33 34 35 36 37 38 39 40 41
            bias_attr=False)
        bn_name = "bn_" + name
        self._batch_norm = BatchNorm(
            num_filters,
            act=act,
            param_attr=ParamAttr(name=bn_name + "_scale"),
            bias_attr=ParamAttr(name=bn_name + "_offset"),
            moving_mean_name=bn_name + '_mean',
            moving_variance_name=bn_name + '_variance')
W
WuHaobo 已提交
42

43 44 45 46
    def forward(self, inputs):
        y = self._conv(inputs)
        y = self._batch_norm(y)
        return y
W
WuHaobo 已提交
47 48


littletomatodonkey's avatar
littletomatodonkey 已提交
49
class SeparableConv(nn.Layer):
50
    def __init__(self, input_channels, output_channels, stride=1, name=None):
W
wqz960 已提交
51
        super(SeparableConv, self).__init__()
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68

        self._pointwise_conv = ConvBNLayer(
            input_channels, output_channels, 1, name=name + "_sep")
        self._depthwise_conv = ConvBNLayer(
            output_channels,
            output_channels,
            3,
            stride=stride,
            groups=output_channels,
            name=name + "_dw")

    def forward(self, inputs):
        x = self._pointwise_conv(inputs)
        x = self._depthwise_conv(x)
        return x


littletomatodonkey's avatar
littletomatodonkey 已提交
69
class EntryFlowBottleneckBlock(nn.Layer):
70 71 72 73 74 75
    def __init__(self,
                 input_channels,
                 output_channels,
                 stride=2,
                 name=None,
                 relu_first=False):
W
wqz960 已提交
76
        super(EntryFlowBottleneckBlock, self).__init__()
77 78
        self.relu_first = relu_first

79
        self._short = Conv2D(
littletomatodonkey's avatar
littletomatodonkey 已提交
80 81 82
            in_channels=input_channels,
            out_channels=output_channels,
            kernel_size=1,
W
WuHaobo 已提交
83 84
            stride=stride,
            padding=0,
littletomatodonkey's avatar
littletomatodonkey 已提交
85
            weight_attr=ParamAttr(name + "_branch1_weights"),
W
WuHaobo 已提交
86
            bias_attr=False)
W
wqz960 已提交
87
        self._conv1 = SeparableConv(
88 89 90 91
            input_channels,
            output_channels,
            stride=1,
            name=name + "_branch2a_weights")
W
wqz960 已提交
92
        self._conv2 = SeparableConv(
93 94 95 96
            output_channels,
            output_channels,
            stride=1,
            name=name + "_branch2b_weights")
97
        self._pool = MaxPool2D(kernel_size=3, stride=stride, padding=1)
98 99 100 101 102

    def forward(self, inputs):
        conv0 = inputs
        short = self._short(inputs)
        if self.relu_first:
littletomatodonkey's avatar
littletomatodonkey 已提交
103
            conv0 = F.relu(conv0)
104
        conv1 = self._conv1(conv0)
littletomatodonkey's avatar
littletomatodonkey 已提交
105
        conv2 = F.relu(conv1)
106 107
        conv2 = self._conv2(conv2)
        pool = self._pool(conv2)
108
        return paddle.add(x=short, y=pool)
W
WuHaobo 已提交
109 110


littletomatodonkey's avatar
littletomatodonkey 已提交
111
class EntryFlow(nn.Layer):
112
    def __init__(self, block_num=3):
W
wqz960 已提交
113
        super(EntryFlow, self).__init__()
W
WuHaobo 已提交
114

115 116 117 118 119 120
        name = "entry_flow"
        self.block_num = block_num
        self._conv1 = ConvBNLayer(
            3, 32, 3, stride=2, act="relu", name=name + "_conv1")
        self._conv2 = ConvBNLayer(32, 64, 3, act="relu", name=name + "_conv2")
        if block_num == 3:
W
wqz960 已提交
121
            self._conv_0 = EntryFlowBottleneckBlock(
122
                64, 128, stride=2, name=name + "_0", relu_first=False)
W
wqz960 已提交
123
            self._conv_1 = EntryFlowBottleneckBlock(
124
                128, 256, stride=2, name=name + "_1", relu_first=True)
W
wqz960 已提交
125
            self._conv_2 = EntryFlowBottleneckBlock(
126 127
                256, 728, stride=2, name=name + "_2", relu_first=True)
        elif block_num == 5:
W
wqz960 已提交
128
            self._conv_0 = EntryFlowBottleneckBlock(
129
                64, 128, stride=2, name=name + "_0", relu_first=False)
W
wqz960 已提交
130
            self._conv_1 = EntryFlowBottleneckBlock(
131
                128, 256, stride=1, name=name + "_1", relu_first=True)
W
wqz960 已提交
132
            self._conv_2 = EntryFlowBottleneckBlock(
133
                256, 256, stride=2, name=name + "_2", relu_first=True)
W
wqz960 已提交
134
            self._conv_3 = EntryFlowBottleneckBlock(
135
                256, 728, stride=1, name=name + "_3", relu_first=True)
W
wqz960 已提交
136
            self._conv_4 = EntryFlowBottleneckBlock(
137 138 139
                728, 728, stride=2, name=name + "_4", relu_first=True)
        else:
            sys.exit(-1)
W
WuHaobo 已提交
140

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
    def forward(self, inputs):
        x = self._conv1(inputs)
        x = self._conv2(x)

        if self.block_num == 3:
            x = self._conv_0(x)
            x = self._conv_1(x)
            x = self._conv_2(x)
        elif self.block_num == 5:
            x = self._conv_0(x)
            x = self._conv_1(x)
            x = self._conv_2(x)
            x = self._conv_3(x)
            x = self._conv_4(x)
        return x


littletomatodonkey's avatar
littletomatodonkey 已提交
158
class MiddleFlowBottleneckBlock(nn.Layer):
159
    def __init__(self, input_channels, output_channels, name):
W
wqz960 已提交
160
        super(MiddleFlowBottleneckBlock, self).__init__()
161

W
wqz960 已提交
162
        self._conv_0 = SeparableConv(
163 164
            input_channels,
            output_channels,
W
WuHaobo 已提交
165 166
            stride=1,
            name=name + "_branch2a_weights")
W
wqz960 已提交
167
        self._conv_1 = SeparableConv(
168 169
            output_channels,
            output_channels,
W
WuHaobo 已提交
170 171
            stride=1,
            name=name + "_branch2b_weights")
W
wqz960 已提交
172
        self._conv_2 = SeparableConv(
173 174
            output_channels,
            output_channels,
W
WuHaobo 已提交
175 176 177
            stride=1,
            name=name + "_branch2c_weights")

178
    def forward(self, inputs):
littletomatodonkey's avatar
littletomatodonkey 已提交
179
        conv0 = F.relu(inputs)
180
        conv0 = self._conv_0(conv0)
littletomatodonkey's avatar
littletomatodonkey 已提交
181
        conv1 = F.relu(conv0)
182
        conv1 = self._conv_1(conv1)
littletomatodonkey's avatar
littletomatodonkey 已提交
183
        conv2 = F.relu(conv1)
184
        conv2 = self._conv_2(conv2)
185
        return paddle.add(x=inputs, y=conv2)
186 187


littletomatodonkey's avatar
littletomatodonkey 已提交
188
class MiddleFlow(nn.Layer):
189
    def __init__(self, block_num=8):
W
wqz960 已提交
190
        super(MiddleFlow, self).__init__()
191 192

        self.block_num = block_num
W
wqz960 已提交
193
        self._conv_0 = MiddleFlowBottleneckBlock(
194
            728, 728, name="middle_flow_0")
W
wqz960 已提交
195
        self._conv_1 = MiddleFlowBottleneckBlock(
196
            728, 728, name="middle_flow_1")
W
wqz960 已提交
197
        self._conv_2 = MiddleFlowBottleneckBlock(
198
            728, 728, name="middle_flow_2")
W
wqz960 已提交
199
        self._conv_3 = MiddleFlowBottleneckBlock(
200
            728, 728, name="middle_flow_3")
W
wqz960 已提交
201
        self._conv_4 = MiddleFlowBottleneckBlock(
202
            728, 728, name="middle_flow_4")
W
wqz960 已提交
203
        self._conv_5 = MiddleFlowBottleneckBlock(
204
            728, 728, name="middle_flow_5")
W
wqz960 已提交
205
        self._conv_6 = MiddleFlowBottleneckBlock(
206
            728, 728, name="middle_flow_6")
W
wqz960 已提交
207
        self._conv_7 = MiddleFlowBottleneckBlock(
208 209
            728, 728, name="middle_flow_7")
        if block_num == 16:
W
wqz960 已提交
210
            self._conv_8 = MiddleFlowBottleneckBlock(
211
                728, 728, name="middle_flow_8")
W
wqz960 已提交
212
            self._conv_9 = MiddleFlowBottleneckBlock(
213
                728, 728, name="middle_flow_9")
W
wqz960 已提交
214
            self._conv_10 = MiddleFlowBottleneckBlock(
215
                728, 728, name="middle_flow_10")
W
wqz960 已提交
216
            self._conv_11 = MiddleFlowBottleneckBlock(
217
                728, 728, name="middle_flow_11")
W
wqz960 已提交
218
            self._conv_12 = MiddleFlowBottleneckBlock(
219
                728, 728, name="middle_flow_12")
W
wqz960 已提交
220
            self._conv_13 = MiddleFlowBottleneckBlock(
221
                728, 728, name="middle_flow_13")
W
wqz960 已提交
222
            self._conv_14 = MiddleFlowBottleneckBlock(
223
                728, 728, name="middle_flow_14")
W
wqz960 已提交
224
            self._conv_15 = MiddleFlowBottleneckBlock(
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
                728, 728, name="middle_flow_15")

    def forward(self, inputs):
        x = self._conv_0(inputs)
        x = self._conv_1(x)
        x = self._conv_2(x)
        x = self._conv_3(x)
        x = self._conv_4(x)
        x = self._conv_5(x)
        x = self._conv_6(x)
        x = self._conv_7(x)
        if self.block_num == 16:
            x = self._conv_8(x)
            x = self._conv_9(x)
            x = self._conv_10(x)
            x = self._conv_11(x)
            x = self._conv_12(x)
            x = self._conv_13(x)
            x = self._conv_14(x)
            x = self._conv_15(x)
        return x


littletomatodonkey's avatar
littletomatodonkey 已提交
248
class ExitFlowBottleneckBlock(nn.Layer):
249 250
    def __init__(self, input_channels, output_channels1, output_channels2,
                 name):
W
wqz960 已提交
251
        super(ExitFlowBottleneckBlock, self).__init__()
252

253
        self._short = Conv2D(
littletomatodonkey's avatar
littletomatodonkey 已提交
254 255 256
            in_channels=input_channels,
            out_channels=output_channels2,
            kernel_size=1,
W
WuHaobo 已提交
257 258
            stride=2,
            padding=0,
littletomatodonkey's avatar
littletomatodonkey 已提交
259
            weight_attr=ParamAttr(name + "_branch1_weights"),
W
WuHaobo 已提交
260
            bias_attr=False)
W
wqz960 已提交
261
        self._conv_1 = SeparableConv(
262 263 264 265
            input_channels,
            output_channels1,
            stride=1,
            name=name + "_branch2a_weights")
W
wqz960 已提交
266
        self._conv_2 = SeparableConv(
267 268 269 270
            output_channels1,
            output_channels2,
            stride=1,
            name=name + "_branch2b_weights")
271
        self._pool = MaxPool2D(kernel_size=3, stride=2, padding=1)
272 273 274

    def forward(self, inputs):
        short = self._short(inputs)
littletomatodonkey's avatar
littletomatodonkey 已提交
275
        conv0 = F.relu(inputs)
276
        conv1 = self._conv_1(conv0)
littletomatodonkey's avatar
littletomatodonkey 已提交
277
        conv2 = F.relu(conv1)
278 279
        conv2 = self._conv_2(conv2)
        pool = self._pool(conv2)
280
        return paddle.add(x=short, y=pool)
W
WuHaobo 已提交
281 282


littletomatodonkey's avatar
littletomatodonkey 已提交
283
class ExitFlow(nn.Layer):
284
    def __init__(self, class_dim):
W
wqz960 已提交
285
        super(ExitFlow, self).__init__()
W
WuHaobo 已提交
286

287
        name = "exit_flow"
W
WuHaobo 已提交
288

W
wqz960 已提交
289
        self._conv_0 = ExitFlowBottleneckBlock(
290
            728, 728, 1024, name=name + "_1")
W
wqz960 已提交
291 292
        self._conv_1 = SeparableConv(1024, 1536, stride=1, name=name + "_2")
        self._conv_2 = SeparableConv(1536, 2048, stride=1, name=name + "_3")
293
        self._pool = AdaptiveAvgPool2D(1)
294 295 296 297
        stdv = 1.0 / math.sqrt(2048 * 1.0)
        self._out = Linear(
            2048,
            class_dim,
littletomatodonkey's avatar
littletomatodonkey 已提交
298 299
            weight_attr=ParamAttr(
                name="fc_weights", initializer=Uniform(-stdv, stdv)),
300 301 302 303 304
            bias_attr=ParamAttr(name="fc_offset"))

    def forward(self, inputs):
        conv0 = self._conv_0(inputs)
        conv1 = self._conv_1(conv0)
littletomatodonkey's avatar
littletomatodonkey 已提交
305
        conv1 = F.relu(conv1)
306
        conv2 = self._conv_2(conv1)
littletomatodonkey's avatar
littletomatodonkey 已提交
307
        conv2 = F.relu(conv2)
308
        pool = self._pool(conv2)
L
littletomatodonkey 已提交
309
        pool = paddle.flatten(pool, start_axis=1, stop_axis=-1)
310 311
        out = self._out(pool)
        return out
W
WuHaobo 已提交
312

313

littletomatodonkey's avatar
littletomatodonkey 已提交
314
class Xception(nn.Layer):
315 316 317 318 319 320 321
    def __init__(self,
                 entry_flow_block_num=3,
                 middle_flow_block_num=8,
                 class_dim=1000):
        super(Xception, self).__init__()
        self.entry_flow_block_num = entry_flow_block_num
        self.middle_flow_block_num = middle_flow_block_num
W
wqz960 已提交
322 323 324
        self._entry_flow = EntryFlow(entry_flow_block_num)
        self._middle_flow = MiddleFlow(middle_flow_block_num)
        self._exit_flow = ExitFlow(class_dim)
325 326 327 328 329 330

    def forward(self, inputs):
        x = self._entry_flow(inputs)
        x = self._middle_flow(x)
        x = self._exit_flow(x)
        return x
W
WuHaobo 已提交
331 332


W
wqz960 已提交
333 334
def Xception41(**args):
    model = Xception(entry_flow_block_num=3, middle_flow_block_num=8, **args)
W
WuHaobo 已提交
335 336 337
    return model


W
wqz960 已提交
338 339
def Xception65(**args):
    model = Xception(entry_flow_block_num=3, middle_flow_block_num=16, **args)
W
WuHaobo 已提交
340 341 342
    return model


W
wqz960 已提交
343 344
def Xception71(**args):
    model = Xception(entry_flow_block_num=5, middle_flow_block_num=16, **args)
littletomatodonkey's avatar
littletomatodonkey 已提交
345
    return model