RuntimeError: Given groups=1, weight of size [128, 64, 3, 3], expected input[2, 3, 64, 64] to have 64 channels, but got 3 channels instead

jeremy_eudaric: <p>Hello l got this issue for a week l tried many strategies but it still doesn´t work.<br>
my code many thnaks <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"><br>
class DoubleConv(nn.Module):</p>
<pre><code>def __init__(self, in_channels, out_channels):
    
    super(DoubleConv, self).__init__()
    
    self.double_conv = nn.Sequential(
        nn.Conv2d(in_channels, out_channels, kernel_size=3, padding=2),
        nn.BatchNorm2d(out_channels),
        nn.ReLU(inplace=True),
        nn.Conv2d(out_channels, out_channels, kernel_size=3, padding=2),
        nn.BatchNorm2d(out_channels),
        nn.ReLU(inplace=True),
    )

def forward(self, out):
    
    x=self.double_conv(out)
    
    
    return x
</code></pre>
<h1><a name="in5-1" class="anchor" href="#in5-1"></a>In[5]:</h1>
<p>class DownBlock(nn.Module):<br>
def <strong>init</strong>(self, in_channels, out_channels):</p>
<pre><code>    super(DownBlock, self).__init__()
    
    self.double_conv = DoubleConv(in_channels, out_channels)
    self.down_sample = nn.MaxPool2d(2)

def forward(self, x):
    down_out = self.down_sample(x)
    skip_out = self.double_conv(down_out)
    return (down_out, skip_out)
</code></pre>
<h1><a name="in6-2" class="anchor" href="#in6-2"></a>In[6]:</h1>
<p>class  UpBlock (nn.Module):<br>
“”“Upscaling then double conv”“”</p>
<pre><code>def __init__(self, in_channels, out_channels, up_sample_mode):
    super(UpBlock, self).__init__()
    if up_sample_mode == 'conv_transpose':
        self.up_sample = nn.ConvTranspose2d(in_channels-out_channels, in_channels-out_channels, kernel_size=2, stride=2)        
    elif up_sample_mode == 'bilinear':
        self.up_sample = nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True)
    else:
        raise ValueError("Unsupported `up_sample_mode` (can take one of `conv_transpose` or `bilinear`)")
    self.double_conv = DoubleConv(in_channels, out_channels)

def forward(self, down_input, skip_input):
    x = self.up_sample(down_input)
    x = torch.cat([x, skip_input], dim=1)
    return self.double_conv(x)
</code></pre>
<h1><a name="in9-3" class="anchor" href="#in9-3"></a>In[9]:</h1>
<p>class Siamese_UNet_Assement(nn.Module):<br>
def <strong>init</strong>(self, out_classes, up_sample_mode=‘conv_transpose’):</p>
<pre><code>    super(Siamese_UNet_Assement, self).__init__()
    
    self.up_sample_mode = up_sample_mode
    # Downsampling Path
    self.down_conv1 = DownBlock(3, 64)
    self.down_conv2 = DownBlock(64, 128)
    self.down_conv3 = DownBlock(128, 256)
    self.down_conv4 = DownBlock(256, 512)
    # Bottleneck
    self.double_conv = DoubleConv(512, 1024)
    # Upsampling Path
    self.up_conv4 = UpBlock(1024, 512, self.up_sample_mode)
    self.up_conv3 = UpBlock(512, 256, self.up_sample_mode)
    self.up_conv2 = UpBlock(256, 128, self.up_sample_mode)
    self.up_conv1 = UpBlock(128 , 64, self.up_sample_mode)
    # Final Convolution
    self.conv_last = nn.Conv2d(64, out_classes, kernel_size=1)

