U
    Ð‹ÑhÈ?  ã                	   @   s”  U d dl Z d dlZd dlZd dlZd dlZd dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZ
d dlZ
d dlZd dlmZmZmZmZmZmZmZmZmZmZ d dlmZ d dlZd dlZd dlmZ ddl m!Z!m"Z" dZ#dRe$ee$ej%f e&dd	œd
d„Z'dSee$ej%f e&e$dœdd„Z(ee$ej%f e$ee)dœdd„Z*dTee$ej%f ee$ e)dœdd„Z+dUe$e&e$dœdd„Z,e$ee$ dœdd„Z-dVe$ee$ej%f eee$ej%f  ee$ e&ddœdd„Z.dWee$ej%f e)ee$ d!œd"d#„Z/dXee$ej%f e$e)ee$ d$œd%d&„Z0dYe$ee$ej%f eee$ej%f  ee$ d'œd(d)„Z1ee$ej%f ee$ej%f ee$ dd*œd+d,„Z2ej3ej4d-œZ5ee$e&f e6d.< ee$ej%f ee$ej%f ee$ dd*œd/d0„Z7e2e7d1œZ8ee$eee$ej%f ee$ej%f ee$ gdf f e6d2< e j9ej9ej9d3œZ:ee$ed4ef f e6d5< d6d6d7d8œZ;ee$eee$ ee$ f f e6d9< ee$ej%f ee$ee$ ee$ f d:œd;d<„Z<dZee$ej%f eee$ej%f  e)ej%d=œd>d?„Z=d[ee$ej%f eee$ej%f  e)ee$ej%f d=œd@dA„Z>d\e$ee$ej%f eee$ej%f  eee$ej%f  ee$ e)ddBœdCdD„Z?ee$dEœdFdG„Z@edHe$eAƒZBd]eBee$ eeeB  ee$ eBdIœdJdK„ZCd^ee$ej%f e&ejDdLœdMdN„ZEejFejFdOœdPdQ„ZGdS )_é    N)
ÚAnyÚCallableÚDictÚIOÚIterableÚListÚOptionalÚTupleÚTypeVarÚUnion)Úurlparse)Útqdmé   )Ú#_download_file_from_remote_locationÚ_is_remote_location_availablezpytorch/visioné €  )ÚurlÚfilenameÚ
chunk_sizeÚreturnc                 C   s‚   t j t jj| dtid�¡�\}t|dƒ�F}t|jd��.}| |¡ }r`| 	|¡ | 
t|ƒ¡ q8W 5 Q R X W 5 Q R X W 5 Q R X d S )Nú
User-Agent©ÚheadersÚwb)Útotal)ÚurllibÚrequestÚurlopenÚRequestÚ
USER_AGENTÚopenr   ÚlengthÚreadÚwriteÚupdateÚlen)r   r   r   ÚresponseÚfhZpbarÚchunk© r)   úH/tmp/user/2202/pip-unpacked-wheel-mjxxo1pc/torchvision/datasets/utils.pyÚ_urlretrieve   s
    
r+   é   )Úfpathr   r   c              	   C   sX   t jdkrtjdd�}nt ¡ }t| dƒ� }| |¡ }rF| |¡ q,W 5 Q R X | ¡ S )N)é   é	   F)ZusedforsecurityÚrb)ÚsysÚversion_infoÚhashlibÚmd5r    r"   r$   Ú	hexdigest)r-   r   r4   Úfr(   r)   r)   r*   Úcalculate_md5#   s    
r7   )r-   r4   Úkwargsr   c                 K   s   |t | f|ŽkS ©N)r7   )r-   r4   r8   r)   r)   r*   Ú	check_md51   s    r:   )r-   r4   r   c                 C   s&   t j | ¡sdS |d krdS t| |ƒS )NFT)ÚosÚpathÚisfiler:   )r-   r4   r)   r)   r*   Úcheck_integrity5   s
    r>   r.   )r   Úmax_hopsr   c                 C   s’   | }dt dœ}t|d ƒD ]V}tj tjj| |d�¡�4}|j| ksL|jd kr`| W  5 Q R £   S |j} W 5 Q R X qtd|› d|› d| › d�ƒ‚d S )	NÚHEAD)ZMethodr   é   r   zRequest to z
 exceeded z( redirects. The last redirect points to Ú.)r   Úranger   r   r   r   r   ÚRecursionError)r   r?   Zinitial_urlr   Ú_r&   r)   r)   r*   Ú_get_redirect_url=   s    
