File size: 7,555 Bytes
1ab1a09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
178
179
180
181
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# copyright (c) 2021 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 paddle
import paddle.nn as nn
import paddle.nn.functional as F

from paddleseg.models import layers
from paddleseg.cvlibs import manager
from paddleseg.utils import utils


class MLAHeads(nn.Layer):
    def __init__(self, mlahead_channels=128):
        super(MLAHeads, self).__init__()
        self.head2 = nn.Sequential(
            layers.ConvBNReLU(
                mlahead_channels * 2,
                mlahead_channels,
                3,
                padding=1,
                bias_attr=False),
            layers.ConvBNReLU(
                mlahead_channels,
                mlahead_channels,
                3,
                padding=1,
                bias_attr=False))
        self.head3 = nn.Sequential(
            layers.ConvBNReLU(
                mlahead_channels * 2,
                mlahead_channels,
                3,
                padding=1,
                bias_attr=False),
            layers.ConvBNReLU(
                mlahead_channels,
                mlahead_channels,
                3,
                padding=1,
                bias_attr=False))
        self.head4 = nn.Sequential(
            layers.ConvBNReLU(
                mlahead_channels * 2,
                mlahead_channels,
                3,
                padding=1,
                bias_attr=False),
            layers.ConvBNReLU(
                mlahead_channels,
                mlahead_channels,
                3,
                padding=1,
                bias_attr=False))
        self.head5 = nn.Sequential(
            layers.ConvBNReLU(
                mlahead_channels * 2,
                mlahead_channels,
                3,
                padding=1,
                bias_attr=False),
            layers.ConvBNReLU(
                mlahead_channels,
                mlahead_channels,
                3,
                padding=1,
                bias_attr=False))

    def forward(self, mla_p2, mla_p3, mla_p4, mla_p5):
        head2 = F.interpolate(
            self.head2(mla_p2),
            size=(4 * mla_p2.shape[3], 4 * mla_p2.shape[3]),
            mode='bilinear',
            align_corners=True)
        head3 = F.interpolate(
            self.head3(mla_p3),
            size=(4 * mla_p3.shape[3], 4 * mla_p3.shape[3]),
            mode='bilinear',
            align_corners=True)
        head4 = F.interpolate(
            self.head4(mla_p4),
            size=(4 * mla_p4.shape[3], 4 * mla_p4.shape[3]),
            mode='bilinear',
            align_corners=True)
        head5 = F.interpolate(
            self.head5(mla_p5),
            size=(4 * mla_p5.shape[3], 4 * mla_p5.shape[3]),
            mode='bilinear',
            align_corners=True)

        return paddle.concat([head2, head3, head4, head5], axis=1)


@manager.MODELS.add_component
class MLATransformer(nn.Layer):
    def __init__(self,
                 num_classes,
                 in_channels,
                 backbone,
                 mlahead_channels=128,
                 aux_channels=256,
                 norm_layer=nn.BatchNorm2D,
                 pretrained=None,
                 **kwargs):
        super(MLATransformer, self).__init__()

        self.BatchNorm = norm_layer
        self.mlahead_channels = mlahead_channels
        self.num_classes = num_classes
        self.in_channels = in_channels
        self.backbone = backbone

        self.mlahead = MLAHeads(mlahead_channels=self.mlahead_channels)
        self.cls = nn.Conv2D(
            4 * self.mlahead_channels, self.num_classes, 3, padding=1)

        self.conv0 = layers.ConvBNReLU(
            self.in_channels[0],
            self.in_channels[0] * 2,
            3,
            padding=1,
            bias_attr=False)
        self.conv1 = layers.ConvBNReLU(
            self.in_channels[1],
            self.in_channels[1],
            3,
            padding=1,
            bias_attr=False)
        self.conv21 = layers.ConvBNReLU(
            self.in_channels[2],
            self.in_channels[2],
            3,
            padding=1,
            bias_attr=False)
        self.conv22 = layers.ConvBNReLU(
            self.in_channels[2],
            self.in_channels[2] // 2,
            3,
            padding=1,
            bias_attr=False)
        self.conv31 = layers.ConvBNReLU(
            self.in_channels[3],
            self.in_channels[3],
            3,
            padding=1,
            bias_attr=False)
        self.conv32 = layers.ConvBNReLU(
            self.in_channels[3],
            self.in_channels[3] // 2,
            3,
            padding=1,
            bias_attr=False)
        self.conv33 = layers.ConvBNReLU(
            self.in_channels[3] // 2,
            self.in_channels[3] // 4,
            3,
            padding=1,
            bias_attr=False)

        self.aux_head = nn.Sequential(
            layers.ConvBN(
                in_channels=self.in_channels[2],
                out_channels=aux_channels,
                kernel_size=3,
                padding=1,
                bias_attr=False),
            nn.Conv2D(
                in_channels=aux_channels,
                out_channels=self.num_classes,
                kernel_size=1, ))

        self.pretrained = pretrained
        self.init_weight()

    def init_weight(self):
        if self.pretrained is not None:
            utils.load_entire_model(self, self.pretrained)

    def forward(self, x):
        inputs = self.backbone(x)

        inputs0 = self.conv0(inputs[0])
        inputs1 = F.interpolate(
            self.conv1(inputs[1]),
            size=inputs[0].shape[2:],
            mode='bilinear',
            align_corners=True)
        inputs2 = F.interpolate(
            self.conv21(inputs[2]),
            scale_factor=2,
            mode='bilinear',
            align_corners=True)
        inputs2 = F.interpolate(
            self.conv22(inputs2),
            size=inputs[0].shape[2:],
            mode='bilinear',
            align_corners=True)
        inputs3 = F.interpolate(
            self.conv31(inputs[3]),
            scale_factor=2,
            mode='bilinear',
            align_corners=True)
        inputs3 = F.interpolate(
            self.conv32(inputs3),
            scale_factor=2,
            mode='bilinear',
            align_corners=True)
        inputs3 = F.interpolate(
            self.conv33(inputs3),
            size=inputs[0].shape[2:],
            mode='bilinear',
            align_corners=True)
        inputs2 = inputs2 + inputs3
        inputs1 = inputs1 + inputs2
        inputs0 = inputs0 + inputs1

        feats = self.mlahead(inputs0, inputs1, inputs2, inputs3)
        logit = self.cls(feats)
        logit_list = [logit]

        if self.training:
            logit_list.append(self.aux_head(inputs[2]))

        logit_list = [
            F.interpolate(
                logit, paddle.shape(x)[2:], mode='bilinear', align_corners=True)
            for logit in logit_list
        ]
        return logit_list