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
首選說一下這個(gè)問題,這個(gè)問題提示想要得到的是464個(gè)通道數(shù)但是實(shí)際上得到的是116個(gè)通道。
例如我給某個(gè)深度學(xué)習(xí)網(wǎng)絡(luò)中加CBAM注意力集中機(jī)制,具體可參照此文章鏈接: link.(以下為實(shí)現(xiàn)代碼):
# 通道注意力機(jī)制
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)
# 空間注意力機(jī)制
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)
問題出現(xiàn)的可能原因1:
# 在網(wǎng)絡(luò)的某層加入CBAM注意力機(jī)制
self.ca = ChannelAttention(self.inplanes)
self.sa = SpatialAttention()
self.inplanes修改為你上一層輸出的通道數(shù);
出現(xiàn)原因2:是我在假的過程中出現(xiàn)的錯(cuò)誤,是一個(gè)非常小的錯(cuò)誤,就是在初始層和末尾分別加入CBAM的時(shí)候,沒有區(qū)分不同位置加入后的函數(shù)名,因此出現(xiàn)錯(cuò)誤,例如如下:
# 在網(wǎng)絡(luò)的第一層加入CBAM注意力機(jī)制
self.ca = ChannelAttention(self.inplanes)
self.sa = SpatialAttention()
# 在網(wǎng)絡(luò)的最后層加入CBAM注意力機(jī)制
self.ca1 = ChannelAttention(self.inplanes)
self.sa1 = SpatialAttention()
哎?。。。⌒枰獏^(qū)分函數(shù)名;
出現(xiàn)原因3:如果不是在開始或者最后層加入的注意力機(jī)制,而是在網(wǎng)絡(luò)結(jié)構(gòu)中加入,例如可以在resnet中的殘差結(jié)構(gòu)中,加入后可print(model)看一看是不是和自己想的一樣,我出現(xiàn)的問題是我想在每個(gè)block中加入注意力集中機(jī)制,因此把加入的部分寫在的模型結(jié)構(gòu)的block中,結(jié)果也出現(xiàn)了2所出現(xiàn)的問題,原因還是和2一樣。
總結(jié):其實(shí)加入注意力集中機(jī)制還是比較容易的,仔細(xì)再仔細(xì),一定沒問題,共勉。文章來源:http://www.zghlxwxcb.cn/news/detail-402922.html
附集中注意力集中機(jī)制實(shí)現(xiàn)代碼(PYTORCH):文章來源地址http://www.zghlxwxcb.cn/news/detail-402922.html
#SE
class 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注意力機(jī)制
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)
#CoorAttention
class 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) / 6
class 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
到了這里,關(guān)于RuntimeError: Given groups=1, weight of size [14, 464, 1, 1], expected input問題解決的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!