-
Notifications
You must be signed in to change notification settings - Fork 412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MulticlassJaccardIndex returns 0 for classes not present in prediction and target #1693
Comments
Hi! thanks for your contribution!, great first issue! |
sklearn raises a warning in the described case: https://scikit-learn.org/stable/modules/generated/sklearn.metrics.jaccard_score.html |
I thought a bit more about this problem and I think it is questionable if we could set the IoU to 1.0 for unseen classes. I found that the JaccardIndex implementation internally updates a confusion matrix. The attached code shows the calculations for two "batches", where class 2 is missing in the first. As one can see, the default value of 0.0 when a class is missing, leads to a worse IoU. BUT calling compute() after all batches calculates the IoU values again based on the accumulated entries in the confusion matrix over all batches. In my opinion, this results in the correct values for the IoU values and should raise a warning or error if a class was missing in all batches. Nevertheless, a problem arises in combination with the lightning log process, as it calculates the mean over the macro IoU from the batches and does not use a final compute() based on the confusion matrix with the values from all batches. Personally, I don't use self.log in the validation/test step anymore, but update the JaccardMetrics there. At the end of the validation/test epoch I call compute() once, using the confusion matrix over all batches. Then I log this value.
|
🐛 Bug
MulticlassJaccardIndex returns 0 for classes not present in prediction and target. This leads to bad (worse) results if used in conjunction with average="macro".
There was a similar issue when the metrics were still a part of pytorch lightning: Lightning-AI/pytorch-lightning#3098
To Reproduce
MWE:
Second example (adapted copy from Lightning-AI/pytorch-lightning#3098):
Expected behavior
See comments in the MWEs.
Environment
The text was updated successfully, but these errors were encountered: