module.py 12.3 KB
Newer Older
H
haoyuying 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
# 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.

import os
import sys
import math

import paddle
from paddle import ParamAttr
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.nn import Conv2d, BatchNorm, Linear, Dropout
from paddle.nn import AdaptiveAvgPool2d, MaxPool2d, AvgPool2d
from paddle.nn.initializer import Uniform
from paddlehub.module.module import moduleinfo
from paddlehub.module.cv_module import ImageClassifierModule


class ConvBNLayer(nn.Layer):
    """Basic conv bn layer."""
W
wuzewu 已提交
32

H
haoyuying 已提交
33 34 35 36 37 38 39 40 41 42
    def __init__(self,
                 num_channels: int,
                 num_filters: int,
                 filter_size: int,
                 stride: int = 1,
                 groups: int = 1,
                 act: str = None,
                 name: str = None):
        super(ConvBNLayer, self).__init__()

W
wuzewu 已提交
43 44 45 46 47 48 49 50 51
        self._conv = Conv2d(
            in_channels=num_channels,
            out_channels=num_filters,
            kernel_size=filter_size,
            stride=stride,
            padding=(filter_size - 1) // 2,
            groups=groups,
            weight_attr=ParamAttr(name=name + "_weights"),
            bias_attr=False)
H
haoyuying 已提交
52
        bn_name = "bn_" + name
W
wuzewu 已提交
53 54 55 56 57 58 59
        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')
H
haoyuying 已提交
60 61 62 63 64 65 66 67 68

    def forward(self, inputs: paddle.Tensor):
        y = self._conv(inputs)
        y = self._batch_norm(y)
        return y


class SeparableConv(nn.Layer):
    """Basic separable conv layer, it contains pointwise conv and depthwise conv."""
W
wuzewu 已提交
69

H
haoyuying 已提交
70 71 72 73
    def __init__(self, input_channels: int, output_channels: int, stride: int = 1, name: str = None):
        super(SeparableConv, self).__init__()

        self._pointwise_conv = ConvBNLayer(input_channels, output_channels, 1, name=name + "_sep")
W
wuzewu 已提交
74 75
        self._depthwise_conv = ConvBNLayer(
            output_channels, output_channels, 3, stride=stride, groups=output_channels, name=name + "_dw")
H
haoyuying 已提交
76 77 78 79 80 81 82 83 84

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


class EntryFlowBottleneckBlock(nn.Layer):
    """Basic entry flow bottleneck block for Xception."""
W
wuzewu 已提交
85

H
haoyuying 已提交
86 87 88 89 90 91 92 93 94
    def __init__(self,
                 input_channels: int,
                 output_channels: int,
                 stride: int = 2,
                 name: str = None,
                 relu_first: bool = False):
        super(EntryFlowBottleneckBlock, self).__init__()
        self.relu_first = relu_first

W
wuzewu 已提交
95 96 97 98 99 100 101 102
        self._short = Conv2d(
            in_channels=input_channels,
            out_channels=output_channels,
            kernel_size=1,
            stride=stride,
            padding=0,
            weight_attr=ParamAttr(name + "_branch1_weights"),
            bias_attr=False)
H
haoyuying 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
        self._conv1 = SeparableConv(input_channels, output_channels, stride=1, name=name + "_branch2a_weights")
        self._conv2 = SeparableConv(output_channels, output_channels, stride=1, name=name + "_branch2b_weights")
        self._pool = MaxPool2d(kernel_size=3, stride=stride, padding=1)

    def forward(self, inputs: paddle.Tensor):
        conv0 = inputs
        short = self._short(inputs)
        if self.relu_first:
            conv0 = F.relu(conv0)
        conv1 = self._conv1(conv0)
        conv2 = F.relu(conv1)
        conv2 = self._conv2(conv2)
        pool = self._pool(conv2)
        return paddle.elementwise_add(x=short, y=pool)


class EntryFlow(nn.Layer):
    """Entry flow for Xception."""
W
wuzewu 已提交
121

H
haoyuying 已提交
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
    def __init__(self, block_num: int = 3):
        super(EntryFlow, self).__init__()

        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:
            self._conv_0 = EntryFlowBottleneckBlock(64, 128, stride=2, name=name + "_0", relu_first=False)
            self._conv_1 = EntryFlowBottleneckBlock(128, 256, stride=2, name=name + "_1", relu_first=True)
            self._conv_2 = EntryFlowBottleneckBlock(256, 728, stride=2, name=name + "_2", relu_first=True)
        elif block_num == 5:
            self._conv_0 = EntryFlowBottleneckBlock(64, 128, stride=2, name=name + "_0", relu_first=False)
            self._conv_1 = EntryFlowBottleneckBlock(128, 256, stride=1, name=name + "_1", relu_first=True)
            self._conv_2 = EntryFlowBottleneckBlock(256, 256, stride=2, name=name + "_2", relu_first=True)
            self._conv_3 = EntryFlowBottleneckBlock(256, 728, stride=1, name=name + "_3", relu_first=True)
            self._conv_4 = EntryFlowBottleneckBlock(728, 728, stride=2, name=name + "_4", relu_first=True)
        else:
            sys.exit(-1)

    def forward(self, inputs: paddle.Tensor):
        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


class MiddleFlowBottleneckBlock(nn.Layer):
    """Basic middle flow bottleneck block for Xception."""
W
wuzewu 已提交
161

