Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions ignite/contrib/metrics/average_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from ignite.metrics import EpochMetric


def average_precision_compute_fn(y_preds: torch.Tensor, y_targets: torch.Tensor):
def average_precision_compute_fn(y_preds: torch.Tensor, y_targets: torch.Tensor) -> float:
try:
from sklearn.metrics import average_precision_score
except ImportError:
Expand Down Expand Up @@ -45,7 +45,7 @@ def activated_output_transform(output):

"""

def __init__(self, output_transform: Callable = lambda x: x, check_compute_fn: bool = False):
def __init__(self, output_transform: Callable = lambda x: x, check_compute_fn: bool = False) -> None:
super(AveragePrecision, self).__init__(
average_precision_compute_fn, output_transform=output_transform, check_compute_fn=check_compute_fn
)
17 changes: 10 additions & 7 deletions ignite/contrib/metrics/gpu_info.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
import warnings
from typing import Tuple, Union
from typing import Any, Dict, List, Tuple, Union

import torch

Expand Down Expand Up @@ -34,7 +34,7 @@ class GpuInfo(Metric):
event_name=Events.ITERATION_COMPLETED)
"""

def __init__(self):
def __init__(self) -> None:
try:
import pynvml
except ImportError:
Expand All @@ -52,20 +52,20 @@ def __init__(self):
self.nvsmi = nvidia_smi.getInstance()
super(GpuInfo, self).__init__()

def reset(self):
def reset(self) -> None:
pass

def update(self, output: Tuple[torch.Tensor, torch.Tensor]):
def update(self, output: Tuple[torch.Tensor, torch.Tensor]) -> None:
pass

def compute(self):
def compute(self) -> List[Dict[str, Any]]:
data = self.nvsmi.DeviceQuery("memory.used, memory.total, utilization.gpu")
if len(data) == 0 or ("gpu" not in data):
warnings.warn("No GPU information available")
return []
return data["gpu"]

def completed(self, engine: Engine, name: str):
def completed(self, engine: Engine, name: str) -> None:
data = self.compute()
if len(data) < 1:
warnings.warn("No GPU information available")
Expand Down Expand Up @@ -104,5 +104,8 @@ def completed(self, engine: Engine, name: str):
# Do not set GPU utilization information
pass

def attach(self, engine: Engine, name: str = "gpu", event_name: Union[str, EventEnum] = Events.ITERATION_COMPLETED):
# TODO: see issue https://github.com/pytorch/ignite/issues/1405
def attach( # type: ignore
self, engine: Engine, name: str = "gpu", event_name: Union[str, EventEnum] = Events.ITERATION_COMPLETED
) -> None:
engine.add_event_handler(event_name, self.completed, name)
6 changes: 3 additions & 3 deletions ignite/contrib/metrics/precision_recall_curve.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from typing import Callable
from typing import Any, Callable, Tuple

import torch

from ignite.metrics import EpochMetric


def precision_recall_curve_compute_fn(y_preds: torch.Tensor, y_targets: torch.Tensor):
def precision_recall_curve_compute_fn(y_preds: torch.Tensor, y_targets: torch.Tensor) -> Tuple[Any, Any, Any]:
try:
from sklearn.metrics import precision_recall_curve
except ImportError:
Expand Down Expand Up @@ -46,7 +46,7 @@ def activated_output_transform(output):

