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

Add functional regression metrics #2492

Merged
merged 13 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from 12 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

- Added a PSNR metric: peak signal-to-noise ratio ([#2483](https://github.com/PyTorchLightning/pytorch-lightning/pull/2483))

- Added functional regression metrics ([#2492](https://github.com/PyTorchLightning/pytorch-lightning/pull/2492))

### Changed


Expand Down
2 changes: 1 addition & 1 deletion docs/source/introduction_guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ quickly becomes onerous and starts distracting from the core research code.
Goal of this guide
------------------
This guide walks through the major parts of the library to help you understand
what each parts does. But at the end of the day, you write the same PyTorch code... just organize it
what each part does. But at the end of the day, you write the same PyTorch code... just organize it
into the LightningModule template which means you keep ALL the flexibility without having to deal with
any of the boilerplate code

Expand Down
30 changes: 30 additions & 0 deletions docs/source/metrics.rst
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,36 @@ iou (F)
.. autofunction:: pytorch_lightning.metrics.functional.iou
:noindex:

mse (F)
^^^^^^^

.. autofunction:: pytorch_lightning.metrics.functional.mse
:noindex:

rmse (F)
^^^^^^^^

.. autofunction:: pytorch_lightning.metrics.functional.rmse
:noindex:

mae (F)
^^^^^^^

.. autofunction:: pytorch_lightning.metrics.functional.mae
:noindex:

rmsle (F)
^^^^^^^^^

.. autofunction:: pytorch_lightning.metrics.functional.rmsle
:noindex:

psnr (F)
^^^^^^^^

.. autofunction:: pytorch_lightning.metrics.functional.psnr
:noindex:

stat_scores_multiple_classes (F)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
2 changes: 1 addition & 1 deletion pytorch_lightning/core/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def prepare_data(self):
# don't do this
self.something = else

def setup(step):
def setup(stage):
data = Load_data(...)
self.l1 = nn.Linear(28, data.num_classes)

Expand Down
2 changes: 1 addition & 1 deletion pytorch_lightning/core/lightning.py
Original file line number Diff line number Diff line change
Expand Up @@ -1321,7 +1321,7 @@ def prepare_data(self):

model.prepare_data()
if ddp/tpu: init()
model.setup(step)
model.setup(stage)
model.train_dataloader()
model.val_dataloader()
model.test_dataloader()
Expand Down
12 changes: 6 additions & 6 deletions pytorch_lightning/metrics/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from pytorch_lightning.metrics.converters import numpy_metric, tensor_metric
from pytorch_lightning.metrics.metric import Metric, TensorMetric, NumpyMetric
from pytorch_lightning.metrics.regression import (
MAE,
MSE,
PSNR,
RMSE,
MAE,
RMSLE,
PSNR
RMSLE
)
from pytorch_lightning.metrics.classification import (
Accuracy,
Expand Down Expand Up @@ -48,10 +48,10 @@
'IoU',
]
__regression_metrics = [
'MAE',
'MSE',
'PSNR',
'RMSE',
'MAE',
'RMSLE',
'PSNR'
'RMSLE'
]
__all__ = __regression_metrics + __classification_metrics + ['SklearnMetric']
7 changes: 7 additions & 0 deletions pytorch_lightning/metrics/functional/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@
to_onehot,
iou,
)
from pytorch_lightning.metrics.functional.regression import (
mae,
mse,
psnr,
rmse,
rmsle
)
154 changes: 149 additions & 5 deletions pytorch_lightning/metrics/functional/regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,139 @@
from pytorch_lightning.metrics.functional.reduction import reduce


def mse(
pred: torch.Tensor,
target: torch.Tensor,
reduction: str = 'elementwise_mean'
) -> torch.Tensor:
"""
Computes mean squared error

Args:
pred: estimated labels
target: ground truth labels
reduction: method for reducing mse (default: takes the mean)
Available reduction methods:

- elementwise_mean: takes the mean
- none: pass array
- sum: add elements

Return:
Tensor with MSE

Example:

>>> x = torch.tensor([0., 1, 2, 3])
>>> y = torch.tensor([0., 1, 2, 2])
>>> mse(x, y)
tensor(0.2500)

"""
mse = F.mse_loss(pred, target, reduction='none')
mse = reduce(mse, reduction=reduction)
return mse


def rmse(
pred: torch.Tensor,
target: torch.Tensor,
reduction: str = 'elementwise_mean'
) -> torch.Tensor:
"""
Computes root mean squared error

Args:
pred: estimated labels
target: ground truth labels
reduction: method for reducing rmse (default: takes the mean)
Available reduction methods:

- elementwise_mean: takes the mean
- none: pass array
- sum: add elements

Return:
Tensor with RMSE


>>> x = torch.tensor([0., 1, 2, 3])
>>> y = torch.tensor([0., 1, 2, 2])
>>> rmse(x, y)
tensor(0.5000)

"""
rmse = torch.sqrt(mse(pred, target, reduction=reduction))
return rmse


def mae(
pred: torch.Tensor,
target: torch.Tensor,
reduction: str = 'elementwise_mean'
) -> torch.Tensor:
"""
Computes mean absolute error

Args:
pred: estimated labels
target: ground truth labels
reduction: method for reducing mae (default: takes the mean)
Available reduction methods:

- elementwise_mean: takes the mean
- none: pass array
- sum: add elements

Return:
Tensor with MAE

Example:

>>> x = torch.tensor([0., 1, 2, 3])
>>> y = torch.tensor([0., 1, 2, 2])
>>> mae(x, y)
tensor(0.2500)

"""
mae = F.l1_loss(pred, target, reduction='none')
mae = reduce(mae, reduction=reduction)
return mae


def rmsle(
pred: torch.Tensor,
target: torch.Tensor,
reduction: str = 'elementwise_mean'
) -> torch.Tensor:
"""
Computes root mean squared log error

Args:
pred: estimated labels
target: ground truth labels
reduction: method for reducing rmsle (default: takes the mean)
Available reduction methods:

- elementwise_mean: takes the mean
- none: pass array
- sum: add elements

Return:
Tensor with RMSLE

Example:

>>> x = torch.tensor([0., 1, 2, 3])
>>> y = torch.tensor([0., 1, 2, 2])
>>> rmsle(x, y)
tensor(0.0207)

"""
rmsle = mse(torch.log(pred + 1), torch.log(target + 1), reduction=reduction)
return rmsle


def psnr(
pred: torch.Tensor,
target: torch.Tensor,
Expand All @@ -12,14 +145,22 @@ def psnr(
reduction: str = 'elementwise_mean'
) -> torch.Tensor:
"""
Computes the peak signal-to-noise ratio metric
Computes the peak signal-to-noise ratio

Args:
pred: estimated signal
target: groun truth signal
data_range: the range of the data. If None, it is determined from the data (max - min).
data_range: the range of the data. If None, it is determined from the data (max - min)
base: a base of a logarithm to use (default: 10)
reduction: method for reducing psnr (default: takes the mean)
Available reduction methods:

- elementwise_mean: takes the mean
- none: pass array
- sum add elements

Return:
Tensor with PSNR score

Example:

Expand All @@ -29,12 +170,15 @@ def psnr(
>>> metric = PSNR()
>>> metric(pred, target)
tensor(2.5527)

"""

if data_range is None:
data_range = max(target.max() - target.min(), pred.max() - pred.min())
else:
data_range = torch.tensor(float(data_range))
mse = F.mse_loss(pred.view(-1), target.view(-1), reduction=reduction)
psnr_base_e = 2 * torch.log(data_range) - torch.log(mse)
return psnr_base_e * (10 / torch.log(torch.tensor(base)))

mse_score = mse(pred.view(-1), target.view(-1), reduction=reduction)
psnr_base_e = 2 * torch.log(data_range) - torch.log(mse_score)
psnr = psnr_base_e * (10 / torch.log(torch.tensor(base)))
return psnr
Loading