Shortcuts

Frechet Inception Distance (FID)

Module Interface

class torchmetrics.image.fid.FrechetInceptionDistance(feature=2048, reset_real_features=True, compute_on_step=None, **kwargs)[source]

Calculates Fréchet inception distance (FID) which is used to access the quality of generated images. Given by

FID = |\mu - \mu_w| + tr(\Sigma + \Sigma_w - 2(\Sigma \Sigma_w)^{\frac{1}{2}})

where \mathcal{N}(\mu, \Sigma) is the multivariate normal distribution estimated from Inception v3 [1] features calculated on real life images and \mathcal{N}(\mu_w, \Sigma_w) is the multivariate normal distribution estimated from Inception v3 features calculated on generated (fake) images. The metric was originally proposed in [1].

Using the default feature extraction (Inception v3 using the original weights from [2]), the input is expected to be mini-batches of 3-channel RGB images of shape (3 x H x W) with dtype uint8. All images will be resized to 299 x 299 which is the size of the original training data. The boolian flag real determines if the images should update the statistics of the real distribution or the fake distribution.

Note

using this metrics requires you to have scipy install. Either install as pip install torchmetrics[image] or pip install scipy

Note

using this metric with the default feature extractor requires that torch-fidelity is installed. Either install as pip install torchmetrics[image] or pip install torch-fidelity

Note

the forward method can be used but compute_on_step is disabled by default (oppesit of all other metrics) as this metric does not really make sense to calculate on a single batch. This means that by default forward will just call update underneat.

Parameters
  • feature (Union[int, Module]) –

    Either an integer or nn.Module:

    • an integer will indicate the inceptionv3 feature layer to choose. Can be one of the following: 64, 192, 768, 2048

    • an nn.Module for using a custom feature extractor. Expects that its forward method returns an [N,d] matrix where N is the batch size and d is the feature size.

  • reset_real_features (bool) – Whether to also reset the real features. Since in many cases the real dataset does not change, the features can cached them to avoid recomputing them which is costly. Set this to False if your dataset does not change.

  • compute_on_step (Optional[bool]) –

    Forward only calls update() and returns None if this is set to False.

    Deprecated since version v0.8: Argument has no use anymore and will be removed v0.9.

  • kwargs (Dict[str, Any]) – Additional keyword arguments, see Advanced metric settings for more info.

References

[1] Rethinking the Inception Architecture for Computer Vision Christian Szegedy, Vincent Vanhoucke, Sergey Ioffe, Jonathon Shlens, Zbigniew Wojna https://arxiv.org/abs/1512.00567

[2] GANs Trained by a Two Time-Scale Update Rule Converge to a Local Nash Equilibrium, Martin Heusel, Hubert Ramsauer, Thomas Unterthiner, Bernhard Nessler, Sepp Hochreiter https://arxiv.org/abs/1706.08500

Raises
  • ValueError – If feature is set to an int (default settings) and torch-fidelity is not installed

  • ValueError – If feature is set to an int not in [64, 192, 768, 2048]

  • TypeError – If feature is not an str, int or torch.nn.Module

  • ValueError – If reset_real_features is not an bool

Example

>>> import torch
>>> _ = torch.manual_seed(123)
>>> from torchmetrics.image.fid import FrechetInceptionDistance
>>> fid = FrechetInceptionDistance(feature=64)
>>> # generate two slightly overlapping image intensity distributions
>>> imgs_dist1 = torch.randint(0, 200, (100, 3, 299, 299), dtype=torch.uint8)
>>> imgs_dist2 = torch.randint(100, 255, (100, 3, 299, 299), dtype=torch.uint8)
>>> fid.update(imgs_dist1, real=True)
>>> fid.update(imgs_dist2, real=False)
>>> fid.compute()
tensor(12.7202)

Initializes internal Module state, shared by both nn.Module and ScriptModule.

compute()[source]

Calculate FID score based on accumulated extracted features from the two distributions.

Return type

Tensor

reset()[source]

This method automatically resets the metric state variables to their default value.

Return type

None

update(imgs, real)[source]

Update the state with extracted features.

Parameters
  • imgs (Tensor) – tensor with images feed to the feature extractor

  • real (bool) – bool indicating if imgs belong to the real or the fake distribution

Return type

None

Read the Docs v: v0.8.1
Versions
latest
stable
v0.8.1
v0.8.0
v0.7.3
v0.7.2
v0.7.1
v0.7.0
v0.6.2
v0.6.1
v0.6.0
v0.5.1
v0.5.0
v0.4.1
v0.4.0
v0.3.2
v0.3.1
v0.3.0
v0.2.0
v0.1.0
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.