Hi Ben!



 benkolber:

pixel values of 0,1,2, and 3 (3 classes total, with pixel 0 being the background).


The short story is that you have four classes, not three – “background”
counts as one of your classes.


When retrieving the output from the model, I have:
output from model = (b_s, 3, 256, 256)
predicted mask = (b_s, 256, 256)


Your model output has shape [b_s, n_classes = 3, 256, 256].  So,
yes, your model is predicting (only) three classes.


I use a cross-entropy loss function, but receive the following error:
IndexError: Target 3 is out of bounds.



CrossEntropyLoss sees that its input (your model output) has
n_classes = 3, so it will require that your target only has values
for three classes.  That is, your target values must be integer class
labels running from [0, n_classes - 1], i.e., be in (0, 1, 2).
But your target has values in (0, 1, 2, 3).  When CrossEntropyLoss
hits the “fourth” class value of 3, it throws the error you see.
It sounds like your problem really does have four classes, three
foreground classes plus one background class.  So leave your target
as it is, but run your model with n_classes = 4 so that its output
has shape [b_s, 4, 256, 256].
Note, you might want to check whether your dataset is unbalanced.
It is not unusual to have many more background pixels than foreground.
(I’m not saying you do, but you might.)  If so, you might consider using
CrossEntropyLoss's weight argument to reweight the classes in your
loss calculation.
Best.
K. Frank