[docs]classSBU(VisionDataset):"""`SBU Captioned Photo <http://www.cs.virginia.edu/~vicente/sbucaptions/>`_ Dataset. Args: root (str or ``pathlib.Path``): Root directory of dataset where tarball ``SBUCaptionedPhotoDataset.tar.gz`` exists. transform (callable, optional): A function/transform that takes in a PIL image or torch.Tensor, depends on the given loader, 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. loader (callable, optional): A function to load an image given its path. By default, it uses PIL as its image loader, but users could also pass in ``torchvision.io.decode_image`` for decoding image data into tensors directly. """url="https://www.cs.rice.edu/~vo9/sbucaptions/SBUCaptionedPhotoDataset.tar.gz"filename="SBUCaptionedPhotoDataset.tar.gz"md5_checksum="9aec147b3488753cf758b4d493422285"def__init__(self,root:Union[str,Path],transform:Optional[Callable]=None,target_transform:Optional[Callable]=None,download:bool=True,loader:Callable[[str],Any]=default_loader,)->None:super().__init__(root,transform=transform,target_transform=target_transform)self.loader=loaderifdownload:self.download()ifnotself._check_integrity():raiseRuntimeError("Dataset not found or corrupted. You can use download=True to download it")# Read the caption for each photoself.photos=[]self.captions=[]file1=os.path.join(self.root,"dataset","SBU_captioned_photo_dataset_urls.txt")file2=os.path.join(self.root,"dataset","SBU_captioned_photo_dataset_captions.txt")forline1,line2inzip(open(file1),open(file2)):url=line1.rstrip()photo=os.path.basename(url)filename=os.path.join(self.root,"dataset",photo)ifos.path.exists(filename):caption=line2.rstrip()self.photos.append(photo)self.captions.append(caption)
[docs]def__getitem__(self,index:int)->Tuple[Any,Any]:""" Args: index (int): Index Returns: tuple: (image, target) where target is a caption for the photo. """filename=os.path.join(self.root,"dataset",self.photos[index])img=self.loader(filename)ifself.transformisnotNone:img=self.transform(img)target=self.captions[index]ifself.target_transformisnotNone:target=self.target_transform(target)returnimg,target
def__len__(self)->int:"""The number of photos in the dataset."""returnlen(self.photos)def_check_integrity(self)->bool:"""Check the md5 checksum of the downloaded tarball."""root=self.rootfpath=os.path.join(root,self.filename)ifnotcheck_integrity(fpath,self.md5_checksum):returnFalsereturnTruedefdownload(self)->None:"""Download and extract the tarball, and download each individual photo."""ifself._check_integrity():returndownload_and_extract_archive(self.url,self.root,self.root,self.filename,self.md5_checksum)# Download individual photoswithopen(os.path.join(self.root,"dataset","SBU_captioned_photo_dataset_urls.txt"))asfh:forlineinfh:url=line.rstrip()try:download_url(url,os.path.join(self.root,"dataset"))exceptOSError:# The images point to public images on Flickr.# Note: Images might be removed by users at anytime.pass
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.