Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

oxford_iiit_pet.py 5.5 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
  1. import os
  2. import os.path
  3. import pathlib
  4. from typing import Any, Callable, Optional, Sequence, Tuple, Union
  5. from PIL import Image
  6. from .utils import download_and_extract_archive, verify_str_arg
  7. from .vision import VisionDataset
  8. class OxfordIIITPet(VisionDataset):
  9. """`Oxford-IIIT Pet Dataset <https://www.robots.ox.ac.uk/~vgg/data/pets/>`_.
  10. Args:
  11. root (str or ``pathlib.Path``): Root directory of the dataset.
  12. split (string, optional): The dataset split, supports ``"trainval"`` (default) or ``"test"``.
  13. target_types (string, sequence of strings, optional): Types of target to use. Can be ``category`` (default) or
  14. ``segmentation``. Can also be a list to output a tuple with all specified target types. The types represent:
  15. - ``category`` (int): Label for one of the 37 pet categories.
  16. - ``binary-category`` (int): Binary label for cat or dog.
  17. - ``segmentation`` (PIL image): Segmentation trimap of the image.
  18. If empty, ``None`` will be returned as target.
  19. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed
  20. version. E.g, ``transforms.RandomCrop``.
  21. target_transform (callable, optional): A function/transform that takes in the target and transforms it.
  22. transforms (callable, optional): A function/transform that takes input sample
  23. and its target as entry and returns a transformed version.
  24. download (bool, optional): If True, downloads the dataset from the internet and puts it into
  25. ``root/oxford-iiit-pet``. If dataset is already downloaded, it is not downloaded again.
  26. """
  27. _RESOURCES = (
  28. ("https://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz", "5c4f3ee8e5d25df40f4fd59a7f44e54c"),
  29. ("https://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz", "95a8c909bbe2e81eed6a22bccdf3f68f"),
  30. )
  31. _VALID_TARGET_TYPES = ("category", "binary-category", "segmentation")
  32. def __init__(
  33. self,
  34. root: Union[str, pathlib.Path],
  35. split: str = "trainval",
  36. target_types: Union[Sequence[str], str] = "category",
  37. transforms: Optional[Callable] = None,
  38. transform: Optional[Callable] = None,
  39. target_transform: Optional[Callable] = None,
  40. download: bool = False,
  41. ):
  42. self._split = verify_str_arg(split, "split", ("trainval", "test"))
  43. if isinstance(target_types, str):
  44. target_types = [target_types]
  45. self._target_types = [
  46. verify_str_arg(target_type, "target_types", self._VALID_TARGET_TYPES) for target_type in target_types
  47. ]
  48. super().__init__(root, transforms=transforms, transform=transform, target_transform=target_transform)
  49. self._base_folder = pathlib.Path(self.root) / "oxford-iiit-pet"
  50. self._images_folder = self._base_folder / "images"
  51. self._anns_folder = self._base_folder / "annotations"
  52. self._segs_folder = self._anns_folder / "trimaps"
  53. if download:
  54. self._download()
  55. if not self._check_exists():
  56. raise RuntimeError("Dataset not found. You can use download=True to download it")
  57. image_ids = []
  58. self._labels = []
  59. self._bin_labels = []
  60. with open(self._anns_folder / f"{self._split}.txt") as file:
  61. for line in file:
  62. image_id, label, bin_label, _ = line.strip().split()
  63. image_ids.append(image_id)
  64. self._labels.append(int(label) - 1)
  65. self._bin_labels.append(int(bin_label) - 1)
  66. self.bin_classes = ["Cat", "Dog"]
  67. self.classes = [
  68. " ".join(part.title() for part in raw_cls.split("_"))
  69. for raw_cls, _ in sorted(
  70. {(image_id.rsplit("_", 1)[0], label) for image_id, label in zip(image_ids, self._labels)},
  71. key=lambda image_id_and_label: image_id_and_label[1],
  72. )
  73. ]
  74. self.bin_class_to_idx = dict(zip(self.bin_classes, range(len(self.bin_classes))))
  75. self.class_to_idx = dict(zip(self.classes, range(len(self.classes))))
  76. self._images = [self._images_folder / f"{image_id}.jpg" for image_id in image_ids]
  77. self._segs = [self._segs_folder / f"{image_id}.png" for image_id in image_ids]
  78. def __len__(self) -> int:
  79. return len(self._images)
  80. def __getitem__(self, idx: int) -> Tuple[Any, Any]:
  81. image = Image.open(self._images[idx]).convert("RGB")
  82. target: Any = []
  83. for target_type in self._target_types:
  84. if target_type == "category":
  85. target.append(self._labels[idx])
  86. elif target_type == "binary-category":
  87. target.append(self._bin_labels[idx])
  88. else: # target_type == "segmentation"
  89. target.append(Image.open(self._segs[idx]))
  90. if not target:
  91. target = None
  92. elif len(target) == 1:
  93. target = target[0]
  94. else:
  95. target = tuple(target)
  96. if self.transforms:
  97. image, target = self.transforms(image, target)
  98. return image, target
  99. def _check_exists(self) -> bool:
  100. for folder in (self._images_folder, self._anns_folder):
  101. if not (os.path.exists(folder) and os.path.isdir(folder)):
  102. return False
  103. else:
  104. return True
  105. def _download(self) -> None:
  106. if self._check_exists():
  107. return
  108. for url, md5 in self._RESOURCES:
  109. download_and_extract_archive(url, download_root=str(self._base_folder), md5=md5)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...