Hi Patrice!



 Patrice:

I have applied target.mean() as you recommended, to determine the fraction of 1s in the target tensor and it gave me very small values (0.2128, 0.099, 0.0421) for different tensors (images). I have used 0.2128 as the pos_weight.


You’re doing it backwards.  You have fewer 1s than 0s, but by using
0.2128 for pos_weight you are further reducing the weight of the 1s
pixels in your loss function.
Let’s say that averaged over multiple images your fraction of 1s is
about 0.10.  Then, approximately, you would want to use a value
of pos_weight of 1.0 / 0.10 = 10.0 to increase the weight of
the 1s.
More precisely (but we don’t need to be precise about the value of
pos_weight), you would use pos_weight = (1.0 - 0.10) / 0.10.
Good luck.
K. Frank