import torch
import torch.nn as nn
import torch.nn.init as init

class channelAttention(nn.Module):
    def __init__(self, channel, reduction=16):
        super(channelAttention, self).__init__()
        self.Maxpooling = nn.AdaptiveMaxPool2d(1)
        self.Avepooling = nn.AdaptiveAvgPool2d(1)
        self.ca = nn.Sequential()
        self.ca.add_module('conv1',nn.Conv2d(channel, channel//reduction, 1, bias=False))
        self.ca.add_module('Relu', nn.ReLU())
        self.ca.add_module('conv2',nn.Conv2d(channel//reduction, channel, 1, bias=False))
        self.sigmod = nn.Sigmoid()

    def forward(self, x):
        M_out = self.Maxpooling(x)
        A_out = self.Avepooling(x)
        M_out = self.ca(M_out)
        A_out = self.ca(A_out)
        out = self.sigmod(M_out+A_out)
        return out

class SpatialAttention(nn.Module):
    def __init__(self, kernel_size=7):
        super().__init__()
        self.conv = nn.Conv2d(in_channels=2, out_channels=1, kernel_size=kernel_size, padding=kernel_size // 2)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        max_result, _ = torch.max(x, dim=1, keepdim=True)
        avg_result = torch.mean(x, dim=1, keepdim=True)
        result = torch.cat([max_result, avg_result], dim=1)
        output = self.conv(result)
        output = self.sigmoid(output)
        return output

class CBAM(nn.Module):
    def __init__(self, channel, reduction=16, kernel_size=7):
        super().__init__()
        self.ca = channelAttention(channel, reduction)
        self.sa = SpatialAttention(kernel_size)

    def init_weights(self):
        for m in self.modules():#权重初始化
            if isinstance(m, nn.Conv2d):
                init.kaiming_normal_(m.weight, mode='fan_out')
                if m.bias is not None:
                    init.constant_(m.bias, 0)
            elif isinstance(m, nn.BatchNorm2d):
                init.constant_(m.weight, 1)
                init.constant_(m.bias, 0)
            elif isinstance(m, nn.Linear):
                init.normal_(m.weight, std=0.001)
                if m.bias is not None:
                    init.constant_(m.bias, 0)

    def forward(self, x):
        # b,c_,_ = x.size()
        # residual = x
        out = x*self.ca(x)
        out = out*self.sa(out)
        return out

if __name__ == '__main__':
    input=torch.randn(50,512,7,7)
    kernel_size=input.shape[2]
    cbam = CBAM(channel=512,reduction=16,kernel_size=kernel_size)
    output=cbam(input)
    print(output.shape)
