-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add stub PSNR metric * Fix linter * Add data range as parameter * Add tests * Add scikit-image * Add PSNR to regression metrics and add functional * Refactor to functional * Fix linter * Fix linter, again * Fix linter, again * Fix typo in test * Fix typo in another test * Add scikit-image to conda * Lift numpy requirement * Add random tests * Update CHANGELOG.md * Apply suggestions from code review Co-authored-by: Jirka Borovec <Borda@users.noreply.github.com>
- Loading branch information
1 parent
899cd74
commit 1dc7242
Showing
9 changed files
with
163 additions
and
5 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ dependencies: | |
- autopep8 | ||
- twine==1.13.0 | ||
- pillow<7.0.0 | ||
- scikit-image | ||
|
||
# Optional | ||
- scipy>=0.13.3 | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import torch | ||
from torch.nn import functional as F | ||
|
||
from pytorch_lightning.metrics.functional.reduction import reduce | ||
|
||
|
||
def psnr( | ||
pred: torch.Tensor, | ||
target: torch.Tensor, | ||
data_range: float = None, | ||
base: float = 10.0, | ||
reduction: str = 'elementwise_mean' | ||
) -> torch.Tensor: | ||
""" | ||
Computes the peak signal-to-noise ratio metric | ||
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). | ||
base: a base of a logarithm to use (default: 10) | ||
reduction: method for reducing psnr (default: takes the mean) | ||
Example: | ||
>>> from pytorch_lightning.metrics.regression import PSNR | ||
>>> pred = torch.tensor([[0.0, 1.0], [2.0, 3.0]]) | ||
>>> target = torch.tensor([[3.0, 2.0], [1.0, 0.0]]) | ||
>>> 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))) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,7 @@ flake8 | |
flake8-black | ||
check-manifest | ||
twine==1.13.0 | ||
|
||
scikit-image | ||
black==19.10b0 | ||
pre-commit>=1.0 | ||
|
||
|
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,37 @@ | ||
import pytest | ||
import torch | ||
|
||
from skimage.metrics import peak_signal_noise_ratio as ski_psnr | ||
from pytorch_lightning.metrics.functional.regression import psnr | ||
|
||
|
||
@pytest.mark.parametrize(['sklearn_metric', 'torch_metric'], [ | ||
pytest.param(ski_psnr, psnr, id='peak_signal_noise_ratio') | ||
]) | ||
def test_psnr_against_sklearn(sklearn_metric, torch_metric): | ||
"""Compare PL metrics to sklearn version.""" | ||
device = 'cuda' if torch.cuda.is_available() else 'cpu' | ||
|
||
pred = torch.randint(10, (500,), device=device, dtype=torch.double) | ||
target = torch.randint(10, (500,), device=device, dtype=torch.double) | ||
assert torch.allclose( | ||
torch.tensor(sklearn_metric(target.cpu().detach().numpy(), | ||
pred.cpu().detach().numpy(), | ||
data_range=10), dtype=torch.double, device=device), | ||
torch_metric(pred, target, data_range=10)) | ||
|
||
pred = torch.randint(5, (500,), device=device, dtype=torch.double) | ||
target = torch.randint(10, (500,), device=device, dtype=torch.double) | ||
assert torch.allclose( | ||
torch.tensor(sklearn_metric(target.cpu().detach().numpy(), | ||
pred.cpu().detach().numpy(), | ||
data_range=10), dtype=torch.double, device=device), | ||
torch_metric(pred, target, data_range=10)) | ||
|
||
pred = torch.randint(10, (500,), device=device, dtype=torch.double) | ||
target = torch.randint(5, (500,), device=device, dtype=torch.double) | ||
assert torch.allclose( | ||
torch.tensor(sklearn_metric(target.cpu().detach().numpy(), | ||
pred.cpu().detach().numpy(), | ||
data_range=5), dtype=torch.double, device=device), | ||
torch_metric(pred, target, data_range=5)) |
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