class_docstr: Calculate Kernel Inception Distance (KID) which is used to access the quality of generated images.
  
  .. math::
      KID = MMD(f_{real}, f_{fake})^2
  
  where :math:`MMD` is the maximum mean discrepancy and :math:`I_{real}, I_{fake}` are extracted features
  from real and fake images, see `kid ref1`_ for more details. In particular, calculating the MMD requires the
  evaluation of a polynomial kernel function :math:`k`
  
  .. math::
      k(x,y) = (\gamma * x^T y + coef)^{degree}
  
  which controls the distance between two features. In practise the MMD is calculated over a number of
  subsets to be able to both get the mean and standard deviation of KID.
  
  Using the default feature extraction (Inception v3 using the original weights from `kid ref2`_), the input is
  expected to be mini-batches of 3-channel RGB images of shape ``(3xHxW)``. 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 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 of shape ``(N,C,H,W)``
  - ``real`` (`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
  
  - ``kid_mean`` (:class:`~torch.Tensor`): float scalar tensor with mean value over subsets
  - ``kid_std`` (:class:`~torch.Tensor`): float scalar tensor with mean value over subsets
  
  Args:
      feature: Either an str, integer or ``nn.Module``:
  
          - an str or integer will indicate the inceptionv3 feature layer to choose. Can be one of the following:
            'logits_unbiased', 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.
  
      subsets: Number of subsets to calculate the mean and standard deviation scores over
      subset_size: Number of randomly picked samples in each subset
      degree: Degree of the polynomial kernel function
      gamma: Scale-length of polynomial kernel. If set to ``None`` will be automatically set to the feature size
      coef: Bias term in the polynomial kernel.
      reset_real_features: 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 to ``False`` if
          your dataset does not change.
      kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.
  
  Raises:
      ValueError:
          If ``feature`` is set to an ``int`` (default settings) and ``torch-fidelity`` is not installed
      ValueError:
          If ``feature`` is set to an ``int`` not in ``(64, 192, 768, 2048)``
      ValueError:
          If ``subsets`` is not an integer larger than 0
      ValueError:
          If ``subset_size`` is not an integer larger than 0
      ValueError:
          If ``degree`` is not an integer larger than 0
      ValueError:
          If ``gamma`` is neither ``None`` or a float larger than 0
      ValueError:
          If ``coef`` is not an float larger than 0
      ValueError:
          If ``reset_real_features`` is not an ``bool``
  
  Example:
      >>> import torch
      >>> _ = torch.manual_seed(123)
      >>> from torchmetrics.image.kid import KernelInceptionDistance
      >>> kid = KernelInceptionDistance(subset_size=50)
      >>> # 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)
      >>> kid.update(imgs_dist1, real=True)
      >>> kid.update(imgs_dist2, real=False)
      >>> kid.compute()
      (tensor(0.0337), tensor(0.0023))
class_docstr_tok: ["Calculate","Kernel","Inception","Distance","(","KID",")","which","is","used","to","access","the","quality","of","generated","images",".","..","math",":",":","KID","=","MMD","(","f_","{","real","}",",","f_","{","fake","}",")","^2","where",":","math",":","`","MMD","`","is","the","maximum","mean","discrepancy","and",":","math",":","`","I_","{","real","}",",","I_","{","fake","}","`","are","extracted","features","from","real","and","fake","images",",","see","`","kid","ref1","`","_","for","more","details",".","In","particular",",","calculating","the","MMD","requires","the","evaluation","of","a","polynomial","kernel","function",":","math",":","`","k","`","..","math",":",":","k","(","x",",","y",")","=","(","\\gamma","*","x^T","y","+","coef",")","^","{","degree","}","which","controls","the","distance","between","two","features",".","In","practise","the","MMD","is","calculated","over","a","number","of","subsets","to","be","able","to","both","get","the","mean","and","standard","deviation","of","KID",".","Using","the","default","feature","extraction","(","Inception","v3","using","the","original","weights","from","`","kid","ref2","`","_",")",",","the","input","is","expected","to","be","mini-batches","of","3-channel","RGB","images","of","shape","``","(","3xHxW",")","``",".","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","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","of","shape","``","(","N",",","C",",","H",",","W",")","``","-","``","real","``","(","`","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","-","``","kid_mean","``","(",":","class",":","`","~torch.Tensor","`",")",":","float","scalar","tensor","with","mean","value","over","subsets","-","``","kid_std","``","(",":","class",":","`","~torch.Tensor","`",")",":","float","scalar","tensor","with","mean","value","over","subsets","Args",":","feature",":","Either","an","str",",","integer","or","``","nn.Module","``",":","-","an","str","or","integer","will","indicate","the","inceptionv3","feature","layer","to","choose",".","Can","be","one","of","the","following",":","'logits_unbiased","'",",","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",".","subsets",":","Number","of","subsets","to","calculate","the","mean","and","standard","deviation","scores","over","subset_size",":","Number","of","randomly","picked","samples","in","each","subset","degree",":","Degree","of","the","polynomial","kernel","function","gamma",":","Scale-length","of","polynomial","kernel",".","If","set","to","``","None","``","will","be","automatically","set","to","the","feature","size","coef",":","Bias","term","in","the","polynomial","kernel",".","reset_real_features",":","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","to","``","False","``","if","your","dataset","does","not","change",".","kwargs",":","Additional","keyword","arguments",",","see",":","ref",":","`","Metric","kwargs","`","for","more","info",".","Raises",":","ValueError",":","If","``","feature","``","is","set","to","an","``","int","``","(","default","settings",")","and","``","torch-fidelity","``","is","not","installed","ValueError",":","If","``","feature","``","is","set","to","an","``","int","``","not","in","``","(","64",",","192",",","768",",","2048",")","``","ValueError",":","If","``","subsets","``","is","not","an","integer","larger","than","0","ValueError",":","If","``","subset_size","``","is","not","an","integer","larger","than","0","ValueError",":","If","``","degree","``","is","not","an","integer","larger","than","0","ValueError",":","If","``","gamma","``","is","neither","``","None","``","or","a","float","larger","than","0","ValueError",":","If","``","coef","``","is","not","an","float","larger","than","0","ValueError",":","If","``","reset_real_features","``","is","not","an","``","bool","``","Example",":",">",">",">","import","torch",">",">",">","_","=","torch.manual_seed","(","123",")",">",">",">","from","torchmetrics.image.kid","import","KernelInceptionDistance",">",">",">","kid","=","KernelInceptionDistance","(","subset_size=50",")",">",">",">","#","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",")",">",">",">","kid.update","(","imgs_dist1",",","real=True",")",">",">",">","kid.update","(","imgs_dist2",",","real=False",")",">",">",">","kid.compute","(",")","(","tensor","(","0.0337",")",",","tensor","(","0.0023",")",")"]
docstr: Plot a single or multiple values from the metric.
  
  Args:
      val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
          If no value is provided, will automatically call `metric.compute` and plot that result.
      ax: An matplotlib axis object. If provided will add plot to that axis
  
  Returns:
      Figure and Axes object
  
  Raises:
      ModuleNotFoundError:
          If `matplotlib` is not installed
  
  .. plot::
      :scale: 75
  
      >>> # Example plotting a single value
      >>> import torch
      >>> from torchmetrics.image.kid import KernelInceptionDistance
      >>> imgs_dist1 = torch.randint(0, 200, (30, 3, 299, 299), dtype=torch.uint8)
      >>> imgs_dist2 = torch.randint(100, 255, (30, 3, 299, 299), dtype=torch.uint8)
      >>> metric = KernelInceptionDistance(subsets=3, subset_size=20)
      >>> metric.update(imgs_dist1, real=True)
      >>> metric.update(imgs_dist2, real=False)
      >>> fig_, ax_ = metric.plot()
  
  .. plot::
      :scale: 75
  
      >>> # Example plotting multiple values
      >>> import torch
      >>> from torchmetrics.image.kid import KernelInceptionDistance
      >>> imgs_dist1 = lambda: torch.randint(0, 200, (30, 3, 299, 299), dtype=torch.uint8)
      >>> imgs_dist2 = lambda: torch.randint(100, 255, (30, 3, 299, 299), dtype=torch.uint8)
      >>> metric = KernelInceptionDistance(subsets=3, subset_size=20)
      >>> values = [ ]
      >>> for _ in range(3):
      ...     metric.update(imgs_dist1(), real=True)
      ...     metric.update(imgs_dist2(), real=False)
      ...     values.append(metric.compute()[0])
      ...     metric.reset()
      >>> fig_, ax_ = metric.plot(values)
docstr_tok: ["Plot","a","single","or","multiple","values","from","the","metric",".","Args",":","val",":","Either","a","single","result","from","calling","`","metric.forward","`","or","`","metric.compute","`","or","a","list","of","these","results",".","If","no","value","is","provided",",","will","automatically","call","`","metric.compute","`","and","plot","that","result",".","ax",":","An","matplotlib","axis","object",".","If","provided","will","add","plot","to","that","axis","Returns",":","Figure","and","Axes","object","Raises",":","ModuleNotFoundError",":","If","`","matplotlib","`","is","not","installed","..","plot",":",":",":","scale",":","75",">",">",">","#","Example","plotting","a","single","value",">",">",">","import","torch",">",">",">","from","torchmetrics.image.kid","import","KernelInceptionDistance",">",">",">","imgs_dist1","=","torch.randint","(","0",",","200",",","(","30",",","3",",","299",",","299",")",",","dtype=torch.uint8",")",">",">",">","imgs_dist2","=","torch.randint","(","100",",","255",",","(","30",",","3",",","299",",","299",")",",","dtype=torch.uint8",")",">",">",">","metric","=","KernelInceptionDistance","(","subsets=3",",","subset_size=20",")",">",">",">","metric.update","(","imgs_dist1",",","real=True",")",">",">",">","metric.update","(","imgs_dist2",",","real=False",")",">",">",">","fig_",",","ax_","=","metric.plot","(",")","..","plot",":",":",":","scale",":","75",">",">",">","#","Example","plotting","multiple","values",">",">",">","import","torch",">",">",">","from","torchmetrics.image.kid","import","KernelInceptionDistance",">",">",">","imgs_dist1","=","lambda",":","torch.randint","(","0",",","200",",","(","30",",","3",",","299",",","299",")",",","dtype=torch.uint8",")",">",">",">","imgs_dist2","=","lambda",":","torch.randint","(","100",",","255",",","(","30",",","3",",","299",",","299",")",",","dtype=torch.uint8",")",">",">",">","metric","=","KernelInceptionDistance","(","subsets=3",",","subset_size=20",")",">",">",">","values","=","[","]",">",">",">","for","_","in","range","(","3",")",":","...","metric.update","(","imgs_dist1","(",")",",","real=True",")","...","metric.update","(","imgs_dist2","(",")",",","real=False",")","...","values.append","(","metric.compute","(",")","[","0","]",")","...","metric.reset","(",")",">",">",">","fig_",",","ax_","=","metric.plot","(","values",")"]
code: def plot(
      self,
      val: Optional[Union[Tensor, Sequence[Tensor]]] = None,
      ax: Optional[_AX_TYPE] = None,
  ) -> _PLOT_OUT_TYPE:
      """Plot a single or multiple values from the metric.
  
      Args:
          val: Either a single result from calling `metric.forward` or `metric.compute` or a list of these results.
              If no value is provided, will automatically call `metric.compute` and plot that result.
          ax: An matplotlib axis object. If provided will add plot to that axis
  
      Returns:
          Figure and Axes object
  
      Raises:
          ModuleNotFoundError:
              If `matplotlib` is not installed
  
      .. plot::
          :scale: 75
  
          >>> # Example plotting a single value
          >>> import torch
          >>> from torchmetrics.image.kid import KernelInceptionDistance
          >>> imgs_dist1 = torch.randint(0, 200, (30, 3, 299, 299), dtype=torch.uint8)
          >>> imgs_dist2 = torch.randint(100, 255, (30, 3, 299, 299), dtype=torch.uint8)
          >>> metric = KernelInceptionDistance(subsets=3, subset_size=20)
          >>> metric.update(imgs_dist1, real=True)
          >>> metric.update(imgs_dist2, real=False)
          >>> fig_, ax_ = metric.plot()
  
      .. plot::
          :scale: 75
  
          >>> # Example plotting multiple values
          >>> import torch
          >>> from torchmetrics.image.kid import KernelInceptionDistance
          >>> imgs_dist1 = lambda: torch.randint(0, 200, (30, 3, 299, 299), dtype=torch.uint8)
          >>> imgs_dist2 = lambda: torch.randint(100, 255, (30, 3, 299, 299), dtype=torch.uint8)
          >>> metric = KernelInceptionDistance(subsets=3, subset_size=20)
          >>> values = [ ]
          >>> for _ in range(3):
          ...     metric.update(imgs_dist1(), real=True)
          ...     metric.update(imgs_dist2(), real=False)
          ...     values.append(metric.compute()[0])
          ...     metric.reset()
          >>> fig_, ax_ = metric.plot(values)
  
      """
      val = (
          val or self.compute()[0]
      )  # by default we select the mean to plot
      return self._plot(val, ax)
  
file_path: torchmetrics/src/torchmetrics/image/kid.py
imports: from typing import Any, List, Optional, Sequence, Tuple, Union
  import torch
  from torch import Tensor
  from torch.nn import Module
  from torchmetrics.image.fid import NoTrainInceptionV3
  from torchmetrics.metric import Metric
  from torchmetrics.utilities import rank_zero_warn
  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 5 for calling the KernelInceptionDistance obj's underlying member method code with example usage: obj.plot(val, ax) and returns: self