I did not look at the code in details, but from the print and your comment:
Outputs Size torch.Size([1, 1, 256, 256])
targets[:, 0] Size torch.Size([1, 256, 256])

The sizes don’t seem to be the same  So maybe this is the reason?