layer_libs.py 4.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
# Copyright (c) 2020 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.

import paddle
from paddle import nn
import paddle.nn.functional as F
from paddle.nn import Conv2d
from paddle.nn import SyncBatchNorm as BatchNorm


C
chenguowei01 已提交
22
class ConvBNReLU(nn.Layer):
W
wuzewu 已提交
23 24 25 26 27 28
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 padding='same',
                 **kwargs):
29

C
chenguowei01 已提交
30
        super(ConvBNReLU, self).__init__()
31

W
wuzewu 已提交
32 33
        self._conv = Conv2d(
            in_channels, out_channels, kernel_size, padding=padding, **kwargs)
34

C
chenguowei01 已提交
35
        self._batch_norm = BatchNorm(out_channels)
36 37

    def forward(self, x):
C
chenguowei01 已提交
38 39
        x = self._conv(x)
        x = self._batch_norm(x)
40 41 42 43
        x = F.relu(x)
        return x


C
chenguowei01 已提交
44
class ConvBN(nn.Layer):
W
wuzewu 已提交
45 46 47 48 49 50
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 padding='same',
                 **kwargs):
51

C
chenguowei01 已提交
52
        super(ConvBN, self).__init__()
W
wuzewu 已提交
53 54
        self._conv = Conv2d(
            in_channels, out_channels, kernel_size, padding=padding, **kwargs)
C
chenguowei01 已提交
55
        self._batch_norm = BatchNorm(out_channels)
56 57

    def forward(self, x):
C
chenguowei01 已提交
58 59
        x = self._conv(x)
        x = self._batch_norm(x)
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        return x


class ConvReluPool(nn.Layer):
    def __init__(self, in_channels, out_channels):
        super(ConvReluPool, self).__init__()
        self.conv = Conv2d(
            in_channels,
            out_channels,
            kernel_size=3,
            stride=1,
            padding=1,
            dilation=1)

    def forward(self, x):
        x = self.conv(x)
        x = F.relu(x)
        x = F.pool2d(x, pool_size=2, pool_type="max", pool_stride=2)
        return x


W
wuzewu 已提交
81 82 83 84 85 86 87 88
class SeparableConvBNReLU(nn.Layer):
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 padding='same',
                 **kwargs):
        super(SeparableConvBNReLU, self).__init__()
C
chenguowei01 已提交
89
        self.depthwise_conv = ConvBN(
90 91 92
            in_channels,
            out_channels=in_channels,
            kernel_size=kernel_size,
W
wuzewu 已提交
93
            padding=padding,
94 95
            groups=in_channels,
            **kwargs)
C
chenguowei01 已提交
96
        self.piontwise_conv = ConvBNReLU(
W
wuzewu 已提交
97
            in_channels, out_channels, kernel_size=1, padding=padding, groups=1)
98 99 100 101 102 103 104

    def forward(self, x):
        x = self.depthwise_conv(x)
        x = self.piontwise_conv(x)
        return x


W
wuzewu 已提交
105
class DepthwiseConvBN(nn.Layer):
W
wuzewu 已提交
106 107 108 109 110 111
    def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 padding='same',
                 **kwargs):
W
wuzewu 已提交
112 113 114
        super(DepthwiseConvBN, self).__init__()
        self.depthwise_conv = ConvBN(
            in_channels,
W
wuzewu 已提交
115
            out_channels=out_channels,
W
wuzewu 已提交
116
            kernel_size=kernel_size,
W
wuzewu 已提交
117
            padding=padding,
W
wuzewu 已提交
118 119 120 121 122 123 124 125
            groups=in_channels,
            **kwargs)

    def forward(self, x):
        x = self.depthwise_conv(x)
        return x


126
class AuxLayer(nn.Layer):
127
    """
128
    The auxilary layer implementation for auxilary loss
129 130

    Args:
131 132 133 134
        in_channels (int): the number of input channels.
        inter_channels (int): intermediate channels.
        out_channels (int): the number of output channels, which is usually num_classes.
        dropout_prob (float): the droput rate. Default to 0.1.
135 136
    """

137 138 139 140 141 142 143
    def __init__(self,
                 in_channels,
                 inter_channels,
                 out_channels,
                 dropout_prob=0.1):
        super(AuxLayer, self).__init__()

C
chenguowei01 已提交
144
        self.conv_bn_relu = ConvBNReLU(
145 146 147 148
            in_channels=in_channels,
            out_channels=inter_channels,
            kernel_size=3,
            padding=1)
149

150 151 152 153
        self.conv = nn.Conv2d(
            in_channels=inter_channels,
            out_channels=out_channels,
            kernel_size=1)
154

155
        self.dropout_prob = dropout_prob
156 157

    def forward(self, x):
158 159 160 161
        x = self.conv_bn_relu(x)
        x = F.dropout(x, p=self.dropout_prob)
        x = self.conv(x)
        return x