-
-
Notifications
You must be signed in to change notification settings - Fork 695
Feat: implement get_sequence_transform for Accuracy metric #3637
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
Open
Manimaran-tech
wants to merge
11
commits into
pytorch:master
Choose a base branch
from
Manimaran-tech:feature/sequence-accuracy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a4bc092
Feat: implement get_sequence_transform for Accuracy metric
Manimaran-tech 0c1ce12
Refactor get_sequence_transform to utils and support multiple ignore …
Manimaran-tech bf3bdfa
Address review comments on typing and empty lines
Manimaran-tech 90860eb
Revert added empty lines in test_accuracy.py
Manimaran-tech 4dee3fb
Support 3D y_pred and y with identical shape
Manimaran-tech 60918cd
Simplify utils.py branches and split test cases
Manimaran-tech 370bac7
feat: move get_sequence_transform to _BaseClassification as static me…
1c3dfdf
refactor: remove metrics/utils.py and simplify get_sequence_transform…
f93c6d3
refactor: limit sequence transform to documented common shapes
43a0be3
Merge branch 'master' into feature/sequence-accuracy
Manimaran-tech 5f08b5c
refactor: simplify get_sequence_transform to support only (N, S, C)/(…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import torch | ||
| from typing import Callable, Iterable, Sequence | ||
|
|
||
|
|
||
| def get_sequence_transform( | ||
| ignore_index: int | Iterable[int] | None = None, | ||
| output_transform: Callable = lambda x: x, | ||
| ) -> Callable: | ||
| """ | ||
| Returns a callable to transform sequence model outputs for metric evaluation. | ||
| It flattens the sequences and filters out the padding (`ignore_index`). | ||
|
|
||
| Args: | ||
| ignore_index: An integer or an iterable of integers representing padding or | ||
| special tokens to be masked out from the sequence evaluation. | ||
| output_transform: A callable to transform the output into `(y_pred, y)`. | ||
|
|
||
| Returns: | ||
| Callable that flattens `y_pred` and `y` and removes `ignore_index` elements. | ||
| """ | ||
| def wrapper(output: Sequence[torch.Tensor]) -> tuple[torch.Tensor, torch.Tensor]: | ||
| y_pred, y = output_transform(output) | ||
|
|
||
| if y_pred.ndimension() == 3 and y.ndimension() == 2: | ||
| if y_pred.shape[:2] == y.shape: | ||
| # y_pred is (N, L, C), y is (N, L) | ||
| y_pred = y_pred.reshape(-1, y_pred.size(-1)) | ||
| y = y.reshape(-1) | ||
| elif y_pred.shape[0] == y.shape[0] and y_pred.shape[2] == y.shape[1]: | ||
| # y_pred is (N, C, L), y is (N, L) | ||
| y_pred = y_pred.transpose(1, 2).reshape(-1, y_pred.size(1)) | ||
| y = y.reshape(-1) | ||
| else: | ||
| raise ValueError( | ||
| f"y_pred and y have incompatible sequence shapes: " | ||
| f"y_pred={y_pred.shape} vs y={y.shape}" | ||
| ) | ||
| elif y_pred.ndimension() == 3 and y.ndimension() == 3: | ||
| # y_pred is (N, L, C) or (N, C, L), y has the same shape | ||
| if y_pred.shape == y.shape: | ||
| y_pred = y_pred.reshape(-1) | ||
| y = y.reshape(-1) | ||
| else: | ||
| raise ValueError( | ||
| f"y_pred and y have incompatible sequence shapes: " | ||
| f"y_pred={y_pred.shape} vs y={y.shape}" | ||
| ) | ||
| elif y_pred.ndimension() == 2 and y.ndimension() == 2: | ||
| # y_pred is (N, L), y is (N, L) | ||
| if y_pred.shape == y.shape: | ||
| y_pred = y_pred.reshape(-1) | ||
| y = y.reshape(-1) | ||
| else: | ||
| raise ValueError( | ||
| f"y_pred and y have incompatible sequence shapes: " | ||
| f"y_pred={y_pred.shape} vs y={y.shape}" | ||
| ) | ||
| else: | ||
| raise ValueError( | ||
| f"y_pred and y must be 3D/2D, 3D/3D, or 2D/2D arrays " | ||
| f"for sequence transformation. Got {y_pred.ndimension()}D and {y.ndimension()}D." | ||
| ) | ||
|
|
||
| if ignore_index is not None: | ||
| if isinstance(ignore_index, Iterable): | ||
| mask = torch.ones_like(y, dtype=torch.bool) | ||
| for idx in ignore_index: | ||
| mask &= (y != idx) | ||
| else: | ||
| mask = y != ignore_index | ||
|
|
||
| y_pred = y_pred[mask] | ||
| y = y[mask] | ||
|
|
||
| return y_pred, y | ||
|
|
||
| return wrapper | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import pytest | ||
| import torch | ||
|
|
||
| from ignite.metrics.utils import get_sequence_transform | ||
|
|
||
| def test_get_sequence_transform(): | ||
| # test (N, L, C) | ||
| y_pred = torch.tensor( | ||
| [ | ||
| [[0.1, 0.9], [0.8, 0.2], [0.3, 0.7], [0.5, 0.5]], | ||
| [[0.9, 0.1], [0.2, 0.8], [0.4, 0.6], [0.5, 0.5]], | ||
| ] | ||
| ) # shape: (2, 4, 2) | ||
| y = torch.tensor([[1, 0, 1, -1], [0, 1, 0, -1]]) # shape: (2, 4) | ||
|
|
||
| transform = get_sequence_transform(ignore_index=-1) | ||
| y_pred_t, y_t = transform((y_pred, y)) | ||
|
|
||
| assert y_pred_t.shape == (6, 2) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think when you apply mask the returned tensor is 1D, can you double check tests and run them. This test will fail.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my bad its correct |
||
| assert y_t.shape == (6,) | ||
| assert y_t.tolist() == [1, 0, 1, 0, 1, 0] | ||
| assert y_pred_t[:, 1].tolist() == pytest.approx([0.9, 0.2, 0.7, 0.1, 0.8, 0.6]) | ||
|
|
||
| # test (N, C, L) | ||
| y_pred_ncl = y_pred.transpose(1, 2).contiguous() # (2, 2, 4) | ||
| y_pred_t2, y_t2 = transform((y_pred_ncl, y)) | ||
| assert y_pred_t2.shape == (6, 2) | ||
| assert torch.all(y_pred_t2 == y_pred_t) | ||
| assert torch.all(y_t2 == y_t) | ||
|
|
||
| # test binary (N, L) | ||
| y_pred_bin = torch.tensor([[1, 0, 1, 1], [0, 1, 0, 0]]) | ||
| y_bin = torch.tensor([[1, 0, 1, 2], [0, 1, 0, 2]]) | ||
| transform_bin = get_sequence_transform(ignore_index=2) | ||
| y_pred_bin_t, y_bin_t = transform_bin((y_pred_bin, y_bin)) | ||
|
|
||
| assert y_pred_bin_t.shape == (6,) | ||
| assert y_bin_t.shape == (6,) | ||
| assert y_bin_t.tolist() == [1, 0, 1, 0, 1, 0] | ||
| assert y_pred_bin_t.tolist() == [1, 0, 1, 0, 1, 0] | ||
|
|
||
| # test without padding | ||
| transform_nopad = get_sequence_transform() | ||
| y_pred_nopad, y_nopad = transform_nopad((y_pred_bin, y_bin)) | ||
| assert y_pred_nopad.shape == (8,) | ||
| assert y_nopad.shape == (8,) | ||
|
|
||
| # test multiple ignore_index values | ||
| y_bin = torch.tensor([[1, -1, 1, 2], [0, 1, -1, 2]]) | ||
| transform_multi = get_sequence_transform(ignore_index=[-1, 2]) | ||
| y_pred_multi_t, y_multi_t = transform_multi((y_pred_bin, y_bin)) | ||
| assert y_pred_multi_t.shape == (4,) | ||
| assert y_multi_t.shape == (4,) | ||
| assert y_multi_t.tolist() == [1, 1, 0, 1] | ||
|
|
||
| # test 3D tensors matched shaping (N, C, L) with (N, C, L) | ||
| y_pred_3d = torch.tensor([[[0.1, 0.9], [0.8, 0.2]], [[0.3, 0.7], [0.5, 0.5]]]) | ||
| y_3d = torch.tensor([[[1, 0], [1, 1]], [[0, 1], [0, 0]]]) | ||
| transform_3d = get_sequence_transform() | ||
| y_pred_3d_t, y_3d_t = transform_3d((y_pred_3d, y_3d)) | ||
|
|
||
| assert y_pred_3d_t.shape == (8,) | ||
| assert y_3d_t.shape == (8,) | ||
| assert y_pred_3d_t.tolist() == pytest.approx([0.1, 0.9, 0.8, 0.2, 0.3, 0.7, 0.5, 0.5]) | ||
| assert y_3d_t.tolist() == [1, 0, 1, 1, 0, 1, 0, 0] | ||
|
|
||
| # test bad shapes | ||
| y_bad = torch.tensor([1, 0, 1]) | ||
| with pytest.raises(ValueError, match="must be 3D/2D, 3D/3D, or 2D/2D arrays"): | ||
| transform((y_pred_bin, y_bad)) | ||
|
|
||
| y_pred_bad = torch.tensor([[[1], [2]], [[3], [4]]]) | ||
| y_bad = torch.tensor([[1, 2, 3], [4, 5, 6]]) | ||
| with pytest.raises(ValueError, match="incompatible sequence shapes"): | ||
| transform((y_pred_bad, y_bad)) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
For this case the
y_predshould be in shape(N,C)this block is turning it to(N*C)and foryargmax should be taken because the Accuracy metric doesn't support one hot encoded labels.