Run Time Error while Training

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

Tell me how to rectify the error

Permute your input as it seems you are trying to use a channels-last input image while channels-first is expected.

import torch
import torch.nn as nn
import torchvision.models as models
import torch.optim as optim
import torchvision.transforms as transforms
from import Dataset, DataLoader
import os
import numpy as np
from PIL import Image

class ResNet18(nn.Module):
def init(self, num_classes=5):
super(ResNet18, self).init()
self.resnet18 = models.resnet18(pretrained=False)
in_features = self.resnet18.fc.in_features
self.resnet18.fc = nn.Linear(in_features, num_classes)

def forward(self, x):
    return self.resnet18(x)

Create an instance of the ResNet-18 model

model = ResNet18(num_classes=5)

class CustomDataset(Dataset):
def init(self, root_dir):
self.root_dir = root_dir
self.images =
self.labels =

    # Iterate over the folders and load the images and labels
    for folder in os.listdir(root_dir):
        folder_path = os.path.join(root_dir, folder)
        for image_path in os.listdir(folder_path):
            image =, image_path))

            # Convert to RGB format if necessary
            if image.mode != 'RGB':
                image = image.convert('RGB')

            # Convert to torch tensor and keep only the first three channels
            image = torch.from_numpy(np.array(image)[:, :, :3])

            label = int(folder)


def __len__(self):
    return len(self.images)

def __getitem__(self, idx):
    image = self.images[idx]

    return image, self.labels[idx]

Create an instance of the CustomDataset class

dataset = CustomDataset(root_dir=r"C:\Users\KESA VARDHAN\Desktop\Output Folder")
print(“Data is loaded”)

Split the dataset into train, validation, and test sets

train_size = int(0.8 * len(dataset))
val_size = int(0.1 * len(dataset))
test_size = len(dataset) - train_size - val_size

train_dataset, val_dataset, test_dataset =, [train_size, val_size, test_size])

Create data loaders

train_loader =, batch_size=64, shuffle=True)
val_loader =, batch_size=64, shuffle=False)
test_loader =, batch_size=64, shuffle=False)

print(“Data is split”)

Define the loss and optimizer

criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.001)

Train the model

for epoch in range(10):
for inputs, labels in train_loader:
outputs = model(inputs)
loss = criterion(outputs, labels)

    # Update the optimizer parameters

# Evaluate the model on the validation set
with torch.no_grad():
    val_loss = 0.0
    val_acc = 0.0
    for inputs, labels in val_loader:
        outputs = model(inputs)
        loss = criterion(outputs, labels)
        val_loss += loss.item()

        _, predicted = torch.max(, 1)
        correct = (predicted == labels).sum().item()
        val_acc += correct / labels.size(0)

    val_loss /= len(val_loader)
    val_acc /= len(val_loader)

    # Print the training and validation losses and accuracies
    print('Epoch {}/{}: Train Loss {}, Val Loss {}, Train Acc {}, Val Acc {}'.format(
        epoch + 1, 10, loss.item(), val_loss, val_acc, val_acc

Evaluate the model on the test set

with torch.no_grad():
test_loss = 0.0
test_acc = 0.0
for inputs, labels in test_loader:
outputs = model(inputs)
loss = criterion(outputs, labels)
test_loss += loss.item()

    _, predicted = torch.max(, 1)
    correct = (predicted == labels).sum().item()
    test_acc += correct / labels.size(0)

test_loss /= len(test_loader)
test_acc /= len(test_loader)

# Print the test loss and accuracy
print('Test Loss {}, Test Acc {}'.format(test_loss, test_acc))

Save the trained model, “”)
here is my entire code can you please say what change should make to the code so that the data comes in 3 channel

Change this to:

def __getitem__(self, idx):
    image = self.images[idx]
    image = image.permute(0, 3, 1, 2)
    return image, self.labels[idx]

Your images are of size (batch, height, width, channels) when the Conv2d layers take (batch, channels, height, width) by default. See here:

Thank You I did help me run the code