Hi Stark!



 stark:

Your explanation makes sense for multi-class semantic segmentation.
What if I want predicted class labels [N×1×H×W] from torch.sigmoid(logits) either as 0 or 1


If by this you mean that you wish to perform binary semantic
segmentation, then you would do essentially as I described
above, but use torch.sigmoid (alpha * logits) (rather than
torch.softmax()).  And, analogously, the larger you make your
choice of alpha, the “harder” your binary predictions will become.


for a hard loss function? All such attempt using thresholding, torch.one_hot(),… fails to retain the gradient fucntion.


Yes, if you insist on a hard loss function, that is, something like
integer_valued_intersection / integer_valued_union,
you will, as you have seen, lose the gradients.
When you turn something that feeds into your loss function into an
integer, you’ve applied a discontinuous function that isn’t usefully
differentiable.  So you need to design a “soft” version of your desired
loss function through which you can backpropagate.
As an aside, why not use BCEWithLogitsLoss (on a per-pixel basis)
for your binary semantic segmentation?  People often use this loss
function to good effect.
Best.
K. Frank