-
Notifications
You must be signed in to change notification settings - Fork 512
/
aspp_block.py
248 lines (217 loc) · 7.61 KB
/
aspp_block.py
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
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2024 Apple Inc. All Rights Reserved.
#
import argparse
from typing import Optional, Tuple
import torch
import torch.nn.functional as F
from torch import Tensor, nn
from corenet.modeling.layers import (
AdaptiveAvgPool2d,
BaseLayer,
ConvLayer2d,
Dropout2d,
SeparableConv2d,
)
from corenet.modeling.modules import BaseModule
from corenet.utils import logger
from corenet.utils.ddp_utils import is_master
class ASPP(BaseModule):
"""
ASPP module defined in DeepLab papers, `here <https://arxiv.org/abs/1606.00915>`_ and `here <https://arxiv.org/abs/1706.05587>`_
Args:
opts: command-line arguments
in_channels (int): :math:`C_{in}` from an expected input of size :math:`(N, C_{in}, H, W)`
out_channels (int): :math:`C_{out}` from an expected output of size :math:`(N, C_{out}, H, W)`
atrous_rates (Tuple[int]): atrous rates for different branches.
is_sep_conv (Optional[bool]): Use separable convolution instead of standaard conv. Default: False
dropout (Optional[float]): Apply dropout. Default is 0.0
Shape:
- Input: :math:`(N, C_{in}, H, W)`
- Output: :math:`(N, C_{out}, H, W)`
"""
def __init__(
self,
opts,
in_channels: int,
out_channels: int,
atrous_rates: Tuple[int],
is_sep_conv: Optional[bool] = False,
dropout: Optional[float] = 0.0,
*args,
**kwargs
) -> None:
in_proj = ConvLayer2d(
opts=opts,
in_channels=in_channels,
out_channels=out_channels,
kernel_size=1,
stride=1,
use_norm=True,
use_act=True,
)
out_proj = ConvLayer2d(
opts=opts,
in_channels=5 * out_channels,
out_channels=out_channels,
kernel_size=1,
stride=1,
use_norm=True,
use_act=True,
)
aspp_layer = ASPPSeparableConv2d if is_sep_conv else ASPPConv2d
assert len(atrous_rates) == 3
modules = [in_proj]
modules.extend(
[
aspp_layer(
opts=opts,
in_channels=in_channels,
out_channels=out_channels,
dilation=rate,
)
for rate in atrous_rates
]
)
modules.append(
ASPPPooling(opts=opts, in_channels=in_channels, out_channels=out_channels)
)
if not (0.0 <= dropout < 1.0):
if is_master(opts):
logger.warning(
"Dropout value in {} should be between 0 and 1. Got: {}. Setting it to 0.0".format(
self.__class__.__name__, dropout
)
)
dropout = 0.0
super().__init__()
self.convs = nn.ModuleList(modules)
self.project = out_proj
self.in_channels = in_channels
self.out_channels = out_channels
self.atrous_rates = atrous_rates
self.is_sep_conv_layer = is_sep_conv
self.n_atrous_branches = len(atrous_rates)
self.dropout_layer = Dropout2d(p=dropout)
def forward(self, x: Tensor, *args, **kwargs) -> Tensor:
out = []
for conv in self.convs:
out.append(conv(x))
out = torch.cat(out, dim=1)
out = self.project(out)
out = self.dropout_layer(out)
return out
def __repr__(self):
return "{}(in_channels={}, out_channels={}, atrous_rates={}, is_aspp_sep={}, dropout={})".format(
self.__class__.__name__,
self.in_channels,
self.out_channels,
self.atrous_rates,
self.is_sep_conv_layer,
self.dropout_layer.p,
)
class ASPPConv2d(ConvLayer2d):
"""
Convolution with a dilation for the ASPP module
Args:
opts: command-line arguments
in_channels (int): :math:`C_{in}` from an expected input of size :math:`(N, C_{in}, H, W)`
out_channels (int): :math:`C_{out}` from an expected output of size :math:`(N, C_{out}, H, W)`
dilation (int): Dilation rate
Shape:
- Input: :math:`(N, C_{in}, H, W)`
- Output: :math:`(N, C_{out}, H, W)`
"""
def __init__(
self, opts, in_channels: int, out_channels: int, dilation: int, *args, **kwargs
) -> None:
super().__init__(
opts=opts,
in_channels=in_channels,
out_channels=out_channels,
kernel_size=3,
stride=1,
use_norm=True,
use_act=True,
dilation=dilation,
)
def adjust_atrous_rate(self, rate: int) -> None:
"""This function allows to adjust the dilation rate"""
self.block.conv.dilation = rate
# padding is the same here
# see ConvLayer to see the method for computing padding
self.block.conv.padding = rate
class ASPPSeparableConv2d(SeparableConv2d):
"""
Separable Convolution with a dilation for the ASPP module
Args:
opts: command-line arguments
in_channels (int): :math:`C_{in}` from an expected input of size :math:`(N, C_{in}, H, W)`
out_channels (int): :math:`C_{out}` from an expected output of size :math:`(N, C_{out}, H, W)`
dilation (int): Dilation rate
Shape:
- Input: :math:`(N, C_{in}, H, W)`
- Output: :math:`(N, C_{out}, H, W)`
"""
def __init__(
self, opts, in_channels: int, out_channels: int, dilation: int, *args, **kwargs
) -> None:
super().__init__(
opts=opts,
in_channels=in_channels,
out_channels=out_channels,
kernel_size=3,
stride=1,
dilation=dilation,
use_norm=True,
use_act=True,
)
def adjust_atrous_rate(self, rate: int) -> None:
"""This function allows to adjust the dilation rate"""
self.dw_conv.block.conv.dilation = rate
# padding is the same here
# see ConvLayer to see the method for computing padding
self.dw_conv.block.conv.padding = rate
class ASPPPooling(BaseLayer):
"""
ASPP pooling layer
Args:
opts: command-line arguments
in_channels (int): :math:`C_{in}` from an expected input of size :math:`(N, C_{in}, H, W)`
out_channels (int): :math:`C_{out}` from an expected output of size :math:`(N, C_{out}, H, W)`
Shape:
- Input: :math:`(N, C_{in}, H, W)`
- Output: :math:`(N, C_{out}, H, W)`
"""
def __init__(
self, opts, in_channels: int, out_channels: int, *args, **kwargs
) -> None:
super().__init__()
self.aspp_pool = nn.Sequential()
self.aspp_pool.add_module(
name="global_pool", module=AdaptiveAvgPool2d(output_size=1)
)
self.aspp_pool.add_module(
name="conv_1x1",
module=ConvLayer2d(
opts=opts,
in_channels=in_channels,
out_channels=out_channels,
kernel_size=1,
stride=1,
use_norm=True,
use_act=True,
),
)
self.in_channels = in_channels
self.out_channels = out_channels
def forward(self, x: Tensor) -> Tensor:
x_size = x.shape[-2:]
x = self.aspp_pool(x)
x = F.interpolate(x, size=x_size, mode="bilinear", align_corners=False)
return x
def __repr__(self):
return "{}(in_channels={}, out_channels={})".format(
self.__class__.__name__, self.in_channels, self.out_channels
)