Repository: Lightning-AI/torchmetrics

Issue:
FID.compute() throws imprecise error with batch size of 1
## 🐛 Bug

<!-- A clear and concise description of what the bug is. -->
Running FID with a batch size of 1 throws error: ValueError: array must not contain infs or NaNs

### To Reproduce

To reproduce, run the code below. Distribution with size ([1, 3, 299, 299]). 


<!-- If you have a code sample, error messages, stack traces, please provide it here as well -->
See above
<details>
  <summary>Code sample</summary>

<!-- Ideally attach a minimal code sample to reproduce the decried issue.
Minimal means having the shortest code but still preserving the bug. -->
Run the following code

```python
import torch
from torchmetrics.image.fid import FrechetInceptionDistance
batch_size = 1
imgs_dist1 = torch.randint(0, 255, (batch_size, 3, 299, 299), dtype=torch.uint8)
imgs_dist2 = torch.randint(0, 255, (batch_size, 3, 299, 299), dtype=torch.uint8)
fid = FrechetInceptionDistance(feature=2048, normalize=False)
fid.update(imgs_dist1, real=True)
fid.update(imgs_dist2, real=False)
fid.compute()
```

</details>

### Expected behavior

<!-- A clear and concise description of what you expected to happen. -->
I expect to get a FID score between the two 1-batch-sized distributions. 
If it does not work with a batch size of 1, I expect an error message saying this, instead of saying the array cannot have inf or nan values.

### Environment

- TorchMetrics version (and how you installed TM, e.g. `conda`, `pip`, build from source):
- Python & PyTorch Version (e.g., 1.0):
- Any other relevant information such as OS (e.g., Linux):

torchmetrics[image]==0.11.4
pytorch 1.13.1+cu116

### Additional context

<!-- Add any other context about the problem here. -->



Discussion:
Hi! thanks for your contribution!, great first issue!

Resolving patch:
diff --git a/src/torchmetrics/image/fid.py b/src/torchmetrics/image/fid.py
--- a/src/torchmetrics/image/fid.py
+++ b/src/torchmetrics/image/fid.py
@@ -380,6 +380,8 @@ def update(self, imgs: Tensor, real: bool) -> None:
 
     def compute(self) -> Tensor:
         """Calculate FID score based on accumulated extracted features from the two distributions."""
+        if self.real_features_num_samples < 2 or self.fake_features_num_samples < 2:
+            raise RuntimeError("More than one sample is required for both the real and fake distributed to compute FID")
         mean_real = (self.real_features_sum / self.real_features_num_samples).unsqueeze(0)
         mean_fake = (self.fake_features_sum / self.fake_features_num_samples).unsqueeze(0)
 


Test patch:
diff --git a/tests/unittests/image/test_fid.py b/tests/unittests/image/test_fid.py
--- a/tests/unittests/image/test_fid.py
+++ b/tests/unittests/image/test_fid.py
@@ -201,6 +201,18 @@ def test_normalize_arg_false():
         metric.update(img, real=True)
 
 
+def test_not_enough_samples():
+    """Test that an error is raised if not enough samples were provided."""
+    img = torch.randint(0, 255, (1, 3, 299, 299), dtype=torch.uint8)
+    metric = FrechetInceptionDistance()
+    metric.update(img, real=True)
+    metric.update(img, real=False)
+    with pytest.raises(
+        RuntimeError, match="More than one sample is required for both the real and fake distributed to compute FID"
+    ):
+        metric.compute()
+
+
 def test_dtype_transfer_to_submodule():
     """Test that change in dtype also changes the default inception net."""
     imgs = torch.randn(1, 3, 256, 256)