"""

def __init__(self, output_transform: Callable = lambda x: x, check_compute_fn: bool = False):
def __init__(self, output_transform: Callable = lambda x: x, check_compute_fn: bool = False) -> None:
super(PrecisionRecallCurve, self).__init__(
precision_recall_curve_compute_fn, output_transform=output_transform, check_compute_fn=check_compute_fn
)
14 changes: 7 additions & 7 deletions ignite/contrib/metrics/regression/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from ignite.metrics.metric import reinit__is_reduced


def _check_output_shapes(output: Tuple[torch.Tensor, torch.Tensor]):
def _check_output_shapes(output: Tuple[torch.Tensor, torch.Tensor]) -> None:
y_pred, y = output
if y_pred.shape != y.shape:
raise ValueError("Input data shapes should be the same, but given {} and {}".format(y_pred.shape, y.shape))
Expand All @@ -21,7 +21,7 @@ def _check_output_shapes(output: Tuple[torch.Tensor, torch.Tensor]):
raise ValueError("Input y should have shape (N,) or (N, 1), but given {}".format(y.shape))


def _check_output_types(output: Tuple[torch.Tensor, torch.Tensor]):
def _check_output_types(output: Tuple[torch.Tensor, torch.Tensor]) -> None:
y_pred, y = output
if y_pred.dtype not in (torch.float16, torch.float32, torch.float64):
raise TypeError("Input y_pred dtype should be float 16, 32 or 64, but given {}".format(y_pred.dtype))
Expand All @@ -36,7 +36,7 @@ class _BaseRegression(Metric):
# method `_update`.

@reinit__is_reduced
def update(self, output: Tuple[torch.Tensor, torch.Tensor]):
def update(self, output: Tuple[torch.Tensor, torch.Tensor]) -> None:
_check_output_shapes(output)
_check_output_types(output)
y_pred, y = output[0].detach(), output[1].detach()
Expand All @@ -50,7 +50,7 @@ def update(self, output: Tuple[torch.Tensor, torch.Tensor]):
self._update((y_pred, y))

@abstractmethod
def _update(self, output: Tuple[torch.Tensor, torch.Tensor]):
def _update(self, output: Tuple[torch.Tensor, torch.Tensor]) -> None:
pass


Expand All @@ -61,14 +61,14 @@ class _BaseRegressionEpoch(EpochMetric):

def __init__(
self, compute_fn: Callable, output_transform: Callable = lambda x: x, check_compute_fn: bool = True,
):
) -> None:
super(_BaseRegressionEpoch, self).__init__(
compute_fn=compute_fn, output_transform=output_transform, check_compute_fn=check_compute_fn
)

def _check_type(self, output: Tuple[torch.Tensor, torch.Tensor]):
def _check_type(self, output: Tuple[torch.Tensor, torch.Tensor]) -> None:
_check_output_types(output)
super(_BaseRegressionEpoch, self)._check_type(output)

def _check_shape(self, output: Tuple[torch.Tensor, torch.Tensor]):
def _check_shape(self, output: Tuple[torch.Tensor, torch.Tensor]) -> None:
_check_output_shapes(output)
9 changes: 4 additions & 5 deletions ignite/contrib/metrics/regression/canberra_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@ class CanberraMetric(_BaseRegression):

def __init__(
self, output_transform: Callable = lambda x: x, device: Union[str, torch.device] = torch.device("cpu")
):
self._sum_of_errors = None
) -> None:
super(CanberraMetric, self).__init__(output_transform, device)

@reinit__is_reduced
def reset(self):
def reset(self) -> None:
self._sum_of_errors = torch.tensor(0.0, device=self._device)

def _update(self, output: Tuple[torch.Tensor, torch.Tensor]):
def _update(self, output: Tuple[torch.Tensor, torch.Tensor]) -> None:
y_pred, y = output
errors = torch.abs(y - y_pred) / (torch.abs(y_pred) + torch.abs(y))
self._sum_of_errors += torch.sum(errors).to(self._device)

@sync_all_reduce("_sum_of_errors")
def compute(self):
def compute(self) -> float:
return self._sum_of_errors.item()
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,19 @@ class FractionalAbsoluteError(_BaseRegression):
__ https://arxiv.org/abs/1809.03006
"""

def reset(self):
def reset(self) -> None:
self._sum_of_errors = 0.0
self._num_examples = 0

def _update(self, output: Tuple[torch.Tensor, torch.Tensor]):
def _update(self, output: Tuple[torch.Tensor, torch.Tensor]) -> None:
y_pred, y = output
errors = 2 * torch.abs(y.view_as(y_pred) - y_pred) / (torch.abs(y_pred) + torch.abs(y.view_as(y_pred)))
self._sum_of_errors += torch.sum(errors).item()
self._num_examples += y.shape[0]

