BUG解决:RuntimeError: Given groups=1, weight of size [14, 464, 1, 1], expected input[16, 116, 56, 1] to have 464 channels, but got 116 channels instead
首选说一下这个问题,这个问题提示想要得到的是464个通道数但是实际上得到的是116个通道。
例如我给某个深度学习网络中加CBAM注意力集中机制,具体可参照此文章链接: link.(以下为实现代码):

# 通道注意力机制class ChannelAttention(nn.Module):    def __init__(self, in_planes, ratio=16):        super(ChannelAttention, self).__init__()        self.avg_pool = nn.AdaptiveAvgPool2d(1)        self.max_pool = nn.AdaptiveMaxPool2d(1)        self.fc1 = nn.Conv2d(in_planes, in_planes //ratio, 1, bias=False)        self.relu1 = nn.ReLU()        self.fc2 = nn.Conv2d(in_planes //ratio, in_planes, 1, bias=False)        self.sigmoid = nn.Sigmoid()    def forward(self, x):        avg_out = self.fc2(self.relu1(self.fc1(self.avg_pool(x))))        max_out = self.fc2(self.relu1(self.fc1(self.max_pool(x))))        out = avg_out + max_out        return self.sigmoid(out)# 空间注意力机制class SpatialAttention(nn.Module):    def __init__(self, kernel_size=7):        super(SpatialAttention, self).__init__()        assert kernel_size in (3, 7), 'kernel size must be 3 or 7'        padding = 3 if kernel_size == 7 else 1        self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False)        self.sigmoid = nn.Sigmoid()    def forward(self, x):        avg_out = torch.mean(x, dim=1, keepdim=True)        max_out, _ = torch.max(x, dim=1, keepdim=True)        x = torch.cat([avg_out, max_out], dim=1)        x = self.conv1(x)        return self.sigmoid(x)

问题出现的可能原因1:

 # 在网络的某层加入CBAM注意力机制 self.ca = ChannelAttention(self.inplanes) self.sa = SpatialAttention()

self.inplanes修改为你上一层输出的通道数;
出现原因2:是我在假的过程中出现的错误,是一个非常小的错误,就是在初始层和末尾分别加入CBAM的时候,没有区分不同位置加入后的函数名,因此出现错误,例如如下:

 # 在网络的第一层加入CBAM注意力机制 self.ca = ChannelAttention(self.inplanes) self.sa = SpatialAttention() # 在网络的最后层加入CBAM注意力机制 self.ca1 = ChannelAttention(self.inplanes) self.sa1 = SpatialAttention()

哎!!!!需要区分函数名;
出现原因3:如果不是在开始或者最后层加入的注意力机制,而是在网络结构中加入,例如可以在resnet中的残差结构中,加入后可print(model)看一看是不是和自己想的一样,我出现的问题是我想在每个block中加入注意力集中机制,因此把加入的部分写在的模型结构的block中,结果也出现了2所出现的问题,原因还是和2一样。

总结:其实加入注意力集中机制还是比较容易的,仔细再仔细,一定没问题,共勉。

附集中注意力集中机制实现代码(PYTORCH):

#SEclass SELayer(nn.Module):    def __init__(self, c1, r=16):        super(SELayer, self).__init__()        self.avgpool = nn.AdaptiveAvgPool2d(1)        self.l1 = nn.Linear(c1, c1 // r, bias=False)        self.relu = nn.ReLU(inplace=True)        self.l2 = nn.Linear(c1 // r, c1, bias=False)        self.sig = nn.Sigmoid()    def forward(self, x):        b, c, _, _ = x.size()        y = self.avgpool(x).view(b, c)        y = self.l1(y)        y = self.relu(y)        y = self.l2(y)        y = self.sig(y)        y = y.view(b, c, 1, 1)        return x * y.expand_as(x)
# ECA注意力机制class eca_layer(nn.Module):    """Constructs a ECA module.    Args:        channel: Number of channels of the input feature map        k_size: Adaptive selection of kernel size    """    def __init__(self, channel, k_size=3):        super(eca_layer, self).__init__()        self.avg_pool = nn.AdaptiveAvgPool2d(1)        self.conv = nn.Conv1d(1, 1, kernel_size=k_size, padding=(k_size - 1) // 2, bias=False)        self.sigmoid = nn.Sigmoid()    def forward(self, x):        # feature descriptor on the global spatial information        y = self.avg_pool(x)        # Two different branches of ECA module        y = self.conv(y.squeeze(-1).transpose(-1, -2)).transpose(-1, -2).unsqueeze(-1)        # Multi-scale information fusion        y = self.sigmoid(y)        x=x*y.expand_as(x)        return x * y.expand_as(x)
#CoorAttentionclass h_sigmoid(nn.Module):    def __init__(self, inplace=True):        super(h_sigmoid, self).__init__()        self.relu = nn.ReLU6(inplace=inplace)    def forward(self, x):        return self.relu(x + 3) / 6class h_swish(nn.Module):    def __init__(self, inplace=True):        super(h_swish, self).__init__()        self.sigmoid = h_sigmoid(inplace=inplace)    def forward(self, x):        return x * self.sigmoid(x)class CoordAtt(nn.Module):    def __init__(self, inp, oup, reduction=32):        super(CoordAtt, self).__init__()        self.pool_h = nn.AdaptiveAvgPool2d((None, 1))        self.pool_w = nn.AdaptiveAvgPool2d((1, None))        mip = max(8, inp // reduction)        self.conv1 = nn.Conv2d(inp, mip, kernel_size=1, stride=1, padding=0)        self.bn1 = nn.BatchNorm2d(mip)        self.act = h_swish()        self.conv_h = nn.Conv2d(mip, oup, kernel_size=1, stride=1, padding=0)        self.conv_w = nn.Conv2d(mip, oup, kernel_size=1, stride=1, padding=0)    def forward(self, x):        identity = x        n, c, h, w = x.size()        x_h = self.pool_h(x)        x_w = self.pool_w(x).permute(0, 1, 3, 2)        y = torch.cat([x_h, x_w], dim=2)        y = self.conv1(y)        y = self.bn1(y)        y = self.act(y)        x_h, x_w = torch.split(y, [h, w], dim=2)        x_w = x_w.permute(0, 1, 3, 2)        a_h = self.conv_h(x_h).sigmoid()        a_w = self.conv_w(x_w).sigmoid()        out = identity * a_w * a_h        return out