3
I4W^‡G  ã               @   s¨  d dl mZmZmZm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e
jd  dkr�d dlmZ d dlmZ nd dlmZ d dlmZ yd dlmZ W n$ ek
rÜ   G dd„ deƒZY nX ejd	ƒZd
ZdZdZdZdZdZdZ da!dd„ Z"dd„ Z#dd„ Z$dd„ Z%dd„ Z&dd„ Z'dd„ Z(d7d d!„Z)d"d#„ Z*d$d%„ Z+d&d'„ Z,d(d)„ Z-d8d+d,„Z.d9d-d.„Z/d/d0„ Z0d:d1d2„Z1d;d3d4„Z2d<d5d6„Z3dS )=é    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literalsNé   )Úurlparse)Úurlopen)Útqdmc               @   s.   e Zd Zddd„Zdd„ Zdd„ Zd	d
„ ZdS )r	   NFc             C   s   || _ || _d| _d S )Nr   )ÚtotalÚdisableÚn)Úselfr
   r   ÚunitÚ
unit_scaleÚunit_divisor© r   ú*/tmp/pip-build-e66j97b2/torch/torch/hub.pyÚ__init__   s    ztqdm.__init__c             C   sh   | j r
d S |  j|7  _| jd kr8tjjdj| jƒƒ n"tjjdjd| j t| jƒ ƒƒ tjjƒ  d S )Nz{0:.1f} bytesz	{0:.1f}%éd   )	r   r   r
   ÚsysÚstderrÚwriteÚformatÚfloatÚflush)r   r   r   r   r   Úupdate!   s    
"ztqdm.updatec             C   s   | S )Nr   )r   r   r   r   Ú	__enter__,   s    ztqdm.__enter__c             C   s   | j r
d S tjjdƒ d S )NÚ
)r   r   r   r   )r   Úexc_typeÚexc_valÚexc_tbr   r   r   Ú__exit__/   s    ztqdm.__exit__)NFNNN)Ú__name__Ú
__module__Ú__qualname__r   r   r   r!   r   r   r   r   r	      s
    
