<Neck> add ECA after P3P4P5
This commit is contained in:
1 parent
85acef3a28
commit
97c4309f9d
3 files changed
+77
-17
No files matched your search
@@ -1121,3 +1121,25 @@ class Classify(nn.Module):
|
||||
if isinstance(x, list):
|
||||
x = torch.cat(x, 1)
|
||||
return self.linear(self.drop(self.pool(self.conv(x)).flatten(1)))
|
||||
|
||||
# 以下是我自己添加的
|
||||
class ECA(nn.Module):
|
||||
"""Efficient Channel Attention"""
|
||||
def __init__(self, channels, k_size=3):
|
||||
super().__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 forward(self, x):
|
||||
# x: [B, C, H, W]
|
||||
y = self.avg_pool(x) # [B, C, 1, 1]
|
||||
y = y.squeeze(-1).transpose(-1, -2) # [B, 1, C]
|
||||
y = self.conv(y) # [B, 1, C]
|
||||
y = self.sigmoid(y)
|
||||
y = y.transpose(-1, -2).unsqueeze(-1) # [B, C, 1, 1]
|
||||
return x * y
|
||||
Reference in new issue
Block a user