I’m trying to create a CNN to do image recognition, but my training loop is failing in the loss check with the error “Expected input batch_size (1) to match target batch_size (50)”. I see many examples in the internt like mine and a look for solution here and in other pages but could’t figure out how to solve it.
Mainly I got that the output of the model has batch size 1 while the target has batch size 50 as I defined in the dataloader. That’s the code:
train_dl = torch.utils.data.DataLoader(dataset = train_ds,
batch_size = 50,
shuffle = True)
test_dl = torch.utils.data.DataLoader(dataset = test_ds,
batch_size = 50,
shuffle = False)
class ConvNet(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(50, 5, 3, bias=False)
self.pool = nn.MaxPool2d(2, 2)
self.conv2 = nn.Conv2d(5, 1, 3, bias=False)
self.fc1 = nn.Linear(25, 10)
def forward(self, x):
#N 50 28 28
x = F.relu(self.conv1(x)) # N 5 26 26
x = self.pool(x) # N 5 13 13
x = F.relu(self.conv2(x)) # N 1 11 11
x = self.pool(x) # N 1 5 5
x = torch.flatten(x, 1) # N 25
x = self.fc1(x) # N 10
return x
CNN = ConvNet()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(CNN.parameters(), lr=0.001)
n_total_steps = len(train_dl)
for epoch in range(100):
running_loss = 0.0
for i, (images,labels) in enumerate(train_dl):
images = images.float()
labels = labels
outputs = CNN(images)
loss = criterion(outputs, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
running_loss += loss.item()
print(f'[{epoch + 1}] loss: {running_loss / n_total_steps:.3f}')
print('Finished Training')
thanks a lot! So the issue is on my Dataset!
I’m using a dataset in csv format and converting it to a tensor, so I suppose the issue is on my Dataset Class.
Yes, the issue might come from your custom CustDF dataset. Print the shapes of the data and target inside the __getitem__ before returning them to see what’s happening and where the shape mismatch comes from.
I’m still trying to figure out what I’m doing wrong!
The dataset is composed of 784 features and one target. the 784 features are pixels from images of 28x28.
I transformed the tensor using the .view so it has 28x28 as expected instead of 1x784.
I was sure the issue was on the dp. But now I saw this strange outcome. as if the tensor was empty. but it’s not. I can’t add another print to this post, but this tensor exists and is not empty
the view must be (1,28,28) instaed of just (28,28). This is because conv2d expects [50, 1, 28, 28] in this case. (50=batch size, 1=number of channels which in this case is 1 B&W imabe and the 28x28 from the image size)
Thaks a lot for your help and patience. It’s amazing to a newbie to have help from this community.