-
-
Notifications
You must be signed in to change notification settings - Fork 666
Add MetricGroup feature
#3266
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
Merged
Merged
Add MetricGroup feature
#3266
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
62feff2
Initial commit
sadra-barikbin 5ed7068
Add tests
sadra-barikbin c8b368f
Fix two typos
sadra-barikbin 0d792fb
Merge branch 'master' into feature-metric-group
vfdev-5 44f3558
Fix Mypy
sadra-barikbin 1e13f2c
Merge remote-tracking branch 'refs/remotes/upstream/feature-metric-gr…
sadra-barikbin bd8e88b
Fix engine mypy issue
sadra-barikbin 8471c4c
Fix docstring
sadra-barikbin 8301916
Fix another problem in docstring
sadra-barikbin 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
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,54 @@ | ||
| from typing import Any, Callable, Dict, Sequence | ||
|
|
||
| import torch | ||
|
|
||
| from ignite.metrics import Metric | ||
|
|
||
|
|
||
| class MetricGroup(Metric): | ||
| """ | ||
| A class for grouping metrics so that user could manage them easier. | ||
|
|
||
| Args: | ||
| metrics: a dictionary of names to metric instances. | ||
| output_transform: a callable that is used to transform the | ||
| :class:`~ignite.engine.engine.Engine`'s ``process_function``'s output into the | ||
| form expected by the metric. `output_transform` of each metric in the group is also | ||
| called upon its update. | ||
|
|
||
| Examples: | ||
| We construct a group of metrics, attach them to the engine at once and retrieve their result. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import torch | ||
|
|
||
| metric_group = MetricGroup({'acc': Accuracy(), 'precision': Precision(), 'loss': Loss(nn.NLLLoss())}) | ||
| metric_group.attach(default_evaluator, "eval_metrics") | ||
| y_true = torch.tensor([1, 0, 1, 1, 0, 1]) | ||
| y_pred = torch.tensor([1, 0, 1, 0, 1, 1]) | ||
| state = default_evaluator.run([[y_pred, y_true]]) | ||
|
|
||
| # Metrics individually available in `state.metrics` | ||
| state.metrics["acc"], state.metrics["precision"], state.metrics["loss"] | ||
|
|
||
| # And also altogether | ||
| state.metrics["eval_metrics"] | ||
| """ | ||
|
|
||
| _state_dict_all_req_keys = ("metrics",) | ||
|
|
||
| def __init__(self, metrics: Dict[str, Metric], output_transform: Callable = lambda x: x): | ||
| self.metrics = metrics | ||
| super(MetricGroup, self).__init__(output_transform=output_transform) | ||
|
|
||
| def reset(self) -> None: | ||
| for m in self.metrics.values(): | ||
| m.reset() | ||
|
|
||
| def update(self, output: Sequence[torch.Tensor]) -> None: | ||
| for m in self.metrics.values(): | ||
| m.update(m._output_transform(output)) | ||
|
|
||
| def compute(self) -> Dict[str, Any]: | ||
| return {k: m.compute() for k, m in self.metrics.items()} | ||
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,118 @@ | ||
| import pytest | ||
| import torch | ||
|
|
||
| from ignite import distributed as idist | ||
| from ignite.engine import Engine | ||
| from ignite.metrics import Accuracy, MetricGroup, Precision | ||
|
|
||
| torch.manual_seed(41) | ||
|
|
||
|
|
||
| def test_update(): | ||
| precision = Precision() | ||
| accuracy = Accuracy() | ||
|
|
||
| group = MetricGroup({"precision": Precision(), "accuracy": Accuracy()}) | ||
|
|
||
| y_pred = torch.randint(0, 2, (100,)) | ||
| y = torch.randint(0, 2, (100,)) | ||
|
|
||
| precision.update((y_pred, y)) | ||
| accuracy.update((y_pred, y)) | ||
| group.update((y_pred, y)) | ||
|
|
||
| assert precision.state_dict() == group.metrics["precision"].state_dict() | ||
| assert accuracy.state_dict() == group.metrics["accuracy"].state_dict() | ||
|
|
||
|
|
||
| def test_output_transform(): | ||
| def drop_first(output): | ||
| y_pred, y = output | ||
| return (y_pred[1:], y[1:]) | ||
|
|
||
| precision = Precision(output_transform=drop_first) | ||
| accuracy = Accuracy(output_transform=drop_first) | ||
|
|
||
| group = MetricGroup( | ||
| {"precision": Precision(output_transform=drop_first), "accuracy": Accuracy(output_transform=drop_first)} | ||
| ) | ||
|
|
||
| y_pred = torch.randint(0, 2, (100,)) | ||
| y = torch.randint(0, 2, (100,)) | ||
|
|
||
| precision.update(drop_first(drop_first((y_pred, y)))) | ||
| accuracy.update(drop_first(drop_first((y_pred, y)))) | ||
| group.update(drop_first((y_pred, y))) | ||
|
|
||
| assert precision.state_dict() == group.metrics["precision"].state_dict() | ||
| assert accuracy.state_dict() == group.metrics["accuracy"].state_dict() | ||
|
|
||
|
|
||
| def test_compute(): | ||
| precision = Precision() | ||
| accuracy = Accuracy() | ||
|
|
||
| group = MetricGroup({"precision": Precision(), "accuracy": Accuracy()}) | ||
|
|
||
| for _ in range(3): | ||
| y_pred = torch.randint(0, 2, (100,)) | ||
| y = torch.randint(0, 2, (100,)) | ||
|
|
||
| precision.update((y_pred, y)) | ||
| accuracy.update((y_pred, y)) | ||
| group.update((y_pred, y)) | ||
|
|
||
| assert group.compute() == {"precision": precision.compute(), "accuracy": accuracy.compute()} | ||
|
|
||
| precision.reset() | ||
| accuracy.reset() | ||
| group.reset() | ||
|
|
||
| assert precision.state_dict() == group.metrics["precision"].state_dict() | ||
| assert accuracy.state_dict() == group.metrics["accuracy"].state_dict() | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("distributed") | ||
| class TestDistributed: | ||
| def test_integration(self): | ||
| rank = idist.get_rank() | ||
| torch.manual_seed(12 + rank) | ||
|
|
||
| n_epochs = 3 | ||
| n_iters = 5 | ||
| batch_size = 10 | ||
| device = idist.device() | ||
|
|
||
| y_true = torch.randint(0, 2, size=(n_iters * batch_size,)).to(device) | ||
| y_pred = torch.randint(0, 2, (n_iters * batch_size,)).to(device) | ||
|
|
||
| def update(_, i): | ||
| return ( | ||
| y_pred[i * batch_size : (i + 1) * batch_size], | ||
| y_true[i * batch_size : (i + 1) * batch_size], | ||
| ) | ||
|
|
||
| engine = Engine(update) | ||
|
|
||
| precision = Precision() | ||
| precision.attach(engine, "precision") | ||
|
|
||
| accuracy = Accuracy() | ||
| accuracy.attach(engine, "accuracy") | ||
|
|
||
| group = MetricGroup({"eval_metrics.accuracy": Accuracy(), "eval_metrics.precision": Precision()}) | ||
| group.attach(engine, "eval_metrics") | ||
|
|
||
| data = list(range(n_iters)) | ||
| engine.run(data=data, max_epochs=n_epochs) | ||
|
|
||
| assert "eval_metrics" in engine.state.metrics | ||
| assert "eval_metrics.accuracy" in engine.state.metrics | ||
| assert "eval_metrics.precision" in engine.state.metrics | ||
|
|
||
| assert engine.state.metrics["eval_metrics"] == { | ||
| "eval_metrics.accuracy": engine.state.metrics["accuracy"], | ||
| "eval_metrics.precision": engine.state.metrics["precision"], | ||
| } | ||
| assert engine.state.metrics["eval_metrics.accuracy"] == engine.state.metrics["accuracy"] | ||
| assert engine.state.metrics["eval_metrics.precision"] == engine.state.metrics["precision"] |
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.
Uh oh!
There was an error while loading. Please reload this page.