class_docstr: Calculate Memorization-Informed Frechet Inception Distance (MIFID_).
  
  MIFID is a improved variation of the Frechet Inception Distance (FID_) that penalizes memorization of the training
  set by the generator. It is calculated as
  
  .. math::
      MIFID = \frac{FID(F_{real}, F_{fake})}{M(F_{real}, F_{fake})}
  
  where :math:`FID` is the normal FID score and :math:`M` is the memorization penalty. The memorization penalty
  essentially corresponds to the average minimum cosine distance between the features of the real and fake
  distribution.
  
  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 argument ``normalize``
  is ``True`` images are expected to be dtype ``float`` and have values in the ``[0, 1]`` range, else if
  ``normalize`` is set to ``False`` images are expected to have dtype ``uint8`` and 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
  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``
  
  As input to ``forward`` and ``update`` the metric accepts the following input
  
  - ``imgs`` (:class:`~torch.Tensor`): tensor with images feed to the feature extractor with
  - ``real`` (:class:`~bool`): bool indicating if ``imgs`` belong to the real or the fake distribution
  
  As output of `forward` and `compute` the metric returns the following output
  
  - ``mifid`` (:class:`~torch.Tensor`): float scalar tensor with mean MIFID value over samples
  
  Args:
      feature:
          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: Whether to also reset the real features. Since in many cases the real dataset does not
          change, the features can be cached them to avoid recomputing them which is costly. Set this to ``False`` if
          your dataset does not change.
      cosine_distance_eps: Epsilon value for the cosine distance. If the cosine distance is larger than this value
          it is set to 1 and thus ignored in the MIFID calculation.
      kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.
  
  Raises:
      RuntimeError:
          If ``torch`` is version less than 1.10
      ValueError:
          If ``feature`` is set to an ``int`` 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(42)
      >>> from torchmetrics.image.mifid import MemorizationInformedFrechetInceptionDistance
      >>> mifid = MemorizationInformedFrechetInceptionDistance(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)
      >>> mifid.update(imgs_dist1, real=True)
      >>> mifid.update(imgs_dist2, real=False)
      >>> mifid.compute()
      tensor(3003.3691)
class_docstr_tok: ["Calculate","Memorization-Informed","Frechet","Inception","Distance","(","MIFID_",")",".","MIFID","is","a","improved","variation","of","the","Frechet","Inception","Distance","(","FID_",")","that","penalizes","memorization","of","the","training","set","by","the","generator",".","It","is","calculated","as","..","math",":",":","MIFID","=","\\frac","{","FID","(","F_","{","real","}",",","F_","{","fake","}",")","}","{","M","(","F_","{","real","}",",","F_","{","fake","}",")","}","where",":","math",":","`","FID","`","is","the","normal","FID","score","and",":","math",":","`","M","`","is","the","memorization","penalty",".","The","memorization","penalty","essentially","corresponds","to","the","average","minimum","cosine","distance","between","the","features","of","the","real","and","fake","distribution",".","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","argument","``","normalize","``","is","``","True","``","images","are","expected","to","be","dtype","``","float","``","and","have","values","in","the","``","[","0",",","1","]","``","range",",","else","if","``","normalize","``","is","set","to","``","False","``","images","are","expected","to","have","dtype","``","uint8","``","and","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","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","``","As","input","to","``","forward","``","and","``","update","``","the","metric","accepts","the","following","input","-","``","imgs","``","(",":","class",":","`","~torch.Tensor","`",")",":","tensor","with","images","feed","to","the","feature","extractor","with","-","``","real","``","(",":","class",":","`","~bool","`",")",":","bool","indicating","if","``","imgs","``","belong","to","the","real","or","the","fake","distribution","As","output","of","`","forward","`","and","`","compute","`","the","metric","returns","the","following","output","-","``","mifid","``","(",":","class",":","`","~torch.Tensor","`",")",":","float","scalar","tensor","with","mean","MIFID","value","over","samples","Args",":","feature",":","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",":","Whether","to","also","reset","the","real","features",".","Since","in","many","cases","the","real","dataset","does","not","change",",","the","features","can","be","cached","them","to","avoid","recomputing","them","which","is","costly",".","Set","this","to","``","False","``","if","your","dataset","does","not","change",".","cosine_distance_eps",":","Epsilon","value","for","the","cosine","distance",".","If","the","cosine","distance","is","larger","than","this","value","it","is","set","to","1","and","thus","ignored","in","the","MIFID","calculation",".","kwargs",":","Additional","keyword","arguments",",","see",":","ref",":","`","Metric","kwargs","`","for","more","info",".","Raises",":","RuntimeError",":","If","``","torch","``","is","version","less","than","1.10","ValueError",":","If","``","feature","``","is","set","to","an","``","int","``","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","(","42",")",">",">",">","from","torchmetrics.image.mifid","import","MemorizationInformedFrechetInceptionDistance",">",">",">","mifid","=","MemorizationInformedFrechetInceptionDistance","(","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",")",">",">",">","mifid.update","(","imgs_dist1",",","real=True",")",">",">",">","mifid.update","(","imgs_dist2",",","real=False",")",">",">",">","mifid.compute","(",")","tensor","(","3003.3691",")"]
docstr: Calculate FID score based on accumulated extracted features from the two distributions.
docstr_tok: ["Calculate","FID","score","based","on","accumulated","extracted","features","from","the","two","distributions","."]
code: def compute(self) -> Tensor:
      """Calculate FID score based on accumulated extracted features from the two distributions."""
      real_features = dim_zero_cat(self.real_features)
      fake_features = dim_zero_cat(self.fake_features)
  
      mean_real, mean_fake = torch.mean(
          real_features, dim=0
      ), torch.mean(fake_features, dim=0)
      cov_real, cov_fake = torch.cov(real_features.t()), torch.cov(
          fake_features.t()
      )
  
      return _mifid_compute(
          mean_real,
          cov_real,
          real_features,
          mean_fake,
          cov_fake,
          fake_features,
          cosine_distance_eps=self.cosine_distance_eps,
      ).to(self.orig_dtype)
  
file_path: torchmetrics/src/torchmetrics/image/mifid.py
imports: from typing import Any, List, Optional, Sequence, Union
  import torch
  from torch import Tensor
  from torch.nn import Module
  from torchmetrics.image.fid import NoTrainInceptionV3, _compute_fid
  from torchmetrics.metric import Metric
  from torchmetrics.utilities.data import dim_zero_cat
  from torchmetrics.utilities.imports import _MATPLOTLIB_AVAILABLE, _TORCH_FIDELITY_AVAILABLE
  from torchmetrics.utilities.plot import _AX_TYPE, _PLOT_OUT_TYPE
label_desc: Use image node_id 3 for calling the MemorizationInformedFrechetInceptionDistance obj's underlying member method code with example usage: obj.compute() and returns: _mifid_compute