Target out of bounds for semantic segmentation UNET cityscapes

Sai_Surya: <p>I am trying to train cityscapes dataset using UNet multiclass segmentation. my predictions are [B,C,H,W] and my labels [B,H,W].</p>
<p>here is my console with device as cpu</p>
<p>Model loaded<br>
Optimizer and Loss defined<br>
Start training…<br>
Starting Epoch 1<br>
0%|                                                                                                                              | 0/744 [00:17&lt;?, ?it/s]<br>
Traceback (most recent call last):<br>
File “C:\Users\srira\OneDrive\Desktop\Projects\segmentation\Real_time_seg\main.py”, line 81, in <br>
main()<br>
File “C:\Users\srira\OneDrive\Desktop\Projects\segmentation\Real_time_seg\main.py”, line 75, in main<br>
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)<br>
File “C:\Users\srira\OneDrive\Desktop\Projects\segmentation\Real_time_seg\train.py”, line 63, in train_model<br>
train_loss, running_mIOU = train(model, device, train_loader, optimizer, loss_function)<br>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>
File “C:\Users\srira\OneDrive\Desktop\Projects\segmentation\Real_time_seg\train.py”, line 18, in train<br>
loss = 0.8<em>loss_function(prediction, labels)  - 0.2</em>mIOU(labels, prediction)<br>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>
File “C:\Users\srira\anaconda3\envs\DL\Lib\site-packages\torch\nn\modules\module.py”, line 1501, in _call_impl<br>
return forward_call(*args, **kwargs)<br>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>
File “C:\Users\srira\OneDrive\Desktop\Projects\segmentation\Real_time_seg\utils\loss.py”, line 75, in forward<br>
ce = self.nll_loss(log_p, y)<br>
^^^^^^^^^^^^^^^^^^^^^^^<br>
File “C:\Users\srira\anaconda3\envs\DL\Lib\site-packages\torch\nn\modules\module.py”, line 1501, in _call_impl<br>
return forward_call(*args, **kwargs)<br>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>
File “C:\Users\srira\anaconda3\envs\DL\Lib\site-packages\torch\nn\modules\loss.py”, line 216, in forward<br>
return F.nll_loss(input, target, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction)<br>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>
File “C:\Users\srira\anaconda3\envs\DL\Lib\site-packages\torch\nn\functional.py”, line 2704, in nll_loss<br>
return torch._C._nn.nll_loss_nd(input, target, weight, _Reduction.get_enum(reduction), ignore_index)<br>
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^<br>
IndexError: Target 26 is out of bounds.</p>

ptrblck: <p>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.<br>
Either increase the number of outputs or make sure the targets contain valid class indices in <code>[0, nb_classes-1]</code>.</p>

Sai_Surya: <p>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.</p>