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

Skip unrolling follow up #3260

Merged
merged 15 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions ignite/metrics/accuracy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ def __init__(
output_transform: Callable = lambda x: x,
is_multilabel: bool = False,
device: Union[str, torch.device] = torch.device("cpu"),
skip_unrolling: bool = False,
):
self._is_multilabel = is_multilabel
self._type: Optional[str] = None
self._num_classes: Optional[int] = None
super(_BaseClassification, self).__init__(output_transform=output_transform, device=device)
super(_BaseClassification, self).__init__(
output_transform=output_transform, device=device, skip_unrolling=skip_unrolling
)

def reset(self) -> None:
self._type = None
Expand Down Expand Up @@ -114,6 +117,9 @@ class Accuracy(_BaseClassification):
device: specifies which device updates are accumulated on. Setting the metric's
device to be the same as your ``update`` arguments ensures the ``update`` method is non-blocking. By
default, CPU.
skip_unrolling: specifies whether output should be unrolled before being fed to update method. Should be
true for multi-output model, for example, if ``y_pred`` contains multi-ouput as ``(y_pred_a, y_pred_b)``
Alternatively, ``output_transform`` can be used to handle this.

Examples:

Expand Down Expand Up @@ -206,6 +212,9 @@ def thresholded_output_transform(output):
.. testoutput:: 4

0.6666...

.. versionchanged:: 0.5.1
``skip_unrolling`` argument is added.
"""

_state_dict_all_req_keys = ("_num_correct", "_num_examples")
Expand All @@ -215,8 +224,11 @@ def __init__(
output_transform: Callable = lambda x: x,
is_multilabel: bool = False,
device: Union[str, torch.device] = torch.device("cpu"),
skip_unrolling: bool = False,
):
super(Accuracy, self).__init__(output_transform=output_transform, is_multilabel=is_multilabel, device=device)
super(Accuracy, self).__init__(
output_transform=output_transform, is_multilabel=is_multilabel, device=device, skip_unrolling=skip_unrolling
)

@reinit__is_reduced
def reset(self) -> None:
Expand Down
4 changes: 4 additions & 0 deletions ignite/metrics/average_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,16 @@ def activated_output_transform(output):

0.9166...

.. versionchanged:: 0.5.1
simeetnayan81 marked this conversation as resolved.
Show resolved Hide resolved
``skip_unrolling`` argument is added.
"""

def __init__(
self,
output_transform: Callable = lambda x: x,
check_compute_fn: bool = False,
device: Union[str, torch.device] = torch.device("cpu"),
skip_unrolling: bool = False,
):
try:
from sklearn.metrics import average_precision_score # noqa: F401
Expand All @@ -78,4 +81,5 @@ def __init__(
output_transform=output_transform,
check_compute_fn=check_compute_fn,
device=device,
skip_unrolling=skip_unrolling,
)
7 changes: 7 additions & 0 deletions ignite/metrics/cohen_kappa.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class CohenKappa(EpochMetric):
is run on the first batch of data to ensure there are
no issues. User will be warned in case there are any issues computing the function.
device: optional device specification for internal storage.
skip_unrolling: specifies whether output should be unrolled before being fed to update method. Should be
true for multi-output model, for example, if ``y_pred`` contains multi-ouput as ``(y_pred_a, y_pred_b)``
Alternatively, ``output_transform`` can be used to handle this.

Examples:
To use with ``Engine`` and ``process_function``, simply attach the metric instance to the engine.
Expand All @@ -46,6 +49,8 @@ class CohenKappa(EpochMetric):

0.4285...

