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

username: ssalome
cooked: <p>Hi<br>
I’m using VGGNet 16BN for training images with shape(1, 224, 224)<br>
I have 15 output classes.<br>
Hence I have modified the first conv2d and the last linear layer accordingly.<br>
But while training I’m getting this issue after some random iterations say 76, 88, etc. RuntimeError: Given groups=1, weight of size 64 1 3 3, expected input[1, 4, 224, 224] to have 1 channels, but got 4 channels instead<br>
Below the model I’ve provided the training function as well</p>
<p>It would be really great if someone could help me out with this:)</p>
<p>VGG(<br>
(features): Sequential(<br>
<strong>(0): Conv2d(1, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))</strong><br>
(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(2): ReLU(inplace=True)<br>
(3): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(4): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(5): ReLU(inplace=True)<br>
(6): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(7): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(8): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(9): ReLU(inplace=True)<br>
(10): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(11): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(12): ReLU(inplace=True)<br>
(13): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(14): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(15): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(16): ReLU(inplace=True)<br>
(17): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(18): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(19): ReLU(inplace=True)<br>
(20): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(21): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(22): ReLU(inplace=True)<br>
(23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(24): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(25): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(26): ReLU(inplace=True)<br>
(27): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(28): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(29): ReLU(inplace=True)<br>
(30): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(31): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(32): ReLU(inplace=True)<br>
(33): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(34): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(35): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(36): ReLU(inplace=True)<br>
(37): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(38): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(39): ReLU(inplace=True)<br>
(40): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(41): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)<br>
(42): ReLU(inplace=True)<br>
(43): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
)<br>
(avgpool): AdaptiveAvgPool2d(output_size=(7, 7))<br>
(classifier): Sequential(<br>
(0): Linear(in_features=25088, out_features=4096, bias=True)<br>
(1): ReLU(inplace=True)<br>
(2): Dropout(p=0.5, inplace=False)<br>
(3): Linear(in_features=4096, out_features=4096, bias=True)<br>
(4): ReLU(inplace=True)<br>
(5): Dropout(p=0.5, inplace=False)<br>
<strong>(6): Linear(in_features=4096, out_features=15, bias=True)</strong><br>
)<br>
)</p>
<p><strong>Training</strong><br>
loss_epoch_arr = []<br>
max_epochs = 1<br>
n_iters = np.ceil(105/batch_size)</p>
<p>for epoch in range(max_epochs):<br>
for i, data in enumerate(trainloader, 0):</p>
<pre><code>inputs, labels = data
print(inputs.shape, i)
#print(labels)
inputs, labels = inputs.to(device), labels.to(device)

opt.zero_grad()

outputs = vgg(inputs)
loss = loss_fn(outputs, labels)
loss.backward()
opt.step()

del inputs, labels, outputs
torch.cuda.empty_cache()

if i%100 == 0:
  print('Iteration : %d/%d, Loss : %0.2f' % (i, n_iters, loss.item()))
</code></pre>
<p>loss_epoch_arr.append(loss.item())</p>
<p><span class="hashtag">#print</span>(‘Epoch: %d/%d, Test acc: %0.2f, Train acc : %0.2f’ % (epoch, max_epochs, evaluation(testloader,vgg),</p>
<h1>evaluation(trainloader, vgg)))</h1>
<p>print(‘Epoch : %d/%d’, epoch, max_epochs)<br>
print(‘Test Acc : %0.2f’, evaluation(testloader,vgg))<br>
print(‘Train Acc : %0.2f’, evaluation(trainloader, vgg))</p>
<p>plt.plot(loss_epoch_arr)<br>
plt.show()</p>

username: Andrei_Chirap
cooked: <p>Switch from [1,4,224,224] to [4,1,224,224]. [Batch_size, Number of channels, Height, Width] this should be the right order.</p>

username: ssalome
cooked: <p>I was giving batch_size as 1 since only 105 training samples</p>

username: ssalome
cooked: <p>and 45 samples for testing the model</p>

username: ssalome
cooked: <p>the image shape is [1, 224, 224]</p>

username: Andrei_Chirap
cooked: <p>if your image shape is [1,224,224] and you want to train with batch_size = 1, the model input should be [1,1,224,224]</p>

username: Andrei_Chirap
cooked: <p>can you upload the code for data augmentation ?</p>

username: ssalome
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/6/8675ff2ccd1c893cfcbc4985e9f8c1d538db6555.png" data-download-href="https://discuss.pytorch.org/uploads/default/8675ff2ccd1c893cfcbc4985e9f8c1d538db6555" title="Screenshot (1228)"><img src="https://discuss.pytorch.org/uploads/default/original/3X/8/6/8675ff2ccd1c893cfcbc4985e9f8c1d538db6555.png" alt="Screenshot (1228)" data-base62-sha1="jbuMQXfCkHJas12m7WHp8mtirc1" width="690" height="369" data-dominant-color="3D3D3E"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot (1228)</span><span class="informations">962×515 12.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>It does take it in the form of [1, 1, 224, 224]<br>
I’m unable to figure out the problem since when</p>

username: Andrei_Chirap
cooked: <p>ohhh it looks like you have some [RGBA] images in your training set. Use a try catch block and when you catch the exception just continue the training</p>

username: Andrei_Chirap
cooked: <p>something like this</p>
<pre><code class="lang-auto">try:
    code for training
except:
    continue
</code></pre>

username: ssalome
cooked: <p>Oh. Like that I’ll try that. You have an idea about how it is written. Some reference. ?Because I’m a beginner</p>

username: Andrei_Chirap
cooked: <p>make sure to put that pice of code in your training loop</p>

username: ssalome
cooked: <p>okay Thank you so much <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>

username: Andrei_Chirap
cooked: <p>My pleasure <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:">  hopefully will work, it’s not the most elegant solution.</p>

username: ssalome
cooked: <p>oh no problem will try <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>

username: Andrei_Chirap
cooked: <p>the elegant solution is to write your own dataset. they teach you here: <a href="https://pytorch.org/tutorials/beginner/data_loading_tutorial.html" rel="nofollow noopener">https://pytorch.org/tutorials/beginner/data_loading_tutorial.html</a> how to do that, take a look <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

username: ssalome
cooked: <p>Oh ya… I did go through this…For this model I have prepared the dataloader so i think I’ll put a condition for checking for RGB images and will reject those samples.</p>

username: ssalome
cooked: <p>Now that issue is solved you were right!<br>
But I do not understand y does it print <strong>nan</strong> at <em><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/3/23ca4d33bcec323037451e3a06ac3eeb73aa523d.png" data-download-href="https://discuss.pytorch.org/uploads/default/23ca4d33bcec323037451e3a06ac3eeb73aa523d" title="Screenshot (1230)"><img src="https://discuss.pytorch.org/uploads/default/original/3X/2/3/23ca4d33bcec323037451e3a06ac3eeb73aa523d.png" alt="Screenshot (1230)" data-base62-sha1="56C8IoCYfDKD70I9UlRJakzDNx3" width="690" height="431" data-dominant-color="50504F"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot (1230)</span><span class="informations">965×603 14.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div> iteration</em></p>