我正在尝试运行我的网络,但频道数量不正确。想了几天,修好了,还没修好,完全不知道
ChannelAttention类的第一个输入通道数设置为in_planes。 InceptionModule_with_CBAM类中,branch1的out_channels为[64, 96, 128, 16, 32, 32],所以ChannelAttention类in_planes的第一个输入通道数为64+ 96+ 128 + 16 + 32 + 32 = 368。 I3D_with_CBAM 类的 查看self.inception2 =InceptionModule_with_CBAM(256, [128, 128, 192, 32, 96, 64]),输入通道数为256,与368不同,所以给定groups=1,weight of size [23 , 368, 1, 1, 1], 预期输入 [1, 256, 1, 1, 1] 有 368 个通道,但得到 256 个通道反而发生错误。
num_classes和args["num_class"]为1,batch_size为1,args["img_size"]为(360, 240)。所以训练图像是 (360, 240)
注释的打印语句或打印语句旁边没有写形状的原因是因为没有输出
这是我的网络。
class CBAM(nn.Module):
def __init__(self, in_channels):
super(CBAM, self).__init__()
self.channel_attention = ChannelAttention(in_channels)
print('CBAM in_channels1', in_channels) # 368, 640, 624, 648, 664, 704, 1024, 1264
self.spatial_attention = SpatialAttention(channel=in_channels)
print('CBAM in_channels2', in_channels) # 368, 640, 624, 648, 664, 704, 1024, 1264
def forward(self, x):
x_out = self.channel_attention(x)
print('cbam channel_attention: ', x_out.shape) # 368, 640, 624, 648, 664, 704, 1024, 1264
x_out = self.spatial_attention(x_out)
print('cbam spatial_attention: ', x_out.shape) # 368, 640, 624, 648, 664, 704, 1024, 1264
return x_out
class ChannelAttention(nn.Module):
def __init__(self, in_planes, ratio=16):
super(ChannelAttention, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool3d(1)
self.max_pool = nn.AdaptiveMaxPool3d(1)
self.fc1 = nn.Conv3d(in_planes, in_planes // ratio, kernel_size=1, bias=False)
print('channel attnetion1', in_planes) # 368, 640, 624, 648, 664, 704, 1024, 1264
self.relu = nn.ReLU(inplace=True)
self.fc2 = nn.Conv3d(in_planes // ratio, in_planes, kernel_size=1, bias=False)
print('channel attnetion2', in_planes) # 368, 640, 624, 648, 664, 704, 1024, 1264
self.sigmoid = nn.Sigmoid()
def forward(self, x):
print('0', x.shape)
avg_pool = self.avg_pool(x)
print('1', avg_pool.shape)
max_pool = self.max_pool(x)
print('2', max_pool.shape)
avg_pool = self.fc1(avg_pool)
print('3', avg_pool.shape)
max_pool = self.fc1(max_pool)
print('4', max_pool.shape)
avg_pool = self.relu(avg_pool)
print('5', avg_pool.shape)
max_pool = self.relu(max_pool)
print('6', max_pool.shape)
avg_pool = self.fc2(avg_pool)
print('7', avg_pool.shape)
max_pool = self.fc2(max_pool)
print('8', max_pool.shape)
x_out = self.sigmoid(avg_pool + max_pool)
print('9', x_out.shape)
return x_out
class SpatialAttention(nn.Module):
def __init__(self, channel, reduction_ratio=8):
super(SpatialAttention, self).__init__()
self.conv1 = Conv3d_BN(channel, channel // reduction_ratio, kernel_size=1, bias=False)
self.conv2 = Conv3d_BN(channel // reduction_ratio, channel, kernel_size=1, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x_out = x.mean(dim=2, keepdim=True).mean(dim=3, keepdim=True).mean(dim=4, keepdim=True)
print('spatial_attention_xout1: ', x_out.shape)
# x_out = x.mean(dim=1, keepdim=True)
x_out = self.conv1(x_out)
print('spatial_attention_xout2: ', x_out.shape)
x_out = self.conv2(x_out)
print('spatial_attention_xout3: ', x_out.shape)
x_out = self.sigmoid(x_out)
print('spatial_attention_xout4: ', x_out.shape)
return x * x_out
class Conv3d_BN(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, groups=1, bias=False):
super(Conv3d_BN, self).__init__()
self.conv = nn.Conv3d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, groups=groups, bias=bias)
self.bn = nn.BatchNorm3d(out_channels)
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
return x
class InceptionModule_with_CBAM(nn.Module):
def __init__(self, in_channels, out_channels):
super(InceptionModule_with_CBAM, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.branch1 = nn.Sequential(
nn.Conv3d(sum(out_channels), out_channels[0], kernel_size=1),
nn.BatchNorm3d(out_channels[0]),
nn.ReLU(inplace=True),
)
print('out_channels1', out_channels) # [64, 96, 128, 16, 32, 32], [128, 128, 192, 32, 96, 64], [192, 96, 208, 16, 48, 64], [160, 112, 224, 24, 64, 64], [128, 128, 256, 24, 64, 64], [112, 144, 288, 32, 64, 64], [256, 160, 320, 32, 128, 128], [384, 192, 384, 48, 128, 128]
self.branch2 = nn.Sequential(
nn.Conv3d(sum(out_channels), out_channels[1], kernel_size=1),
nn.BatchNorm3d(out_channels[1]),
nn.ReLU(inplace=True),
nn.Conv3d(out_channels[1], out_channels[2], kernel_size=3, padding=1),
nn.BatchNorm3d(out_channels[2]),
nn.ReLU(inplace=True),
)
print('out_channels2',out_channels) # [64, 96, 128, 16, 32, 32], [128, 128, 192, 32, 96, 64], [192, 96, 208, 16, 48, 64], [160, 112, 224, 24, 64, 64], [128, 128, 256, 24, 64, 64], [112, 144, 288, 32, 64, 64], [256, 160, 320, 32, 128, 128], [384, 192, 384, 48, 128, 128]
self.branch3 = nn.Sequential(
nn.Conv3d(sum(out_channels), out_channels[3], kernel_size=1),
nn.BatchNorm3d(out_channels[3]),
nn.ReLU(inplace=True),
nn.Conv3d(out_channels[3], out_channels[4], kernel_size=3, padding=1),
nn.BatchNorm3d(out_channels[4]),
nn.ReLU(inplace=True),
)
print('out_channels3',out_channels) # [64, 96, 128, 16, 32, 32], [128, 128, 192, 32, 96, 64], [192, 96, 208, 16, 48, 64], [160, 112, 224, 24, 64, 64], [128, 128, 256, 24, 64, 64], [112, 144, 288, 32, 64, 64], [256, 160, 320, 32, 128, 128], [384, 192, 384, 48, 128, 128]
self.branch4 = nn.Sequential(
nn.MaxPool3d(kernel_size=3, stride=1, padding=1),
nn.Conv3d(sum(out_channels), out_channels[5], kernel_size=1),
nn.BatchNorm3d(out_channels[5]),
nn.ReLU(inplace=True),
)
print('out_channels4', out_channels) # [64, 96, 128, 16, 32, 32], [128, 128, 192, 32, 96, 64], [192, 96, 208, 16, 48, 64], [160, 112, 224, 24, 64, 64], [128, 128, 256, 24, 64, 64], [112, 144, 288, 32, 64, 64], [256, 160, 320, 32, 128, 128], [384, 192, 384, 48, 128, 128]
out_channels_sum = sum(out_channels)
print('out_channels_sum', out_channels_sum)
self.cbam = CBAM(out_channels_sum)
def forward(self, x):
# print('in_channels:', self.in_channels)
print('in_channels:', x.shape) # torch.Size([1, 64, 500, 60, 90])
x1 = self.branch1(x)
print('x1', x1.shape)
x2 = self.branch2(x)
print('x2', x2.shape)
x3 = self.branch3(x)
print('x3', x3.shape)
x4 = self.branch4(x)
print('x4', x4.shape)
if x1.shape[1] != x4.shape[1]:
x4 = self.conv1x1(x4)
x = torch.cat((x1, x2, x3, x4), 1)
# x = torch.cat([branch1, branch2, branch3, branch4, branch5, branch6], dim=1)
print(x.shape) # torch.Size([1, 256, 500, 60, 90])
x = self.cbam(x)
return x
class I3D_with_CBAM(nn.Module):
def __init__(self, num_classes=args["num_class"]):
super(I3D_with_CBAM, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv3d(3, 64, kernel_size=(7, 7, 7), stride=(2, 2, 2), padding=(3, 3, 3)),
nn.BatchNorm3d(64),
nn.ReLU(inplace=True),
nn.MaxPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2))
)
self.inception1 = InceptionModule_with_CBAM(64, [64, 96, 128, 16, 32, 32])
# self.in_channels = self.inception1.in_channels
# self.cbam1 = CBAM(sum(self.inception1.out_channels))
self.inception2 = InceptionModule_with_CBAM(sum(self.inception1.out_channels), [128, 128, 192, 32, 96, 64])
# self.in_channels = self.inception2.in_channels
# self.cbam2 = CBAM(sum(self.inception2.out_channels))
self.inception3a = InceptionModule_with_CBAM(sum(self.inception2.out_channels), [192, 96, 208, 16, 48, 64])
# self.in_channels = self.inception3a.in_channels
# self.cbam3 = CBAM(sum(self.inception3a.out_channels))
self.inception3b = InceptionModule_with_CBAM(sum(self.inception3a.out_channels), [160, 112, 224, 24, 64, 64])
# self.in_channels = self.inception3b.in_channels
# self.cbam4 = CBAM(sum(self.inception3b.out_channels))
self.inception4a = InceptionModule_with_CBAM(sum(self.inception3b.out_channels), [128, 128, 256, 24, 64, 64])
# self.in_channels = self.inception4a.in_channels
# self.cbam5 = CBAM(sum(self.inception4a.out_channels))
self.inception4b = InceptionModule_with_CBAM(sum(self.inception4a.out_channels), [112, 144, 288, 32, 64, 64])
# self.in_channels = self.inception4b.in_channels
# self.cbam6 = CBAM(sum(self.inception4b.out_channels))
self.inception5a = InceptionModule_with_CBAM(sum(self.inception4b.out_channels), [256, 160, 320, 32, 128, 128])
# self.in_channels = self.inception5a.in_channels
# self.cbam7 = CBAM(sum(self.inception5a.out_channels))
self.inception5b = InceptionModule_with_CBAM(sum(self.inception5a.out_channels), [384, 192, 384, 48, 128, 128])
# self.in_channels = self.inception5b.in_channels
# self.cbam8 = CBAM(sum(self.inception5b.out_channels))
self.avg_pool = nn.AdaptiveAvgPool3d(1)
self.dropout = nn.Dropout(0.3)
self.linear1 = nn.Linear(1024, 512)
self.linear2 = nn.Linear(512, 256)
self.linear3 = nn.Linear(256, 64)
self.linear4 = nn.Sequential(nn.Linear(in_features=64, out_features=num_classes), nn.Sigmoid())
def forward(self, x):
# x = x.permute(0, 2, 1, 3, 4)
x = self.conv1(x)
print('I3D_with_CBAM conv1: ', x.shape) # torch.Size([1, 64, 500, 60, 90])
x = self.inception1(x) # 여기서 RuntimeError: Given groups=1, weight of size [23, 368, 1, 1, 1], expected input[1, 256, 1, 1, 1] to have 368 channels, but got 256 channels instead
print('I3D_with_CBAM inception1: ', x.shape) # torch.Size([2, 368, 5, 5, 5])
# x = self.cbam1(self.inception1.out_channels[-1])
x = self.inception2(x)
print('I3D_with_CBAM inception2: ', x.shape) #
# x = self.cbam2(self.inception2.out_channels[-1])
x = self.inception3a(x)
print('I3D_with_CBAM inception3a: ', x.shape) #
# x = self.cbam3(self.inception3a.out_channels[-1])
x = self.inception3b(x)
print('I3D_with_CBAM inception3b: ', x.shape) #
# x = self.cbam4(self.inception3b.out_channels[-1])
x = self.inception4a(x)
print('I3D_with_CBAM inception4a: ', x.shape) #
# x = self.cbam5(self.inception4a.out_channels[-1])
x = self.inception4b(x)
print('I3D_with_CBAM inception4b: ', x.shape) #
# x = self.cbam6(self.inception4b.out_channels[-1])
x = self.inception5a(x)
print('I3D_with_CBAM inception5a: ', x.shape) #
# x = self.cbam7(self.inception5a.out_channels[-1])
x = self.inception5b(x)
print('I3D_with_CBAM inception5b: ', x.shape) #
# x = self.cbam8(self.inception5.out_channels[-1])
x = self.avg_pool(x)
print('I3D_with_CBAM avg_pool: ', x.shape) #
x = x.view(x.size(0), -1)
print('I3D_with_CBAM view: ', x.shape) #
x = self.dropout(x)
print('I3D_with_CBAM dropout: ', x.shape) #
x = self.linear1(x)
print('I3D_with_CBAM linear1: ', x.shape) #
x = self.dropout(x)
print('I3D_with_CBAM dropout: ', x.shape) #
x = self.linear2(x)
print('I3D_with_CBAM linear2: ', x.shape) #
x = self.dropout(x)
print('I3D_with_CBAM dropout: ', x.shape) #
x = self.linear3(x)
print('I3D_with_CBAM linear3: ', x.shape) #
x = self.dropout(x)
print('I3D_with_CBAM dropout: ', x.shape) #
x = self.linear4(x)
print('I3D_with_CBAM linear4: ', x.shape) #
x = self.dropout(x)
return x
输出
给定 groups=1,权重大小 [23, 368, 1, 1, 1],预期输入 [1, 256, 1, 1, 1] 有 368 个通道,但得到 256 个通道反而发生错误。
期待
培训..
我尝试更改 I3D_with_CBAM:
class I3D_with_CBAM(nn.Module):
def __init__(self, num_classes=args["num_class"]):
super(I3D_with_CBAM, self).__init__()
self.conv1 = nn.Sequential(
nn.Conv3d(3, 64, kernel_size=(7, 7, 7), stride=(2, 2, 2), padding=(3, 3, 3)),
nn.BatchNorm3d(64),
nn.ReLU(inplace=True),
nn.MaxPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2))
)
self.inception1 = InceptionModule_with_CBAM(64, [64, 96, 128, 16, 32, 32])
self.inception2 = InceptionModule_with_CBAM(sum(self.inception1.out_channels), [128, 128, 192, 32, 96, 64])
self.inception3a = InceptionModule_with_CBAM(sum(self.inception2.out_channels), [192, 96, 208, 16, 48, 64])
self.inception3b = InceptionModule_with_CBAM(sum(self.inception3a.out_channels), [160, 112, 224, 24, 64, 64])
self.inception4a = InceptionModule_with_CBAM(sum(self.inception3b.out_channels), [128, 128, 256, 24, 64, 64])
self.inception4b = InceptionModule_with_CBAM(sum(self.inception4a.out_channels), [112, 144, 288, 32, 64, 64])
self.inception5a = InceptionModule_with_CBAM(sum(self.inception4b.out_channels), [256, 160, 320, 32, 128, 128])
self.inception5b = InceptionModule_with_CBAM(sum(self.inception5a.out_channels), [384, 192, 384, 48, 128, 128])
self.cbam1 = CBAM(sum(self.inception1.out_channels))
self.cbam2 = CBAM(sum(self.inception2.out_channels))
self.cbam3a = CBAM(sum(self.inception3a.out_channels))
self.cbam3b = CBAM(sum(self.inception3b.out_channels))
self.cbam4a = CBAM(sum(self.inception4a.out_channels))
self.cbam4b = CBAM(sum(self.inception4b.out_channels))
self.cbam5a = CBAM(sum(self.inception5a.out_channels))
self.cbam5b = CBAM(sum(self.inception5b.out_channels))
self.avg_pool = nn.AdaptiveAvgPool3d(1)
self.dropout = nn.Dropout(0.3)
self.fc1 = nn.Linear(1024, 512)
self.fc2 = nn.Linear(512, 256)
self.fc3 = nn.Linear(256, 64)
self.fc4 = nn.Linear(64, num_classes)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
x = self.conv1(x)
x = self.inception1(x)
x = self.cbam1(x)
x = self.inception2(x)
x = self.cbam2(x)
x = self.inception3a(x)
x = self.cbam3a(x)
x = self.inception3b(x)
x = self.cbam3b(x)
x = self.inception4a(x)
x = self.cbam4a(x)
x = self.inception4b(x)
x = self.cbam4b(x)
x = self.inception5a(x)
x = self.cbam5a(x)
x = self.inception5b(x)
x = self.cbam5b(x)
x = self.avg_pool(x)
x = x.view(x.size(0), -1)
x = self.dropout(x)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
x = self.fc4(x)
x = self.sigmoid(x)
return x
和
class InceptionModule_with_CBAM(nn.Module):
def __init__(self, in_channels, out_channels):
super(InceptionModule_with_CBAM, self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.branch1 = nn.Sequential(
nn.Conv3d(in_channels, out_channels[0], kernel_size=1),
nn.BatchNorm3d(out_channels[0]),
nn.ReLU(inplace=True),
)
``` in this code, nn.Conv3d(in_channels, out_channels[0], kernel_size=1) I changed in_channels to sum(out_channels).
And,
I3D_with_CBAM 类(nn.Module): def init(self, num_classes=args["num_class"]): 超级(I3D_with_CBAM,自我)。init()
self.conv1 = nn.Sequential(
nn.Conv3d(3, 64, kernel_size=(7, 7, 7), stride=(2, 2, 2), padding=(3, 3, 3)),
nn.BatchNorm3d(64),
nn.ReLU(inplace=True),
nn.MaxPool3d(kernel_size=(1, 2, 2), stride=(1, 2, 2))
)
self.inception1 = InceptionModule_with_CBAM(64, [64, 96, 128, 16, 32, 32])
# self.in_channels = self.inception1.in_channels
# self.cbam1 = CBAM(sum(self.inception1.out_channels))
self.inception2 = InceptionModule_with_CBAM(sum(self.inception1.out_channels), [128, 128, 192, 32, 96, 64])
# self.in_channels = self.inception2.in_channels
# self.cbam2 = CBAM(sum(self.inception2.out_channels))
self.inception3a = InceptionModule_with_CBAM(sum(self.inception2.out_channels), [192, 96, 208, 16, 48, 64])
# self.in_channels = self.inception3a.in_channels
# self.cbam3 = CBAM(sum(self.inception3a.out_channels))
self.inception3b = InceptionModule_with_CBAM(sum(self.inception3a.out_channels), [160, 112, 224, 24, 64, 64])
# self.in_channels = self.inception3b.in_channels
# self.cbam4 = CBAM(sum(self.inception3b.out_channels))
self.inception4a = InceptionModule_with_CBAM(sum(self.inception3b.out_channels), [128, 128, 256, 24, 64, 64])
# self.in_channels = self.inception4a.in_channels
# self.cbam5 = CBAM(sum(self.inception4a.out_channels))
self.inception4b = InceptionModule_with_CBAM(sum(self.inception4a.out_channels), [112, 144, 288, 32, 64, 64])
# self.in_channels = self.inception4b.in_channels
# self.cbam6 = CBAM(sum(self.inception4b.out_channels))
self.inception5a = InceptionModule_with_CBAM(sum(self.inception4b.out_channels), [256, 160, 320, 32, 128, 128])
# self.in_channels = self.inception5a.in_channels
# self.cbam7 = CBAM(sum(self.inception5a.out_channels))
self.inception5b = InceptionModule_with_CBAM(sum(self.inception5a.out_channels), [384, 192, 384, 48, 128, 128])
# self.in_channels = self.inception5b.in_channels
# self.cbam8 = CBAM(sum(self.inception5b.out_channels))
self.avg_pool = nn.AdaptiveAvgPool3d(1)
self.dropout = nn.Dropout(0.3)
self.linear1 = nn.Linear(1024, 512)
self.linear2 = nn.Linear(512, 256)
self.linear3 = nn.Linear(256, 64)
self.linear4 = nn.Sequential(nn.Linear(in_features=64, out_features=num_classes), nn.Sigmoid())
self.inception1 = InceptionModule_with_CBAM(64, [64, 96, 128, 16, 32, 32])
Fixed input channel values such as 64 have been modified to values such as 368 (the number of input channels when an error occurred for the first time).
And I tried it with this value too.
self.inception2 = InceptionModule_with_CBAM(sum(self.inception1.out_channels), [128, 128, 192, 32, 96, 64])
But I got same error.
I got error like this yet. But at that time, I could change using permute. But this time, it's not that case. I was so frustrated. Thank you for your help.
Commented networks are unused. I don't know what should I do. Thank you for reading and your help.
If you know me what should I do, I really appreciate it.