File size: 6,903 Bytes
2cdd41c
 
 
 
1615d09
 
 
2cdd41c
 
 
 
 
 
1615d09
 
 
 
 
 
 
 
 
 
2cdd41c
 
 
 
 
 
 
 
 
 
 
 
 
1615d09
2cdd41c
 
 
1615d09
 
 
 
 
 
2cdd41c
1615d09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cdd41c
 
 
 
 
1615d09
 
 
 
 
 
2cdd41c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1615d09
2cdd41c
 
 
1615d09
2cdd41c
 
 
 
 
 
 
 
 
 
1615d09
2cdd41c
 
 
 
 
 
 
1615d09
 
 
2cdd41c
 
 
1615d09
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2cdd41c
 
 
 
 
 
 
1615d09
 
 
 
 
 
 
 
2cdd41c
 
 
1615d09
 
 
 
 
 
2cdd41c
1615d09
2cdd41c
 
 
 
 
 
 
 
 
 
 
1615d09
 
 
 
 
 
2cdd41c
1615d09
2cdd41c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1615d09
 
 
 
 
 
2cdd41c
1615d09
2cdd41c
 
 
 
1615d09
2cdd41c
 
 
 
1615d09
 
 
 
 
 
 
 
2cdd41c
1615d09
2cdd41c
 
 
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
from contextlib import ExitStack

import torch
import torch.nn.functional as F
from torch import nn

from isegm.model import ops

from .basic_blocks import SeparableConv2d
from .resnet import ResNetBackbone


class DeepLabV3Plus(nn.Module):
    def __init__(
        self,
        backbone="resnet50",
        norm_layer=nn.BatchNorm2d,
        backbone_norm_layer=None,
        ch=256,
        project_dropout=0.5,
        inference_mode=False,
        **kwargs
    ):
        super(DeepLabV3Plus, self).__init__()
        if backbone_norm_layer is None:
            backbone_norm_layer = norm_layer

        self.backbone_name = backbone
        self.norm_layer = norm_layer
        self.backbone_norm_layer = backbone_norm_layer
        self.inference_mode = False
        self.ch = ch
        self.aspp_in_channels = 2048
        self.skip_project_in_channels = 256  # layer 1 out_channels

        self._kwargs = kwargs
        if backbone == "resnet34":
            self.aspp_in_channels = 512
            self.skip_project_in_channels = 64

        self.backbone = ResNetBackbone(
            backbone=self.backbone_name,
            pretrained_base=False,
            norm_layer=self.backbone_norm_layer,
            **kwargs
        )

        self.head = _DeepLabHead(
            in_channels=ch + 32,
            mid_channels=ch,
            out_channels=ch,
            norm_layer=self.norm_layer,
        )
        self.skip_project = _SkipProject(
            self.skip_project_in_channels, 32, norm_layer=self.norm_layer
        )
        self.aspp = _ASPP(
            in_channels=self.aspp_in_channels,
            atrous_rates=[12, 24, 36],
            out_channels=ch,
            project_dropout=project_dropout,
            norm_layer=self.norm_layer,
        )

        if inference_mode:
            self.set_prediction_mode()

    def load_pretrained_weights(self):
        pretrained = ResNetBackbone(
            backbone=self.backbone_name,
            pretrained_base=True,
            norm_layer=self.backbone_norm_layer,
            **self._kwargs
        )
        backbone_state_dict = self.backbone.state_dict()
        pretrained_state_dict = pretrained.state_dict()

        backbone_state_dict.update(pretrained_state_dict)
        self.backbone.load_state_dict(backbone_state_dict)

        if self.inference_mode:
            for param in self.backbone.parameters():
                param.requires_grad = False

    def set_prediction_mode(self):
        self.inference_mode = True
        self.eval()

    def forward(self, x, additional_features=None):
        with ExitStack() as stack:
            if self.inference_mode:
                stack.enter_context(torch.no_grad())

            c1, _, c3, c4 = self.backbone(x, additional_features)
            c1 = self.skip_project(c1)

            x = self.aspp(c4)
            x = F.interpolate(x, c1.size()[2:], mode="bilinear", align_corners=True)
            x = torch.cat((x, c1), dim=1)
            x = self.head(x)

        return (x,)


