-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #681 from mv1388/torch_metrics_support
Torch metrics support
- Loading branch information
Showing
7 changed files
with
180 additions
and
4 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
aitoolbox/experiment/result_package/torch_metrics_packages.py
This file contains 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,33 @@ | ||
from aitoolbox.experiment.result_package.abstract_result_packages import AbstractResultPackage | ||
|
||
|
||
class TorchMetricsPackage(AbstractResultPackage): | ||
def __init__(self, torch_metrics): | ||
"""Torch Metrics result package wrapper | ||
https://github.com/Lightning-AI/metrics | ||
Args: | ||
torch_metrics (torchmetrics.Metric or torchmetrics.MetricCollection): single torchmetrics metric object or | ||
a collection of such metrics wrapped inside the MetricCollection | ||
""" | ||
AbstractResultPackage.__init__(self, pkg_name='Torch Metrics', np_array=False) | ||
|
||
self.metric = torch_metrics | ||
|
||
def prepare_results_dict(self): | ||
metric_result = self.metric(self.y_predicted, self.y_true) | ||
|
||
if not isinstance(metric_result, dict): | ||
metric_result = {self.metric.__class__.__name__: metric_result} | ||
|
||
# Add suffix PTLMetrics to indicate that we are using PyTorch Lightning metrics instead of aitoolbox metric | ||
metric_result = {f'{k}_PTLMetrics': v for k, v in metric_result.items()} | ||
|
||
return metric_result | ||
|
||
def metric_compute(self): | ||
return self.metric.compute() | ||
|
||
def metric_reset(self): | ||
self.metric.reset() |
This file contains 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 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 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
41 changes: 41 additions & 0 deletions
41
tests/test_experiment/test_result_package/test_torch_metrics_packages.py
This file contains 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,41 @@ | ||
import unittest | ||
|
||
from tests.utils import DummyTorchMetrics | ||
|
||
from aitoolbox.experiment.result_package.torch_metrics_packages import TorchMetricsPackage | ||
|
||
|
||
class TestTorchMetricsPackage(unittest.TestCase): | ||
def test_prepare_results_dict_formatting_float_result(self): | ||
metric = DummyTorchMetrics(return_float=True) | ||
result_package = TorchMetricsPackage(metric) | ||
|
||
result_package.prepare_result_package([], []) | ||
result_dict = result_package.get_results() | ||
|
||
self.assertEqual(result_dict, {'DummyTorchMetrics_PTLMetrics': 123.4}) | ||
|
||
def test_prepare_results_dict_formatting_dict_result(self): | ||
metric = DummyTorchMetrics(return_float=False) | ||
result_package = TorchMetricsPackage(metric) | ||
|
||
result_package.prepare_result_package([], []) | ||
result_dict = result_package.get_results() | ||
|
||
self.assertEqual(result_dict, {'metric_1_PTLMetrics': 12.34, 'metric_2_PTLMetrics': 56.78}) | ||
|
||
def test_metric_compute(self): | ||
metric = DummyTorchMetrics() | ||
result_package = TorchMetricsPackage(metric) | ||
|
||
for i in range(100): | ||
result_package.metric_compute() | ||
self.assertEqual(metric.compute_ctr, i + 1) | ||
|
||
def test_metric_reset(self): | ||
metric = DummyTorchMetrics() | ||
result_package = TorchMetricsPackage(metric) | ||
|
||
for i in range(100): | ||
result_package.metric_reset() | ||
self.assertEqual(metric.reset_ctr, i + 1) |
This file contains 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 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