We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Jaccard Index behaviour when num_classes is absent
The [jaccard index function](https://github.com/PyTorchLightning/metrics/blob/8fade87062a7b87c1e6429bbe1c4e0112b3713a5/torchmetrics/functional/classification/jaccard.py#L69) determines the number of classes automatically.
This can result in strange results if the user forgets to specify `num_classes`. For example, in IoU determination in semantic segmentation, not all images have all classes. So determining them automatically will almost always lead to wrong number.
Additionally, the [warning](https://github.com/PyTorchLightning/metrics/blob/8fade87062a7b87c1e6429bbe1c4e0112b3713a5/torchmetrics/utilities/data.py#L156) that shows up makes it seem like something is wrong, when the user specifies the number of classes explicitly (but this is not the case at all):

```python
/run/determined/pythonuserbase/lib/python3.7/site-packages/torchmetrics/utilities/prints.py:36: RuntimeWarning: You have set 11 number of classes which is different from predicted (1000) and target (10) number of classes
```

Would it be crazy if `num_classes` requirement is default and the automatic determination of classes is a special option that must be enabled?



--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `torchmetrics/functional/classification/jaccard.py`
Content:
```
1 # Copyright The PyTorch Lightning team.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #     http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 from typing import Optional
15 
16 import torch
17 from torch import Tensor
18 
19 from torchmetrics.functional.classification.confusion_matrix import _confusion_matrix_update
20 from torchmetrics.utilities.data import get_num_classes
21 from torchmetrics.utilities.distributed import reduce
22 
23 
24 def _jaccard_from_confmat(
25     confmat: Tensor,
26     num_classes: int,
27     ignore_index: Optional[int] = None,
28     absent_score: float = 0.0,
29     reduction: str = "elementwise_mean",
30 ) -> Tensor:
31     """Computes the intersection over union from confusion matrix.
32 
33     Args:
34         confmat: Confusion matrix without normalization
35         num_classes: Number of classes for a given prediction and target tensor
36         ignore_index: optional int specifying a target class to ignore. If given, this class index does not contribute
37             to the returned score, regardless of reduction method.
38         absent_score: score to use for an individual class, if no instances of the class index were present in `pred`
39             AND no instances of the class index were present in `target`.
40         reduction: a method to reduce metric score over labels.
41 
42             - ``'elementwise_mean'``: takes the mean (default)
43             - ``'sum'``: takes the sum
44             - ``'none'``: no reduction will be applied
45     """
46 
47     # Remove the ignored class index from the scores.
48     if ignore_index is not None and 0 <= ignore_index < num_classes:
49         confmat[ignore_index] = 0.0
50 
51     intersection = torch.diag(confmat)
52     union = confmat.sum(0) + confmat.sum(1) - intersection
53 
54     # If this class is absent in both target AND pred (union == 0), then use the absent_score for this class.
55     scores = intersection.float() / union.float()
56     scores[union == 0] = absent_score
57 
58     if ignore_index is not None and 0 <= ignore_index < num_classes:
59         scores = torch.cat(
60             [
61                 scores[:ignore_index],
62                 scores[ignore_index + 1 :],
63             ]
64         )
65 
66     return reduce(scores, reduction=reduction)
67 
68 
69 def jaccard_index(
70     preds: Tensor,
71     target: Tensor,
72     ignore_index: Optional[int] = None,
73     absent_score: float = 0.0,
74     threshold: float = 0.5,
75     num_classes: Optional[int] = None,
76     reduction: str = "elementwise_mean",
77 ) -> Tensor:
78     r"""
79     Computes `Jaccard index`_
80 
81     .. math:: J(A,B) = \frac{|A\cap B|}{|A\cup B|}
82 
83     Where: :math:`A` and :math:`B` are both tensors of the same size,
84     containing integer class values. They may be subject to conversion from
85     input data (see description below).
86 
87     Note that it is different from box IoU.
88 
89     If preds and target are the same shape and preds is a float tensor, we use the ``self.threshold`` argument
90     to convert into integer labels. This is the case for binary and multi-label probabilities.
91 
92     If pred has an extra dimension as in the case of multi-class scores we
93     perform an argmax on ``dim=1``.
94 
95     Args:
96         preds: tensor containing predictions from model (probabilities, or labels) with shape ``[N, d1, d2, ...]``
97         target: tensor containing ground truth labels with shape ``[N, d1, d2, ...]``
98         ignore_index: optional int specifying a target class to ignore. If given,
99             this class index does not contribute to the returned score, regardless
100             of reduction method. Has no effect if given an int that is not in the
101             range [0, num_classes-1], where num_classes is either given or derived
102             from pred and target. By default, no index is ignored, and all classes are used.
103         absent_score: score to use for an individual class, if no instances of
104             the class index were present in `pred` AND no instances of the class
105             index were present in `target`. For example, if we have 3 classes,
106             [0, 0] for `pred`, and [0, 2] for `target`, then class 1 would be
107             assigned the `absent_score`.
108         threshold:
109             Threshold value for binary or multi-label probabilities.
110         num_classes:
111             Optionally specify the number of classes
112         reduction: a method to reduce metric score over labels.
113 
114             - ``'elementwise_mean'``: takes the mean (default)
115             - ``'sum'``: takes the sum
116             - ``'none'``: no reduction will be applied
117 
118     Return:
119         IoU score: Tensor containing single value if reduction is
120         'elementwise_mean', or number of classes if reduction is 'none'
121 
122     Example:
123         >>> from torchmetrics.functional import jaccard_index
124         >>> target = torch.randint(0, 2, (10, 25, 25))
125         >>> pred = torch.tensor(target)
126         >>> pred[2:5, 7:13, 9:15] = 1 - pred[2:5, 7:13, 9:15]
127         >>> jaccard_index(pred, target)
128         tensor(0.9660)
129     """
130 
131     num_classes = get_num_classes(preds=preds, target=target, num_classes=num_classes)
132     confmat = _confusion_matrix_update(preds, target, num_classes, threshold)
133     return _jaccard_from_confmat(confmat, num_classes, ignore_index, absent_score, reduction)
134
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/torchmetrics/functional/classification/jaccard.py b/torchmetrics/functional/classification/jaccard.py
--- a/torchmetrics/functional/classification/jaccard.py
+++ b/torchmetrics/functional/classification/jaccard.py
@@ -69,10 +69,10 @@
 def jaccard_index(
     preds: Tensor,
     target: Tensor,
+    num_classes: int,
     ignore_index: Optional[int] = None,
     absent_score: float = 0.0,
     threshold: float = 0.5,
-    num_classes: Optional[int] = None,
     reduction: str = "elementwise_mean",
 ) -> Tensor:
     r"""
@@ -95,6 +95,7 @@
     Args:
         preds: tensor containing predictions from model (probabilities, or labels) with shape ``[N, d1, d2, ...]``
         target: tensor containing ground truth labels with shape ``[N, d1, d2, ...]``
+        num_classes: Specify the number of classes
         ignore_index: optional int specifying a target class to ignore. If given,
             this class index does not contribute to the returned score, regardless
             of reduction method. Has no effect if given an int that is not in the
@@ -107,8 +108,7 @@
             assigned the `absent_score`.
         threshold:
             Threshold value for binary or multi-label probabilities.
-        num_classes:
-            Optionally specify the number of classes
+
         reduction: a method to reduce metric score over labels.
 
             - ``'elementwise_mean'``: takes the mean (default)
@@ -124,7 +124,7 @@
         >>> target = torch.randint(0, 2, (10, 25, 25))
         >>> pred = torch.tensor(target)
         >>> pred[2:5, 7:13, 9:15] = 1 - pred[2:5, 7:13, 9:15]
-        >>> jaccard_index(pred, target)
+        >>> jaccard_index(pred, target, num_classes=2)
         tensor(0.9660)
     """

{"golden_diff": "diff --git a/torchmetrics/functional/classification/jaccard.py b/torchmetrics/functional/classification/jaccard.py\n--- a/torchmetrics/functional/classification/jaccard.py\n+++ b/torchmetrics/functional/classification/jaccard.py\n@@ -69,10 +69,10 @@\n def jaccard_index(\n     preds: Tensor,\n     target: Tensor,\n+    num_classes: int,\n     ignore_index: Optional[int] = None,\n     absent_score: float = 0.0,\n     threshold: float = 0.5,\n-    num_classes: Optional[int] = None,\n     reduction: str = \"elementwise_mean\",\n ) -> Tensor:\n     r\"\"\"\n@@ -95,6 +95,7 @@\n     Args:\n         preds: tensor containing predictions from model (probabilities, or labels) with shape ``[N, d1, d2, ...]``\n         target: tensor containing ground truth labels with shape ``[N, d1, d2, ...]``\n+        num_classes: Specify the number of classes\n         ignore_index: optional int specifying a target class to ignore. If given,\n             this class index does not contribute to the returned score, regardless\n             of reduction method. Has no effect if given an int that is not in the\n@@ -107,8 +108,7 @@\n             assigned the `absent_score`.\n         threshold:\n             Threshold value for binary or multi-label probabilities.\n-        num_classes:\n-            Optionally specify the number of classes\n+\n         reduction: a method to reduce metric score over labels.\n \n             - ``'elementwise_mean'``: takes the mean (default)\n@@ -124,7 +124,7 @@\n         >>> target = torch.randint(0, 2, (10, 25, 25))\n         >>> pred = torch.tensor(target)\n         >>> pred[2:5, 7:13, 9:15] = 1 - pred[2:5, 7:13, 9:15]\n-        >>> jaccard_index(pred, target)\n+        >>> jaccard_index(pred, target, num_classes=2)\n         tensor(0.9660)\n     \"\"\"\n", "issue": "Jaccard Index behaviour when num_classes is absent\nThe [jaccard index function](https://github.com/PyTorchLightning/metrics/blob/8fade87062a7b87c1e6429bbe1c4e0112b3713a5/torchmetrics/functional/classification/jaccard.py#L69) determines the number of classes automatically.\r\nThis can result in strange results if the user forgets to specify `num_classes`. For example, in IoU determination in semantic segmentation, not all images have all classes. So determining them automatically will almost always lead to wrong number.\r\nAdditionally, the [warning](https://github.com/PyTorchLightning/metrics/blob/8fade87062a7b87c1e6429bbe1c4e0112b3713a5/torchmetrics/utilities/data.py#L156) that shows up makes it seem like something is wrong, when the user specifies the number of classes explicitly (but this is not the case at all):\r\n\r\n```python\r\n/run/determined/pythonuserbase/lib/python3.7/site-packages/torchmetrics/utilities/prints.py:36: RuntimeWarning: You have set 11 number of classes which is different from predicted (1000) and target (10) number of classes\r\n```\r\n\r\nWould it be crazy if `num_classes` requirement is default and the automatic determination of classes is a special option that must be enabled?\r\n\r\n\n", "before_files": [{"content": "# Copyright The PyTorch Lightning team.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nfrom typing import Optional\n\nimport torch\nfrom torch import Tensor\n\nfrom torchmetrics.functional.classification.confusion_matrix import _confusion_matrix_update\nfrom torchmetrics.utilities.data import get_num_classes\nfrom torchmetrics.utilities.distributed import reduce\n\n\ndef _jaccard_from_confmat(\n    confmat: Tensor,\n    num_classes: int,\n    ignore_index: Optional[int] = None,\n    absent_score: float = 0.0,\n    reduction: str = \"elementwise_mean\",\n) -> Tensor:\n    \"\"\"Computes the intersection over union from confusion matrix.\n\n    Args:\n        confmat: Confusion matrix without normalization\n        num_classes: Number of classes for a given prediction and target tensor\n        ignore_index: optional int specifying a target class to ignore. If given, this class index does not contribute\n            to the returned score, regardless of reduction method.\n        absent_score: score to use for an individual class, if no instances of the class index were present in `pred`\n            AND no instances of the class index were present in `target`.\n        reduction: a method to reduce metric score over labels.\n\n            - ``'elementwise_mean'``: takes the mean (default)\n            - ``'sum'``: takes the sum\n            - ``'none'``: no reduction will be applied\n    \"\"\"\n\n    # Remove the ignored class index from the scores.\n    if ignore_index is not None and 0 <= ignore_index < num_classes:\n        confmat[ignore_index] = 0.0\n\n    intersection = torch.diag(confmat)\n    union = confmat.sum(0) + confmat.sum(1) - intersection\n\n    # If this class is absent in both target AND pred (union == 0), then use the absent_score for this class.\n    scores = intersection.float() / union.float()\n    scores[union == 0] = absent_score\n\n    if ignore_index is not None and 0 <= ignore_index < num_classes:\n        scores = torch.cat(\n            [\n                scores[:ignore_index],\n                scores[ignore_index + 1 :],\n            ]\n        )\n\n    return reduce(scores, reduction=reduction)\n\n\ndef jaccard_index(\n    preds: Tensor,\n    target: Tensor,\n    ignore_index: Optional[int] = None,\n    absent_score: float = 0.0,\n    threshold: float = 0.5,\n    num_classes: Optional[int] = None,\n    reduction: str = \"elementwise_mean\",\n) -> Tensor:\n    r\"\"\"\n    Computes `Jaccard index`_\n\n    .. math:: J(A,B) = \\frac{|A\\cap B|}{|A\\cup B|}\n\n    Where: :math:`A` and :math:`B` are both tensors of the same size,\n    containing integer class values. They may be subject to conversion from\n    input data (see description below).\n\n    Note that it is different from box IoU.\n\n    If preds and target are the same shape and preds is a float tensor, we use the ``self.threshold`` argument\n    to convert into integer labels. This is the case for binary and multi-label probabilities.\n\n    If pred has an extra dimension as in the case of multi-class scores we\n    perform an argmax on ``dim=1``.\n\n    Args:\n        preds: tensor containing predictions from model (probabilities, or labels) with shape ``[N, d1, d2, ...]``\n        target: tensor containing ground truth labels with shape ``[N, d1, d2, ...]``\n        ignore_index: optional int specifying a target class to ignore. If given,\n            this class index does not contribute to the returned score, regardless\n            of reduction method. Has no effect if given an int that is not in the\n            range [0, num_classes-1], where num_classes is either given or derived\n            from pred and target. By default, no index is ignored, and all classes are used.\n        absent_score: score to use for an individual class, if no instances of\n            the class index were present in `pred` AND no instances of the class\n            index were present in `target`. For example, if we have 3 classes,\n            [0, 0] for `pred`, and [0, 2] for `target`, then class 1 would be\n            assigned the `absent_score`.\n        threshold:\n            Threshold value for binary or multi-label probabilities.\n        num_classes:\n            Optionally specify the number of classes\n        reduction: a method to reduce metric score over labels.\n\n            - ``'elementwise_mean'``: takes the mean (default)\n            - ``'sum'``: takes the sum\n            - ``'none'``: no reduction will be applied\n\n    Return:\n        IoU score: Tensor containing single value if reduction is\n        'elementwise_mean', or number of classes if reduction is 'none'\n\n    Example:\n        >>> from torchmetrics.functional import jaccard_index\n        >>> target = torch.randint(0, 2, (10, 25, 25))\n        >>> pred = torch.tensor(target)\n        >>> pred[2:5, 7:13, 9:15] = 1 - pred[2:5, 7:13, 9:15]\n        >>> jaccard_index(pred, target)\n        tensor(0.9660)\n    \"\"\"\n\n    num_classes = get_num_classes(preds=preds, target=target, num_classes=num_classes)\n    confmat = _confusion_matrix_update(preds, target, num_classes, threshold)\n    return _jaccard_from_confmat(confmat, num_classes, ignore_index, absent_score, reduction)\n", "path": "torchmetrics/functional/classification/jaccard.py"}], "after_files": [{"content": "# Copyright The PyTorch Lightning team.\n#\n# Licensed under the Apache License, Version 2.0 (the \"License\");\n# you may not use this file except in compliance with the License.\n# You may obtain a copy of the License at\n#\n#     http://www.apache.org/licenses/LICENSE-2.0\n#\n# Unless required by applicable law or agreed to in writing, software\n# distributed under the License is distributed on an \"AS IS\" BASIS,\n# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n# See the License for the specific language governing permissions and\n# limitations under the License.\nfrom typing import Optional\n\nimport torch\nfrom torch import Tensor\n\nfrom torchmetrics.functional.classification.confusion_matrix import _confusion_matrix_update\nfrom torchmetrics.utilities.data import get_num_classes\nfrom torchmetrics.utilities.distributed import reduce\n\n\ndef _jaccard_from_confmat(\n    confmat: Tensor,\n    num_classes: int,\n    ignore_index: Optional[int] = None,\n    absent_score: float = 0.0,\n    reduction: str = \"elementwise_mean\",\n) -> Tensor:\n    \"\"\"Computes the intersection over union from confusion matrix.\n\n    Args:\n        confmat: Confusion matrix without normalization\n        num_classes: Number of classes for a given prediction and target tensor\n        ignore_index: optional int specifying a target class to ignore. If given, this class index does not contribute\n            to the returned score, regardless of reduction method.\n        absent_score: score to use for an individual class, if no instances of the class index were present in `pred`\n            AND no instances of the class index were present in `target`.\n        reduction: a method to reduce metric score over labels.\n\n            - ``'elementwise_mean'``: takes the mean (default)\n            - ``'sum'``: takes the sum\n            - ``'none'``: no reduction will be applied\n    \"\"\"\n\n    # Remove the ignored class index from the scores.\n    if ignore_index is not None and 0 <= ignore_index < num_classes:\n        confmat[ignore_index] = 0.0\n\n    intersection = torch.diag(confmat)\n    union = confmat.sum(0) + confmat.sum(1) - intersection\n\n    # If this class is absent in both target AND pred (union == 0), then use the absent_score for this class.\n    scores = intersection.float() / union.float()\n    scores[union == 0] = absent_score\n\n    if ignore_index is not None and 0 <= ignore_index < num_classes:\n        scores = torch.cat(\n            [\n                scores[:ignore_index],\n                scores[ignore_index + 1 :],\n            ]\n        )\n\n    return reduce(scores, reduction=reduction)\n\n\ndef jaccard_index(\n    preds: Tensor,\n    target: Tensor,\n    num_classes: int,\n    ignore_index: Optional[int] = None,\n    absent_score: float = 0.0,\n    threshold: float = 0.5,\n    reduction: str = \"elementwise_mean\",\n) -> Tensor:\n    r\"\"\"\n    Computes `Jaccard index`_\n\n    .. math:: J(A,B) = \\frac{|A\\cap B|}{|A\\cup B|}\n\n    Where: :math:`A` and :math:`B` are both tensors of the same size,\n    containing integer class values. They may be subject to conversion from\n    input data (see description below).\n\n    Note that it is different from box IoU.\n\n    If preds and target are the same shape and preds is a float tensor, we use the ``self.threshold`` argument\n    to convert into integer labels. This is the case for binary and multi-label probabilities.\n\n    If pred has an extra dimension as in the case of multi-class scores we\n    perform an argmax on ``dim=1``.\n\n    Args:\n        preds: tensor containing predictions from model (probabilities, or labels) with shape ``[N, d1, d2, ...]``\n        target: tensor containing ground truth labels with shape ``[N, d1, d2, ...]``\n        num_classes: Specify the number of classes\n        ignore_index: optional int specifying a target class to ignore. If given,\n            this class index does not contribute to the returned score, regardless\n            of reduction method. Has no effect if given an int that is not in the\n            range [0, num_classes-1], where num_classes is either given or derived\n            from pred and target. By default, no index is ignored, and all classes are used.\n        absent_score: score to use for an individual class, if no instances of\n            the class index were present in `pred` AND no instances of the class\n            index were present in `target`. For example, if we have 3 classes,\n            [0, 0] for `pred`, and [0, 2] for `target`, then class 1 would be\n            assigned the `absent_score`.\n        threshold:\n            Threshold value for binary or multi-label probabilities.\n\n        reduction: a method to reduce metric score over labels.\n\n            - ``'elementwise_mean'``: takes the mean (default)\n            - ``'sum'``: takes the sum\n            - ``'none'``: no reduction will be applied\n\n    Return:\n        IoU score: Tensor containing single value if reduction is\n        'elementwise_mean', or number of classes if reduction is 'none'\n\n    Example:\n        >>> from torchmetrics.functional import jaccard_index\n        >>> target = torch.randint(0, 2, (10, 25, 25))\n        >>> pred = torch.tensor(target)\n        >>> pred[2:5, 7:13, 9:15] = 1 - pred[2:5, 7:13, 9:15]\n        >>> jaccard_index(pred, target, num_classes=2)\n        tensor(0.9660)\n    \"\"\"\n\n    num_classes = get_num_classes(preds=preds, target=target, num_classes=num_classes)\n    confmat = _confusion_matrix_update(preds, target, num_classes, threshold)\n    return _jaccard_from_confmat(confmat, num_classes, ignore_index, absent_score, reduction)\n", "path": "torchmetrics/functional/classification/jaccard.py"}]}