| 220 | |
| 221 | |
| 222 | class SpatialAttentionModule(nn.Module): |
| 223 | def __init__(self): |
| 224 | super(SpatialAttentionModule, self).__init__() |
| 225 | self.conv2d = nn.Conv2d(in_channels=2, out_channels=1, kernel_size=7, stride=1, padding=3) |
| 226 | self.sigmoid = nn.Sigmoid() |
| 227 | |
| 228 | def forward(self, x): |
| 229 | avgout = torch.mean(x, dim=1, keepdim=True) |
| 230 | maxout, _ = torch.max(x, dim=1, keepdim=True) |
| 231 | out = torch.cat([avgout, maxout], dim=1) |
| 232 | out = self.sigmoid(self.conv2d(out)) |
| 233 | return out |
| 234 | |
| 235 | |
| 236 | class CBAM(nn.Module): |