Skip to content
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

score() returns information with predictions and labels #70

Merged
merged 17 commits into from
Aug 31, 2023

Conversation

cofri
Copy link
Contributor

@cofri cofri commented Aug 28, 2023

This PR aims at returning both score and predictions when calling score() function. Many changes have been introduced:

  • predict_tensor() in FeatureExtractor now returns the intermediate features + the model output (logits) as a list of tensors. The logits tensor is always the last element of the list.
  • predict() in FeatureExtractor now returns a tuple of 2 elements: the features (legacy behaviour) and a dictionary containing the logits and the labels.
  • The flag return_labels in predict() was removed. This flag is now useless since labels are in the new dictionary.
  • score() in detectors returns a tuple of 2 elements: the OOD scores (legacy behaviour) and the same dictionary as above.
  • output_layers_id has been renamed to feature_layers_id

The initialization of _features is done once in __init__()
It is not necessary to initialize it again when _get_features_hook()
is called.
At feature extractor construction, the output_layers_id are used to return
features. The model output, i.e the logits, are now also returned.
As a consequence, predict_tensor() always returns a list of features + logits,
even for a single tensor.

Note that, for PyTorch feature extractor, a new member `self._hook_layers_id`
is introduced: it is simply the output_layers_id + the last layer id (-1) for
the model logits. This new member variable is used to register the hooks in
prepare_extractor() and to return the features+logits in predict_tensor().
This commit prepares the new architecture of predict() where a dictionary
will be returned with extra information, such as labels and logits.
predict() now returns a 2-tuple of features and extra information. Extra
information contains the logits of the model and the labels.
Since predict() now returns a tuple (features, info), all calls to predict()
are updated.
The two methods `score()` and `_score_tensor()` of OOD detectors now
return a tuple:
- a NumPy array of OOD scores
- a dictionary containing extra information e.g. logits and labels. This
  dictionary is the same as in extractor `predict()`.
@cofri cofri force-pushed the feat/score_and_predict_together branch from 9e8ddb5 to 0675fd4 Compare August 29, 2023 14:49
The argument `return_labels` has been removed and predict() now returns a dict
containing logits and labels.
The unit tests are updated to reflect these changes.
The `score()` function of OOD detectors now returns a tuple of scores and a
dictionary containing extra information. The tests now reflect these changes.
@cofri cofri force-pushed the feat/score_and_predict_together branch from 0675fd4 to b2cc11f Compare August 29, 2023 15:04
@cofri cofri changed the title Feat/score and predict together score() returns information with predictions and labels Aug 29, 2023
We now use the info["logits"] to compute ood scores for these detectors
@github-actions
Copy link

github-actions bot commented Aug 29, 2023

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
1941 1773 91% 70% 🟢

New Files

No new covered files...

Modified Files

File Coverage Status
oodeel/datasets/tf_data_handler.py 85% 🟢
oodeel/datasets/torch_data_handler.py 89% 🟢
oodeel/eval/plots/features.py 94% 🟢
oodeel/eval/plots/plotly.py 92% 🟢
oodeel/extractor/feature_extractor.py 94% 🟢
oodeel/extractor/keras_feature_extractor.py 87% 🟢
oodeel/extractor/torch_feature_extractor.py 98% 🟢
oodeel/methods/base.py 75% 🟢
oodeel/methods/dknn.py 100% 🟢
oodeel/methods/energy.py 100% 🟢
oodeel/methods/entropy.py 100% 🟢
oodeel/methods/mahalanobis.py 98% 🟢
oodeel/methods/mls.py 100% 🟢
oodeel/methods/odin.py 100% 🟢
oodeel/methods/vim.py 86% 🟢
TOTAL 93% 🟢

updated for commit: 8a6adb2 by action🐍

Copy link
Member

@paulnovello paulnovello left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While reviewing, I felt that finally there were many ,_ cluttering the codebase. I wonder if we would better only return a dict like {features:..., pred:..., labels:..., ...}...
Otherwise, mostly a few explanations to add in doc.

README.md Show resolved Hide resolved
docs/index.md Show resolved Hide resolved
docs/pages/getting_started.ipynb Outdated Show resolved Hide resolved
oodeel/methods/base.py Show resolved Hide resolved
docs/pages/feature_extractor_tuto.ipynb Outdated Show resolved Hide resolved
oodeel/extractor/feature_extractor.py Outdated Show resolved Hide resolved
oodeel/methods/base.py Outdated Show resolved Hide resolved
oodeel/methods/dknn.py Outdated Show resolved Hide resolved
oodeel/methods/energy.py Outdated Show resolved Hide resolved
oodeel/methods/entropy.py Outdated Show resolved Hide resolved
oodeel/methods/mahalanobis.py Outdated Show resolved Hide resolved
oodeel/methods/mls.py Outdated Show resolved Hide resolved
oodeel/methods/odin.py Outdated Show resolved Hide resolved
oodeel/methods/vim.py Outdated Show resolved Hide resolved
y-prudent and others added 5 commits August 30, 2023 22:25
* Now _score_tensor outputs scores and logits while labels are accessed
in the score function
* For consistency and to spare VRAM, labels and logits are converted to np arrays
@paulnovello paulnovello self-requested a review August 31, 2023 09:41
@paulnovello paulnovello marked this pull request as ready for review August 31, 2023 09:42
@paulnovello paulnovello merged commit 5efa5bd into master Aug 31, 2023
10 checks passed
@cofri cofri deleted the feat/score_and_predict_together branch August 31, 2023 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add score_predict to avoid duplicate forwards when also using the model for inference
3 participants