MCPcopy Create free account
hub / github.com/CompVis/zigma / KernelInceptionDistance

Class KernelInceptionDistance

utils/torchmetric_kid.py:67–282  ·  view source on GitHub ↗

r""" Calculates Kernel Inception Distance (KID) which is used to access the quality of generated images. Given by .. 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

Source from the content-addressed store, hash-verified

65
66
67class KernelInceptionDistance(Metric):
68 r"""
69 Calculates Kernel Inception Distance (KID) which is used to access the quality of generated images. Given by
70
71 .. math::
72 KID = MMD(f_{real}, f_{fake})^2
73
74 where :math:`MMD` is the maximum mean discrepancy and :math:`I_{real}, I_{fake}` are extracted features
75 from real and fake images, see [1] for more details. In particular, calculating the MMD requires the
76 evaluation of a polynomial kernel function :math:`k`
77
78 .. math::
79 k(x,y) = (\gamma * x^T y + coef)^{degree}
80
81 which controls the distance between two features. In practise the MMD is calculated over a number of
82 subsets to be able to both get the mean and standard deviation of KID.
83
84 Using the default feature extraction (Inception v3 using the original weights from [2]), the input is
85 expected to be mini-batches of 3-channel RGB images of shape (3 x H x W) with dtype uint8. All images
86 will be resized to 299 x 299 which is the size of the original training data.
87
88 .. note:: using this metric with the default feature extractor requires that ``torch-fidelity``
89 is installed. Either install as ``pip install torchmetrics[image]`` or
90 ``pip install torch-fidelity``
91
92 .. note:: the ``forward`` method can be used but ``compute_on_step`` is disabled by default (oppesit of
93 all other metrics) as this metric does not really make sense to calculate on a single batch. This
94 means that by default ``forward`` will just call ``update`` underneat.
95
96 Args:
97 feature: Either an str, integer or ``nn.Module``:
98
99 - an str or integer will indicate the inceptionv3 feature layer to choose. Can be one of the following:
100 'logits_unbiased', 64, 192, 768, 2048
101 - an ``nn.Module`` for using a custom feature extractor. Expects that its forward method returns
102 an ``[N,d]`` matrix where ``N`` is the batch size and ``d`` is the feature size.
103
104 subsets: Number of subsets to calculate the mean and standard deviation scores over
105 subset_size: Number of randomly picked samples in each subset
106 degree: Degree of the polynomial kernel function
107 gamma: Scale-length of polynomial kernel. If set to ``None`` will be automatically set to the feature size
108 coef: Bias term in the polynomial kernel.
109 reset_real_features: Whether to also reset the real features. Since in many cases the real dataset does not
110 change, the features can cached them to avoid recomputing them which is costly. Set this to ``False`` if
111 your dataset does not change.
112 compute_on_step:
113 Forward only calls ``update()`` and returns None if this is set to False.
114
115 .. deprecated:: v0.8
116 Argument has no use anymore and will be removed v0.9.
117
118 kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info.
119
120 References:
121 [1] Demystifying MMD GANs
122 Mikołaj Bińkowski, Danica J. Sutherland, Michael Arbel, Arthur Gretton
123 https://arxiv.org/abs/1801.01401
124

Callers 1

__init__Method · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected