Diauro:

Basically I have PNG image but already labelled with index 0 , 1 , 2, 3


In that case you don’t need any mapping, as your mask will already contain the class indices.



 Diauro:

``
I tried to add in the calculation of the loss: target_ = torch.empty(batch_size, 1,64,64)  target = target_.to(device)


torch.empty uses uninitialized memory to create the tensor, so it might contain invalid values (e.g. NaNs).
For a multi-class segmentation (each pixel belongs to one class only). you should use nn.CrossEntropyLoss instead of nn.BCEWithLogitsLoss.
The latter criterion can be used for a multi-label classification/segmentation (each pixel can belong to zero, one, or more classes).