RuntimeError: Given weight of size 3 1 5 5, expected bias to be 1-dimensional with 3 elements, but got bias of size [6] instead

Aakash_Bhatia: <p>class LeNet(nn.Module):<br>
def  <strong>init</strong> (self):<br>
super(LeNet, self). <strong>init</strong> ()<br>
self.conv1 = nn.Conv2d(1, 6, 5,bias=False)<br>
self.conv2 = nn.Conv2d(6, 16, 5)<br>
self.fc1 = nn.Linear(16 * 5 * 5, 120)<br>
self.fc2 = nn.Linear(120, 84)<br>
self.fc3 = nn.Linear(84, 10)</p>
<pre><code class="lang-auto">def forward(self, x):
    out = self.conv1(x)
    out = F.relu(out)
    out = F.max_pool2d(out, 2)
    out = F.relu(self.conv2(out))
    out = F.max_pool2d(out, 2)
    out = out.view(out.size(0), -1)
    out = F.relu(self.fc1(out))
    out = F.relu(self.fc2(out))
    out = self.fc3(out)
    return out
</code></pre>
<p>model = LeNet().to(device=device)</p>
<p>if args.model:<br>
if os.path.isfile(args.model):<br>
print("=&gt; loading checkpoint ‘{}’".format(args.model))<br>
checkpoint = torch.load(args.model)<br>
args.start_epoch = checkpoint[‘epoch’]<br>
best_prec1 = checkpoint[‘best_prec1’]<br>
model.load_state_dict(checkpoint[‘state_dict’])<br>
print("=&gt; loaded checkpoint ‘{}’ (epoch {}) Prec1: {:f}".format(args.model, checkpoint[‘epoch’], best_prec1))<br>
else:<br>
print("=&gt; no checkpoint found at ‘{}’".format(args.resume))</p>
<p>print(‘Pre-processing Successful!’)</p>
<p>def test(model):<br>
kwargs = {‘num_workers’: 1, ‘pin_memory’: True} if args.cuda else {}<br>
test_loader = torch.utils.data.DataLoader(<br>
datasets.MNIST(’./data’, train=True, download=True,<br>
transform=transforms.Compose([<br>
transforms.Resize((32, 32)),<br>
transforms.ToTensor(),<br>
transforms.Normalize((0.1307,), (0.3081,))<br>
])),<br>
batch_size=args.test_batch_size, shuffle=True, **kwargs)</p>
<p>getting this error: RuntimeError: Given groups=1, weight of size 3 6 5 5, expected input[256, 3, 14, 14] to have 6 channels, but got 3 channels instead<br>
if i change bias = True then i am getting this error:<br>
RuntimeError: Given weight of size 3 1 5 5, expected bias to be 1-dimensional with 3 elements, but got bias of size [6] instead</p>

ptrblck: <p>Your model seems to work fine with <code>bias=False</code> and <code>bias=True</code>:</p>
<pre><code class="lang-python">class LeNet(nn.Module):
    def __init__(self):
        super(LeNet, self).__init__()
        self.conv1 = nn.Conv2d(1, 6, 5,bias=False)
        self.conv2 = nn.Conv2d(6, 16, 5)
        self.fc1 = nn.Linear(16 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)
    
    def forward(self, x):
        out = self.conv1(x)
        out = F.relu(out)
        out = F.max_pool2d(out, 2)
        out = F.relu(self.conv2(out))
        out = F.max_pool2d(out, 2)
        out = out.view(out.size(0), -1)
        out = F.relu(self.fc1(out))
        out = F.relu(self.fc2(out))
        out = self.fc3(out)
        return out

model = LeNet()
x = torch.randn(1, 1, 32, 32)
out = model(x)
</code></pre>
<p>If you are running the code in a notebook, make sure to initialize all cells before the actual model execution.</p>

VickyRS: <p>I also have similar problem. My input image is 1-channel but I got this</p>
<p>RuntimeError: Given groups=1, weight of size [60, 1, 3, 3], expected input[1, 2, 264, 184] to have 1 channels, but got 2 channels instead</p>
<p>I tried checking the model</p>
<p>x = torch.randn((1, 1, height, width))<br>
x = model(x)<br>
print(x.shape)<br>
print(model)</p>
<p>I got this - torch.Size([1, 1, 1056, 736])</p>
<p>so pls I need help ASAP plsss</p>
<p>Actually I’m using a SWINIR model for superresolution but the model initially accepts RGB image but I’m using grayscale</p>

ptrblck: <aside class="quote no-group" data-username="VickyRS" data-post="3" data-topic="65982">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/v/b77776/48.png" class="avatar"> VickyRS:</div>
<blockquote>
<p>but I’m using grayscale</p>
</blockquote>
</aside>
<p>Your input images have 2 channels, which is not a grayscale format, so you might need to revisit your data loading and processing pipeline making sure a single channel is used.</p>

VickyRS: <p>Thank you so much but I was unable to solve the error. I have searched every line in the code and do not seem to find where it converted my 1 channel data to 2 channels.</p>
<p>Also, I was trying to do transfer learning SWINIR. Like I said it was designed for 3 channels so I get size mismatch:</p>
<p>size mismatch for conv_first.weight: copying a param with shape torch.Size([180, 3, 3, 3]) from checkpoint, the shape in current model is torch.Size([180, 1, 3, 3]).<br>
size mismatch for conv_last.weight: copying a param with shape torch.Size([3, 64, 3, 3]) from checkpoint, the shape in current model is torch.Size([1, 64, 3, 3]).<br>
size mismatch for conv_last.bias: copying a param with shape torch.Size([3]) from checkpoint, the shape in current model is torch.Size([1]).</p>
<p>is it possible to remove this mismatch layers and train the model?</p>

ptrblck: <p>In case you are manipulating the model by replacing the first and last conv layers, load the <code>state_dict</code> beforehand, and replace the layers afterwards to avoid these shape mismatch errors.</p>