FID#
- class ignite.metrics.FID(num_features=None, feature_extractor=None, output_transform=<function FID.<lambda>>, device=device(type='cpu'))[source]#
Calculates Frechet Inception Distance.
$\text{FID} = |\mu_{1} - \mu_{2}| + \text{Tr}(\sigma_{1} + \sigma_{2} - {2}\sqrt{\sigma_1*\sigma_2})$where $\mu_1$ and $\sigma_1$ refer to the mean and covariance of the train data and $\mu_2$ and $\sigma_2$ refer to the mean and covariance of the test data.
More details can be found in Heusel et al. 2002
In addition, a faster and online computation approach can be found in Chen et al. 2014
Remark:
This implementation is inspired by pytorch_fid package which can be found here
Note
The default Inception model requires the torchvision module to be installed. FID also requires scipy library for matrix square root calculations.
- Parameters
num_features (Optional[int]) – number of features predicted by the model or the reduced feature vector of the image. Default value is 2048.
feature_extractor (Optional[torch.nn.modules.module.Module]) – a torch Module for extracting the features from the input data. It returns a tensor of shape (batch_size, num_features). If neither
num_features
norfeature_extractor
are defined, by default we use an ImageNet pretrained Inception Model. If onlynum_features
is defined butfeature_extractor
is not defined,feature_extractor
is assigned Identity Function. Please note that the model will be implicitly converted to device mentioned in thedevice
argument.output_transform (Callable) – a callable that is used to transform the
Engine
’sprocess_function
’s output into the form expected by the metric. This can be useful if, for example, you have a multi-output model and you want to compute the metric with respect to one of the outputs. By default, metrics require the output as(y_pred, y)
or{'y_pred': y_pred, 'y': y}
.device (Union[str, torch.device]) – specifies which device updates are accumulated on. Setting the metric’s device to be the same as your
update
arguments ensures theupdate
method is non-blocking. By default, CPU.
- Return type
Examples
For more information on how metric works with
Engine
, visit Attach Engine API.from collections import OrderedDict import torch from torch import nn, optim from ignite.engine import * from ignite.handlers import * from ignite.metrics import * from ignite.utils import * from ignite.contrib.metrics.regression import * from ignite.contrib.metrics import * # create default evaluator for doctests def eval_step(engine, batch): return batch default_evaluator = Engine(eval_step) # create default optimizer for doctests param_tensor = torch.zeros([1], requires_grad=True) default_optimizer = torch.optim.SGD([param_tensor], lr=0.1) # create default trainer for doctests # as handlers could be attached to the trainer, # each test must define his own trainer using `.. testsetup:` def get_default_trainer(): def train_step(engine, batch): return batch return Engine(train_step) # create default model for doctests default_model = nn.Sequential(OrderedDict([ ('base', nn.Linear(4, 2)), ('fc', nn.Linear(2, 1)) ])) manual_seed(666)
metric = FID(num_features=1, feature_extractor=default_model) metric.attach(default_evaluator, "fid") y_true = torch.ones(10, 4) y_pred = torch.ones(10, 4) state = default_evaluator.run([[y_pred, y_true]]) print(state.metrics["fid"])
0.0
Note
The default torchvision model used is InceptionV3 pretrained on ImageNet. This can lead to differences in results with pytorch_fid. To find comparable results, the following model wrapper should be used:
import torch.nn as nn # wrapper class as feature_extractor class WrapperInceptionV3(nn.Module): def __init__(self, fid_incv3): super().__init__() self.fid_incv3 = fid_incv3 @torch.no_grad() def forward(self, x): y = self.fid_incv3(x) y = y[0] y = y[:, :, 0, 0] return y # use cpu rather than cuda to get comparable results device = "cpu" # pytorch_fid model dims = 2048 block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] model = InceptionV3([block_idx]).to(device) # wrapper model to pytorch_fid model wrapper_model = WrapperInceptionV3(model) wrapper_model.eval(); # comparable metric pytorch_fid_metric = FID(num_features=dims, feature_extractor=wrapper_model)
Important, pytorch_fid results depend on the batch size if the device is cuda.
New in version 0.4.6.
Methods
Computes the metric based on it's accumulated state.
Resets the metric to it's initial state.
Updates the metric's state using the passed batch output.
- compute()[source]#
Computes the metric based on it’s accumulated state.
By default, this is called at the end of each epoch.
- Returns
- the actual quantity of interest. However, if a
Mapping
is returned, it will be (shallow) flattened into engine.state.metrics whencompleted()
is called. - Return type
Any
- Raises
NotComputableError – raised when the metric cannot be computed.
- reset()[source]#
Resets the metric to it’s initial state.
By default, this is called at the start of each epoch.
- Return type
- update(output)[source]#
Updates the metric’s state using the passed batch output.
By default, this is called once for each batch.
- Parameters
output (Sequence[torch.Tensor]) – the is the output from the engine’s process function.
- Return type