ValueError: Expected input batch_size (1) to match target batch_size (160)

Dataset: CAMVID
Input: torch.Size([1, 3, 160, 160])
Target: torch.Size([160, 160, 3])
Predictions torch.Size([1, 3, 160, 160])

Train code:

model.train()
for idx in tqdm(range(EPOCHS)):
train_images, train_masks = torch.tensor(train_img[idx]), torch.tensor(train_lbl[idx])

inputs = Variable((train_images).unsqueeze(0), requires_grad=True).float().permute(0, 3, 1, 2)
targets = Variable((train_masks), requires_grad=True).long()

print('Input:',inputs.shape)
print('Target:',targets.shape)

predictions = model(inputs)
print('Predictions',predictions.shape)
loss = criterion(predictions, targets)

Network:
super(UNet, self).init()
self.pool = torch.nn.MaxPool2d((2,2))
self.up_samp = torch.nn.Upsample(scale_factor=2, mode=‘nearest’)
self.down_conv1 = down_block(3, 32)
self.down_conv2 = down_block(32, 64)
self.down_conv3 = down_block(64, 128)
self.down_conv4 = down_block(128, 256)
self.down_conv5 = down_block(256, 512)
self.down_conv6 = down_block(512, 1024)
self.up_conv1 = up_block(1024, 512)
self.up_conv2 = up_block(512, 256)
self.up_conv3 = up_block(256, 128)
self.up_conv4 = up_block(128, 64)
self.up_conv5 = up_block(64, 32)
self.out = out(32, 32)

What should my targets looks like?
How should i build the evaluation part for multi class classification?
Thanks

A multi-class classification would usually contain a single class index for each sample.
For the posted input shape with a batch size of 1, your target would have the shape [batch_size=1] and contain a value in [0, nb_classes-1] assuming you are using nn.CrossentropyLoss.

1 Like

Thanks for getting back. I tried to remove the the 3 from the target and replaced it with a 1 since it was in NumPy before passing it into a tensor and it worked . Would you consider it to be the right way to do it?