[docs]classLSUN(VisionDataset):"""`LSUN <https://paperswithcode.com/dataset/lsun>`_ dataset. You will need to install the ``lmdb`` package to use this dataset: run ``pip install lmdb`` Args: root (str or ``pathlib.Path``): Root directory for the database files. classes (string or list): One of {'train', 'val', 'test'} or a list of categories to load. e,g. ['bedroom_train', 'church_outdoor_train']. 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. """def__init__(self,root:Union[str,Path],classes:Union[str,List[str]]="train",transform:Optional[Callable]=None,target_transform:Optional[Callable]=None,)->None:super().__init__(root,transform=transform,target_transform=target_transform)self.classes=self._verify_classes(classes)# for each class, create an LSUNClassDatasetself.dbs=[]forcinself.classes:self.dbs.append(LSUNClass(root=os.path.join(root,f"{c}_lmdb"),transform=transform))self.indices=[]count=0fordbinself.dbs:count+=len(db)self.indices.append(count)self.length=countdef_verify_classes(self,classes:Union[str,List[str]])->List[str]:categories=["bedroom","bridge","church_outdoor","classroom","conference_room","dining_room","kitchen","living_room","restaurant","tower",]dset_opts=["train","val","test"]try:classes=cast(str,classes)verify_str_arg(classes,"classes",dset_opts)ifclasses=="test":classes=[classes]else:classes=[c+"_"+classesforcincategories]exceptValueError:ifnotisinstance(classes,Iterable):msg="Expected type str or Iterable for argument classes, but got type {}."raiseValueError(msg.format(type(classes)))classes=list(classes)msg_fmtstr_type="Expected type str for elements in argument classes, but got type {}."forcinclasses:verify_str_arg(c,custom_msg=msg_fmtstr_type.format(type(c)))c_short=c.split("_")category,dset_opt="_".join(c_short[:-1]),c_short[-1]msg_fmtstr="Unknown value '{}' for {}. Valid values are {{{}}}."msg=msg_fmtstr.format(category,"LSUN class",iterable_to_str(categories))verify_str_arg(category,valid_values=categories,custom_msg=msg)msg=msg_fmtstr.format(dset_opt,"postfix",iterable_to_str(dset_opts))verify_str_arg(dset_opt,valid_values=dset_opts,custom_msg=msg)returnclasses
[docs]def__getitem__(self,index:int)->Tuple[Any,Any]:""" Args: index (int): Index Returns: tuple: Tuple (image, target) where target is the index of the target category. """target=0sub=0forindinself.indices:ifindex<ind:breaktarget+=1sub=inddb=self.dbs[target]index=index-subifself.target_transformisnotNone:target=self.target_transform(target)img,_=db[index]returnimg,target
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.