def compute(self):
def compute(self) -> float:
if self._num_examples == 0:
raise NotComputableError(
"FractionalAbsoluteError must have at least " "one example before it can be computed."
"FractionalAbsoluteError must have at least one example before it can be computed."
)
return self._sum_of_errors / self._num_examples
6 changes: 3 additions & 3 deletions ignite/contrib/metrics/regression/fractional_bias.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ class FractionalBias(_BaseRegression):

"""

def reset(self):
def reset(self) -> None:
self._sum_of_errors = 0.0
self._num_examples = 0

def _update(self, output: Tuple[torch.Tensor, torch.Tensor]):
def _update(self, output: Tuple[torch.Tensor, torch.Tensor]) -> None:
y_pred, y = output
errors = 2 * (y.view_as(y_pred) - y_pred) / (y_pred + y.view_as(y_pred))
self._sum_of_errors += torch.sum(errors).item()
self._num_examples += y.shape[0]

def compute(self):
def compute(self) -> float:
if self._num_examples == 0:
raise NotComputableError("FractionalBias must have at least one example before it can be computed.")
return self._sum_of_errors / self._num_examples
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Tuple
from typing import Tuple, Union, cast

import torch

Expand All @@ -22,19 +22,19 @@ class GeometricMeanAbsoluteError(_BaseRegression):
__ https://arxiv.org/abs/1809.03006
"""

def reset(self):
self._sum_of_errors = 0.0
def reset(self) -> None:
self._sum_of_errors = 0.0 # type: Union[float, torch.Tensor]
self._num_examples = 0

def _update(self, output: Tuple[torch.Tensor, torch.Tensor]):
def _update(self, output: Tuple[torch.Tensor, torch.Tensor]) -> None:
y_pred, y = output
errors = torch.log(torch.abs(y.view_as(y_pred) - y_pred))
self._sum_of_errors += torch.sum(errors)
self._num_examples += y.shape[0]

def compute(self):
def compute(self) -> float:
if self._num_examples == 0:
raise NotComputableError(
"GeometricMeanAbsoluteError must have at " "least one example before it can be computed."
"GeometricMeanAbsoluteError must have at least one example before it can be computed."
)
return torch.exp(self._sum_of_errors / self._num_examples).item()
return torch.exp(cast(torch.Tensor, self._sum_of_errors) / self._num_examples).item()
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Tuple
from typing import Tuple, Union, cast

import torch

Expand All @@ -24,12 +24,12 @@ class GeometricMeanRelativeAbsoluteError(_BaseRegression):

"""

def reset(self):
self._sum_y = 0.0
def reset(self) -> None:
self._sum_y = 0.0 # type: Union[float, torch.Tensor]
self._num_examples = 0
self._sum_of_errors = 0.0
self._sum_of_errors = 0.0 # type: Union[float, torch.Tensor]

def _update(self, output: Tuple[torch.Tensor, torch.Tensor]):
def _update(self, output: Tuple[torch.Tensor, torch.Tensor]) -> None:
y_pred, y = output
self._sum_y += y.sum()
self._num_examples += y.shape[0]
Expand All @@ -38,9 +38,9 @@ def _update(self, output: Tuple[torch.Tensor, torch.Tensor]):
denominator = torch.abs(y.view_as(y_pred) - y_mean)
self._sum_of_errors += torch.log(numerator / denominator).sum()

def compute(self):
def compute(self) -> float:
if self._num_examples == 0:
raise NotComputableError(
"GeometricMeanRelativeAbsoluteError must have at least " "one example before it can be computed."
"GeometricMeanRelativeAbsoluteError must have at least one example before it can be computed."
)
return torch.exp(torch.mean(self._sum_of_errors / self._num_examples)).item()
return torch.exp(torch.mean(cast(torch.Tensor, self._sum_of_errors) / self._num_examples)).item()
7 changes: 3 additions & 4 deletions ignite/contrib/metrics/regression/manhattan_distance.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,17 @@ class ManhattanDistance(_BaseRegression):
def __init__(
self, output_transform: Callable = lambda x: x, device: Union[str, torch.device] = torch.device("cpu")
):
self._sum_of_errors = None
super(ManhattanDistance, self).__init__(output_transform, device)

@reinit__is_reduced
def reset(self):
def reset(self) -> None:
self._sum_of_errors = torch.tensor(0.0, device=self._device)

def _update(self, output: Tuple[torch.Tensor, torch.Tensor]):
def _update(self, output: Tuple[torch.Tensor, torch.Tensor]) -> None:
y_pred, y = output
errors = torch.abs(y - y_pred)
self._sum_of_errors += torch.sum(errors).to(self._device)

@sync_all_reduce("_sum_of_errors")
def compute(self):
def compute(self) -> float:
return self._sum_of_errors.item()
8 changes: 4 additions & 4 deletions ignite/contrib/metrics/regression/maximum_absolute_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ class MaximumAbsoluteError(_BaseRegression):

"""

