RuntimeError: Given groups=1, weight of size [32, 6, 3, 3, 3], expected input[18, 1, 6, 32, 32] to have 6 channels, but got 1 channels instead

I have this error for this code

RuntimeError: Given groups=1, weight of size [32, 6, 3, 3, 3], expected input[18, 1, 6, 32, 32] to have 6 channels, but got 1 channels instead

can you help me
my input shape [18, 1, 6, 32, 32] this patched from MRA images with 6 channels

class GeneratorNet(nn.Module):
def init(self):
super(GeneratorNet, self).init()

    self.conv1 = nn.Conv3d(6, 32, kernel_size=3, padding=1)
    self.conv2 = nn.Conv3d(32, 64, kernel_size=3, padding=1)
    self.conv3 = nn.Conv3d(64, 128, kernel_size=3, padding=1)
    self.conv4 = nn.Conv3d(128, 256, kernel_size=3, padding=1)
    self.conv5 = nn.Conv3d(256, 256, kernel_size=3, padding=1)

    self.tconv1 = nn.ConvTranspose2d(6, 32, kernel_size=3, padding=1)
    self.tconv2 = nn.ConvTranspose2d(32, 64, kernel_size=3, padding=1)
    self.tconv3 = nn.ConvTranspose2d(64, 128, kernel_size=3, padding=1)
    self.tconv4 = nn.ConvTranspose2d(128, 256, kernel_size=3, padding=1)
    self.tconv5 = nn.ConvTranspose2d(256, 256, kernel_size=3, padding=1)

    self.relu = nn.ReLU()

def forward(self, x):
    # encoder
    residual_1 = x
    out = self.relu(self.conv1(x))
    out = self.relu(self.conv2(out))
    residual_2 = out
    out = self.relu(self.conv3(out))
    out = self.relu(self.conv4(out))
    residual_3 = out
    out = self.relu(self.conv5(out))
    # decoder
    out = self.tconv1(out)
    out += residual_3
    out = self.tconv2(self.relu(out))
    out = self.tconv3(self.relu(out))
    out += residual_2
    out = self.tconv4(self.relu(out))
    out = self.tconv5(self.relu(out))
    out += residual_1
    out = self.relu(out)
    return out

The input to nn.Conv3d layers is expected to have the shape [batch_size, channels, depth, height, width]. In your case this would most likely correspond to [18, 6, 1, 32, 32], so you might want to permute the input into the expected shape.

1 Like

Thank you so much
my input shape as like as expected shape [batch_size, channels, depth, height, width] where
batch_size 18, channels =1, depth=6, height=32 and width=32 , this is as torch.Size([18, 1, 6, 32, 32]
I got my problem with nn.Conv3d(1, 32, kernel_size=3, padding=1) because i have one channel not 6, 6 is depth. now I an asking about this error

RuntimeError: Error(s) in loading state_dict for GeneratorNet:
Missing key(s) in state_dict: “conv1.0.weight”, “conv1.0.bias”, “conv1.1.weight”, “conv1.1.bias”, “conv1.1.running_mean”, “conv1.1.running_var”, “conv2.0.weight”, “conv2.0.bias”, “conv2.1.weight”, “conv2.1.bias”, “conv2.1.running_mean”, “conv2.1.running_var”, “conv3.0.weight”, “conv3.0.bias”, “conv3.1.weight”, “conv3.1.bias”, “conv3.1.running_mean”, “conv3.1.running_var”, “conv4.0.weight”, “conv4.0.bias”, “conv4.1.weight”, “conv4.1.bias”, “conv4.1.running_mean”, “conv4.1.running_var”, “deConv1_1.weight”, “deConv1_1.bias”, “deConv1.0.weight”, “deConv1.0.bias”, “deConv1.0.running_mean”, “deConv1.0.running_var”, “deConv2_1.weight”, “deConv2_1.bias”, “deConv2.0.weight”, “deConv2.0.bias”, “deConv2.0.running_mean”, “deConv2.0.running_var”, “deConv3_1.weight”, “deConv3_1.bias”, “deConv3.0.weight”, “deConv3.0.bias”, “deConv3.0.running_mean”, “deConv3.0.running_var”, “deConv4_1.weight”, “deConv4_1.bias”.
Unexpected key(s) in state_dict: “state_dict”, “generator”.

The new error is raised as it seems you are loading an object (dict) with state_dict and generator keys inside. I guess you want to index the ['state_dict'] key when using model.load_state_dict.

1 Like