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

Hi
I’m using VGGNet 16BN for training images with shape(1, 224, 224)
I have 15 output classes.
Hence I have modified the first conv2d and the last linear layer accordingly.
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
Below the model I’ve provided the training function as well

It would be really great if someone could help me out with this:)

VGG(
(features): Sequential(
(0): Conv2d(1, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(1): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(2): ReLU(inplace=True)
(3): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(4): BatchNorm2d(64, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(5): ReLU(inplace=True)
(6): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(7): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(8): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(9): ReLU(inplace=True)
(10): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(11): BatchNorm2d(128, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(12): ReLU(inplace=True)
(13): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(14): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(15): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(16): ReLU(inplace=True)
(17): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(18): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(19): ReLU(inplace=True)
(20): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(21): BatchNorm2d(256, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(22): ReLU(inplace=True)
(23): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(24): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(25): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(26): ReLU(inplace=True)
(27): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(28): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(29): ReLU(inplace=True)
(30): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(31): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(32): ReLU(inplace=True)
(33): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
(34): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(35): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(36): ReLU(inplace=True)
(37): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(38): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(39): ReLU(inplace=True)
(40): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
(41): BatchNorm2d(512, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)
(42): ReLU(inplace=True)
(43): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)
)
(avgpool): AdaptiveAvgPool2d(output_size=(7, 7))
(classifier): Sequential(
(0): Linear(in_features=25088, out_features=4096, bias=True)
(1): ReLU(inplace=True)
(2): Dropout(p=0.5, inplace=False)
(3): Linear(in_features=4096, out_features=4096, bias=True)
(4): ReLU(inplace=True)
(5): Dropout(p=0.5, inplace=False)
(6): Linear(in_features=4096, out_features=15, bias=True)
)
)

Training
loss_epoch_arr = []
max_epochs = 1
n_iters = np.ceil(105/batch_size)

for epoch in range(max_epochs):
for i, data in enumerate(trainloader, 0):

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()))

loss_epoch_arr.append(loss.item())

#print(‘Epoch: %d/%d, Test acc: %0.2f, Train acc : %0.2f’ % (epoch, max_epochs, evaluation(testloader,vgg),

evaluation(trainloader, vgg)))

print(‘Epoch : %d/%d’, epoch, max_epochs)
print(‘Test Acc : %0.2f’, evaluation(testloader,vgg))
print(‘Train Acc : %0.2f’, evaluation(trainloader, vgg))

plt.plot(loss_epoch_arr)
plt.show()

Switch from [1,4,224,224] to [4,1,224,224]. [Batch_size, Number of channels, Height, Width] this should be the right order.

I was giving batch_size as 1 since only 105 training samples

and 45 samples for testing the model

the image shape is [1, 224, 224]

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]

can you upload the code for data augmentation ?

It does take it in the form of [1, 1, 224, 224]
I’m unable to figure out the problem since when

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

something like this

try:
    code for training
except:
    continue

Oh. Like that I’ll try that. You have an idea about how it is written. Some reference. ?Because I’m a beginner

make sure to put that pice of code in your training loop

okay Thank you so much :smiley:

My pleasure :smiley: hopefully will work, it’s not the most elegant solution.

oh no problem will try :smiley:

the elegant solution is to write your own dataset. they teach you here: https://pytorch.org/tutorials/beginner/data_loading_tutorial.html how to do that, take a look :wink:

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.

Now that issue is solved you were right!
But I do not understand y does it print nan at

iteration