class_docstr: Calculates Frechet Inception Distance.
  
  .. math::
     \text{FID} = |\mu_{1} - \mu_{2}| + \text{Tr}(\sigma_{1} + \sigma_{2} - {2}\sqrt{\sigma_1*\sigma_2})
  
  where :math:`\mu_1` and :math:`\sigma_1` refer to the mean and covariance of the train data and
  :math:`\mu_2` and :math:`\sigma_2` refer to the mean and covariance of the test data.
  
  More details can be found in `Heusel et al. 2017`__
  
  __ https://arxiv.org/pdf/1706.08500.pdf
  
  In addition, a faster and online computation approach can be found in `Mathiasen et al. 2020`__
  
  __ https://arxiv.org/pdf/2009.14075.pdf
  
  Remark:
  
      This implementation is inspired by `pytorch_fid` package which can be found `here`__
  
      __ https://github.com/mseitzer/pytorch-fid
  
  .. note::
      The default Inception model requires the `torchvision` module to be installed.
      FID also requires `scipy` library for matrix square root calculations.
  
  Args:
      num_features: number of features predicted by the model or the reduced feature vector of the image.
          Default value is 2048.
      feature_extractor: 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: a callable that is used to transform the
          :class:`~ignite.engine.engine.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: 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.
  
  Examples:
  
      For more information on how metric works with :class:`~ignite.engine.engine.Engine`, visit :ref:`attach-engine`.
  
      .. include:: defaults.rst
          :start-after: :orphan:
  
      .. testcode::
  
          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"])
  
      .. testoutput::
  
          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:
  
      .. code::
  
          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`.
  
  .. versionadded:: 0.4.6
class_docstr_tok: ["Calculates","Frechet","Inception","Distance",".","..","math",":",":","\\text","{","FID","}","=","|\\mu_","{","1","}","-","\\mu_","{","2","}","|","+","\\text","{","Tr","}","(","\\sigma_","{","1","}","+","\\sigma_","{","2","}","-","{","2","}","\\sqrt","{","\\sigma_1","*","\\sigma_2","}",")","where",":","math",":","`","\\mu_1","`","and",":","math",":","`","\\sigma_1","`","refer","to","the","mean","and","covariance","of","the","train","data","and",":","math",":","`","\\mu_2","`","and",":","math",":","`","\\sigma_2","`","refer","to","the","mean","and","covariance","of","the","test","data",".","More","details","can","be","found","in","`","Heusel","et","al",".","2017","`","__","__","https",":","\/\/arxiv.org\/pdf\/1706.08500.pdf","In","addition",",","a","faster","and","online","computation","approach","can","be","found","in","`","Mathiasen","et","al",".","2020","`","__","__","https",":","\/\/arxiv.org\/pdf\/2009.14075.pdf","Remark",":","This","implementation","is","inspired","by","`","pytorch_fid","`","package","which","can","be","found","`","here","`","__","__","https",":","\/\/github.com\/mseitzer\/pytorch-fid","..","note",":",":","The","default","Inception","model","requires","the","`","torchvision","`","module","to","be","installed",".","FID","also","requires","`","scipy","`","library","for","matrix","square","root","calculations",".","Args",":","num_features",":","number","of","features","predicted","by","the","model","or","the","reduced","feature","vector","of","the","image",".","Default","value","is","2048.","feature_extractor",":","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",":","a","callable","that","is","used","to","transform","the",":","class",":","`","~ignite.engine.engine.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",":","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",".","Examples",":","For","more","information","on","how","metric","works","with",":","class",":","`","~ignite.engine.engine.Engine","`",",","visit",":","ref",":","`","attach-engine","`",".","..","include",":",":","defaults.rst",":","start-after",":",":","orphan",":","..","testcode",":",":","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","''","]",")","..","testoutput",":",":","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",":","..","code",":",":","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","`",".","..","versionadded",":",":","0.4.6"]
code: def reset(self) -> None:
      self._train_sigma = torch.zeros(
          (self._num_features, self._num_features),
          dtype=torch.float64,
          device=self._device,
      )
  
      self._train_total = torch.zeros(
          self._num_features, dtype=torch.float64, device=self._device
      )
  
      self._test_sigma = torch.zeros(
          (self._num_features, self._num_features),
          dtype=torch.float64,
          device=self._device,
      )
  
      self._test_total = torch.zeros(
          self._num_features, dtype=torch.float64, device=self._device
      )
      self._num_examples: int = 0
  
      super(FID, self).reset()
  
file_path: ignite/ignite/metrics/gan/fid.py
imports: import warnings
  from typing import Callable, Optional, Sequence, Union
  import torch
  from packaging.version import Version
  from ignite.metrics.gan.utils import _BaseInceptionMetric, InceptionModel
  from ignite.metrics.metric import reinit__is_reduced, sync_all_reduce
label_desc: Use image node_id 4 for calling the FID obj's underlying member method code with example usage: obj.reset() without return types