ÿrF   )r   r   c                 C   sB   t | ƒ}t d|j¡d krd S t d|j¡}|d kr8d S | d¡S )Nz(drive|docs)[.]google[.]comz/file/d/(?P<id>[^/]*)Úid)r   ÚreÚmatchÚnetlocr<   Úgroup)r   ÚpartsrI   r)   r)   r*   Ú_get_google_drive_file_idM   s    rM   )r   Úrootr   r4   Úmax_redirect_hopsr   c              
   C   s>  t j |¡}|st j | ¡}t  t j ||¡¡}t j|dd� t||ƒrXtd| ƒ dS t	ƒ rjt
|| ƒ n¼t| |d�} t| ƒ}|dk	r”t||||ƒS z"td|  d | ƒ t| |ƒ W nn tjjtfk
�r$ } zF| dd… d	k�r|  d
d¡} td|  d | ƒ t| |ƒ n|‚W 5 d}~X Y nX t||ƒ�s:tdƒ‚dS )a¯  Download a file from a url and place it in root.

    Args:
        url (str): URL to download file from
        root (str): Directory to place downloaded file in
        filename (str, optional): Name to save the file under. If None, use the basename of the URL
        md5 (str, optional): MD5 checksum of the download. If None, do not check
        max_redirect_hops (int, optional): Maximum number of redirect hops allowed
    T©Úexist_okz$Using downloaded and verified file: N)r?   zDownloading ú to é   Úhttpszhttps:zhttp:z;Failed download. Trying https -> http instead. Downloading úFile not found or corrupted.)r;   r<   Ú
expanduserÚbasenameÚfspathÚjoinÚmakedirsr>   Úprintr   r   rF   rM   Údownload_file_from_google_driver+   r   ÚerrorÚURLErrorÚOSErrorÚreplaceÚRuntimeError)r   rN   r   r4   rO   r-   Úfile_idÚer)   r)   r*   Údownload_urlZ   s2    
rd   F)rN   Úprefixr   c                    sB   t j ˆ ¡‰ ‡ fdd„t  ˆ ¡D ƒ}|dkr>‡ fdd„|D ƒ}|S )a  List all directories at a given root

    Args:
        root (str): Path to directory whose folders need to be listed
        prefix (bool, optional): If true, prepends the path to each result, otherwise
            only returns the name of the directories found
    c                    s&   g | ]}t j t j ˆ |¡¡r|‘qS r)   )r;   r<   ÚisdirrY   ©Ú.0Úp©rN   r)   r*   Ú
<listcomp>›   s      zlist_dir.<locals>.<listcomp>Tc                    s   g | ]}t j ˆ |¡‘qS r)   ©r;   r<   rY   ©rh   Údrj   r)   r*   rk   �   s     ©r;   r<   rV   Úlistdir)rN   re   Údirectoriesr)   rj   r*   Úlist_dir’   s
    rr   )rN   Úsuffixre   r   c                    sD   t j ˆ ¡‰ ‡ ‡fdd„t  ˆ ¡D ƒ}|dkr@‡ fdd„|D ƒ}|S )a¾  List all files ending with a suffix at a given root

    Args:
        root (str): Path to directory whose folders need to be listed
        suffix (str or tuple): Suffix of the files to match, e.g. '.png' or ('.jpg', '.png').
            It uses the Python "str.endswith" method and is passed directly
        prefix (bool, optional): If true, prepends the path to each result, otherwise
            only returns the name of the files found
    c                    s0   g | ](}t j t j ˆ |¡¡r| ˆ¡r|‘qS r)   )r;   r<   r=   rY   Úendswithrg   ©rN   rs   r)   r*   rk   ¬   s      
 zlist_files.<locals>.<listcomp>Tc                    s   g | ]}t j ˆ |¡‘qS r)   rl   rm   rj   r)   r*   rk   ®   s     ro   )rN   rs   re   Úfilesr)   ru   r*   Ú
list_files¡   s
    
