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

username: anoud_abdalziz
cooked: <p>I have this error for this code</p>
<p>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</p>
<p>can you help me<br>
my input shape  [18, 1, 6, 32, 32]  this patched from MRA images  with 6 channels</p>
<p>class GeneratorNet(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(GeneratorNet, self).<strong>init</strong>()</p>
<pre><code>    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</code></pre>

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

username: anoud_abdalziz
cooked: <p>Thank you so much<br>
my  input shape as like as expected shape <code>[batch_size, channels, depth, height, width]</code> where<br>
batch_size 18, channels =1, depth=6, height=32 and width=32 , this is  as  torch.Size([18, 1, 6, 32, 32]<br>
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</p>
<p>RuntimeError: Error(s) in loading state_dict for GeneratorNet:<br>
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”.<br>
Unexpected key(s) in state_dict: “state_dict”, “generator”.</p>

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