[docs]classFakeData(VisionDataset):"""A fake dataset that returns randomly generated images and returns them as PIL images Args: size (int, optional): Size of the dataset. Default: 1000 images image_size(tuple, optional): Size if the returned images. Default: (3, 224, 224) num_classes(int, optional): Number of classes in the dataset. Default: 10 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. random_offset (int): Offsets the index-based random seed used to generate each image. Default: 0 """def__init__(self,size:int=1000,image_size:Tuple[int,int,int]=(3,224,224),num_classes:int=10,transform:Optional[Callable]=None,target_transform:Optional[Callable]=None,random_offset:int=0,)->None:super().__init__(transform=transform,target_transform=target_transform)self.size=sizeself.num_classes=num_classesself.image_size=image_sizeself.random_offset=random_offset
[docs]def__getitem__(self,index:int)->Tuple[Any,Any]:""" Args: index (int): Index Returns: tuple: (image, target) where target is class_index of the target class. """# create random image that is consistent with the index idifindex>=len(self):raiseIndexError(f"{self.__class__.__name__} index out of range")rng_state=torch.get_rng_state()torch.manual_seed(index+self.random_offset)img=torch.randn(*self.image_size)target=torch.randint(0,self.num_classes,size=(1,),dtype=torch.long)[0]torch.set_rng_state(rng_state)# convert to PIL Imageimg=transforms.ToPILImage()(img)ifself.transformisnotNone:img=self.transform(img)ifself.target_transformisnotNone:target=self.target_transform(target)returnimg,target.item()
def__len__(self)->int:returnself.size
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.