[docs]classCaltech101(VisionDataset):"""`Caltech 101 <https://data.caltech.edu/records/20086>`_ Dataset. .. warning:: This class needs `scipy <https://docs.scipy.org/doc/>`_ to load target files from `.mat` format. Args: root (str or ``pathlib.Path``): Root directory of dataset where directory ``caltech101`` exists or will be saved to if download is set to True. target_type (string or list, optional): Type of target to use, ``category`` or ``annotation``. Can also be a list to output a tuple with all specified target types. ``category`` represents the target class, and ``annotation`` is a list of points from a hand-generated outline. Defaults to ``category``. 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 in root directory. If dataset is already downloaded, it is not downloaded again. .. warning:: To download the dataset `gdown <https://github.com/wkentaro/gdown>`_ is required. """def__init__(self,root:Union[str,Path],target_type:Union[List[str],str]="category",transform:Optional[Callable]=None,target_transform:Optional[Callable]=None,download:bool=False,)->None:super().__init__(os.path.join(root,"caltech101"),transform=transform,target_transform=target_transform)os.makedirs(self.root,exist_ok=True)ifisinstance(target_type,str):target_type=[target_type]self.target_type=[verify_str_arg(t,"target_type",("category","annotation"))fortintarget_type]ifdownload:self.download()ifnotself._check_integrity():raiseRuntimeError("Dataset not found or corrupted. You can use download=True to download it")self.categories=sorted(os.listdir(os.path.join(self.root,"101_ObjectCategories")))self.categories.remove("BACKGROUND_Google")# this is not a real class# For some reason, the category names in "101_ObjectCategories" and# "Annotations" do not always match. This is a manual map between the# two. Defaults to using same name, since most names are fine.name_map={"Faces":"Faces_2","Faces_easy":"Faces_3","Motorbikes":"Motorbikes_16","airplanes":"Airplanes_Side_2",}self.annotation_categories=list(map(lambdax:name_map[x]ifxinname_mapelsex,self.categories))self.index:List[int]=[]self.y=[]for(i,c)inenumerate(self.categories):n=len(os.listdir(os.path.join(self.root,"101_ObjectCategories",c)))self.index.extend(range(1,n+1))self.y.extend(n*[i])
[docs]def__getitem__(self,index:int)->Tuple[Any,Any]:""" Args: index (int): Index Returns: tuple: (image, target) where the type of target specified by target_type. """importscipy.ioimg=Image.open(os.path.join(self.root,"101_ObjectCategories",self.categories[self.y[index]],f"image_{self.index[index]:04d}.jpg",))target:Any=[]fortinself.target_type:ift=="category":target.append(self.y[index])elift=="annotation":data=scipy.io.loadmat(os.path.join(self.root,"Annotations",self.annotation_categories[self.y[index]],f"annotation_{self.index[index]:04d}.mat",))target.append(data["obj_contour"])target=tuple(target)iflen(target)>1elsetarget[0]ifself.transformisnotNone:img=self.transform(img)ifself.target_transformisnotNone:target=self.target_transform(target)returnimg,target
def_check_integrity(self)->bool:# can be more robust and check hash of filesreturnos.path.exists(os.path.join(self.root,"101_ObjectCategories"))def__len__(self)->int:returnlen(self.index)defdownload(self)->None:ifself._check_integrity():returndownload_and_extract_archive("https://drive.google.com/file/d/137RyRjvTBkBiIfeYBNZBtViDHQ6_Ewsp",self.root,filename="101_ObjectCategories.tar.gz",md5="b224c7392d521a49829488ab0f1120d9",)download_and_extract_archive("https://drive.google.com/file/d/175kQy3UsZ0wUEHZjqkUDdNVssr7bgh_m",self.root,filename="Annotations.tar",md5="6f83eeb1f24d99cab4eb377263132c91",)defextra_repr(self)->str:return"Target type: {target_type}".format(**self.__dict__)
[docs]classCaltech256(VisionDataset):"""`Caltech 256 <https://data.caltech.edu/records/20087>`_ Dataset. Args: root (str or ``pathlib.Path``): Root directory of dataset where directory ``caltech256`` exists or will be saved to if download is set to True. 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 in root directory. If dataset is already downloaded, it is not downloaded again. """def__init__(self,root:str,transform:Optional[Callable]=None,target_transform:Optional[Callable]=None,download:bool=False,)->None:super().__init__(os.path.join(root,"caltech256"),transform=transform,target_transform=target_transform)os.makedirs(self.root,exist_ok=True)ifdownload:self.download()ifnotself._check_integrity():raiseRuntimeError("Dataset not found or corrupted. You can use download=True to download it")self.categories=sorted(os.listdir(os.path.join(self.root,"256_ObjectCategories")))self.index:List[int]=[]self.y=[]for(i,c)inenumerate(self.categories):n=len([itemforiteminos.listdir(os.path.join(self.root,"256_ObjectCategories",c))ifitem.endswith(".jpg")])self.index.extend(range(1,n+1))self.y.extend(n*[i])
[docs]def__getitem__(self,index:int)->Tuple[Any,Any]:""" Args: index (int): Index Returns: tuple: (image, target) where target is index of the target class. """img=Image.open(os.path.join(self.root,"256_ObjectCategories",self.categories[self.y[index]],f"{self.y[index]+1:03d}_{self.index[index]:04d}.jpg",))target=self.y[index]ifself.transformisnotNone:img=self.transform(img)ifself.target_transformisnotNone:target=self.target_transform(target)returnimg,target
def_check_integrity(self)->bool:# can be more robust and check hash of filesreturnos.path.exists(os.path.join(self.root,"256_ObjectCategories"))def__len__(self)->int:returnlen(self.index)defdownload(self)->None:ifself._check_integrity():returndownload_and_extract_archive("https://drive.google.com/file/d/1r6o0pSROcV1_VwT4oSjA2FBUSCWGuxLK",self.root,filename="256_ObjectCategories.tar",md5="67b4f42ca05d46448c6bb8ecd2220f6d",)
Docs
Access comprehensive developer documentation for PyTorch
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.