r"""Calculate the Inception Score (IS) which is used to access how realistic generated images are. .. math:: IS = exp(\mathbb{E}_x KL(p(y | x ) || p(y))) where :math:`KL(p(y | x) || p(y))` is the KL divergence between the conditional distribution :math:`p(y|x)` and the margianl
| 32 | |
| 33 | |
| 34 | class InceptionScore(Metric): |
| 35 | r"""Calculate the Inception Score (IS) which is used to access how realistic generated images are. |
| 36 | |
| 37 | .. math:: |
| 38 | IS = exp(\mathbb{E}_x KL(p(y | x ) || p(y))) |
| 39 | |
| 40 | where :math:`KL(p(y | x) || p(y))` is the KL divergence between the conditional distribution :math:`p(y|x)` |
| 41 | and the margianl distribution :math:`p(y)`. Both the conditional and marginal distribution is calculated |
| 42 | from features extracted from the images. The score is calculated on random splits of the images such that |
| 43 | both a mean and standard deviation of the score are returned. The metric was originally proposed in |
| 44 | `inception ref1`_. |
| 45 | |
| 46 | Using the default feature extraction (Inception v3 using the original weights from `inception ref2`_), the input |
| 47 | is expected to be mini-batches of 3-channel RGB images of shape ``(3xHxW)``. If argument ``normalize`` |
| 48 | is ``True`` images are expected to be dtype ``float`` and have values in the ``[0,1]`` range, else if |
| 49 | ``normalize`` is set to ``False`` images are expected to have dtype uint8 and take values in the ``[0, 255]`` |
| 50 | range. All images will be resized to 299 x 299 which is the size of the original training data. |
| 51 | |
| 52 | .. note:: using this metric with the default feature extractor requires that ``torch-fidelity`` |
| 53 | is installed. Either install as ``pip install torchmetrics[image]`` or |
| 54 | ``pip install torch-fidelity`` |
| 55 | |
| 56 | As input to ``forward`` and ``update`` the metric accepts the following input |
| 57 | |
| 58 | - ``imgs`` (:class:`~torch.Tensor`): tensor with images feed to the feature extractor |
| 59 | |
| 60 | As output of `forward` and `compute` the metric returns the following output |
| 61 | |
| 62 | - ``fid`` (:class:`~torch.Tensor`): float scalar tensor with mean FID value over samples |
| 63 | |
| 64 | Args: |
| 65 | feature: |
| 66 | Either an str, integer or ``nn.Module``: |
| 67 | |
| 68 | - an str or integer will indicate the inceptionv3 feature layer to choose. Can be one of the following: |
| 69 | 'logits_unbiased', 64, 192, 768, 2048 |
| 70 | - an ``nn.Module`` for using a custom feature extractor. Expects that its forward method returns |
| 71 | an ``(N,d)`` matrix where ``N`` is the batch size and ``d`` is the feature size. |
| 72 | |
| 73 | splits: integer determining how many splits the inception score calculation should be split among |
| 74 | kwargs: Additional keyword arguments, see :ref:`Metric kwargs` for more info. |
| 75 | |
| 76 | Raises: |
| 77 | ValueError: |
| 78 | If ``feature`` is set to an ``str`` or ``int`` and ``torch-fidelity`` is not installed |
| 79 | ValueError: |
| 80 | If ``feature`` is set to an ``str`` or ``int`` and not one of ``('logits_unbiased', 64, 192, 768, 2048)`` |
| 81 | TypeError: |
| 82 | If ``feature`` is not an ``str``, ``int`` or ``torch.nn.Module`` |
| 83 | |
| 84 | Example: |
| 85 | >>> import torch |
| 86 | >>> _ = torch.manual_seed(123) |
| 87 | >>> from torchmetrics.image.inception import InceptionScore |
| 88 | >>> inception = InceptionScore() |
| 89 | >>> # generate some images |
| 90 | >>> imgs = torch.randint(0, 255, (100, 3, 299, 299), dtype=torch.uint8) |
| 91 | >>> inception.update(imgs) |