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

I used Pytorch transform to transform the data and Pytorch model to train them. But, I am geting the error message as “RuntimeError: Given groups=1, weight of size [244, 244, 3, 3], expected input[8, 1, 244, 244] to have 244 channels, but got 1 channels instead”.

Any suggestion on how to fix weight shape to accept the input?

Note:
I had converted the image to grayScale while tranforms.

train_transforms = transforms.Compose([
             transforms.ToPILImage(),
             transforms.Grayscale(num_output_channels=1),
             transforms.Resize((244,244)),
             transforms.RandomHorizontalFlip(),
             transforms.ToTensor(),
             transforms.Normalize((0.5,), (0.5,))
             ])

train_df = CustomImageDataset(train_data,train_transforms)
train_dataloader = torch.utils.data.DataLoader(
            train_df,
            batch_size=8,
            num_workers=1,
            shuffle=True,
        )

train_features, train_labels = next(iter(train_dataloader))
print(f"Feature batch shape: {train_features.size()}")
# this code output: Feature batch shape: torch.Size([8, 1, 244, 244])

import torch
from torch import nn
from d2l import torch as d2l

net = nn.Sequential(
    nn.Conv2d(244, 244, kernel_size=3, stride=1, padding=1), nn.ReLU(),
    nn.Dropout(p=0.5),
    nn.MaxPool2d(kernel_size=3, stride=1),
    nn.Dropout(p=0.5),
    nn.Conv2d(244, 256, kernel_size=3, padding=1), nn.ReLU(),
    nn.Dropout(p=0.5),
    nn.MaxPool2d(kernel_size=3, stride=1),
    nn.Dropout(p=0.5),
    nn.Conv2d(256, 256, kernel_size=3, padding=1), nn.ReLU(),
    nn.Linear(256, 3))

lr, num_epochs = 0.01, 10
d2l.train_ch6(net, train_dataloader, test_dataloader, num_epochs, lr, d2l.try_gpu())

The first conv layer:

nn.Conv2d(244, 244, kernel_size=3, stride=1, padding=1)

expects the input to have 244 channels, since in_channels=224.
If you are passing an input with a single channel, change in_channels to 1:

nn.Conv2d(1, 244, kernel_size=3, stride=1, padding=1)
1 Like

Thanks @ptrblck I just changed it and it is thowing an error “RuntimeError: mat1 and mat2 shapes cannot be multiplied (491520x240 and 256x3)”. Any idea why it is doing it?

This error is most likely raised in the linear layer.
Flatten the activation via nn.Flatten() after the last conv layer:

    nn.Conv2d(256, 256, kernel_size=3, padding=1), nn.ReLU(),
    nn.Linear(256, 3))

and make sure the in_features of nn.Linear match the activation features.

1 Like