Frechet Inception Distance (FID)¶
Module Interface¶
- class torchmetrics.image.fid.FrechetInceptionDistance(feature=2048, reset_real_features=True, normalize=False, **kwargs)[source]
Calculates Fréchet inception distance (FID) which is used to access the quality of generated images. Given by.

where
is the multivariate normal distribution estimated from Inception v3
(fid ref1) features calculated on real life images and
is the
multivariate normal distribution estimated from Inception v3 features calculated on generated (fake) images.
The metric was originally proposed in fid ref1.Using the default feature extraction (Inception v3 using the original weights from fid ref2), the input is expected to be mini-batches of 3-channel RGB images of shape
(3 x H x W). If argumentnormalizeisTrueimages are expected to be dtypefloatand have values in the[0, 1]range, else ifnormalizeis set toFalseimages are expected to have dtypeuint8and take values in the[0, 255]range. All images will be resized to 299 x 299 which is the size of the original training data. The boolian flagrealdetermines if the images should update the statistics of the real distribution or the fake distribution.Note
using this metrics requires you to have
scipyinstall. Either install aspip install torchmetrics[image]orpip install scipyNote
using this metric with the default feature extractor requires that
torch-fidelityis installed. Either install aspip install torchmetrics[image]orpip install torch-fidelityAs input to
forwardandupdatethe metric accepts the following inputimgs(Tensor): tensor with images feed to the feature extractor withreal(bool): bool indicating ifimgsbelong to the real or the fake distribution
As output of forward and compute the metric returns the following output
fid(Tensor): float scalar tensor with mean FID value over samples
- 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.Modulefor using a custom feature extractor. Expects that its forward method returns an(N,d)matrix whereNis the batch size anddis 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 toFalseif your dataset does not change.kwargs¶ (
Any) – Additional keyword arguments, see Advanced metric settings for more info.
- Raises
ValueError – If
featureis set to anint(default settings) andtorch-fidelityis not installedValueError – If
featureis set to anintnot in [64, 192, 768, 2048]TypeError – If
featureis not anstr,intortorch.nn.ModuleValueError – If
reset_real_featuresis not anbool
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.