landskape-ai / triplet-attention

Official PyTorch Implementation for "Rotate to Attend: Convolutional Triplet Attention Module." [WACV 2021]
https://openaccess.thecvf.com/content/WACV2021/html/Misra_Rotate_to_Attend_Convolutional_Triplet_Attention_Module_WACV_2021_paper.html
MIT License
406 stars 49 forks source link

怎么修改能一键替换SE 和CBAM #17

Closed dengfenglai321 closed 3 years ago

dengfenglai321 commented 3 years ago

如题,有没有方法直接一键替换SE和CBAM

triplet_attention模块初始化并不是与SE一样是channels参数

# SE attention
class SE(nn.Module):
    def __init__(self, in_channels, channels, se_ratio=12):
        super(SE, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Sequential(
            nn.Conv2d(in_channels, channels // se_ratio, kernel_size=1, padding=0),
            nn.BatchNorm2d(channels // se_ratio),
            nn.ReLU(inplace=True),
            nn.Conv2d(channels // se_ratio, channels, kernel_size=1, padding=0),
            nn.Sigmoid()
        )

    def forward(self, x):
        y = self.avg_pool(x)
        y = self.fc(y)
        return x * y
digantamisra98 commented 3 years ago

@cendelian SE and CBAM can be directly replaced with Triplet. CBAM, SE and Triplet are dimension preserving modules and thus don't affect the number of channels of the input tensor. SE and CBAM requires the channel parameter because it has a MLP structure which is not present in Triplet Attention. Hopefully this clarifies.

dengfenglai321 commented 3 years ago

@cendelian SE and CBAM can be directly replaced with Triplet. CBAM, SE and Triplet are dimension preserving modules and thus don't affect the number of channels of the input tensor. SE and CBAM requires the channel parameter because it has a MLP structure which is not present in Triplet Attention. Hopefully this clarifies.

thanks for you reply. and if I use Triplet to replace SE or CBAM. Does the pretrain can use? for example. I use resnet with SE attention, but when I replace SE with triplet, the resnet pretrain can use? maybe i need train resnet with triplet from scrach?

digantamisra98 commented 3 years ago

@cendelian No you can't use a SE or CBAM pretrained network and replace them with Triplet. You need to retrain the network with Triple Attention from scratch. But we have provided the weights for pretrained networks which you can use.