Repository: pytorch/vision

Issue:
‍‍‍‍`CocoDetection` accepts `slice` as argument to its `__getitem__` and returns wrong result
Hi, although `CocoDetection` declares its `__getitem__` argument to be of type `int`, it actually accepts `slice` as well. However it returns a wrong result. As this might be confusing for some users, I suggest to add a type check at the beginning of the `__getitem__`.
https://github.com/pytorch/vision/blob/cab01fc1b7c7f0620ab95c6064f2a3fb583e9bee/torchvision/datasets/coco.py#L46-L49

```python
ds[:2]
```
```
(tensor([[[0.3373, 0.3373, 0.3373,  ..., 0.2863, 0.2863, 0.2863],
          [0.3373, 0.3373, 0.3373,  ..., 0.2863, 0.2863, 0.2863],
          [0.3373, 0.3373, 0.3373,  ..., 0.2863, 0.2863, 0.2863],
          ...,
          [0.4196, 0.4196, 0.4196,  ..., 0.4137, 0.3980, 0.3235],
          [0.4196, 0.4196, 0.4196,  ..., 0.4137, 0.3980, 0.3235],
          [0.4196, 0.4196, 0.4196,  ..., 0.4137, 0.3980, 0.3235]],
 
         [[0.1608, 0.1608, 0.1608,  ..., 0.1216, 0.1216, 0.1216],
          [0.1608, 0.1608, 0.1608,  ..., 0.1216, 0.1216, 0.1216],
          [0.1608, 0.1608, 0.1608,  ..., 0.1216, 0.1216, 0.1216],
          ...,
          [0.5686, 0.5686, 0.5686,  ..., 0.5333, 0.5137, 0.4353],
          [0.5686, 0.5686, 0.5686,  ..., 0.5333, 0.5137, 0.4353],
          [0.5686, 0.5686, 0.5686,  ..., 0.5333, 0.5137, 0.4353]],
 
         [[0.1412, 0.1412, 0.1412,  ..., 0.1608, 0.1608, 0.1608],
          [0.1412, 0.1412, 0.1412,  ..., 0.1608, 0.1608, 0.1608],
          [0.1412, 0.1412, 0.1412,  ..., 0.1608, 0.1608, 0.1608],
          ...,
          [0.0902, 0.0902, 0.0902,  ..., 0.1333, 0.1451, 0.1020],
          [0.0902, 0.0902, 0.0902,  ..., 0.1333, 0.1451, 0.1020],
          [0.0902, 0.0902, 0.0902,  ..., 0.1333, 0.1451, 0.1020]]]),
 {'category_id': tensor([2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3,
          1]),
  'bbox': tensor([[ 390,  240,   58,   92],
          [ 391,  319,   66,   99],
          [ 663,  331,   54,  106],
          [ 796,  324,   50,  102],
          [1245,  276,   35,  117],
          [ 599,  265,   58,   91],
          [ 755,  228,   42,   91],
          [ 788,  261,   61,   97],
          [ 250,  374,   73,  105],
          [ 224,  449,   29,   28],
          [  12,   85,   13,   32],
          [  88,   70,   13,   27],
          [ 102,   99,   14,   33],
          [ 100,  132,   14,   32],
          [ 151,  160,   13,   34],
          [ 177,  115,   18,   32],
          [ 214,   73,   12,   28],
          [ 267,   58,   13,   23],
          [ 358,   71,   14,   33],
          [ 356,  160,   17,   36],
          [ 231,  132,   14,   33],
          [ 234,   88,   16,   29],
          [ 248,   90,   13,   25],
          [  60,  109,   13,   32],
          [ 243,   75,    7,    5]]),
  'iscrowd': tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
          0])})
```
The image in the returned tuple is that of the `ds[0]` and the target is the combined annotations of `ds[0]` and `ds[1]` which is wrong. This is due to the fact that `ds.coco` could work with a list of inputs in its `getAnnIds()`.

cc @pmeier


Discussion:

Resolving patch:
diff --git a/torchvision/datasets/coco.py b/torchvision/datasets/coco.py
--- a/torchvision/datasets/coco.py
+++ b/torchvision/datasets/coco.py
@@ -44,6 +44,10 @@ def _load_target(self, id: int) -> List[Any]:
         return self.coco.loadAnns(self.coco.getAnnIds(id))
 
     def __getitem__(self, index: int) -> Tuple[Any, Any]:
+
+        if not isinstance(index, int):
+            raise ValueError(f"Index must be of type integer, got {type(index)} instead.")
+
         id = self.ids[index]
         image = self._load_image(id)
         target = self._load_target(id)


Test patch:
diff --git a/test/test_datasets.py b/test/test_datasets.py
--- a/test/test_datasets.py
+++ b/test/test_datasets.py
@@ -827,6 +827,11 @@ def test_transforms_v2_wrapper_spawn(self):
         with self.create_dataset(transform=v2.Resize(size=expected_size)) as (dataset, _):
             datasets_utils.check_transforms_v2_wrapper_spawn(dataset, expected_size=expected_size)
 
+    def test_slice_error(self):
+        with self.create_dataset() as (dataset, _):
+            with pytest.raises(ValueError, match="Index must be of type integer"):
+                dataset[:2]
+
 
 class CocoCaptionsTestCase(CocoDetectionTestCase):
     DATASET_CLASS = datasets.CocoCaptions