3
Á3W^_  ã               @   sh   d dl Z d dlZd dlmZ d dlZddlmZ ddlm	Z	 G dd„ deƒZ
dd	„ Zd
d„ Zdd„ ZdS )é    N)ÚImageé   )ÚVisionDataset)Údownload_urlc                   sÊ   e Zd ZdZdddgdddgdd	d
gdddgdddgdddgdœZdddddddœZdddddddœZddddd d!d"œZd#Zd$Z	d%Z
d7‡ fd)d*„	Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Z‡  ZS )8Ú	PhotoToura9  `Learning Local Image Descriptors Data <http://phototour.cs.washington.edu/patches/default.htm>`_ Dataset.


    Args:
        root (string): Root directory where images are.
        name (string): Name of the dataset to load.
        transform (callable, optional): A function/transform that  takes in an 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.

    z:http://matthewalunbrown.com/patchdata/notredame_harris.zipznotredame_harris.zipZ 69f8c90f78e171349abdf0307afefe4dz9http://matthewalunbrown.com/patchdata/yosemite_harris.zipzyosemite_harris.zipZ a73253d1c6fbd3ba2613c45065c00d46z8http://matthewalunbrown.com/patchdata/liberty_harris.zipzliberty_harris.zipZ c731fcfb3abb4091110d0ae8c7ba182cz,http://icvl.ee.ic.ac.uk/vbalnt/notredame.zipznotredame.zipZ 509eda8535847b8c0a90bbb210c83484z+http://icvl.ee.ic.ac.uk/vbalnt/yosemite.zipzyosemite.zipZ 533b2e8eb7ede31be40abc317b2fd4f0z*http://icvl.ee.ic.ac.uk/vbalnt/liberty.zipzliberty.zipZ fdd9152f138ea5ef2091746689176414)Únotredame_harrisÚyosemite_harrisÚliberty_harrisÚ	notredameÚyosemiteÚlibertygž^)Ëß?gq¬‹Ûh ß?g„O¯”eÜ?)r
   r   r   r   r   r	   g$¹ü‡ôÛÇ?g¡Ö4ï8EÇ?g¹ü‡ôÛ×É?i¿$ ióª	 i,Þ iÃÊ i`á i¯ö )r
   r   r   r	   r   r   Zbmpzinfo.txtzm50_100000_100000_0.txtTNFc                s²   t t| ƒj||d� || _tjj| j|ƒ| _tjj| jdj	|ƒƒ| _
tjj| jdj	|ƒƒ| _|| _| j| | _| j| | _|r†| jƒ  | jƒ s–tdƒ‚tj| jƒ\| _| _| _d S )N)Ú	transformz{}.zipz{}.ptzDataset not found.z) You can use download=True to download itz;Dataset not found. You can use download=True to download it)Úsuperr   Ú__init__ÚnameÚosÚpathÚjoinÚrootÚdata_dirÚformatÚ	data_downÚ	data_fileÚtrainÚmeanÚstdÚdownloadÚ_check_datafile_existsÚRuntimeErrorÚtorchÚloadÚdataÚlabelsÚmatches)Úselfr   r   r   r   r   )Ú	__class__© úE/tmp/pip-build-7wfi2gtz/torchvision/torchvision/datasets/phototour.pyr   C   s    zPhotoTour.__init__c             C   s|   | j r(| j| }| jdk	r$| j|ƒ}|S | j| }| j|d  | j|d   }}| jdk	rn| j|ƒ}| j|ƒ}|||d fS )zs
        Args:
            index (int): Index

        Returns:
            tuple: (data1, data2, matches)
        Nr   r   é   )r   r!   r   r#   )r$   Úindexr!   ÚmZdata1Zdata2r&   r&   r'   Ú__getitem__X   s    






zPhotoTour.__getitem__c             C   s   | j r| j| j S t| jƒS )N)r   Úlensr   Úlenr#   )r$   r&   r&   r'   Ú__len__l   s    zPhotoTour.__len__c             C   s   t jj| jƒS )N)r   r   Úexistsr   )r$   r&   r&   r'   r   q   s    z PhotoTour._check_datafile_existsc             C   s   t jj| jƒS )N)r   r   r/   r   )r$   r&   r&   r'   Ú_check_downloadedt   s    zPhotoTour._check_downloadedc       	      C   s$  | j ƒ rtdj| jƒƒ d S | jƒ sº| j| j d }| j| j d }| j| j d }tjj	| j
|ƒ}t|| j
||ƒ tdj| jƒƒ dd l}|j|dƒ�}|j| jƒ W d Q R X tj|ƒ tdj| jƒƒ t| j| j| j| j ƒt| j| jƒt| j| jƒf}t| jdƒ�}tj||ƒ W d Q R X d S )	Nz# Found cached data {}r   r   r(   z# Extracting data {}
Úrz# Caching data {}Úwb)r   Úprintr   r   r0   Úurlsr   r   r   r   r   r   r   ÚzipfileÚZipFileÚ
extractallr   ÚunlinkÚread_image_fileÚ	image_extr,   Úread_info_fileÚ	info_fileÚread_matches_filesÚmatches_filesÚopenr   Úsave)	r$   ÚurlÚfilenameÚmd5Úfpathr5   ÚzZdatasetÚfr&   r&   r'   r   w   s(    
zPhotoTour.downloadc             C   s   dj | jdkrdndƒS )Nz	Split: {}TZTrainZTest)r   r   )r$   r&   r&   r'   Ú
extra_repr™   s    zPhotoTour.extra_repr)TNF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r4   r   r   r,   r:   r<   r>   r   r+   r.   r   r0   r   rG   Ú__classcell__r&   r&   )r%   r'   r      sF   
"r   c          	   C   s¢   dd„ }dd„ }g }|| |ƒ}xj|D ]b}t j|ƒ}xRtdddƒD ]B}	x<tdddƒD ],}
|j|
|	|
d |	d fƒ}|j||ƒƒ qRW q@W q$W tjtj|d|… ƒƒS )	z+Return a Tensor containing the patches
    c             S   s   t j| jƒ t jd�jddƒS )z1Convert PIL image type to numpy 2D array
        )Zdtypeé@   )ÚnpÚarrayZgetdataZuint8Zreshape)Z_imgr&   r&   r'   Ú	PIL2array¡   s    z"read_image_file.<locals>.PIL2arrayc             S   s>   g }x0t j| ƒD ]"}|j|ƒr|jt jj| |ƒƒ qW t|ƒS )zOReturn a list with the file names of the images containing the patches
        )r   ÚlistdirÚendswithÚappendr   r   Úsorted)Z	_data_dirZ
_image_extÚfilesZfile_dirr&   r&   r'   Ú
find_files¦   s
    
z#read_image_file.<locals>.find_filesr   i   rM   N)	r   r?   ÚrangeZcroprS   r   Z
ByteTensorrN   rO   )r   r:   ÚnrP   rV   ZpatchesÚ
list_filesrD   ÚimgÚyÚxÚpatchr&   r&   r'   r9   �   s    



r9   c          	   C   s<   g }t tjj| |ƒdƒ�}dd„ |D ƒ}W dQ R X tj|ƒS )zmReturn a Tensor containing the list of labels
       Read the file and keep only the ID of the 3D point.
    r1   c             S   s   g | ]}t |jƒ d  ƒ‘qS )r   )ÚintÚsplit)Ú.0Úliner&   r&   r'   ú
<listcomp>Â   s    z"read_info_file.<locals>.<listcomp>N)r?   r   r   r   r   Ú
LongTensor)r   r<   r"   rF   r&   r&   r'   r;   ¼   s    r;   c             C   st   g }t tjj| |ƒdƒ�L}xD|D ]<}|jƒ }|jt|d ƒt|d ƒt|d |d kƒgƒ q W W dQ R X tj|ƒS )z§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.
    r1   r   é   r   é   N)	r?   r   r   r   r_   rS   r^   r   rc   )r   Zmatches_filer#   rF   ra   Z
line_splitr&   r&   r'   r=   Æ   s    
&r=   )r   ZnumpyrN   ZPILr   r   Zvisionr   Úutilsr   r   r9   r;   r=   r&   r&   r&   r'   Ú<module>   s    
