Open NUDTbowen opened 1 year ago
Hello, author. If I want to merge the module containing multiplication,How i shoudl do ? For example,the【Convolutional Block Attention Module】is following:
class ChannelAttentionModule(nn.Module): def __init__(self, channel, ratio=16): super(ChannelAttentionModule, self).__init__() self.avg_pool = nn.AdaptiveAvgPool2d(1) self.max_pool = nn.AdaptiveMaxPool2d(1) self.shared_MLP = nn.Sequential( nn.Conv2d(channel, channel // ratio, 1, bias=False), nn.ReLU(), nn.Conv2d(channel // ratio, channel, 1, bias=False) ) self.sigmoid = nn.Sigmoid() def forward(self, x): avgout = self.shared_MLP(self.avg_pool(x)) # print(avgout.shape) maxout = self.shared_MLP(self.max_pool(x)) return self.sigmoid(avgout + maxout) class SpatialAttentionModule(nn.Module): def __init__(self): super(SpatialAttentionModule, self).__init__() self.conv2d = nn.Conv2d(in_channels=2, out_channels=1, kernel_size=7, stride=1, padding=3,bias=False) self.sigmoid = nn.Sigmoid() def forward(self, x): avgout = torch.mean(x, dim=1, keepdim=True) maxout, _ = torch.max(x, dim=1, keepdim=True) out = torch.cat([avgout, maxout], dim=1) out = self.sigmoid(self.conv2d(out)) return out class CBAM(nn.Module): def __init__(self, channel): super(CBAM, self).__init__() self.channel_attention = ChannelAttentionModule(channel) self.spatial_attention = SpatialAttentionModule() def forward(self, x): cha_weight=self.channel_attention(x) out = cha_weight * x # print('outchannels:{}'.format(out.shape)) spa_weight=self.spatial_attention(out) out = spa_weight * out return out
Hello, author. If I want to merge the module containing multiplication,How i shoudl do ? For example,the【Convolutional Block Attention Module】is following: