Skip to content

Commit

Permalink
Merge branch 'master' into refactor/map
Browse files Browse the repository at this point in the history
  • Loading branch information
Borda authored Jan 14, 2022
2 parents 2b7a2a0 + 1d1b7d5 commit 8818e55
Show file tree
Hide file tree
Showing 12 changed files with 261 additions and 92 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* `torchmetrics.functional.ssim` -> `torchmetrics.functional.scale_invariant_signal_noise_ratio`
* `torchmetrics.SSIM` -> `torchmetrics.StructuralSimilarityIndexMeasure`
- Renamed detection `MAP` to `MeanAveragePrecision` metric ([#754](https://github.com/PyTorchLightning/metrics/pull/754))
- Renamed Fidelity & LPIPS image metric: ([#752](https://github.com/PyTorchLightning/metrics/pull/752))
* `torchmetrics.image.FID` -> `torchmetrics.image.FrechetInceptionDistance`
* `torchmetrics.image.KID` -> `torchmetrics.image.KernelInceptionDistance`
* `torchmetrics.image.LPIPS` -> `torchmetrics.image.LearnedPerceptualImagePatchSimilarity`

### Removed

Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ We currently have implemented metrics within the following domains:
)
- Classification (
[Accuracy](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#accuracy),
[F1](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#f1),
[F1Score](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#f1score),
[AUROC](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#auroc)
and [19 more](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#classification-metrics)
)
Expand All @@ -284,21 +284,21 @@ We currently have implemented metrics within the following domains:
and [few more](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#retrieval)
)
- Image (
[FID](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#fid),
[KID](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#kid),
[SSIM](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#ssim)
[FrechetInceptionDistance](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#FrechetInceptionDistance),
[KernelInceptionDistance](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#KernelInceptionDistance),
[StructuralSimilarityIndexMeasure](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#StructuralSimilarityIndexMeasure)
and [2 more](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#image-metrics)
)
- Regression (
[ExplainedVariance](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#explainedvariance),
[PearsonCorrcoef](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#pearsoncorrcoef),
[PearsonCorrCoef](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#pearsoncorrcoef),
[R2Score](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#r2score)
and [few more](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#regression-metrics)
)
- Text (
[BleuScore](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#bleuscore),
[RougeScore](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#rougescore),
[WER](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#wer)
[WordErrorRate](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#WordErrorRate)
and [few more](https://torchmetrics.readthedocs.io/en/latest/references/modules.html#text)
)

Expand Down
24 changes: 12 additions & 12 deletions docs/source/references/modules.rst
Original file line number Diff line number Diff line change
Expand Up @@ -368,28 +368,28 @@ Image
Image quality metrics can be used to access the quality of synthetic generated images from machine
learning algorithms such as `Generative Adverserial Networks (GANs) <https://en.wikipedia.org/wiki/Generative_adversarial_network>`_.

FID
~~~
FrechetInceptionDistance
~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: torchmetrics.image.fid.FID
.. autoclass:: torchmetrics.image.fid.FrechetInceptionDistance
:noindex:

IS
~~
InceptionScore
~~~~~~~~~~~~~~

.. autoclass:: torchmetrics.image.inception.IS
.. autoclass:: torchmetrics.image.inception.InceptionScore
:noindex:

KID
~~~
KernelInceptionDistance
~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: torchmetrics.image.kid.KID
.. autoclass:: torchmetrics.image.kid.KernelInceptionDistance
:noindex:

LPIPS
~~~~~
LearnedPerceptualImagePatchSimilarity
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: torchmetrics.image.lpip_similarity.LPIPS
.. autoclass:: torchmetrics.image.lpip.LearnedPerceptualImagePatchSimilarity
:noindex:

MultiScaleStructuralSimilarityIndexMeasure
Expand Down
20 changes: 10 additions & 10 deletions tests/image/test_fid.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from scipy.linalg import sqrtm as scipy_sqrtm
from torch.utils.data import Dataset

from torchmetrics.image.fid import FID, sqrtm
from torchmetrics.image.fid import FrechetInceptionDistance, sqrtm
from torchmetrics.utilities.imports import _TORCH_FIDELITY_AVAILABLE

torch.manual_seed(42)
Expand Down Expand Up @@ -47,7 +47,7 @@ def test_no_train():
class MyModel(torch.nn.Module):
def __init__(self):
super().__init__()
self.metric = FID()
self.metric = FrechetInceptionDistance()

def forward(self, x):
return x
Expand All @@ -61,7 +61,7 @@ def forward(self, x):
@pytest.mark.skipif(not _TORCH_FIDELITY_AVAILABLE, reason="test requires torch-fidelity")
def test_fid_pickle():
"""Assert that we can initialize the metric and pickle it."""
metric = FID()
metric = FrechetInceptionDistance()
assert metric

# verify metrics work after being loaded from pickled state
Expand All @@ -73,32 +73,32 @@ def test_fid_raises_errors_and_warnings():
"""Test that expected warnings and errors are raised."""
with pytest.warns(
UserWarning,
match="Metric `FID` will save all extracted features in buffer."
match="Metric `FrechetInceptionDistance` will save all extracted features in buffer."
" For large datasets this may lead to large memory footprint.",
):
_ = FID()
_ = FrechetInceptionDistance()

if _TORCH_FIDELITY_AVAILABLE:
with pytest.raises(ValueError, match="Integer input to argument `feature` must be one of .*"):
_ = FID(feature=2)
_ = FrechetInceptionDistance(feature=2)
else:
with pytest.raises(
ModuleNotFoundError,
match="FID metric requires that `Torch-fidelity` is installed."
" Either install as `pip install torchmetrics[image-quality]` or `pip install torch-fidelity`.",
):
_ = FID()
_ = FrechetInceptionDistance()

with pytest.raises(TypeError, match="Got unknown input to argument `feature`"):
_ = FID(feature=[1, 2])
_ = FrechetInceptionDistance(feature=[1, 2])


@pytest.mark.skipif(not _TORCH_FIDELITY_AVAILABLE, reason="test requires torch-fidelity")
@pytest.mark.parametrize("feature", [64, 192, 768, 2048])
def test_fid_same_input(feature):
"""if real and fake are update on the same data the fid score should be
0."""
metric = FID(feature=feature)
metric = FrechetInceptionDistance(feature=feature)

for _ in range(2):
img = torch.randint(0, 255, (10, 3, 299, 299), dtype=torch.uint8)
Expand Down Expand Up @@ -128,7 +128,7 @@ def test_compare_fid(tmpdir, feature=2048):
"""check that the hole pipeline give the same result as torch-fidelity."""
from torch_fidelity import calculate_metrics

metric = FID(feature=feature).cuda()
metric = FrechetInceptionDistance(feature=feature).cuda()

# Generate some synthetic data
img1 = torch.randint(0, 180, (100, 3, 299, 299), dtype=torch.uint8)
Expand Down
26 changes: 14 additions & 12 deletions tests/image/test_inception.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import torch
from torch.utils.data import Dataset

from torchmetrics.image.inception import IS
from torchmetrics.image.inception import InceptionScore
from torchmetrics.utilities.imports import _TORCH_FIDELITY_AVAILABLE

torch.manual_seed(42)
Expand All @@ -30,21 +30,23 @@ def test_no_train():
class MyModel(torch.nn.Module):
def __init__(self):
super().__init__()
self.metric = IS()
self.metric = InceptionScore()

def forward(self, x):
return x

model = MyModel()
model.train()
assert model.training
assert not model.metric.inception.training, "IS metric was changed to training mode which should not happen"
assert (
not model.metric.inception.training
), "InceptionScore metric was changed to training mode which should not happen"


@pytest.mark.skipif(not _TORCH_FIDELITY_AVAILABLE, reason="test requires torch-fidelity")
def test_is_pickle():
"""Assert that we can initialize the metric and pickle it."""
metric = IS()
metric = InceptionScore()
assert metric

# verify metrics work after being loaded from pickled state
Expand All @@ -56,29 +58,29 @@ def test_is_raises_errors_and_warnings():
"""Test that expected warnings and errors are raised."""
with pytest.warns(
UserWarning,
match="Metric `IS` will save all extracted features in buffer."
match="Metric `InceptionScore` will save all extracted features in buffer."
" For large datasets this may lead to large memory footprint.",
):
IS()
InceptionScore()

if _TORCH_FIDELITY_AVAILABLE:
with pytest.raises(ValueError, match="Integer input to argument `feature` must be one of .*"):
_ = IS(feature=2)
_ = InceptionScore(feature=2)
else:
with pytest.raises(
ModuleNotFoundError,
match="IS metric requires that `Torch-fidelity` is installed."
match="InceptionScore metric requires that `Torch-fidelity` is installed."
" Either install as `pip install torchmetrics[image-quality]` or `pip install torch-fidelity`.",
):
IS()
InceptionScore()

with pytest.raises(TypeError, match="Got unknown input to argument `feature`"):
IS(feature=[1, 2])
InceptionScore(feature=[1, 2])


@pytest.mark.skipif(not _TORCH_FIDELITY_AVAILABLE, reason="test requires torch-fidelity")
def test_is_update_compute():
metric = IS()
metric = InceptionScore()

for _ in range(2):
img = torch.randint(0, 255, (10, 3, 299, 299), dtype=torch.uint8)
Expand Down Expand Up @@ -106,7 +108,7 @@ def test_compare_is(tmpdir):
"""check that the hole pipeline give the same result as torch-fidelity."""
from torch_fidelity import calculate_metrics

metric = IS(splits=1).cuda()
metric = InceptionScore(splits=1).cuda()

# Generate some synthetic data
img1 = torch.randint(0, 255, (100, 3, 299, 299), dtype=torch.uint8)
Expand Down
34 changes: 17 additions & 17 deletions tests/image/test_kid.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import torch
from torch.utils.data import Dataset

from torchmetrics.image.kid import KID
from torchmetrics.image.kid import KernelInceptionDistance
from torchmetrics.utilities.imports import _TORCH_FIDELITY_AVAILABLE

torch.manual_seed(42)
Expand All @@ -30,7 +30,7 @@ def test_no_train():
class MyModel(torch.nn.Module):
def __init__(self):
super().__init__()
self.metric = KID()
self.metric = KernelInceptionDistance()

def forward(self, x):
return x
Expand All @@ -44,7 +44,7 @@ def forward(self, x):
@pytest.mark.skipif(not _TORCH_FIDELITY_AVAILABLE, reason="test requires torch-fidelity")
def test_kid_pickle():
"""Assert that we can initialize the metric and pickle it."""
metric = KID()
metric = KernelInceptionDistance()
assert metric

# verify metrics work after being loaded from pickled state
Expand All @@ -56,27 +56,27 @@ def test_kid_raises_errors_and_warnings():
"""Test that expected warnings and errors are raised."""
with pytest.warns(
UserWarning,
match="Metric `KID` will save all extracted features in buffer."
match="Metric `Kernel Inception Distance` will save all extracted features in buffer."
" For large datasets this may lead to large memory footprint.",
):
KID()
KernelInceptionDistance()

if _TORCH_FIDELITY_AVAILABLE:
with pytest.raises(ValueError, match="Integer input to argument `feature` must be one of .*"):
KID(feature=2)
KernelInceptionDistance(feature=2)
else:
with pytest.raises(
ModuleNotFoundError,
match="KID metric requires that `Torch-fidelity` is installed."
match="Kernel Inception Distance metric requires that `Torch-fidelity` is installed."
" Either install as `pip install torchmetrics[image]` or `pip install torch-fidelity`.",
):
KID()
KernelInceptionDistance()

with pytest.raises(TypeError, match="Got unknown input to argument `feature`"):
KID(feature=[1, 2])
KernelInceptionDistance(feature=[1, 2])

with pytest.raises(ValueError, match="Argument `subset_size` should be smaller than the number of samples"):
m = KID()
m = KernelInceptionDistance()
m.update(torch.randint(0, 255, (5, 3, 299, 299), dtype=torch.uint8), real=True)
m.update(torch.randint(0, 255, (5, 3, 299, 299), dtype=torch.uint8), real=False)
m.compute()
Expand All @@ -85,26 +85,26 @@ def test_kid_raises_errors_and_warnings():
@pytest.mark.skipif(not _TORCH_FIDELITY_AVAILABLE, reason="test requires torch-fidelity")
def test_kid_extra_parameters():
with pytest.raises(ValueError, match="Argument `subsets` expected to be integer larger than 0"):
KID(subsets=-1)
KernelInceptionDistance(subsets=-1)

with pytest.raises(ValueError, match="Argument `subset_size` expected to be integer larger than 0"):
KID(subset_size=-1)
KernelInceptionDistance(subset_size=-1)

with pytest.raises(ValueError, match="Argument `degree` expected to be integer larger than 0"):
KID(degree=-1)
KernelInceptionDistance(degree=-1)

with pytest.raises(ValueError, match="Argument `gamma` expected to be `None` or float larger than 0"):
KID(gamma=-1)
KernelInceptionDistance(gamma=-1)

with pytest.raises(ValueError, match="Argument `coef` expected to be float larger than 0"):
KID(coef=-1)
KernelInceptionDistance(coef=-1)


@pytest.mark.skipif(not _TORCH_FIDELITY_AVAILABLE, reason="test requires torch-fidelity")
@pytest.mark.parametrize("feature", [64, 192, 768, 2048])
def test_kid_same_input(feature):
"""test that the metric works."""
metric = KID(feature=feature, subsets=5, subset_size=2)
metric = KernelInceptionDistance(feature=feature, subsets=5, subset_size=2)

for _ in range(2):
img = torch.randint(0, 255, (10, 3, 299, 299), dtype=torch.uint8)
Expand Down Expand Up @@ -135,7 +135,7 @@ def test_compare_kid(tmpdir, feature=2048):
"""check that the hole pipeline give the same result as torch-fidelity."""
from torch_fidelity import calculate_metrics

metric = KID(feature=feature, subsets=1, subset_size=100).cuda()
metric = KernelInceptionDistance(feature=feature, subsets=1, subset_size=100).cuda()

# Generate some synthetic data
img1 = torch.randint(0, 180, (100, 3, 299, 299), dtype=torch.uint8)
Expand Down
Loading

0 comments on commit 8818e55

Please sign in to comment.