-
Notifications
You must be signed in to change notification settings - Fork 3
/
UAFM.py
326 lines (273 loc) · 10.8 KB
/
UAFM.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
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
326
import torch
import torch.nn as nn
import torch.nn.functional as F
def avg_reduce_channel(x):
# Reduce channel by avg
# Return cat([avg_ch_0, avg_ch_1, ...])
if not isinstance(x, (list, tuple)):
return torch.mean(x, dim=1, keepdim=True)
elif len(x) == 1:
return torch.mean(x[0], dim=1, keepdim=True)
else:
res = []
for xi in x:
res.append(torch.mean(xi, dim=1, keepdim=True))
return torch.cat(res, dim=1)
def avg_reduce_hw(x):
# Reduce hw by avg
# Return cat([avg_pool_0, avg_pool_1, ...])
if not isinstance(x, (list, tuple)):
return F.adaptive_avg_pool2d(x, 1)
elif len(x) == 1:
return F.adaptive_avg_pool2d(x[0], 1)
else:
res = []
for xi in x:
res.append(F.adaptive_avg_pool2d(xi, 1))
return torch.cat(res, dim=1)
def avg_max_reduce_channel_helper(x, use_concat=True):
# Reduce hw by avg and max, only support single input
assert not isinstance(x, (list, tuple))
mean_value = torch.mean(x, dim=1, keepdim=True)
max_value = torch.max(x, dim=1, keepdim=True)[0]
# print("mean_value: ", mean_value)
# print("max_value: ", max_value)
if use_concat:
res = torch.cat([mean_value, max_value], dim=1)
else:
res = [mean_value, max_value]
return res
def avg_max_reduce_channel(x):
# Reduce hw by avg and max
# Return cat([avg_ch_0, max_ch_0, avg_ch_1, max_ch_1, ...])
if not isinstance(x, (list, tuple)):
return avg_max_reduce_channel_helper(x)
elif len(x) == 1:
return avg_max_reduce_channel_helper(x[0])
else:
res = []
for xi in x:
res.extend(avg_max_reduce_channel_helper(xi, False))
return torch.cat(res, dim=1)
def avg_max_reduce_hw_helper(x, is_training, use_concat=True):
assert not isinstance(x, (list, tuple))
avg_pool = F.adaptive_avg_pool2d(x, 1)
# TODO(pjc): when dim=[2, 3], the paddle.max api has bug for training.
if is_training:
max_pool = F.adaptive_max_pool2d(x, 1)
else:
max_pool = F.adaptive_max_pool2d(x, 1)
if use_concat:
res = torch.cat([avg_pool, max_pool], dim=1)
else:
res = [avg_pool, max_pool]
return res
def avg_max_reduce_hw(x, is_training):
# Reduce hw by avg and max
# Return cat([avg_pool_0, avg_pool_1, ..., max_pool_0, max_pool_1, ...])
if not isinstance(x, (list, tuple)):
return avg_max_reduce_hw_helper(x, is_training)
elif len(x) == 1:
return avg_max_reduce_hw_helper(x[0], is_training)
else:
res_avg = []
res_max = []
for xi in x:
avg, max = avg_max_reduce_hw_helper(xi, is_training, False)
res_avg.append(avg)
res_max.append(max)
res = res_avg + res_max
return torch.cat(res, dim=1)
class ConvBNReLU(nn.Module):
def __init__(self, in_planes, out_planes, kernel=3, stride=1):
super(ConvBNReLU, self).__init__()
self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel, stride=stride, padding=kernel // 2, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
out = self.relu(self.bn(self.conv(x)))
return out
class ConvBN(nn.Module):
def __init__(self, in_planes, out_planes, kernel=3, stride=1):
super(ConvBN, self).__init__()
self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel, stride=stride, padding=kernel // 2, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
def forward(self, x):
out = self.bn(self.conv(x))
return out
class ConvBNAct(nn.Module):
def __init__(self, in_planes, out_planes, kernel=3, stride=1, act_type="leakyrelu"):
super(ConvBNAct, self).__init__()
self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel, stride=stride, padding=kernel // 2, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
if act_type == "leakyrelu":
self.act = nn.LeakyReLU(inplace=True)
else:
self.act = nn.ReLU(inplace=True)
def forward(self, x):
out = self.act(self.bn(self.conv(x)))
return out
class UAFM(nn.Module):
"""
The base of Unified Attention Fusion Module.
Args:
x_ch (int): The channel of x tensor, which is the low level feature.
y_ch (int): The channel of y tensor, which is the high level feature.
out_ch (int): The channel of output tensor.
ksize (int, optional): The kernel size of the conv for x tensor. Default: 3.
resize_mode (str, optional): The resize model in unsampling y tensor. Default: bilinear.
"""
def __init__(self, x_ch, y_ch, out_ch, ksize=3, resize_mode='bilinear'):
super().__init__()
self.conv_x = ConvBNReLU(
x_ch, y_ch, kernel=ksize)
self.conv_out = ConvBNReLU(
y_ch, out_ch, kernel=3)
self.resize_mode = resize_mode
def check(self, x, y):
assert x.ndim == 4 and y.ndim == 4
x_h, x_w = x.shape[2:]
y_h, y_w = y.shape[2:]
assert x_h >= y_h and x_w >= y_w
def prepare(self, x, y):
x = self.prepare_x(x, y)
y = self.prepare_y(x, y)
return x, y
def prepare_x(self, x, y):
x = self.conv_x(x)
return x
def prepare_y(self, x, y):
y_up = F.interpolate(y, x.shape[2:], mode=self.resize_mode)
return y_up
def fuse(self, x, y):
out = x + y
out = self.conv_out(out)
return out
def forward(self, x, y):
"""
Args:
x (Tensor): The low level feature.
y (Tensor): The high level feature.
"""
self.check(x, y)
x, y = self.prepare(x, y)
out = self.fuse(x, y)
return out
class UAFM_ChAtten(UAFM):
"""
The UAFM with channel attention, which uses mean and max values.
Args:
x_ch (int): The channel of x tensor, which is the low level feature.
y_ch (int): The channel of y tensor, which is the high level feature.
out_ch (int): The channel of output tensor.
ksize (int, optional): The kernel size of the conv for x tensor. Default: 3.
resize_mode (str, optional): The resize model in unsampling y tensor. Default: bilinear.
"""
def __init__(self, x_ch, y_ch, out_ch, ksize=3, resize_mode='bilinear'):
super().__init__(x_ch, y_ch, out_ch, ksize, resize_mode)
self.conv_xy_atten = nn.Sequential(
ConvBNAct(
4 * y_ch,
y_ch // 2,
kernel=1,
act_type="leakyrelu"),
ConvBN(y_ch // 2, y_ch, kernel=1))
def fuse(self, x, y):
"""
Args:
x (Tensor): The low level feature.
y (Tensor): The high level feature.
"""
atten = avg_max_reduce_hw([x, y], self.training)
atten = F.sigmoid(self.conv_xy_atten(atten))
out = x * atten + y * (1 - atten)
out = self.conv_out(out)
return out
class UAFM_ChAtten_S(UAFM):
"""
The UAFM with channel attention, which uses mean values.
Args:
x_ch (int): The channel of x tensor, which is the low level feature.
y_ch (int): The channel of y tensor, which is the high level feature.
out_ch (int): The channel of output tensor.
ksize (int, optional): The kernel size of the conv for x tensor. Default: 3.
resize_mode (str, optional): The resize model in unsampling y tensor. Default: bilinear.
"""
def __init__(self, x_ch, y_ch, out_ch, ksize=3, resize_mode='bilinear'):
super().__init__(x_ch, y_ch, out_ch, ksize, resize_mode)
self.conv_xy_atten = nn.Sequential(
ConvBNAct(
2 * y_ch,
y_ch // 2,
kernel=1,
act_type="leakyrelu"),
ConvBN(
y_ch // 2, y_ch, kernel=1))
def fuse(self, x, y):
"""
Args:
x (Tensor): The low level feature.
y (Tensor): The high level feature.
"""
atten = avg_reduce_hw([x, y])
atten = F.sigmoid(self.conv_xy_atten(atten))
out = x * atten + y * (1 - atten)
out = self.conv_out(out)
return out
class UAFM_SpAtten(UAFM):
"""
The UAFM with spatial attention, which uses mean and max values.
Args:
x_ch (int): The channel of x tensor, which is the low level feature.
y_ch (int): The channel of y tensor, which is the high level feature.
out_ch (int): The channel of output tensor.
ksize (int, optional): The kernel size of the conv for x tensor. Default: 3.
resize_mode (str, optional): The resize model in unsampling y tensor. Default: bilinear.
"""
def __init__(self, x_ch, y_ch, out_ch, ksize=3, resize_mode='bilinear'):
super().__init__(x_ch, y_ch, out_ch, ksize, resize_mode)
self.conv_xy_atten = nn.Sequential(
ConvBNReLU(
4, 2, kernel=3),
ConvBN(
2, 1, kernel=3))
def fuse(self, x, y):
"""
Args:
x (Tensor): The low level feature.
y (Tensor): The high level feature.
"""
atten = avg_max_reduce_channel([x, y])
# print(atten.shape)
atten = F.sigmoid(self.conv_xy_atten(atten))
out = x * atten + y * (1 - atten)
out = self.conv_out(out)
return out
class UAFM_SpAtten_S(UAFM):
"""
The UAFM with spatial attention, which uses mean values.
Args:
x_ch (int): The channel of x tensor, which is the low level feature.
y_ch (int): The channel of y tensor, which is the high level feature.
out_ch (int): The channel of output tensor.
ksize (int, optional): The kernel size of the conv for x tensor. Default: 3.
resize_mode (str, optional): The resize model in unsampling y tensor. Default: bilinear.
"""
def __init__(self, x_ch, y_ch, out_ch, ksize=3, resize_mode='bilinear'):
super().__init__(x_ch, y_ch, out_ch, ksize, resize_mode)
self.conv_xy_atten = nn.Sequential(
ConvBNReLU(
2, 2, kernel=3),
ConvBN(
2, 1, kernel=3))
def fuse(self, x, y):
"""
Args:
x (Tensor): The low level feature.
y (Tensor): The high level feature.
"""
atten = avg_reduce_channel([x, y])
atten = F.sigmoid(self.conv_xy_atten(atten))
out = x * atten + y * (1 - atten)
out = self.conv_out(out)
return out