| 49 | |
| 50 | |
| 51 | class ChannelAttention(nn.Module): |
| 52 | def __init__(self,channel,reduction=16): |
| 53 | super().__init__() |
| 54 | self.maxpool=nn.AdaptiveMaxPool2d(1) |
| 55 | self.avgpool=nn.AdaptiveAvgPool2d(1) |
| 56 | self.se=nn.Sequential( |
| 57 | nn.Conv2d(channel,channel//reduction,1,bias=False), |
| 58 | nn.ReLU(), |
| 59 | nn.Conv2d(channel//reduction,channel,1,bias=False) |
| 60 | ) |
| 61 | self.sigmoid=nn.Sigmoid() |
| 62 | |
| 63 | def forward(self, x) : |
| 64 | max_result=self.maxpool(x) |
| 65 | avg_result=self.avgpool(x) |
| 66 | max_out=self.se(max_result) |
| 67 | avg_out=self.se(avg_result) |
| 68 | output=self.sigmoid(max_out+avg_out) |
| 69 | return output |
| 70 | |
| 71 | class SpatialAttention(nn.Module): |
| 72 | def __init__(self,kernel_size=7): |