Skip to content

Commit

Permalink
Added Type Check for cocodetection dataset (#8227)
Browse files Browse the repository at this point in the history
Co-authored-by: RazaProdigy <[email protected]>
Co-authored-by: Nicolas Hug <[email protected]>
  • Loading branch information
3 people authored Mar 13, 2024
1 parent 6d64cb3 commit 8afd547
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions test/test_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions torchvision/datasets/coco.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8afd547

Please sign in to comment.