Target out of bounds for semantic segmentation UNET cityscapes

I am trying to train cityscapes dataset using UNet multiclass segmentation. my predictions are [B,C,H,W] and my labels [B,H,W].

here is my console with device as cpu

Model loaded
Optimizer and Loss defined
Start training…
Starting Epoch 1
0%| | 0/744 [00:17<?, ?it/s]
Traceback (most recent call last):
File “C:\Users\srira\OneDrive\Desktop\Projects\segmentation\Real_time_seg\main.py”, line 81, in
main()
File “C:\Users\srira\OneDrive\Desktop\Projects\segmentation\Real_time_seg\main.py”, line 75, in main
train.train_model(num_epochs=EPOCHS, model=model, device=device, train_loader=train_loader, val_loader=val_loader, optimizer=optimizer, loss_function=criterion, scheduler=scheduler, save_path = SAVE_PATH)
File “C:\Users\srira\OneDrive\Desktop\Projects\segmentation\Real_time_seg\train.py”, line 63, in train_model
train_loss, running_mIOU = train(model, device, train_loader, optimizer, loss_function)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\srira\OneDrive\Desktop\Projects\segmentation\Real_time_seg\train.py”, line 18, in train
loss = 0.8loss_function(prediction, labels) - 0.2mIOU(labels, prediction)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\srira\anaconda3\envs\DL\Lib\site-packages\torch\nn\modules\module.py”, line 1501, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\srira\OneDrive\Desktop\Projects\segmentation\Real_time_seg\utils\loss.py”, line 75, in forward
ce = self.nll_loss(log_p, y)
^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\srira\anaconda3\envs\DL\Lib\site-packages\torch\nn\modules\module.py”, line 1501, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\srira\anaconda3\envs\DL\Lib\site-packages\torch\nn\modules\loss.py”, line 216, in forward
return F.nll_loss(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File “C:\Users\srira\anaconda3\envs\DL\Lib\site-packages\torch\nn\functional.py”, line 2704, in nll_loss
return torch._C._nn.nll_loss_nd(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
IndexError: Target 26 is out of bounds.

Based on the error message it seems your model returns logits for less than 27 classes while the target contains a class label of 26 indicating that at least 27 classes (0-26) are expected.
Either increase the number of outputs or make sure the targets contain valid class indices in [0, nb_classes-1].

1 Like

Thanks for the response, I wanna use only 19 classes, anyway it has been solved. while returning the label in the dataloader I have used, label.clamp(max=18). This worked fine for me.