rw   )rb   rN   r   r4   c                 C   s²   zddl }W n tk
r(   tdƒ‚Y nX tj |¡}|s>| }t tj ||¡¡}tj|dd� t	||ƒrŠt
d|rvdnd› d	|› �ƒ dS |j| |d
td� t	||ƒs®tdƒ‚dS )af  Download a Google Drive file from  and place it in root.

    Args:
        file_id (str): id of file to be downloaded
        root (str): Directory to place downloaded file in
        filename (str, optional): Name to save the file under. If None, use the id of the file.
        md5 (str, optional): MD5 checksum of the download. If None, do not check
    r   Nz`To download files from GDrive, 'gdown' is required. You can install it with 'pip install gdown'.TrP   zUsing downloaded zand verified Ú zfile: F)rG   ÚoutputÚquietÚ
user_agentrU   )ÚgdownÚModuleNotFoundErrorra   r;   r<   rV   rX   rY   rZ   r>   r[   Údownloadr   )rb   rN   r   r4   r|   r-   r)   r)   r*   r\   ²   s"    ÿ


r\   )Ú	from_pathÚto_pathÚcompressionr   c              	   C   s<   t  | |rd|dd … › �nd¡�}| |¡ W 5 Q R X d S )Nzr:rA   Úr)Útarfiler    Ú
extractall)r   r€   r�   Útarr)   r)   r*   Ú_extract_tarØ   s    $r†   )ú.bz2ú.xzÚ_ZIP_COMPRESSION_MAPc              	   C   s8   t j| d|rt| nt jd��}| |¡ W 5 Q R X d S )Nr‚   )r�   )ÚzipfileÚZipFiler‰   Ú
ZIP_STOREDr„   )r   r€   r�   Úzipr)   r)   r*   Ú_extract_zipå   s      ÿrŽ   )ú.tarz.zipÚ_ARCHIVE_EXTRACTORS)r‡   ú.gzrˆ   .Ú_COMPRESSED_FILE_OPENERS)r�   r‡   )r�   r‘   )z.tbzz.tbz2z.tgzÚ_FILE_TYPE_ALIASES)Úfiler   c                 C   sÂ   t  | ¡j}|s td| › d�ƒ‚|d }|tkr>|ft| ˜S |tkrP||dfS |tkrŒt|ƒdkr‚|d }|tkr‚|| ||fS |d|fS tt	tƒt	tƒB t	tƒB ƒ}td|› d|› d	�ƒ‚dS )
a  Detect the archive type and/or compression of a file.

    Args:
        file (str): the filename

    Returns:
        (tuple): tuple of suffix, archive type, and compression

    Raises:
        RuntimeError: if file has no suffix or suffix is not supported
    zFile 'zP' has no suffixes that could be used to detect the archive type and compression.éÿÿÿÿNrA   éþÿÿÿz&Unknown compression or archive type: 'z'.
Known suffixes are: 'z'.)
ÚpathlibÚPathÚsuffixesra   r“   r�   r’   r%   ÚsortedÚset)r”   r™   rs   Zsuffix2Zvalid_suffixesr)   r)   r*   Ú_detect_file_typeþ   s$    
ÿ

rœ   )r   r€   Úremove_finishedr   c           	   
   C   s¨   t | ƒ\}}}|s"td|› d�ƒ‚|dkrNt t | ¡ ||dk	rF|nd¡¡}t| }|| dƒ�*}t|dƒ�}| 	| 
¡ ¡ W 5 Q R X W 5 Q R X |ržt | ¡ t |¡S )a©  Decompress a file.

    The compression is automatically detected from the file name.

    Args:
        from_path (str): Path to the file to be decompressed.
        to_path (str): Path to the decompressed file. If omitted, ``from_path`` without compression extension is used.
        remove_finished (bool): If ``True``, remove the file after the extraction.

    Returns:
        (str): Path to the decompressed file.
    z*Couldn't detect a compression from suffix rB   Nrx   r0   r   )rœ   ra   r—   r˜   r;   rX   r`   r’   r    r#   r"   Úremove)	r   r€   r�   rs   Úarchive_typer�   Zcompressed_file_openerZrfhZwfhr)   r)   r*   Ú_decompress)  s    $"
r    c           	   	      s¦   t jttt jf dœ‡ fdd„}|dkr4tj ˆ ¡}tˆ ƒ\}}}|svtˆ tj 	|tj 
ˆ ¡ |d¡¡|d�}||ƒS t| }|ˆ ||ƒ |r˜t ˆ ¡ |t  |¡ƒS )aD  Extract an archive.

    The archive type and a possible compression is automatically detected from the file name. If the file is compressed
    but not an archive the call is dispatched to :func:`decompress`.

    Args:
        from_path (str): Path to the file to be extracted.
        to_path (str): Path to the directory the file will be extracted to. If omitted, the directory of the file is
            used.
        remove_finished (bool): If ``True``, remove the file after the extraction.

    Returns:
        (str): Path to the directory the file was extracted to.
    )Úret_pathr   c                    s   t ˆ tƒrt | ¡S | S d S r9   )Ú
isinstanceÚstrr;   rX   )r¡   ©r   r)   r*   Úpath_or_stra  s    

