BCELoss for binary pixel-wise segmentation pytorch
I'm implementing a UNet for binary segmentation while using Sigmoid and BCELoss. The problem is that after several iterations the network tries to predict very small values per pixel  while for some regions it should predict values close to one (for ground truth mask region). Does it give any intuition about the wrong behavior?

Besides, there exist NLLLoss2d which is used for pixel-wise loss. Currently, I'm simply ignoring this and I'm using MSELoss() directly. Should I use NLLLoss2d with Sigmoid activation layer?

Thanks

You might want to use torch.nn.BCEWithLogitsLoss(), replacing the Sigmoid and the BCELoss function. 

An excerpt from the docs tells you why its always better to use this loss function implementation.


  This loss combines a Sigmoid layer and the BCELoss in one single class. This version is more numerically stable than using a plain Sigmoid followed by a BCELoss as, by combining the operations into one layer, we take advantage of the log-sum-exp trick for numerical stability.