r	   z-([a-f0-9]*)\.ZmasterZ
TORCH_HOMEÚXDG_CACHE_HOMEz~/.cacheÚdependenciesz
hubconf.pyi    c             C   sx   t jdkr<dd l}|jj| |ƒ}|jj|ƒ}|jj|ƒ |S t jdkr`ddlm	} || |ƒj
ƒ S dd l}|j| |ƒS d S )Né   é   r   )ÚSourceFileLoader)r'   r(   )r'   r   )r   Úversion_infoÚimportlib.utilÚutilÚspec_from_file_locationÚmodule_from_specÚloaderÚexec_moduleÚimportlib.machineryr)   Úload_moduleÚimpÚload_source)ÚnameÚpathÚ	importlibÚspecÚmoduler)   r3   r   r   r   Úimport_moduleC   s    

r:   c             C   s2   t jj| ƒr.t jj| ƒr$t j| ƒ n
tj| ƒ d S )N)Úosr6   ÚexistsÚisfileÚremoveÚshutilÚrmtree)r6   r   r   r   Ú_remove_if_existsR   s    rA   c             C   s   dj | ||ƒS )Nz'https://github.com/{}/{}/archive/{}.zip)r   )Ú
repo_ownerÚ	repo_nameÚbranchr   r   r   Ú_git_archive_linkZ   s    rE   c             C   s   |t | ƒkrd S t| |ƒS )N)ÚdirÚgetattr)r9   Ú	func_namer   r   r   Ú_load_attr_from_module^   s    rI   c              C   s*   t jjt jtt jjt jttƒdƒƒƒ} | S )NÚtorch)r;   r6   Ú
expanduserÚgetenvÚENV_TORCH_HOMEÚjoinÚENV_XDG_CACHE_HOMEÚDEFAULT_CACHE_DIR)Ú
torch_homer   r   r   Ú_get_torch_homee   s    rR   c              C   sJ   t jdƒrtjdƒ td kr0tƒ } t jj| dƒat jjtƒsFt j	tƒ d S )NZ	TORCH_HUBz:TORCH_HUB is deprecated, please use env TORCH_HOME insteadZhub)
r;   rL   ÚwarningsÚwarnÚhub_dirrR   r6   rN   r<   Úmakedirs)rQ   r   r   r   Ú_setup_hubdirl   s    

rW   c             C   s8   t }d| kr| jdƒ\}}n| }|jdƒ\}}|||fS )Nú:ú/)ÚMASTER_BRANCHÚsplit)ÚgithubrD   Z	repo_inforB   rC   r   r   r   Ú_parse_repo_infoz   s    r]   Tc          
   C   s  t | ƒ\}}}tjjtdj|||gƒƒ}| o8tjj|ƒ}|rV|rþtjjdj	|ƒƒ n¨tjjt|d ƒ}t
|ƒ t|||ƒ}	tjjdj	|	|ƒƒ t|	|dd� tj|ƒ�4}
|
jƒ d j}tjjt|ƒ}t
|ƒ |
jtƒ W d Q R X t
|ƒ t
|ƒ tj||ƒ |S )NÚ_zUsing cache found in {}
z.zipzDownloading: "{}" to {}
F)Úprogressr   )r]   r;   r6   rN   rU   r<   r   r   r   r   rA   rE   Údownload_url_to_fileÚzipfileÚZipFileÚinfolistÚfilenameÚ
extractallr?   Úmove)r\   Úforce_reloadÚverboserB   rC   rD   Úrepo_dirZ	use_cacheÚcached_fileÚurlÚcached_zipfileZextraced_repo_nameZextracted_repor   r   r   Ú_get_cache_or_reload„   s(    rm   c             C   sÒ   t jdkr"dd l}|jj| ƒd k	S t jdkrBdd l}|j| ƒd k	S ydd l}|j| ƒ}|r^dS W n t	k
rt   Y nX t j
}xL|D ]D}t jj|ƒ}|r‚y|j| |gƒ}|r®dS W q‚ t	k
rÄ   Y q‚X q‚W dS d S )Nr'   é   r   TF)r'   rn   )r'   r'   )r   r*   r+   r,   Ú	find_specZimportlib.find_loaderÚfind_loaderr3   Úfind_moduleÚImportErrorr6   Úpath_importer_cacheÚget)r5   r7   r3   Úresultr6   ÚitemÚimporterr   r   r   Ú_check_module_exists©   s0    




rx   c             C   s@   t | tƒ}|d k	r<dd„ |D ƒ}t|ƒr<tdjdj|ƒƒƒ‚d S )Nc             S   s   g | ]}t |ƒs|‘qS r   )rx   )Ú.0Úpkgr   r   r   ú
<listcomp>Ô   s    z'_check_dependencies.<locals>.<listcomp>zMissing dependencies: {}z, )rI   ÚVAR_DEPENDENCYÚlenÚRuntimeErrorr   rN   )Úmr&   Zmissing_depsr   r   r   Ú_check_dependenciesÐ   s
    
r€   c             C   sH   t |tƒstdƒ‚t| ƒ t| |ƒ}|d ks6t|ƒ rDtdj|ƒƒ‚|S )Nz8Invalid input: model should be a string of function namez"Cannot find callable {} in hubconf)Ú
isinstanceÚstrÚ
ValueErrorr€   rI   Úcallabler~   r   )r   ÚmodelÚfuncr   r   r   Ú_load_entry_from_hubconfÙ   s    

r‡   c             C   s   | a dS )aÿ  
    Optionally set hub_dir to a local dir to save downloaded models & weights.

    If ``set_dir`` is not called, default path is ``$TORCH_HOME/hub`` where
    environment variable ``$TORCH_HOME`` defaults to ``$XDG_CACHE_HOME/torch``.
    ``$XDG_CACHE_HOME`` follows the X Design Group specification of the Linux
    filesytem layout, with a default value ``~/.cache`` if the environment
    variable is not set.


    Args:
        d (string): path to a local folder to save downloaded models & weights.
    N)rU   )Údr   r   r   Úset_dirë   s    r‰   Fc                sX   t ƒ  t| |dƒ}tjjd|ƒ tt|d t ƒ‰ tjj|ƒ ‡ fdd„tˆ ƒD ƒ}|S )aJ  
    List all entrypoints available in `github` hubconf.

    Args:
        github (string): a string with format "repo_owner/repo_name[:tag_name]" with an optional
            tag/branch. The default branch is `master` if not specified.
            Example: 'pytorch/vision[:hub]'
        force_reload (bool, optional): whether to discard the existing cache and force a fresh download.
            Default is `False`.
    Returns:
        entrypoints: a list of available entrypoint names

    Example:
        >>> entrypoints = torch.hub.list('pytorch/vision', force_reload=True)
    Tr   rY   c                s*   g | ]"}t tˆ |ƒƒr|jd ƒ r|‘qS )r^   )r„   rG   Ú
startswith)ry   Úf)Ú
hub_moduler   r   r{     s    zlist.<locals>.<listcomp>)	rW   rm   r   r6   Úinsertr:   ÚMODULE_HUBCONFr>   rF   )r\   rg   ri   Zentrypointsr   )rŒ   r   Úlistý   s    r�   c             C   sN   t ƒ  t| |dƒ}tjjd|ƒ tt|d t ƒ}tjj|ƒ t||ƒ}|j	S )aN  
    Show the docstring of entrypoint `model`.

    Args:
        github (string): a string with format <repo_owner/repo_name[:tag_name]> with an optional
            tag/branch. The default branch is `master` if not specified.
            Example: 'pytorch/vision[:hub]'
        model (string): a string of entrypoint name defined in repo's hubconf.py
        force_reload (bool, optional): whether to discard the existing cache and force a fresh download.
            Default is `False`.
    Example:
        >>> print(torch.hub.help('pytorch/vision', 'resnet18', force_reload=True))
    Tr   rY   )
rW   rm   r   r6   r�   r:   rŽ   r>   r‡   Ú__doc__)r\   r…   rg   ri   rŒ   Úentryr   r   r   Úhelp  s    
r’   c       	      O   s†   t ƒ  |jddƒ}|jddƒ |jddƒ}|jddƒ t| ||ƒ}tjjd|ƒ tt|d t ƒ}t	||ƒ}|||Ž}tjj
|ƒ |S )aý  
    Load a model from a github repo, with pretrained weights.

    Args:
        github (string): a string with format "repo_owner/repo_name[:tag_name]" with an optional
            tag/branch. The default branch is `master` if not specified.
            Example: 'pytorch/vision[:hub]'
        model (string): a string of entrypoint name defined in repo's hubconf.py
        *args (optional): the corresponding args for callable `model`.
        force_reload (bool, optional): whether to force a fresh download of github repo unconditionally.
            Default is `False`.
        verbose (bool, optional): If False, mute messages about hitting local caches. Note that the message
            about first download is cannot be muted.
            Default is `True`.
        **kwargs (optional): the corresponding kwargs for callable `model`.

    Returns:
        a single model with corresponding pretrained weights.

    Example:
        >>> model = torch.hub.load('pytorch/vision', 'resnet50', pretrained=True)
    rg   FNrh   Tr   rY   )rW   rt   Úpoprm   r   r6   r�   r:   rŽ   r‡   r>   )	r\   r…   ÚargsÚkwargsrg   rh   ri   rŒ   r‘   r   r   r   Úload@  s    

r–   c             C   sp  d}t | ƒ}|jƒ }t|dƒr*|jdƒ}n
|jdƒ}|dk	rTt|ƒdkrTt|d ƒ}tjj	|ƒ}tjj
|ƒ}tjd|d�}	zÊ|dk	rŒtjƒ }
t|| ddd	d
��N}xF|jdƒ}t|ƒdkr¼P |	j|ƒ |dk	rØ|
j|ƒ |jt|ƒƒ q¤W W dQ R X |	jƒ  |dk	�r4|
jƒ }|dt|ƒ… |k�r4tdj||ƒƒ‚tj|	j|ƒ W d|	jƒ  tjj|	jƒ�rjtj|	jƒ X dS )aY  Download object at the given URL to a local path.

    Args:
        url (string): URL of the object to download
        dst (string): Full path where object will be saved, e.g. `/tmp/temporary_file`
        hash_prefix (string, optional): If not None, the SHA256 downloaded file should start with `hash_prefix`.
            Default: None
        progress (bool, optional): whether or not to display a progress bar to stderr
            Default: True

    Example:
        >>> torch.hub.download_url_to_file('https://s3.amazonaws.com/pytorch/models/resnet18-5c106cde.pth', '/tmp/temporary_file')

    NÚ
getheaderszContent-Lengthr   F)ÚdeleterF   ÚBTi   )r
   r   r   r   r   i    z,invalid hash value (expected "{}", got "{}"))r   ÚinfoÚhasattrr—   Úget_allr}   Úintr;   r6   rK   ÚdirnameÚtempfileÚNamedTemporaryFileÚhashlibÚsha256r	   Úreadr   r   ÚcloseÚ	hexdigestr~   r   r?   rf   r5   r<   r>   )rk   ÚdstÚhash_prefixr_   Ú	file_sizeÚuÚmetaÚcontent_lengthZdst_dirr‹   r¢   ZpbarÚbufferÚdigestr   r   r   r`   n  sD    






