Hi Bigtree!



 bigtree:

Without Sigmoid, the out will be a random floating number may out of range [0, 1].
what should do to regulate the out to either 0 or 1 for each pixel when doing inference?


Well, it won’t be a random floating-point number.  It will still be the
prediction of the network, but expressed as a raw-score logit that
runs from -inf to inf, rather than a probability between 0 and 1.
The question is how to convert a probabilistic prediction (whether
expressed as a logit or a probability) to a hard 0-1 prediction.
If it were a probability, we would typically threshold the probability
against 0.5 (e.g., p < 0.5 means a hard prediction of 0, while
p > 0.5 means a hard prediction of 1).  This is equivalent to
thresholding the logit against 0.0:  logit < 0.0 means 0 and
logit > 0.0 means 1.  This is because a logit of 0.0
corresponds to a probability of 0.5, i.e. sigmoid (0.0) = 0.5.
In your case:


pred = out > 0.75



you choose to threshold your probability against 0.75.  (There’s
nothing wrong with doing this.)  This corresponds to thresholding
your logit against log (0.75 / (1 - 0.75)) = 1.099 (which
is to say that sigmoid (1.099) = 0.75).
That is, leave out the Sigmoid (so that out is a logit), and calculate
your hard 0-1 prediction as
pred = out > log (0.75 / (1 - 0.75))

Equivalently you could pass out through sigmoid() after
calculating your loss with BCEWithLogitsLoss, and threshold
the result against 0.75:
pred = sigmoid (out) > 0.75

Best.
K. Frank