Hi Bran!



 stark:

I would like my network to provide me the quantitative measure. It could be the probability for each present class OR the estimated class count.


My intuition would be to use MSELoss on the class counts.
To me this seems to make more sense than the class “probabilities”
(normalized class counts).  Let’s say a sample image has one
instance of class B and one of class C, but your network only
detects class B.  Your actual normalized class count is 1/2 for
class B (and class C, as well), but your predicted value is 1.
So your network got class B right – one instance – but is being
penalized because the normalized class count is wrong.  It seems
to me that you want to give your network “full credit” for correctly
detecting exactly one instance of B (while penalizing it, of course,
for missing the instance of C).
Mean-squared-error makes sense because if, let’s say, you have
4 instances of C, detecting 3 or 5 is wrong, but not as wrong as
detecting 2 or 6.  (Mean-absolute-error would also work this way;
I don’t have intuition about which would be better.)
Note, if you use normalized class counts you are throwing away
information that might be helpful in training your network.  An image
with three instances of B and three of C has the same normalized
class counts as an image with one of each.  But rewarding your
network for correctly detecting all of the B and C instances would
seem to be making better use of your training data.
One comment on cross-entropy:  If you want to go the probability
route, it is true that cross-entropy is a measure of how much two
probability distributions deviate from one another.  But pytorch’s
built-in CrossEntropyLoss won’t work in this case because it
only handles the special case where the targets are integer class
labels – that is, where the target probability distribution is all 0.0’s
except for one 1.0.  If you want to use cross-entropy for this case
you would have to write your own “soft-label” cross-entropy loss
function.
Good luck.
K. Frank