.. versionchanged:: 0.5.1
``skip_unrolling`` argument is added.
"""

def __init__(
Expand All @@ -54,6 +59,7 @@ def __init__(
weights: Optional[str] = None,
check_compute_fn: bool = False,
device: Union[str, torch.device] = torch.device("cpu"),
skip_unrolling: bool = False,
):
try:
from sklearn.metrics import cohen_kappa_score # noqa: F401
Expand All @@ -72,6 +78,7 @@ def __init__(
output_transform=output_transform,
check_compute_fn=check_compute_fn,
device=device,
skip_unrolling=skip_unrolling,
)

def get_cohen_kappa_fn(self) -> Callable[[torch.Tensor, torch.Tensor], float]:
Expand Down
11 changes: 10 additions & 1 deletion ignite/metrics/confusion_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class ConfusionMatrix(Metric):
device: specifies which device updates are accumulated on. Setting the metric's
device to be the same as your ``update`` arguments ensures the ``update`` method is non-blocking. By
default, CPU.
skip_unrolling: specifies whether output should be unrolled before being fed to update method. Should be
true for multi-output model, for example, if ``y_pred`` contains multi-ouput as ``(y_pred_a, y_pred_b)``
Alternatively, ``output_transform`` can be used to handle this.

Note:
The confusion matrix is formatted such that columns are predictions and rows are targets.
Expand Down Expand Up @@ -98,6 +101,9 @@ def binary_one_hot_output_transform(output):

tensor([[2, 1],
[1, 1]])

.. versionchanged:: 0.5.1
``skip_unrolling`` argument is added.
"""

