[docs]classPhotoTour(VisionDataset):"""`Multi-view Stereo Correspondence <http://matthewalunbrown.com/patchdata/patchdata.html>`_ Dataset. .. note:: We only provide the newer version of the dataset, since the authors state that it is more suitable for training descriptors based on difference of Gaussian, or Harris corners, as the patches are centred on real interest point detections, rather than being projections of 3D points as is the case in the old dataset. The original dataset is available under http://phototour.cs.washington.edu/patches/default.htm. Args: root (str or ``pathlib.Path``): Root directory where images are. name (string): Name of the dataset to load. transform (callable, optional): A function/transform that takes in a PIL image and returns a transformed version. 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. """urls={"notredame_harris":["http://matthewalunbrown.com/patchdata/notredame_harris.zip","notredame_harris.zip","69f8c90f78e171349abdf0307afefe4d",],"yosemite_harris":["http://matthewalunbrown.com/patchdata/yosemite_harris.zip","yosemite_harris.zip","a73253d1c6fbd3ba2613c45065c00d46",],"liberty_harris":["http://matthewalunbrown.com/patchdata/liberty_harris.zip","liberty_harris.zip","c731fcfb3abb4091110d0ae8c7ba182c",],"notredame":["http://icvl.ee.ic.ac.uk/vbalnt/notredame.zip","notredame.zip","509eda8535847b8c0a90bbb210c83484",],"yosemite":["http://icvl.ee.ic.ac.uk/vbalnt/yosemite.zip","yosemite.zip","533b2e8eb7ede31be40abc317b2fd4f0"],"liberty":["http://icvl.ee.ic.ac.uk/vbalnt/liberty.zip","liberty.zip","fdd9152f138ea5ef2091746689176414"],}means={"notredame":0.4854,"yosemite":0.4844,"liberty":0.4437,"notredame_harris":0.4854,"yosemite_harris":0.4844,"liberty_harris":0.4437,}stds={"notredame":0.1864,"yosemite":0.1818,"liberty":0.2019,"notredame_harris":0.1864,"yosemite_harris":0.1818,"liberty_harris":0.2019,}lens={"notredame":468159,"yosemite":633587,"liberty":450092,"liberty_harris":379587,"yosemite_harris":450912,"notredame_harris":325295,}image_ext="bmp"info_file="info.txt"matches_files="m50_100000_100000_0.txt"def__init__(self,root:Union[str,Path],name:str,train:bool=True,transform:Optional[Callable]=None,download:bool=False,)->None:super().__init__(root,transform=transform)self.name=nameself.data_dir=os.path.join(self.root,name)self.data_down=os.path.join(self.root,f"{name}.zip")self.data_file=os.path.join(self.root,f"{name}.pt")self.train=trainself.mean=self.means[name]self.std=self.stds[name]ifdownload:self.download()ifnotself._check_datafile_exists():self.cache()# load the serialized dataself.data,self.labels,self.matches=torch.load(self.data_file,weights_only=True)
[docs]def__getitem__(self,index:int)->Union[torch.Tensor,Tuple[Any,Any,torch.Tensor]]:""" Args: index (int): Index Returns: tuple: (data1, data2, matches) """ifself.train:data=self.data[index]ifself.transformisnotNone:data=self.transform(data)returndatam=self.matches[index]data1,data2=self.data[m[0]],self.data[m[1]]ifself.transformisnotNone:data1=self.transform(data1)data2=self.transform(data2)returndata1,data2,m[2]
def__len__(self)->int:returnlen(self.dataifself.trainelseself.matches)def_check_datafile_exists(self)->bool:returnos.path.exists(self.data_file)def_check_downloaded(self)->bool:returnos.path.exists(self.data_dir)defdownload(self)->None:ifself._check_datafile_exists():returnifnotself._check_downloaded():# download filesurl=self.urls[self.name][0]filename=self.urls[self.name][1]md5=self.urls[self.name][2]fpath=os.path.join(self.root,filename)download_url(url,self.root,filename,md5)importzipfilewithzipfile.ZipFile(fpath,"r")asz:z.extractall(self.data_dir)os.unlink(fpath)defcache(self)->None:# process and save as torch filesdataset=(read_image_file(self.data_dir,self.image_ext,self.lens[self.name]),read_info_file(self.data_dir,self.info_file),read_matches_files(self.data_dir,self.matches_files),)withopen(self.data_file,"wb")asf:torch.save(dataset,f)defextra_repr(self)->str:split="Train"ifself.trainisTrueelse"Test"returnf"Split: {split}"
defread_image_file(data_dir:str,image_ext:str,n:int)->torch.Tensor:"""Return a Tensor containing the patches"""defPIL2array(_img:Image.Image)->np.ndarray:"""Convert PIL image type to numpy 2D array"""returnnp.array(_img.getdata(),dtype=np.uint8).reshape(64,64)deffind_files(_data_dir:str,_image_ext:str)->List[str]:"""Return a list with the file names of the images containing the patches"""files=[]# find those files with the specified extensionforfile_dirinos.listdir(_data_dir):iffile_dir.endswith(_image_ext):files.append(os.path.join(_data_dir,file_dir))returnsorted(files)# sort files in ascend order to keep relationspatches=[]list_files=find_files(data_dir,image_ext)forfpathinlist_files:img=Image.open(fpath)foryinrange(0,img.height,64):forxinrange(0,img.width,64):patch=img.crop((x,y,x+64,y+64))patches.append(PIL2array(patch))returntorch.ByteTensor(np.array(patches[:n]))defread_info_file(data_dir:str,info_file:str)->torch.Tensor:"""Return a Tensor containing the list of labels Read the file and keep only the ID of the 3D point. """withopen(os.path.join(data_dir,info_file))asf:labels=[int(line.split()[0])forlineinf]returntorch.LongTensor(labels)defread_matches_files(data_dir:str,matches_file:str)->torch.Tensor:"""Return a Tensor containing the ground truth matches Read the file and keep only 3D point ID. Matches are represented with a 1, non matches with a 0. """matches=[]withopen(os.path.join(data_dir,matches_file))asf:forlineinf:line_split=line.split()matches.append([int(line_split[0]),int(line_split[3]),int(line_split[1]==line_split[4])])returntorch.LongTensor(matches)
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.