Ashar
Ashar

Reputation: 774

Pytorch CNN: Expected input to have 1 channel but got 60000 channels instead

While implementing a NN for Fashion MNIST dataset, I'm getting the following error:

RuntimeError: Given groups=1, weight of size [6, 1, 5, 5], expected input[1, 60000, 28, 28] to have 1 channels, but got 60000 channels instead

I'm inferring that 60000 is the length of my entire dataset, but not sure why is the algorithm giving this error. Can someone help me fix this please?

My dataset:

(X_train, y_train), (X_test, y_test) = fashion_mnist.load_data()
train_data = []
test_data = []
train_data.append([X_train, y_train])
test_data.append([X_test, y_test])

trainloader = torch.utils.data.DataLoader(train_data, shuffle=True, batch_size=100)
testloader = torch.utils.data.DataLoader(test_data, shuffle=True, batch_size=100)

I'm getting the error in following order (as per stack trace):

      8     #making predictions
----> 9     y_pred = model(images)

     32     #first hidden layer
---> 33     x = self.conv1(x)

Update 1

Added the line:

images = images.transpose(0, 1)

to transpose the image as pointed out by Ivan but now getting the error:

RuntimeError: expected scalar type Byte but found Float

Upvotes: 0

Views: 1216

Answers (2)

Ivan
Ivan

Reputation: 40618

You input is shaped (1, 60000, 28, 28), while it should be shaped (60000, 1, 28, 28). You can fix this by transposing the first two axes:

>>> x.transpose(0, 1)

Upvotes: 2

Balaji
Balaji

Reputation: 1087

From the looks of it you used TensorFlow's datasets. I've used torchvison's dataset and it works fine

import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision.datasets import FashionMNIST
from torchvision import transforms


class Network(nn.Module):
  def __init__(self):
    super(Network,self).__init__()
    self.conv1 = nn.Conv2d(in_channels=1,out_channels=6,kernel_size=5)
    self.conv2 = nn.Conv2d(in_channels=6,out_channels=12,kernel_size=5)
    self.fc1 = nn.Linear(in_features=12*4*4,out_features=120)
    self.fc2 = nn.Linear(in_features=120,out_features=60)
    self.fc3 = nn.Linear(in_features=60,out_features=40)
    self.out = nn.Linear(in_features=40,out_features=10)
  def forward(self,x):
    #input layer
    x = x
    #first hidden layer
    x = self.conv1(x)
    x = F.relu(x)
    x = F.max_pool2d(x,kernel_size=2,stride=2)
    #second hidden layer
    x = self.conv2(x)
    x = F.relu(x)
    x = F.max_pool2d(x,kernel_size=2,stride=2)
    #third hidden layer
    x = x.reshape(-1,12*4*4)
    x = self.fc1(x)
    x = F.relu(x)
    #fourth hidden layer
    x = self.fc2(x)
    x = F.relu(x)
    #fifth hidden layer
    x = self.fc3(x)
    x = F.relu(x)
    #output layer
    x = self.out(x)
    return x


batch_size = 1000
train_dataset = FashionMNIST(
    '../data', train=True, download=True, 
    transform=transforms.ToTensor())
trainloader = torch.utils.data.DataLoader(train_dataset, batch_size=batch_size, shuffle=True)

test_dataset = FashionMNIST(
    '../data', train=False, download=True, 
    transform=transforms.ToTensor())
testloader = torch.utils.data.DataLoader(test_dataset, batch_size=batch_size, shuffle=True)

model = Network()

losses = []
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters())
epochs = 10

for i in range(epochs):
    batch_loss = []
    for j, (data, targets) in enumerate(trainloader):
        optimizer.zero_grad()
        ypred = model(data)
        loss = criterion(ypred, targets.reshape(-1))
        loss.backward()
        optimizer.step()
        batch_loss.append(loss.item())
    if i>10: 
        optimizer.lr = 0.0005
    losses .append(sum(batch_loss) / len(batch_loss))
    print('Epoch {}:\tloss {:.4f}'.format(i, losses [-1]))

Upvotes: 0

Related Questions