File size: 7,626 Bytes
a7dedf9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from torch import nn, Tensor

from typing import Union

from .blocks import DepthSeparableConv2d, conv1x1, conv3x3
from .utils import _init_weights


class ConvDownsample(nn.Module):
    def __init__(
        self,
        in_channels: int,
        out_channels: int,
        norm_layer: Union[nn.BatchNorm2d, nn.GroupNorm, None] = nn.BatchNorm2d,
        activation: nn.Module = nn.ReLU(inplace=True),
        groups: int = 1,
    ) -> None:
        super().__init__()
        assert isinstance(groups, int) and groups > 0, f"Number of groups should be an integer greater than 0, but got {groups}."
        assert in_channels % groups == 0, f"Number of input channels {in_channels} should be divisible by number of groups {groups}."
        assert out_channels % groups == 0, f"Number of output channels {out_channels} should be divisible by number of groups {groups}."
        self.grouped_conv = groups > 1

        # conv1 is used for downsampling
        # self.conv1 = nn.Conv2d(
        #     in_channels=in_channels,
        #     out_channels=in_channels,
        #     kernel_size=2,
        #     stride=2,
        #     padding=0,
        #     bias=not norm_layer,
        #     groups=groups,
        # )
        # if self.grouped_conv:
        #     self.conv1_1x1 = conv1x1(in_channels, in_channels, stride=1, bias=not norm_layer)
        self.conv1 = nn.AvgPool2d(kernel_size=2, stride=2)  # downsample by 2
        if self.grouped_conv:
            self.conv1_1x1 = nn.Identity()
        
        self.norm1 = norm_layer(in_channels) if norm_layer else nn.Identity()
        self.act1 = activation

        self.conv2 = conv3x3(
            in_channels=in_channels,
            out_channels=in_channels,
            stride=1,
            groups=groups,
            bias=not norm_layer,
        )
        if self.grouped_conv:
            self.conv2_1x1 = conv1x1(in_channels, in_channels, stride=1, bias=not norm_layer)
        
        self.norm2 = norm_layer(in_channels) if norm_layer else nn.Identity()
        self.act2 = activation

        self.conv3 = conv3x3(
            in_channels=in_channels,
            out_channels=out_channels,
            stride=1,
            groups=groups,
            bias=not norm_layer,
        )
        if self.grouped_conv:
            self.conv3_1x1 = conv1x1(out_channels, out_channels, stride=1, bias=not norm_layer)

        self.norm3 = norm_layer(out_channels) if norm_layer else nn.Identity()
        self.act3 = activation

        self.downsample = nn.Sequential(
            nn.AvgPool2d(kernel_size=2, stride=2),  # make sure the spatial sizes match
            conv1x1(in_channels, out_channels, stride=1, bias=not norm_layer),
            norm_layer(out_channels) if norm_layer else nn.Identity(),
        )
        
        self.apply(_init_weights)
    
    def forward(self, x: Tensor) -> Tensor:
        identity = x

        # downsample
        out = self.conv1(x)
        out = self.conv1_1x1(out) if self.grouped_conv else out
        out = self.norm1(out)
        out = self.act1(out)

        out = self.conv2(out)
        out = self.conv2_1x1(out) if self.grouped_conv else out
        out = self.norm2(out)
        out = self.act2(out)

        out = self.conv3(out)
        out = self.conv3_1x1(out) if self.grouped_conv else out
        out = self.norm3(out)

        # shortcut
        out += self.downsample(identity)
        out = self.act3(out)
        return out


class LightConvDownsample(nn.Module):
    def __init__(
        self,
        in_channels: int,
        out_channels: int,
        norm_layer: Union[nn.BatchNorm2d, nn.GroupNorm, None] = nn.BatchNorm2d,
        activation: nn.Module = nn.ReLU(inplace=True),
    ) -> None:
        super().__init__()
        self.conv1 = DepthSeparableConv2d(
            in_channels=in_channels,
            out_channels=in_channels,
            kernel_size=2,
            stride=2,
            padding=0,
            bias=not norm_layer,
        )
        self.norm1 = norm_layer(in_channels) if norm_layer else nn.Identity()
        self.act1 = activation

        self.conv2 = DepthSeparableConv2d(
            in_channels=in_channels,
            out_channels=out_channels,
            kernel_size=3,
            stride=1,
            padding=1,
            bias=not norm_layer,
        )
        self.norm2 = norm_layer(out_channels) if norm_layer else nn.Identity()
        self.act2 = activation

        self.conv3 = DepthSeparableConv2d(
            in_channels=out_channels,
            out_channels=out_channels,
            kernel_size=3,
            stride=1,
            padding=1,
            bias=not norm_layer,
        )
        self.norm3 = norm_layer(out_channels) if norm_layer else nn.Identity()
        self.act3 = activation

        self.downsample = nn.Sequential(
            nn.AvgPool2d(kernel_size=2, stride=2),  # make sure the spatial sizes match
            conv1x1(in_channels, out_channels, stride=1, bias=not norm_layer),
            norm_layer(out_channels) if norm_layer else nn.Identity(),
        )

        self.apply(_init_weights)

    def forward(self, x: Tensor) -> Tensor:
        identity = x

        # downsample
        out = self.conv1(x)
        out = self.norm1(out)
        out = self.act1(out)

        # refine 1
        out = self.conv2(out)
        out = self.norm2(out)
        out = self.act2(out)

        # refine 2
        out = self.conv3(out)
        out = self.norm3(out)

        # shortcut
        out += self.downsample(identity)
        out = self.act3(out)
        return x
    

class LighterConvDownsample(nn.Module):
    def __init__(
        self,
        in_channels: int,
        out_channels: int,
        norm_layer: Union[nn.BatchNorm2d, nn.GroupNorm, None] = nn.BatchNorm2d,
        activation: nn.Module = nn.ReLU(inplace=True),
    ) -> None:
        super().__init__()
        self.conv1 = DepthSeparableConv2d(
            in_channels=in_channels,
            out_channels=in_channels,
            kernel_size=2,
            stride=2,
            padding=0,
            bias=not norm_layer,
        )
        self.norm1 = norm_layer(in_channels) if norm_layer else nn.Identity()
        self.act1 = activation

        self.conv2 = conv3x3(
            in_channels=in_channels,
            out_channels=in_channels,
            stride=1,
            groups=in_channels,
            bias=not norm_layer,
        )
        self.norm2 = norm_layer(in_channels) if norm_layer else nn.Identity()
        self.act2 = activation

        self.conv3 = conv1x1(
            in_channels=in_channels,
            out_channels=out_channels,
            stride=1,
            bias=not norm_layer,
        )
        self.norm3 = norm_layer(out_channels) if norm_layer else nn.Identity()
        self.act3 = activation

        self.downsample = nn.Sequential(
            nn.AvgPool2d(kernel_size=2, stride=2),  # make sure the spatial sizes match
            conv1x1(in_channels, out_channels, stride=1, bias=not norm_layer),
            norm_layer(out_channels) if norm_layer else nn.Identity(),
        )
    
    def forward(self, x: Tensor) -> Tensor:
        identity = x

        # downsample
        out = self.conv1(x)
        out = self.norm1(out)
        out = self.act1(out)

        # refine, depthwise conv
        out = self.conv2(out)
        out = self.norm2(out)
        out = self.act2(out)

        # refine, pointwise conv
        out = self.conv3(out)
        out = self.norm3(out)

        # shortcut
        out += self.downsample(identity)
        out = self.act3(out)
        return out