[docs]classSTL10(VisionDataset):"""`STL10 <https://cs.stanford.edu/~acoates/stl10/>`_ Dataset. Args: root (str or ``pathlib.Path``): Root directory of dataset where directory ``stl10_binary`` exists. split (string): One of {'train', 'test', 'unlabeled', 'train+unlabeled'}. Accordingly, dataset is selected. folds (int, optional): One of {0-9} or None. For training, loads one of the 10 pre-defined folds of 1k samples for the standard evaluation procedure. If no value is passed, loads the 5k samples. 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. """base_folder="stl10_binary"url="http://ai.stanford.edu/~acoates/stl10/stl10_binary.tar.gz"filename="stl10_binary.tar.gz"tgz_md5="91f7769df0f17e558f3565bffb0c7dfb"class_names_file="class_names.txt"folds_list_file="fold_indices.txt"train_list=[["train_X.bin","918c2871b30a85fa023e0c44e0bee87f"],["train_y.bin","5a34089d4802c674881badbb80307741"],["unlabeled_X.bin","5242ba1fed5e4be9e1e742405eb56ca4"],]test_list=[["test_X.bin","7f263ba9f9e0b06b93213547f721ac82"],["test_y.bin","36f9794fa4beb8a2c72628de14fa638e"]]splits=("train","train+unlabeled","unlabeled","test")def__init__(self,root:Union[str,Path],split:str="train",folds:Optional[int]=None,transform:Optional[Callable]=None,target_transform:Optional[Callable]=None,download:bool=False,)->None:super().__init__(root,transform=transform,target_transform=target_transform)self.split=verify_str_arg(split,"split",self.splits)self.folds=self._verify_folds(folds)ifdownload:self.download()elifnotself._check_integrity():raiseRuntimeError("Dataset not found or corrupted. You can use download=True to download it")# now load the picked numpy arraysself.labels:Optional[np.ndarray]ifself.split=="train":self.data,self.labels=self.__loadfile(self.train_list[0][0],self.train_list[1][0])self.labels=cast(np.ndarray,self.labels)self.__load_folds(folds)elifself.split=="train+unlabeled":self.data,self.labels=self.__loadfile(self.train_list[0][0],self.train_list[1][0])self.labels=cast(np.ndarray,self.labels)self.__load_folds(folds)unlabeled_data,_=self.__loadfile(self.train_list[2][0])self.data=np.concatenate((self.data,unlabeled_data))self.labels=np.concatenate((self.labels,np.asarray([-1]*unlabeled_data.shape[0])))elifself.split=="unlabeled":self.data,_=self.__loadfile(self.train_list[2][0])self.labels=np.asarray([-1]*self.data.shape[0])else:# self.split == 'test':self.data,self.labels=self.__loadfile(self.test_list[0][0],self.test_list[1][0])class_file=os.path.join(self.root,self.base_folder,self.class_names_file)ifos.path.isfile(class_file):withopen(class_file)asf:self.classes=f.read().splitlines()def_verify_folds(self,folds:Optional[int])->Optional[int]:iffoldsisNone:returnfoldselifisinstance(folds,int):iffoldsinrange(10):returnfoldsmsg="Value for argument folds should be in the range [0, 10), but got {}."raiseValueError(msg.format(folds))else:msg="Expected type None or int for argument folds, but got type {}."raiseValueError(msg.format(type(folds)))
[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. """target:Optional[int]ifself.labelsisnotNone:img,target=self.data[index],int(self.labels[index])else:img,target=self.data[index],None# doing this so that it is consistent with all other datasets# to return a PIL Imageimg=Image.fromarray(np.transpose(img,(1,2,0)))ifself.transformisnotNone:img=self.transform(img)ifself.target_transformisnotNone:target=self.target_transform(target)returnimg,target
def__len__(self)->int:returnself.data.shape[0]def__loadfile(self,data_file:str,labels_file:Optional[str]=None)->Tuple[np.ndarray,Optional[np.ndarray]]:labels=Noneiflabels_file:path_to_labels=os.path.join(self.root,self.base_folder,labels_file)withopen(path_to_labels,"rb")asf:labels=np.fromfile(f,dtype=np.uint8)-1# 0-basedpath_to_data=os.path.join(self.root,self.base_folder,data_file)withopen(path_to_data,"rb")asf:# read whole file in uint8 chunkseverything=np.fromfile(f,dtype=np.uint8)images=np.reshape(everything,(-1,3,96,96))images=np.transpose(images,(0,1,3,2))returnimages,labelsdef_check_integrity(self)->bool:forfilename,md5inself.train_list+self.test_list:fpath=os.path.join(self.root,self.base_folder,filename)ifnotcheck_integrity(fpath,md5):returnFalsereturnTruedefdownload(self)->None:ifself._check_integrity():returndownload_and_extract_archive(self.url,self.root,filename=self.filename,md5=self.tgz_md5)self._check_integrity()defextra_repr(self)->str:return"Split: {split}".format(**self.__dict__)def__load_folds(self,folds:Optional[int])->None:# loads one of the folds if specifiediffoldsisNone:returnpath_to_folds=os.path.join(self.root,self.base_folder,self.folds_list_file)withopen(path_to_folds)asf:str_idx=f.read().splitlines()[folds]list_idx=np.fromstring(str_idx,dtype=np.int64,sep=" ")self.data=self.data[list_idx,:,:,:]ifself.labelsisnotNone:self.labels=self.labels[list_idx]
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.