H
haoyuying 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    def __init__(self, input_channels: int, output_channels: int, name: str):
        super(MiddleFlowBottleneckBlock, self).__init__()

        self._conv_0 = SeparableConv(input_channels, output_channels, stride=1, name=name + "_branch2a_weights")
        self._conv_1 = SeparableConv(output_channels, output_channels, stride=1, name=name + "_branch2b_weights")
        self._conv_2 = SeparableConv(output_channels, output_channels, stride=1, name=name + "_branch2c_weights")

    def forward(self, inputs: paddle.Tensor):
        conv0 = F.relu(inputs)
        conv0 = self._conv_0(conv0)
        conv1 = F.relu(conv0)
        conv1 = self._conv_1(conv1)
        conv2 = F.relu(conv1)
        conv2 = self._conv_2(conv2)
        return paddle.elementwise_add(x=inputs, y=conv2)


class MiddleFlow(nn.Layer):
    """Middle flow for Xception."""
W
wuzewu 已提交
181

H
haoyuying 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
    def __init__(self, block_num: int = 8):
        super(MiddleFlow, self).__init__()

        self.block_num = block_num
        self._conv_0 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_0")
        self._conv_1 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_1")
        self._conv_2 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_2")
        self._conv_3 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_3")
        self._conv_4 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_4")
        self._conv_5 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_5")
        self._conv_6 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_6")
        self._conv_7 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_7")
        if block_num == 16:
            self._conv_8 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_8")
            self._conv_9 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_9")
            self._conv_10 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_10")
            self._conv_11 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_11")
            self._conv_12 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_12")
            self._conv_13 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_13")
            self._conv_14 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_14")
            self._conv_15 = MiddleFlowBottleneckBlock(728, 728, name="middle_flow_15")

    def forward(self, inputs: paddle.Tensor):
        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


class ExitFlowBottleneckBlock(nn.Layer):
    """Basic exit flow bottleneck block for Xception."""
W
wuzewu 已提交
227

H
haoyuying 已提交
228 229 230
    def __init__(self, input_channels: int, output_channels1: int, output_channels2: int, name: str):
        super(ExitFlowBottleneckBlock, self).__init__()

W
wuzewu 已提交
231 232 233 234 235 236 237 238
        self._short = Conv2d(
            in_channels=input_channels,
            out_channels=output_channels2,
            kernel_size=1,
            stride=2,
            padding=0,
            weight_attr=ParamAttr(name + "_branch1_weights"),
            bias_attr=False)
H
haoyuying 已提交
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
        self._conv_1 = SeparableConv(input_channels, output_channels1, stride=1, name=name + "_branch2a_weights")
        self._conv_2 = SeparableConv(output_channels1, output_channels2, stride=1, name=name + "_branch2b_weights")
        self._pool = MaxPool2d(kernel_size=3, stride=2, padding=1)

    def forward(self, inputs: paddle.Tensor):
        short = self._short(inputs)
        conv0 = F.relu(inputs)
        conv1 = self._conv_1(conv0)
        conv2 = F.relu(conv1)
        conv2 = self._conv_2(conv2)
        pool = self._pool(conv2)
        return paddle.elementwise_add(x=short, y=pool)


class ExitFlow(nn.Layer):
    def __init__(self, class_dim: int):
        super(ExitFlow, self).__init__()

        name = "exit_flow"

        self._conv_0 = ExitFlowBottleneckBlock(728, 728, 1024, name=name + "_1")
        self._conv_1 = SeparableConv(1024, 1536, stride=1, name=name + "_2")
        self._conv_2 = SeparableConv(1536, 2048, stride=1, name=name + "_3")
        self._pool = AdaptiveAvgPool2d(1)
        stdv = 1.0 / math.sqrt(2048 * 1.0)
W
wuzewu 已提交
264 265 266 267 268
        self._out = Linear(
            2048,
            class_dim,
            weight_attr=ParamAttr(name="fc_weights", initializer=Uniform(-stdv, stdv)),
            bias_attr=ParamAttr(name="fc_offset"))
H
haoyuying 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281

    def forward(self, inputs: paddle.Tensor):
        conv0 = self._conv_0(inputs)
        conv1 = self._conv_1(conv0)
        conv1 = F.relu(conv1)
        conv2 = self._conv_2(conv1)
        conv2 = F.relu(conv2)
        pool = self._pool(conv2)
        pool = paddle.reshape(pool, [0, -1])
        out = self._out(pool)
        return out


W
wuzewu 已提交
282 283 284 285 286 287 288 289 290
@moduleinfo(
    name="xception71_imagenet",
    type="CV/classification",
    author="paddlepaddle",
    author_email="",
    summary="Xception71_imagenet is a classification model, "
    "this module is trained with Imagenet dataset.",
    version="1.1.0",
    meta=ImageClassifierModule)
H
haoyuying 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
class Xception71(nn.Layer):
    def __init__(self, class_dim=1000, load_checkpoint: str = None):
        super(Xception71, self).__init__()
        self.entry_flow_block_num = 5
        self.middle_flow_block_num = 16
        self._entry_flow = EntryFlow(self.entry_flow_block_num)
        self._middle_flow = MiddleFlow(self.middle_flow_block_num)
        self._exit_flow = ExitFlow(class_dim)

        if load_checkpoint is not None:
            model_dict = paddle.load(load_checkpoint)[0]
            self.set_dict(model_dict)
            print("load custom checkpoint success")

        else:
            checkpoint = os.path.join(self.directory, 'xception71_imagenet.pdparams')
            if not os.path.exists(checkpoint):
                os.system(
                    'wget https://paddlehub.bj.bcebos.com/dygraph/image_classification/xception71_imagenet.pdparams -O'
                    + checkpoint)
            model_dict = paddle.load(checkpoint)[0]
            self.set_dict(model_dict)
            print("load pretrained checkpoint success")

    def forward(self, inputs):
        x = self._entry_flow(inputs)
        x = self._middle_flow(x)
        x = self._exit_flow(x)
        return x