class _SkipProject(nn.Module):
    def __init__(self, in_channels, out_channels, norm_layer=nn.BatchNorm2d):
        super(_SkipProject, self).__init__()
        _activation = ops.select_activation_function("relu")

        self.skip_project = nn.Sequential(
            nn.Conv2d(in_channels, out_channels, kernel_size=1, bias=False),
            norm_layer(out_channels),
            _activation(),
        )

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


class _DeepLabHead(nn.Module):
    def __init__(
        self, out_channels, in_channels, mid_channels=256, norm_layer=nn.BatchNorm2d
    ):
        super(_DeepLabHead, self).__init__()

        self.block = nn.Sequential(
            SeparableConv2d(
                in_channels=in_channels,
                out_channels=mid_channels,
                dw_kernel=3,
                dw_padding=1,
                activation="relu",
                norm_layer=norm_layer,
            ),
            SeparableConv2d(
                in_channels=mid_channels,
                out_channels=mid_channels,
                dw_kernel=3,
                dw_padding=1,
                activation="relu",
                norm_layer=norm_layer,
            ),
            nn.Conv2d(
                in_channels=mid_channels, out_channels=out_channels, kernel_size=1
            ),
        )

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


class _ASPP(nn.Module):
    def __init__(
        self,
        in_channels,
        atrous_rates,
        out_channels=256,
        project_dropout=0.5,
        norm_layer=nn.BatchNorm2d,
    ):
        super(_ASPP, self).__init__()

        b0 = nn.Sequential(
            nn.Conv2d(
                in_channels=in_channels,
                out_channels=out_channels,
                kernel_size=1,
                bias=False,
            ),
            norm_layer(out_channels),
            nn.ReLU(),
        )

        rate1, rate2, rate3 = tuple(atrous_rates)
        b1 = _ASPPConv(in_channels, out_channels, rate1, norm_layer)
        b2 = _ASPPConv(in_channels, out_channels, rate2, norm_layer)
        b3 = _ASPPConv(in_channels, out_channels, rate3, norm_layer)
        b4 = _AsppPooling(in_channels, out_channels, norm_layer=norm_layer)

        self.concurent = nn.ModuleList([b0, b1, b2, b3, b4])

        project = [
            nn.Conv2d(
                in_channels=5 * out_channels,
                out_channels=out_channels,
                kernel_size=1,
                bias=False,
            ),
            norm_layer(out_channels),
            nn.ReLU(),
        ]
        if project_dropout > 0:
            project.append(nn.Dropout(project_dropout))
        self.project = nn.Sequential(*project)

    def forward(self, x):
        x = torch.cat([block(x) for block in self.concurent], dim=1)

        return self.project(x)


class _AsppPooling(nn.Module):
    def __init__(self, in_channels, out_channels, norm_layer):
        super(_AsppPooling, self).__init__()

        self.gap = nn.Sequential(
            nn.AdaptiveAvgPool2d((1, 1)),
            nn.Conv2d(
                in_channels=in_channels,
                out_channels=out_channels,
                kernel_size=1,
                bias=False,
            ),
            norm_layer(out_channels),
            nn.ReLU(),
        )

    def forward(self, x):
        pool = self.gap(x)
        return F.interpolate(pool, x.size()[2:], mode="bilinear", align_corners=True)


def _ASPPConv(in_channels, out_channels, atrous_rate, norm_layer):
    block = nn.Sequential(
        nn.Conv2d(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=3,
            padding=atrous_rate,
            dilation=atrous_rate,
            bias=False,
        ),
        norm_layer(out_channels),
        nn.ReLU(),
    )

    return block