[docs]classOxfordIIITPet(VisionDataset):"""`Oxford-IIIT Pet Dataset <https://www.robots.ox.ac.uk/~vgg/data/pets/>`_. Args: root (str or ``pathlib.Path``): Root directory of the dataset. split (string, optional): The dataset split, supports ``"trainval"`` (default) or ``"test"``. target_types (string, sequence of strings, optional): Types of target to use. Can be ``category`` (default) or ``segmentation``. Can also be a list to output a tuple with all specified target types. The types represent: - ``category`` (int): Label for one of the 37 pet categories. - ``binary-category`` (int): Binary label for cat or dog. - ``segmentation`` (PIL image): Segmentation trimap of the image. If empty, ``None`` will be returned as target. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. E.g, ``transforms.RandomCrop``. target_transform (callable, optional): A function/transform that takes in the target and transforms it. download (bool, optional): If True, downloads the dataset from the internet and puts it into ``root/oxford-iiit-pet``. If dataset is already downloaded, it is not downloaded again. """_RESOURCES=(("https://www.robots.ox.ac.uk/~vgg/data/pets/data/images.tar.gz","5c4f3ee8e5d25df40f4fd59a7f44e54c"),("https://www.robots.ox.ac.uk/~vgg/data/pets/data/annotations.tar.gz","95a8c909bbe2e81eed6a22bccdf3f68f"),)_VALID_TARGET_TYPES=("category","binary-category","segmentation")def__init__(self,root:Union[str,pathlib.Path],split:str="trainval",target_types:Union[Sequence[str],str]="category",transforms:Optional[Callable]=None,transform:Optional[Callable]=None,target_transform:Optional[Callable]=None,download:bool=False,):self._split=verify_str_arg(split,"split",("trainval","test"))ifisinstance(target_types,str):target_types=[target_types]self._target_types=[verify_str_arg(target_type,"target_types",self._VALID_TARGET_TYPES)fortarget_typeintarget_types]super().__init__(root,transforms=transforms,transform=transform,target_transform=target_transform)self._base_folder=pathlib.Path(self.root)/"oxford-iiit-pet"self._images_folder=self._base_folder/"images"self._anns_folder=self._base_folder/"annotations"self._segs_folder=self._anns_folder/"trimaps"ifdownload:self._download()ifnotself._check_exists():raiseRuntimeError("Dataset not found. You can use download=True to download it")image_ids=[]self._labels=[]self._bin_labels=[]withopen(self._anns_folder/f"{self._split}.txt")asfile:forlineinfile:image_id,label,bin_label,_=line.strip().split()image_ids.append(image_id)self._labels.append(int(label)-1)self._bin_labels.append(int(bin_label)-1)self.bin_classes=["Cat","Dog"]self.classes=[" ".join(part.title()forpartinraw_cls.split("_"))forraw_cls,_insorted({(image_id.rsplit("_",1)[0],label)forimage_id,labelinzip(image_ids,self._labels)},key=lambdaimage_id_and_label:image_id_and_label[1],)]self.bin_class_to_idx=dict(zip(self.bin_classes,range(len(self.bin_classes))))self.class_to_idx=dict(zip(self.classes,range(len(self.classes))))self._images=[self._images_folder/f"{image_id}.jpg"forimage_idinimage_ids]self._segs=[self._segs_folder/f"{image_id}.png"forimage_idinimage_ids]def__len__(self)->int:returnlen(self._images)
To analyze traffic and optimize your experience, we serve cookies on this site. By clicking or navigating, you agree to allow our usage of cookies. As the current maintainers of this site, Facebook’s Cookies Policy applies. Learn more, including about available controls: Cookies Policy.