z$extract_archive.<locals>.path_or_strNrx   )r�   )r—   r˜   r   r£   r;   r<   Údirnamerœ   r    rY   rW   r`   r�   rž   )	r   r€   r�   r¥   rs   rŸ   r�   r¡   Z	extractorr)   r¤   r*   Úextract_archiveM  s      ý
r§   )r   Údownload_rootÚextract_rootr   r4   r�   r   c                 C   sh   t j |¡}|d kr|}|s(t j | ¡}t| |||ƒ t j ||¡}td|› d|› �ƒ t|||ƒ d S )NzExtracting rR   )r;   r<   rV   rW   rd   rY   r[   r§   )r   r¨   r©   r   r4   r�   Úarchiver)   r)   r*   Údownload_and_extract_archive}  s    r«   )Úiterabler   c                 C   s   dd  dd„ | D ƒ¡ d S )Nú'z', 'c                 S   s   g | ]}t |ƒ‘qS r)   )r£   )rh   Úitemr)   r)   r*   rk   “  s     z#iterable_to_str.<locals>.<listcomp>)rY   )r¬   r)   r)   r*   Úiterable_to_str’  s    r¯   ÚT)ÚvalueÚargÚvalid_valuesÚ
custom_msgr   c                 C   s|   t | tƒs6|d krd}nd}|jt| ƒ|d�}t|ƒ‚|d krB| S | |krx|d k	rX|}nd}|j| |t|ƒd�}t|ƒ‚| S )Nz'Expected type str, but got type {type}.z:Expected type str for argument {arg}, but got type {type}.)Útyper²   zPUnknown value '{value}' for argument {arg}. Valid values are {{{valid_values}}}.)r±   r²   r³   )r¢   r£   Úformatrµ   Ú
ValueErrorr¯   )r±   r²   r³   r´   Úmsgr)   r)   r*   Úverify_str_arg™  s    
r¹   )Ú	file_nameÚslice_channelsr   c              	   C   sú   t | dƒ�Ž}| ¡  ¡ }|dkr(tdƒ‚t d| ¡ ¡}|sDtdƒ‚dd„ | ¡ D ƒ\}}t| ¡  ¡ ƒ}|dk r~d	}| }nd
}t	j
||d d�}	W 5 Q R X |dkrªdnd}
|	 |||
¡ ddd¡}	t	j|	dd�}	|	d|…dd…dd…f }	|	 t	j¡S )aD  Read file in .pfm format. Might contain either 1 or 3 channels of data.

    Args:
        file_name (str): Path to the file.
        slice_channels (int): Number of channels to slice out of the file.
            Useful for reading different data formats stored in .pfm files: Optical Flows, Stereo Disparity Maps, etc.
    r0   )ó   PFs   PfzInvalid PFM files   ^(\d+)\s(\d+)\s$zMalformed PFM header.c                 s   s   | ]}t |ƒV  qd S r9   )Úint)rh   Zdimr)   r)   r*   Ú	<genexpr>Æ  s     z_read_pfm.<locals>.<genexpr>r   ú<ú>r6   )Údtyper¼   r.   rA   r   )ZaxisN)r    ÚreadlineÚrstripr·   rH   rI   Ú	ExceptionÚgroupsÚfloatÚnpZfromfileZreshapeZ	transposeÚflipZastypeZfloat32)rº   r»   r6   ÚheaderZ	dim_matchÚwÚhZscaleZendianÚdataZpfm_channelsr)   r)   r*   Ú	_read_pfmµ  s&    	rÍ   )Útr   c                 C   sD   |   ¡  tj¡j| j|  ¡ fžŽ  d¡j| jd d… džŽ  | j¡S )Nr•   )r•   )Ú
contiguousÚviewÚtorchZuint8ÚshapeZelement_sizerÈ   rÁ   )rÎ   r)   r)   r*   Ú_flip_byte_orderÙ  s    BÿrÓ   )r   )r,   )N)r.   )NNr.   )F)F)NN)NF)NF)NNNF)NNN)r   )HÚbz2Úgzipr3   Úlzmar;   Úos.pathr—   rH   r1   rƒ   r   Úurllib.errorÚurllib.requestrŠ   Útypingr   r   r   r   r   r   r   r	   r
   r   Úurllib.parser   ZnumpyrÇ   rÑ   Ztorch.utils.model_zoor   Z_internally_replaced_utilsr   r   r   r£   r˜   r½   r+   r7   Úboolr:   r>   rF   rM   rd   rr   rw   r\   r†   Ú	ZIP_BZIP2ÚZIP_LZMAr‰   Ú__annotations__rŽ   r�   r    r’   r“   rœ   r    r§   r«   r¯   Úbytesr°   r¹   ZndarrayrÍ   ZTensorrÓ   r)   r)   r)   r*   Ú<module>   sè    0 "   ûú8"$  üü'  þþ  þ
þ<ýý&,-  ýü&  ýü3    úù   ü
û $