forked from MenghaoGuo/Awesome-Vision-Attentions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eca_module.py
executable file
·40 lines (29 loc) · 1002 Bytes
/
eca_module.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
import jittor as jt
import jittor.nn as nn
class ECALayer(nn.Module):
"""
Constructs a ECA module.
Args:
k_size: Adaptive selection of kernel size
"""
def __init__(self, k_size=3):
super(ECALayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.conv = nn.Conv1d(1, 1, kernel_size=k_size,
padding=(k_size - 1) // 2, bias=False)
self.sigmoid = nn.Sigmoid()
def execute(self, x):
# feature descriptor on the global spatial information
y = self.avg_pool(x)
# Two different branches of ECA module
y = self.conv(y.squeeze(-1).transpose(-1, -2)
).transpose(-1, -2).unsqueeze(-1)
y = self.sigmoid(y)
return x * y.expand_as(x)
def main():
attention_block = ECALayer()
input = jt.rand([4, 64, 32, 32])
output = attention_block(input)
print(input.size(), output.size())
if __name__ == '__main__':
main()