_state_dict_all_req_keys = ("confusion_matrix", "_num_examples")
Expand All @@ -108,6 +114,7 @@ def __init__(
average: Optional[str] = None,
output_transform: Callable = lambda x: x,
device: Union[str, torch.device] = torch.device("cpu"),
skip_unrolling: bool = True,
):
if average is not None and average not in ("samples", "recall", "precision"):
raise ValueError("Argument average can None or one of 'samples', 'recall', 'precision'")
Expand All @@ -118,7 +125,9 @@ def __init__(
self.num_classes = num_classes
self._num_examples = 0
self.average = average
super(ConfusionMatrix, self).__init__(output_transform=output_transform, device=device)
super(ConfusionMatrix, self).__init__(
output_transform=output_transform, device=device, skip_unrolling=skip_unrolling
)

@reinit__is_reduced
def reset(self) -> None:
Expand Down
9 changes: 8 additions & 1 deletion ignite/metrics/cosine_similarity.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ class CosineSimilarity(Metric):
device: specifies which device updates are accumulated on. Setting the
metric's device to be the same as your ``update`` arguments ensures the ``update`` method is
non-blocking. By default, CPU.
skip_unrolling: specifies whether output should be unrolled before being fed to update method. Should be
true for multi-output model, for example, if ``y_pred`` contains multi-ouput as ``(y_pred_a, y_pred_b)``
Alternatively, ``output_transform`` can be used to handle this.

Examples:
To use with ``Engine`` and ``process_function``, simply attach the metric instance to the engine.
Expand Down Expand Up @@ -65,15 +68,19 @@ class CosineSimilarity(Metric):
.. testoutput::

0.5080491304397583

.. versionchanged:: 0.5.1
``skip_unrolling`` argument is added.
"""

def __init__(
self,
eps: float = 1e-8,
output_transform: Callable = lambda x: x,
device: Union[str, torch.device] = torch.device("cpu"),
skip_unrolling: bool = False,
):
super().__init__(output_transform, device)
super().__init__(output_transform, device, skip_unrolling=skip_unrolling)

self.eps = eps

Expand Down
6 changes: 6 additions & 0 deletions ignite/metrics/entropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ class Entropy(Metric):
device: specifies which device updates are accumulated on. Setting the
metric's device to be the same as your ``update`` arguments ensures the ``update`` method is
non-blocking. By default, CPU.
skip_unrolling: specifies whether output should be unrolled before being fed to update method. Should be
true for multi-output model, for example, if ``y_pred`` contains multi-ouput as ``(y_pred_a, y_pred_b)``
Alternatively, ``output_transform`` can be used to handle this.

Examples:
To use with ``Engine`` and ``process_function``, simply attach the metric instance to the engine.
Expand Down Expand Up @@ -58,6 +61,9 @@ class Entropy(Metric):
.. testoutput::

0.8902875582377116

.. versionchanged:: 0.5.1
``skip_unrolling`` argument is added.
"""

_state_dict_all_req_keys = ("_sum_of_entropies", "_num_examples")
Expand Down
8 changes: 7 additions & 1 deletion ignite/metrics/epoch_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ def mse_fn(y_preds, y_targets):
Warnings:
EpochMetricWarning: User is warned that there are issues with ``compute_fn`` on a batch of data processed.
To disable the warning, set ``check_compute_fn=False``.

.. versionchanged:: 0.5.1
``skip_unrolling`` argument is added.
"""

_state_dict_all_req_keys = ("_predictions", "_targets")
Expand All @@ -75,14 +78,17 @@ def __init__(
output_transform: Callable = lambda x: x,
check_compute_fn: bool = True,
device: Union[str, torch.device] = torch.device("cpu"),
skip_unrolling: bool = False,
) -> None:
if not callable(compute_fn):
raise TypeError("Argument compute_fn should be callable.")

self.compute_fn = compute_fn
self._check_compute_fn = check_compute_fn

super(EpochMetric, self).__init__(output_transform=output_transform, device=device)
super(EpochMetric, self).__init__(
output_transform=output_transform, device=device, skip_unrolling=skip_unrolling
)

@reinit__is_reduced
def reset(self) -> None:
Expand Down
6 changes: 6 additions & 0 deletions ignite/metrics/js_divergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ class JSDivergence(KLDivergence):
device: specifies which device updates are accumulated on. Setting the
metric's device to be the same as your ``update`` arguments ensures the ``update`` method is
non-blocking. By default, CPU.
skip_unrolling: specifies whether output should be unrolled before being fed to update method. Should be
true for multi-output model, for example, if ``y_pred`` contains multi-ouput as ``(y_pred_a, y_pred_b)``
Alternatively, ``output_transform`` can be used to handle this.

Examples:
To use with ``Engine`` and ``process_function``, simply attach the metric instance to the engine.
Expand Down Expand Up @@ -71,6 +74,9 @@ class JSDivergence(KLDivergence):
.. testoutput::

0.16266516844431558

.. versionchanged:: 0.5.1
``skip_unrolling`` argument is added.
"""

def _update(self, y_pred: torch.Tensor, y: torch.Tensor) -> None:
Expand Down
6 changes: 6 additions & 0 deletions ignite/metrics/kl_divergence.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class KLDivergence(Metric):
device: specifies which device updates are accumulated on. Setting the
metric's device to be the same as your ``update`` arguments ensures the ``update`` method is
non-blocking. By default, CPU.
skip_unrolling: specifies whether output should be unrolled before being fed to update method. Should be
true for multi-output model, for example, if ``y_pred`` contains multi-ouput as ``(y_pred_a, y_pred_b)``
Alternatively, ``output_transform`` can be used to handle this.

Examples:
To use with ``Engine`` and ``process_function``, simply attach the metric instance to the engine.
Expand Down Expand Up @@ -65,6 +68,9 @@ class KLDivergence(Metric):
.. testoutput::

0.7220296859741211

.. versionchanged:: 0.5.1
``skip_unrolling`` argument is added.
"""

_state_dict_all_req_keys = ("_sum_of_kl", "_num_examples")
Expand Down
14 changes: 12 additions & 2 deletions ignite/metrics/maximum_mean_discrepancy.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ class MaximumMeanDiscrepancy(Metric):
device: specifies which device updates are accumulated on. Setting the
metric's device to be the same as your ``update`` arguments ensures the ``update`` method is
non-blocking. By default, CPU.
skip_unrolling: specifies whether output should be unrolled before being fed to update method. Should be
true for multi-output model, for example, if ``y_pred`` contains multi-ouput as ``(y_pred_a, y_pred_b)``
Alternatively, ``output_transform`` can be used to handle this.

Examples:
To use with ``Engine`` and ``process_function``, simply attach the metric instance to the engine.
Expand Down Expand Up @@ -76,15 +79,22 @@ class MaximumMeanDiscrepancy(Metric):
.. testoutput::

1.072697639465332

.. versionchanged:: 0.5.1
``skip_unrolling`` argument is added.
"""

_state_dict_all_req_keys = ("_xx_sum", "_yy_sum", "_xy_sum", "_num_batches")

def __init__(
self, var: float = 1.0, output_transform: Callable = lambda x: x, device: torch.device = torch.device("cpu")
self,
var: float = 1.0,
output_transform: Callable = lambda x: x,
device: torch.device = torch.device("cpu"),
skip_unrolling: bool = False,
):
self.var = var
super().__init__(output_transform, device)
super().__init__(output_transform, device, skip_unrolling=skip_unrolling)

@reinit__is_reduced
def reset(self) -> None:
Expand Down
6 changes: 6 additions & 0 deletions ignite/metrics/mean_absolute_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class MeanAbsoluteError(Metric):
device: specifies which device updates are accumulated on. Setting the
metric's device to be the same as your ``update`` arguments ensures the ``update`` method is
non-blocking. By default, CPU.
skip_unrolling: specifies whether output should be unrolled before being fed to update method. Should be
true for multi-output model, for example, if ``y_pred`` contains multi-ouput as ``(y_pred_a, y_pred_b)``
Alternatively, ``output_transform`` can be used to handle this.

Examples:
To use with ``Engine`` and ``process_function``, simply attach the metric instance to the engine.
Expand Down Expand Up @@ -57,6 +60,9 @@ class MeanAbsoluteError(Metric):
.. testoutput::

2.9375

.. versionchanged:: 0.5.1
``skip_unrolling`` argument is added.
"""

_state_dict_all_req_keys = ("_sum_of_absolute_errors", "_num_examples")
Expand Down
3 changes: 2 additions & 1 deletion ignite/metrics/mean_pairwise_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ def __init__(
eps: float = 1e-6,
output_transform: Callable = lambda x: x,
device: Union[str, torch.device] = torch.device("cpu"),
skip_unrolling=False,
simeetnayan81 marked this conversation as resolved.
Show resolved Hide resolved
) -> None:
super(MeanPairwiseDistance, self).__init__(output_transform, device=device)
super(MeanPairwiseDistance, self).__init__(output_transform, device=device, skip_unrolling=False)
self._p = p
self._eps = eps

Expand Down
6 changes: 6 additions & 0 deletions ignite/metrics/mean_squared_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class MeanSquaredError(Metric):
device: specifies which device updates are accumulated on. Setting the
metric's device to be the same as your ``update`` arguments ensures the ``update`` method is
non-blocking. By default, CPU.
skip_unrolling: specifies whether output should be unrolled before being fed to update method. Should be
true for multi-output model, for example, if ``y_pred`` contains multi-ouput as ``(y_pred_a, y_pred_b)``
Alternatively, ``output_transform`` can be used to handle this.

Examples:
To use with ``Engine`` and ``process_function``, simply attach the metric instance to the engine.
Expand Down Expand Up @@ -57,6 +60,9 @@ class MeanSquaredError(Metric):
.. testoutput::

3.828125

.. versionchanged:: 0.5.1
``skip_unrolling`` argument is added.
"""

_state_dict_all_req_keys = ("_sum_of_squared_errors", "_num_examples")
Expand Down
10 changes: 9 additions & 1 deletion ignite/metrics/multilabel_confusion_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class MultiLabelConfusionMatrix(Metric):
device to be the same as your ``update`` arguments ensures the ``update`` method is non-blocking. By
default, CPU.
normalized: whether to normalize confusion matrix by its sum or not.
skip_unrolling: specifies whether output should be unrolled before being fed to update method. Should be
true for multi-output model, for example, if ``y_pred`` contains multi-ouput as ``(y_pred_a, y_pred_b)``
Alternatively, ``output_transform`` can be used to handle this.

Example:

Expand Down Expand Up @@ -79,6 +82,8 @@ class MultiLabelConfusionMatrix(Metric):

.. versionadded:: 0.4.5

.. versionchanged:: 0.5.1
``skip_unrolling`` argument is added.
"""

_state_dict_all_req_keys = ("confusion_matrix", "_num_examples")
Expand All @@ -89,14 +94,17 @@ def __init__(
output_transform: Callable = lambda x: x,
device: Union[str, torch.device] = torch.device("cpu"),
normalized: bool = False,
skip_unrolling: bool = False,
):
if num_classes <= 1:
raise ValueError("Argument num_classes needs to be > 1")

self.num_classes = num_classes
self._num_examples = 0
self.normalized = normalized
super(MultiLabelConfusionMatrix, self).__init__(output_transform=output_transform, device=device)
super(MultiLabelConfusionMatrix, self).__init__(
output_transform=output_transform, device=device, skip_unrolling=skip_unrolling
)

@reinit__is_reduced
def reset(self) -> None:
Expand Down
Loading