r`   c             C   s   t jdƒ t| |||ƒ d S )Nz·torch.hub._download_url_to_file has been renamed to            torch.hub.download_url_to_file to be a public API,            _download_url_to_file will be removed in after 1.3 release)rS   rT   r`   )rk   r¦   r§   r_   r   r   r   Ú_download_url_to_fileª  s    r®   c             C   sN  t jdƒrtjdƒ |dkr0tƒ }t jj|dƒ}yt j|ƒ W n2 tk
rp } z|j	t	j
kr^n‚ W Y dd}~X nX t| ƒ}t jj|jƒ}t jj||ƒ}	t jj|	ƒsÞtjjdj| |	ƒƒ |rÊtj|ƒjdƒnd}
t| |	|
|d� tj|	ƒ�r@tj|	ƒ�F}|jƒ }t|ƒdk�rtdƒ‚|j|ƒ |d	 j}t jj||ƒ}	W dQ R X tj|	|d
�S )a…  Loads the Torch serialized object at the given URL.

    If downloaded file is a zip file, it will be automatically
    decompressed.

    If the object is already present in `model_dir`, it's deserialized and
    returned.
    The default value of `model_dir` is ``$TORCH_HOME/checkpoints`` where
    environment variable ``$TORCH_HOME`` defaults to ``$XDG_CACHE_HOME/torch``.
    ``$XDG_CACHE_HOME`` follows the X Design Group specification of the Linux
    filesytem layout, with a default value ``~/.cache`` if not set.

    Args:
        url (string): URL of the object to download
        model_dir (string, optional): directory in which to save the object
        map_location (optional): a function or a dict specifying how to remap storage locations (see torch.load)
        progress (bool, optional): whether or not to display a progress bar to stderr.
            Default: True
        check_hash(bool, optional): If True, the filename part of the URL should follow the naming convention
            ``filename-<sha256>.ext`` where ``<sha256>`` is the first eight or more
            digits of the SHA256 hash of the contents of the file. The hash is used to
            ensure unique names and to verify the contents of the file.
            Default: False

    Example:
        >>> state_dict = torch.hub.load_state_dict_from_url('https://s3.amazonaws.com/pytorch/models/resnet18-5c106cde.pth')

    ZTORCH_MODEL_ZOOz@TORCH_MODEL_ZOO is deprecated, please use env TORCH_HOME insteadNZcheckpointszDownloading: "{}" to {}
é   )r_   z0Only one file(not dir) is allowed in the zipfiler   )Úmap_location) r;   rL   rS   rT   rR   r6   rN   rV   ÚOSErrorÚerrnoÚEEXISTr   Úbasenamer<   r   r   r   r   Ú
HASH_REGEXÚsearchÚgroupr`   ra   Ú
is_zipfilerb   rc   r}   r~   re   rd   rJ   r–   )rk   Z	model_dirr°   r_   Z
check_hashrQ   ÚeÚpartsrd   rj   r§   rl   ÚmembersZextraced_namer   r   r   Úload_state_dict_from_url°  s6    



r¼   )T)F)F)NT)NT)NNTF)4Ú
__future__r   r   r   r   r²   r¡   r;   Úrer?   r   rŸ   rJ   rS   ra   r*   r   Úurllib2r   Úurllib.requestÚurllib.parser	   rr   ÚobjectÚcompilerµ   rZ   rM   rO   rP   r|   rŽ   ZREAD_DATA_CHUNKrU   r:   rA   rE   rI   rR   rW   r]   rm   rx   r€   r‡   r‰   r�   r’   r–   r`   r®   r¼   r   r   r   r   Ú<module>   s\   


%'	
!
".
<
