/usr/local/lib/python3.7/dist-packages/torch/nn/_reduction.py in legacy_get_string(size_average, reduce, emit_warning)
33 reduce = True
34
---> 35 if size_average and reduce:
36 ret = 'mean'
37 elif reduce:
RuntimeError: Boolean value of Tensor with more than one value is ambiguous
Should I change the target’s shape from (BxCxWxH) to (BxWxH)?
Is it necessary for the target to be one-hot-encoded?
Your problem is that you are instantiating a CrossEntropyLoss object
with garbled constructor arguments. I would recommend using the
functional form (as you had been doing with binary_cross_entropy()):
If your inputs (your predictions, i.e., the output of your model) has shape [B, C, W, H], then your targets (which I assume you mean by “masks”)
should have shape [B, W, H] (with no class dimension). The values of targets should be long class labels that run from 0 to C - 1, where C
is the number of classes.
Also, your inputs should be raw-score logits, that is, the output of your
model should be the output of its final Linear layer – not followed by softmax() (or sigmoid()).
Last, as an aside, unless you have a very large number of classes,
consider using regular cross entropy as your loss criterion, using class
weights if you have a significant class imbalance in your data. There are
also claims that you are likely to get better results using a focal-loss term
as an add-on to cross-entropy compared to using focal loss alone.
I would certainly suggest at least trying cross entropy. If you have a
significant class imbalance, then try using CrossEntropyLoss’s weight
constructor argument.
Focal loss is a perfectly reasonable loss criterion, and could well work for
you, especially if you very large number of classes. (How many classes do
you have?) But I would only use it if you can show that it gives you better
results than cross entropy for your specific use case.