| 165 | return x * se.view(b, c, 1, 1) |
| 166 | |
| 167 | class SpatialAttention(nn.Module): |
| 168 | def __init__(self, kernel_size=7): |
| 169 | super().__init__() |
| 170 | self.conv = nn.Conv2d(2, 1, kernel_size, padding=kernel_size // 2) |
| 171 | |
| 172 | def forward(self, x): |
| 173 | avg_out = torch.mean(x, dim=1, keepdim=True) |
| 174 | max_out, _ = torch.max(x, dim=1, keepdim=True) |
| 175 | attention = torch.cat([avg_out, max_out], dim=1) |
| 176 | attention = torch.sigmoid(self.conv(attention)) |
| 177 | return x * attention |
| 178 | |
| 179 | class CBAM(nn.Module): |
| 180 | def __init__(self, channels, reduction=16, kernel_size=7): |