File size: 9,639 Bytes
ce190ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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
320
321
322
323
324
325
"""
from https://github.com/LikeLy-Journey/SegmenTron/blob/
4bc605eedde7d680314f63d329277b73f83b1c5f/segmentron/modules/basic.py#L34
"""

from collections import OrderedDict
from pathlib import Path

import torch
import torch.nn as nn
from climategan.blocks import InterpolateNearest2d


class SeparableConv2d(nn.Module):
    def __init__(
        self,
        inplanes,
        planes,
        kernel_size=3,
        stride=1,
        dilation=1,
        relu_first=True,
        bias=False,
        norm_layer=nn.BatchNorm2d,
    ):
        super().__init__()
        depthwise = nn.Conv2d(
            inplanes,
            inplanes,
            kernel_size,
            stride=stride,
            padding=dilation,
            dilation=dilation,
            groups=inplanes,
            bias=bias,
        )
        bn_depth = norm_layer(inplanes)
        pointwise = nn.Conv2d(inplanes, planes, 1, bias=bias)
        bn_point = norm_layer(planes)

        if relu_first:
            self.block = nn.Sequential(
                OrderedDict(
                    [
                        ("relu", nn.ReLU()),
                        ("depthwise", depthwise),
                        ("bn_depth", bn_depth),
                        ("pointwise", pointwise),
                        ("bn_point", bn_point),
                    ]
                )
            )
        else:
            self.block = nn.Sequential(
                OrderedDict(
                    [
                        ("depthwise", depthwise),
                        ("bn_depth", bn_depth),
                        ("relu1", nn.ReLU(inplace=True)),
                        ("pointwise", pointwise),
                        ("bn_point", bn_point),
                        ("relu2", nn.ReLU(inplace=True)),
                    ]
                )
            )

    def forward(self, x):
        return self.block(x)


class _ConvBNReLU(nn.Module):
    def __init__(
        self,
        in_channels,
        out_channels,
        kernel_size,
        stride=1,
        padding=0,
        dilation=1,
        groups=1,
        relu6=False,
        norm_layer=nn.BatchNorm2d,
    ):
        super(_ConvBNReLU, self).__init__()
        self.conv = nn.Conv2d(
            in_channels,
            out_channels,
            kernel_size,
            stride,
            padding,
            dilation,
            groups,
            bias=False,
        )
        self.bn = norm_layer(out_channels)
        self.relu = nn.ReLU6(True) if relu6 else nn.ReLU(True)

    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        x = self.relu(x)
        return x


class _DepthwiseConv(nn.Module):
    """conv_dw in MobileNet"""

    def __init__(
        self, in_channels, out_channels, stride, norm_layer=nn.BatchNorm2d, **kwargs
    ):
        super(_DepthwiseConv, self).__init__()
        self.conv = nn.Sequential(
            _ConvBNReLU(
                in_channels,
                in_channels,
                3,
                stride,
                1,
                groups=in_channels,
                norm_layer=norm_layer,
            ),
            _ConvBNReLU(in_channels, out_channels, 1, norm_layer=norm_layer),
        )

    def forward(self, x):
        return self.conv(x)


class InvertedResidual(nn.Module):
    def __init__(
        self,
        in_channels,
        out_channels,
        stride,
        expand_ratio,
        dilation=1,
        norm_layer=nn.BatchNorm2d,
    ):
        super(InvertedResidual, self).__init__()
        assert stride in [1, 2]
        self.use_res_connect = stride == 1 and in_channels == out_channels

        layers = list()
        inter_channels = int(round(in_channels * expand_ratio))
        if expand_ratio != 1:
            # pw
            layers.append(
                _ConvBNReLU(
                    in_channels, inter_channels, 1, relu6=True, norm_layer=norm_layer
                )
            )
        layers.extend(
            [
                # dw
                _ConvBNReLU(
                    inter_channels,
                    inter_channels,
                    3,
                    stride,
                    dilation,
                    dilation,
                    groups=inter_channels,
                    relu6=True,
                    norm_layer=norm_layer,
                ),
                # pw-linear
                nn.Conv2d(inter_channels, out_channels, 1, bias=False),
                norm_layer(out_channels),
            ]
        )
        self.conv = nn.Sequential(*layers)

    def forward(self, x):
        if self.use_res_connect:
            return x + self.conv(x)
        else:
            return self.conv(x)