def reset(self):
self._max_of_absolute_errors = -1
def reset(self) -> None:
self._max_of_absolute_errors = -1 # type: float

def _update(self, output: Tuple[torch.Tensor, torch.Tensor]):
def _update(self, output: Tuple[torch.Tensor, torch.Tensor]) -> None:
y_pred, y = output
mae = torch.abs(y_pred - y.view_as(y_pred)).max().item()
if self._max_of_absolute_errors < mae:
self._max_of_absolute_errors = mae

def compute(self):
def compute(self) -> float:
if self._max_of_absolute_errors < 0:
raise NotComputableError("MaximumAbsoluteError must have at least one example before it can be computed.")
return self._max_of_absolute_errors
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ class MeanAbsoluteRelativeError(_BaseRegression):

"""

def reset(self):
def reset(self) -> None:
self._sum_of_absolute_relative_errors = 0.0
self._num_samples = 0

def _update(self, output: Tuple[torch.Tensor, torch.Tensor]):
def _update(self, output: Tuple[torch.Tensor, torch.Tensor]) -> None:
y_pred, y = output
if (y == 0).any():
raise NotComputableError("The ground truth has 0.")
absolute_error = torch.abs(y_pred - y.view_as(y_pred)) / torch.abs(y.view_as(y_pred))
self._sum_of_absolute_relative_errors += torch.sum(absolute_error).item()
self._num_samples += y.size()[0]

def compute(self):
def compute(self) -> float:
if self._num_samples == 0:
raise NotComputableError(
"MeanAbsoluteRelativeError must have at least" "one sample before it can be computed."
"MeanAbsoluteRelativeError must have at least one sample before it can be computed."
)
return self._sum_of_absolute_relative_errors / self._num_samples
6 changes: 3 additions & 3 deletions ignite/contrib/metrics/regression/mean_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ class MeanError(_BaseRegression):

"""

def reset(self):
def reset(self) -> None:
self._sum_of_errors = 0.0
self._num_examples = 0

def _update(self, output: Tuple[torch.Tensor, torch.Tensor]):
def _update(self, output: Tuple[torch.Tensor, torch.Tensor]) -> None:
y_pred, y = output
errors = y.view_as(y_pred) - y_pred
self._sum_of_errors += torch.sum(errors).item()
self._num_examples += y.shape[0]

def compute(self):
def compute(self) -> float:
if self._num_examples == 0:
raise NotComputableError("MeanError must have at least one example before it can be computed.")
return self._sum_of_errors / self._num_examples
6 changes: 3 additions & 3 deletions ignite/contrib/metrics/regression/mean_normalized_bias.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class MeanNormalizedBias(_BaseRegression):

"""

def reset(self):
def reset(self) -> None:
self._sum_of_errors = 0.0
self._num_examples = 0

def _update(self, output: Tuple[torch.Tensor, torch.Tensor]):
def _update(self, output: Tuple[torch.Tensor, torch.Tensor]) -> None:
y_pred, y = output

if (y == 0).any():
Expand All @@ -37,7 +37,7 @@ def _update(self, output: Tuple[torch.Tensor, torch.Tensor]):
self._sum_of_errors += torch.sum(errors).item()
self._num_examples += y.shape[0]

def compute(self):
def compute(self) -> float:
if self._num_examples == 0:
raise NotComputableError("MeanNormalizedBias must have at least one example before it can be computed.")
return self._sum_of_errors / self._num_examples
Loading