decoupled_light_FGEM_safe

This commit is contained in:
Zhang Wei committed 2026-03-18 09:46:54 +08:00
1 parent 36a232a655
commit 592c80a4ad
5 files changed
+121 -3

No files matched your search

+33
View File
@@ -1121,3 +1121,36 @@ 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 FGEMSafe(nn.Module):
"""Safer lightweight fine-grained enhancement block for cls branch."""
def __init__(self, c1, c_mid=None, residual_scale=0.1):
super().__init__()
c_mid = c_mid or max(c1 // 2, 32)
self.residual_scale = residual_scale
# 1x1 reduce
self.reduce = Conv(c1, c_mid, 1, 1)
# depthwise 3x3 local modeling
self.dw = nn.Sequential(
nn.Conv2d(c_mid, c_mid, kernel_size=3, stride=1, padding=1, groups=c_mid, bias=False),
nn.BatchNorm2d(c_mid),
nn.SiLU()
)
# pointwise fusion
self.pw = Conv(c_mid, c_mid, 1, 1)
# 1x1 expand
self.expand = Conv(c_mid, c1, 1, 1)
def forward(self, x):
identity = x
y = self.reduce(x)
y = self.dw(y)
y = self.pw(y)
y = self.expand(y)
return identity + self.residual_scale * y