class MobileNetV2(nn.Module):
    def __init__(self, norm_layer=nn.BatchNorm2d, pretrained_path=None, no_init=False):
        super(MobileNetV2, self).__init__()
        output_stride = 16
        self.multiplier = 1.0
        if output_stride == 32:
            dilations = [1, 1]
        elif output_stride == 16:
            dilations = [1, 2]
        elif output_stride == 8:
            dilations = [2, 4]
        else:
            raise NotImplementedError
        inverted_residual_setting = [
            # t, c, n, s
            [1, 16, 1, 1],
            [6, 24, 2, 2],
            [6, 32, 3, 2],
            [6, 64, 4, 2],
            [6, 96, 3, 1],
            [6, 160, 3, 2],
            [6, 320, 1, 1],
        ]
        # building first layer
        input_channels = int(32 * self.multiplier) if self.multiplier > 1.0 else 32
        # last_channels = int(1280 * multiplier) if multiplier > 1.0 else 1280
        self.conv1 = _ConvBNReLU(
            3, input_channels, 3, 2, 1, relu6=True, norm_layer=norm_layer
        )

        # building inverted residual blocks
        self.planes = input_channels
        self.block1 = self._make_layer(
            InvertedResidual,
            self.planes,
            inverted_residual_setting[0:1],
            norm_layer=norm_layer,
        )
        self.block2 = self._make_layer(
            InvertedResidual,
            self.planes,
            inverted_residual_setting[1:2],
            norm_layer=norm_layer,
        )
        self.block3 = self._make_layer(
            InvertedResidual,
            self.planes,
            inverted_residual_setting[2:3],
            norm_layer=norm_layer,
        )
        self.block4 = self._make_layer(
            InvertedResidual,
            self.planes,
            inverted_residual_setting[3:5],
            dilations[0],
            norm_layer=norm_layer,
        )
        self.block5 = self._make_layer(
            InvertedResidual,
            self.planes,
            inverted_residual_setting[5:],
            dilations[1],
            norm_layer=norm_layer,
        )
        self.last_inp_channels = self.planes

        self.up2 = InterpolateNearest2d()

        # weight initialization
        if not no_init:
            self.pretrained_path = pretrained_path
            if pretrained_path is not None:
                self._load_pretrained_model()
            else:
                for m in self.modules():
                    if isinstance(m, nn.Conv2d):
                        nn.init.kaiming_normal_(m.weight, mode="fan_out")
                        if m.bias is not None:
                            nn.init.zeros_(m.bias)
                    elif isinstance(m, nn.BatchNorm2d):
                        nn.init.ones_(m.weight)
                        nn.init.zeros_(m.bias)
                    elif isinstance(m, nn.Linear):
                        nn.init.normal_(m.weight, 0, 0.01)
                        if m.bias is not None:
                            nn.init.zeros_(m.bias)

    def _make_layer(
        self,
        block,
        planes,
        inverted_residual_setting,
        dilation=1,
        norm_layer=nn.BatchNorm2d,
    ):
        features = list()
        for t, c, n, s in inverted_residual_setting:
            out_channels = int(c * self.multiplier)
            stride = s if dilation == 1 else 1
            features.append(
                block(planes, out_channels, stride, t, dilation, norm_layer)
            )
            planes = out_channels
            for i in range(n - 1):
                features.append(
                    block(planes, out_channels, 1, t, norm_layer=norm_layer)
                )
                planes = out_channels
        self.planes = planes
        return nn.Sequential(*features)

    def forward(self, x):
        x = self.conv1(x)
        x = self.block1(x)
        c1 = self.block2(x)
        c2 = self.block3(c1)
        c3 = self.block4(c2)
        c4 = self.up2(self.block5(c3))

        # x = self.features(x)
        # x = self.classifier(x.view(x.size(0), x.size(1)))
        return c4, c1

    def _load_pretrained_model(self):
        assert self.pretrained_path is not None
        assert Path(self.pretrained_path).exists()

        pretrain_dict = torch.load(self.pretrained_path)
        pretrain_dict = {k.replace("encoder.", ""): v for k, v in pretrain_dict.items()}
        model_dict = {}
        state_dict = self.state_dict()
        ignored = []
        for k, v in pretrain_dict.items():
            if k in state_dict:
                model_dict[k] = v
            else:
                ignored.append(k)
        state_dict.update(model_dict)
        self.load_state_dict(state_dict)
        self.loaded_pre_trained = True
        print(
            "    - Loaded pre-trained MobileNetV2: ignored {}/{} keys".format(
                len(ignored), len(pretrain_dict)
            )
        )