-
-
Notifications
You must be signed in to change notification settings - Fork 610
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
Bug Related to Calculation of Binary Metrics #349
Conversation
…alculation type remains the same during training, calculate binary precision using a threshold function vs categorical.
@vfdev-5 please see the following PR. If we stick with this method, it'll be easier to incorporate multilabel as well. |
@anmolsjoshi thanks for the PR ! |
@vfdev-5 no problem!
You are correct, they are the same for Precision and Recall. I took a note from how sklearn calculates precision and recall.
Definitely! I can work on that later tonight. |
Could you please post a reference on sklearn here ? Maybe we could create a common class for Precision, Recall in order to not to copy these methods ? |
Tell me when you are done with the code, I'll update the tests in the same way as for Accuracy. |
@vfdev-5 not sure what went wrong with the pytorch-nightly-cpu 2.7 |
Here is the sklearn reference |
@anmolsjoshi seems like that pytorch nightly is broken for 2.7 |
That's an interesting idea. So a base class with a common |
@vfdev-5 I believe that I'm done with all the tests and code. Feel free to incorporate this into Accuracy.
what are next steps? |
Let's ignore this failing test for a moment, if this is pytorch problem, it will be solved soon. We can ask to be sure. Next step is to refactor classes to avoid code copying. I'll improve tests. |
@vfdev-5 ok I'll start refactoring the classes. Did you want me to work on Accuracy as well? That's entirely fine, just don't want repeated efforts from both of us |
@anmolsjoshi yes please :) |
…port, precision/recall calculation into PrecisionRecallSupport. precision.py and recall.py - use PrecisionRecallSupport
@vfdev-5 I refactored the code, let me know if this works for you! Still working on Accuracy.
_classification_support.py
precision.py
recall.py
|
@anmolsjoshi I think we can create these classes directly in |
Agree, have updated. I also kept
Could you explain that further?
For now, I created two functions in class Precision(_BasePrecisionRecallSupport):
...
def update(self, output):
correct, y_pred, y = self._calculate_correct(output)
all_positives = y_pred.sum(dim=0)
self._sum_positives(correct, all_positives) This way there is no clunky if else statement like before.
Referring to this comment, it might be best to stick with threshold_function for binary calculation. I think initialization of _BaseClassification should stay the same, only thing that is added for Precision and Recall is the term average, which is introduced in _BasePrecisionRecallSupport (child of _BaseClassification).
I'll move Suggestion - let's create one file called precision_recall.py which includes _BasePrecisionRecallSupport, Precision and Recall, and change the init.py appropriately. - My latest commit reflects this, we can obviously revert back to previous setups. Thoughts? |
…pport (child of _BaseClassification). Refactored Accuracy into _BaseClassification.
…recisionRecallSupport, Precision, Recall.
@vfdev-5 Here is a summary of the most up to date commit:
Please note that Binary Accuracy is now being calculated using torch.round, I think it is the preferred method due to inconsistent results in between PyTorch versions. Discussed in this comment. |
@vfdev-5 all tests are now passing. I updated precision and recall so that they have there own unique updates. Let me know if you have any other issues! |
For instance I'm not sure that this is necessary... Let's see |
@anmolsjoshi this is a good point ! Actually, just remarked that in the docs it was directly stated that |
Yes, that's probably my bad when I sent in PR #275, I missed including that docstring. How do you think we should proceed? Still include binary to categorical mapping? Or introduce this change in the release notes? |
@anmolsjoshi no problems with that. We'll take care of it this time :) I'm readjusting the tests now.
Thanks for asking! So, as we discussed #348 in we need to fix the bug and cut the release In another PR we can remove |
I'll commit some modifications on |
ignite/metrics/precision.py
Outdated
y_pred, y = output | ||
|
||
if y.ndimension() + 1 == y_pred.ndimension(): | ||
if y_pred.shape[1] == 2: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@anmolsjoshi could you please recall me why you separate this case and raise a warning ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@vfdev-5 if num_classes=2, it is a binary case that is being fed as a categorical case. I think it'll be helpful to warn the user that only precision for the positive class if calculated in this case. Because in the case of binary, we shouldn't be average the precision of 0 and 1.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But we can also have multiclass 2-classes case which should be computed as N-classes too
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, I'll remove the binary_multiclass entirely and treat it as multiclass.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll take it in new modifications
@anmolsjoshi I did yet not finished the code and tests on precision. Seems binary case of (N, L) is not well coded. If you would like to continue, feel free (wont touch it at least for 7-8h :) |
@vfdev-5 thanks, this looks fantastic! I'll work on it |
@vfdev-5 all tests are passing, let me know what you think! Currently, there are warnings for Precision and Recall using sklearn for cases where number of predicted or actual positives is 0 for a specific class. |
@anmolsjoshi thanks a lot ! That starts look much better than the previous version :) |
@anmolsjoshi I updated the doc. @alykhantejani @jasonkriss any comments, review please |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! My only minor nit is that maybe we shouldn't have the _
for the _Base*
classes as we are importing them from other files.
@jasonkriss thanks for the review! My thought about adding the This is done over here in ignite as well. @vfdev-5 thoughts? |
@jasonkriss as @anmolsjoshi says the idea is to explicitly indicate that these classes are private (and abstract) and shouldn't be used by users. Another solution could be to put them into a I propose to keep it without modification |
@vfdev-5 @anmolsjoshi 👍 I'm fine keeping it as is. |
@anmolsjoshi thank you for the PR |
@anmolsjoshi let's work directly in #333 |
@vfdev-5 sounds good |
Fixes #348
Description:
Bug in Binary Precision/Recall maps binary cases into 2 classes and then averages the metrics of both. This is an incorrect method of calculating precision and recall for Precision and Recall. It should be treated as a one person class only.
I have included the following in the code:
Check list: