Shortcuts

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.

FID=μ1μ2+Tr(σ1+σ22σ1σ2)\text{FID} = |\mu_{1} - \mu_{2}| + \text{Tr}(\sigma_{1} + \sigma_{2} - {2}\sqrt{\sigma_1*\sigma_2})

where μ1\mu_1 and σ1\sigma_1 refer to the mean and covariance of the train data and μ2\mu_2 and σ2\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 nor feature_extractor are defined, by default we use an ImageNet pretrained Inception Model. If only num_features is defined but feature_extractor is not defined, feature_extractor is assigned Identity Function. Please note that the model will be implicitly converted to device mentioned in the device argument.

  • output_transform (Callable) – a callable that is used to transform the Engine’s process_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 the update method is non-blocking. By default, CPU.

Return type

None

Example

import torch
from ignite.metric.gan import FID

y_pred, y = torch.rand(10, 3, 299, 299), torch.rand(10, 3, 299, 299)
m = FID()
m.update((y_pred, y))
print(m.compute())

New in version 0.5.0.

Methods

attach

Attaches current metric to provided engine.

completed

Helper method to compute metric’s value and put into the engine.

compute

Computes the metric based on it’s accumulated state.

detach

Detaches current metric from the engine and no metric’s computation is done during the run.

is_attached

Checks if current metric is attached to provided engine.

iteration_completed

Helper method to update metric’s computation.

reset

Resets the metric to it’s initial state.

started

Helper method to start data gathering for metric’s computation.

update

Updates the metric’s state using the passed batch output.

attach(engine, name, usage=<ignite.metrics.metric.EpochWise object>)#

Attaches current metric to provided engine. On the end of engine’s run, engine.state.metrics dictionary will contain computed metric’s value under provided name.

Parameters
Return type

None

Example:

metric = ...
metric.attach(engine, "mymetric")

assert "mymetric" in engine.run(data).metrics

assert metric.is_attached(engine)

Example with usage:

metric = ...
metric.attach(engine, "mymetric", usage=BatchWise.usage_name)

assert "mymetric" in engine.run(data).metrics

assert metric.is_attached(engine, usage=BatchWise.usage_name)
completed(engine, name)#

Helper method to compute metric’s value and put into the engine. It is automatically attached to the engine with attach(). If metrics’ value is torch tensor, it is explicitly sent to CPU device.

Parameters
  • engine (ignite.engine.engine.Engine) – the engine to which the metric must be attached

  • name (str) – the name of the metric used as key in dict engine.state.metrics

Return type

None

Changed in version 0.4.3: Added dict in metrics results.

Changed in version 0.4.5: metric’s value is put on CPU if torch tensor.

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 when completed() is called.

Return type

Any

Raises

NotComputableError – raised when the metric cannot be computed.

detach(engine, usage=<ignite.metrics.metric.EpochWise object>)#

Detaches current metric from the engine and no metric’s computation is done during the run. This method in conjunction with attach() can be useful if several metrics need to be computed with different periods. For example, one metric is computed every training epoch and another metric (e.g. more expensive one) is done every n-th training epoch.

Parameters
Return type

None

Example:

metric = ...
engine = ...
metric.detach(engine)

assert "mymetric" not in engine.run(data).metrics

assert not metric.is_attached(engine)

Example with usage:

metric = ...
engine = ...
metric.detach(engine, usage="batch_wise")

assert "mymetric" not in engine.run(data).metrics

assert not metric.is_attached(engine, usage="batch_wise")
is_attached(engine, usage=<ignite.metrics.metric.EpochWise object>)#

Checks if current metric is attached to provided engine. If attached, metric’s computed value is written to engine.state.metrics dictionary.

Parameters
Return type

bool

iteration_completed(engine)#

Helper method to update metric’s computation. It is automatically attached to the engine with attach().

Note

engine.state.output is used to compute metric values. The majority of implemented metrics accepts the following formats for engine.state.output: (y_pred, y) or {'y_pred': y_pred, 'y': y}. y_pred and y can be torch tensors or list of tensors/numbers if applicable.

Parameters

engine (ignite.engine.engine.Engine) – the engine to which the metric must be attached

Return type

None

Changed in version 0.4.5: y_pred and y can be torch tensors or list of tensors/numbers

reset()[source]#

Resets the metric to it’s initial state.

By default, this is called at the start of each epoch.

Return type

None

started(engine)#

Helper method to start data gathering for metric’s computation. It is automatically attached to the engine with attach().

Parameters

engine (ignite.engine.engine.Engine) – the engine to which the metric must be attached

Return type

None

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

None