def forward(self, input1,input2):
    
    # Unet1
    
    # Encoder
    enc1_1, skip1_out1 = self.down_conv1(input1)
    enc1_2, skip2_out1 = self.down_conv2(enc1_1)
    enc1_3, skip3_out1 = self.down_conv3(enc1_2)
    enc1_4, skip4_out1 = self.down_conv4(enc1_3)
    bottleneck_1  = self.double_conv(enc1_4)
    
    #Decoder
    dec4_1= self.up_conv4(bottleneck_1, skip4_out1)
    dec3_1_= self.up_conv3(dec4_1, skip3_out1)
    dec2_1= self.up_conv2(dec3_1_, skip2_out1)
    dec1_1 = self.up_conv1(dec2_1, skip1_out1)
    dec1_1 = self.conv_last(dec1_1)
    
    # Unet2
    
    # Encoder
    enc2_1, skip1_out2 = self.down_conv1(input2)
    enc2_2, skip2_out2 = self.down_conv2(enc2_1)
    enc2_3, skip3_out2 = self.down_conv3(enc2_2)
    enc2_4, skip4_out2 = self.down_conv4(enc2_3)
    bottleneck_2  = self.double_conv(enc2_4)
    
    #Decoder
    dec4_2= self.up_conv4(bottleneck_2 , skip4_out2)
    dec3_2_= self.up_conv3(dec4_2, skip3_out2)
    dec2_2= self.up_conv2(dec3_2_, skip2_out2)
    dec1_2 = self.up_conv1(dec2_2, skip1_out2)
    dec1_2 = self.conv_last(dec1_2)
    
    
    
    # Siamese
    output = torch.cat((dec1_1,dec1_2), 1)
    
    
    
    return  output = torch.cat((dec1_1,dec1_2), 1)
</code></pre>
<p>RuntimeError: Given groups=1, weight of size [128, 64, 3, 3], expected input[2, 3, 64, 64] to have 64 channels, but got 3 channels instead</p>

ptrblck: <p>Try to narrow down which layer raises the issue either by checking the full stacktrace or by e.g. adding debug <code>print</code> statements to the <code>forward</code> method.<br>
Based on the error message and the model architecture I would guess the error is raised in <code>self.down_conv2</code> or <code>self.up_conv1</code> as these layers should have the printed weight shape.</p>

jeremy_eudaric: <p>yes the error is from<br>
File ~/codes/Buldings_damage_assessment/models/testUNet_Assement.py:130, in Siamese_UNet_Assement.forward(self, input1, input2)<br>
124 def forward(self, input1,input2):<br>
125<br>
126     # Unet1<br>
127<br>
128     # Encoder<br>
129     enc1_1, skip1_out1 = self.down_conv1(input1)<br>
 → 130     enc1_2, skip2_out1 = self.down_conv2(enc1_1)<br>
131     enc1_3, skip3_out1 = self.down_conv3(enc1_2)<br>
132     enc1_4, skip4_out1 = self.down_conv4(enc1_3)</p>

ptrblck: <p>Thanks for confirming as the error would indeed be expected there.<br>
In <code>self.donw_conv1</code> you are returning two outputs: the downsampled original tensor (which still has <code>3</code> channels) and the output of the <code>DoubleConv</code> layer. You are then passing the first return value (the downsampled original tensor) to <code>self.down_conv2</code>, which will raise the error since the channels do not match.<br>
Maybe you want to pass the second output (<code>skip_out1</code>) to the next layer instead and rename the variables.</p>

jeremy_eudaric: <p>So l made a mistake the skip is with the convolution and after l will pass skip out in to down out</p>
<p>skip_out = self.double_conv(x)<br>
down_out = self.down_sample(skip_out)</p>
<p>but no l got a new error the same<br>
RuntimeError: Given transposed=1, weight of size [512, 512, 2, 2], expected input[2, 1024, 23, 23] to have 512 channels, but got 1024 channels instead</p>
<p><span class="hashtag-raw">#Decoder</span><br>
dec4_1= self.up_conv4(bottleneck_1, skip4_out1)<br>
dec3_1_= self.up_conv3(dec4_1, skip3_out1)<br>
dec2_1= self.up_conv2(dec3_1_, skip2_out1)<br>
dec1_1 = self.up_conv1(dec2_1, skip1_out1)<br>
dec1_1 = self.conv_last(dec1_1)</p>
<p>File ~/codes/Buldings_damage_assessment/models/testUNet_Assement.py:135, in Siamese_UNet_Assement.forward(self, input1, input2)<br>
132 bottleneck_1  = self.double_conv(enc1_4)<br>
134 <span class="hashtag-raw">#Decoder</span><br>
 → 135 dec4_1= self.up_conv4(bottleneck_1, skip4_out1)<br>
136 dec3_1_= self.up_conv3(dec4_1, skip3_out1)<br>
137 dec2_1= self.up_conv2(dec3_1_, skip2_out1)</p>