U
    ­Ê¹jF'  ã                   @   sÊ  d dl Z d dlZd dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d dlmZ d dlmZ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mZ dddddddgZeG dd„ dƒƒZG dd„ deƒZe edœdd„Z!eee f ee dœdd„Z"eee dœdd„Z#edej$d�Z%i Z&d$ee  eede%f gede%f f dœdd„Z'd%ee eee  e df eee  e df ee  dœd d„Z(e edej$f dœd!d„Z)e eej$d"œd#d„Z*dS )&é    N)Ú	dataclass)ÚEnum)Úpartial)Ú	signature)Ú
ModuleType)ÚAnyÚCallableÚDictÚIterableÚListÚMappingÚOptionalÚSetÚTypeÚTypeVarÚUnion)Únné   )Úload_state_dict_from_urlÚWeightsEnumÚWeightsÚ	get_modelÚget_model_builderÚget_model_weightsÚ
get_weightÚlist_modelsc                   @   sB   e Zd ZU dZeed< eed< eeef ed< ee	dœdd„Z
dS )	r   aß  
    This class is used to group important attributes associated with the pre-trained weights.

    Args:
        url (str): The location where we find the weights.
        transforms (Callable): A callable that constructs the preprocessing method (or validation preset transforms)
            needed to use the model. The reason we attach a constructor method rather than an already constructed
            object is because the specific object might have memory and thus we want to delay initialization until
            needed.
        meta (Dict[str, Any]): Stores meta-data related to the weights of the model and its configuration. These can be
            informative attributes (for example the number of parameters/flops, recipe link/methods used in training
            etc), configuration parameters (for example the `num_classes`) needed to construct the model or important
            meta-data (for example the `classes` of a classification model) needed to use the model.
    ÚurlÚ
transformsÚmeta)ÚotherÚreturnc                 C   s†   t |tƒstS | j|jkrdS | j|jkr.dS t | jtƒrvt |jtƒrv| jj|jjkot| jj|jjkot| jj	|jj	kS | j|jkS d S )NF)
Ú
isinstancer   ÚNotImplementedr   r   r   r   ÚfuncÚargsÚkeywords)Úselfr   © r'   úV/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/models/_api.pyÚ__eq__)   s    
ÿýzWeights.__eq__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚstrÚ__annotations__r   r	   r   Úboolr)   r'   r'   r'   r(   r      s
   
c                   @   sp   e Zd ZdZeeedœdd„ƒZeeeeef dœdd„Z	edœd	d
„Z
edd„ ƒZedd„ ƒZedd„ ƒZdS )r   aM  
    This class is the parent class of all model weights. Each model building method receives an optional `weights`
    parameter with its associated pre-trained weights. It inherits from `Enum` and its values should be of type
    `Weights`.

    Args:
        value (Weights): The data class entry with the weight information.
    )Úobjr    c                 C   sV   |d k	rRt |ƒtkr,| | | jd d¡ }n&t|| ƒsRtd| j› d|jj› d�ƒ‚|S )NÚ.Ú z(Invalid Weight class provided; expected z but received )Útyper.   Úreplacer*   r!   Ú	TypeErrorÚ	__class__)Úclsr1   r'   r'   r(   ÚverifyN   s    
ÿzWeightsEnum.verify)r$   Úkwargsr    c                 O   s   t | jf|ž|ŽS ©N)r   r   )r&   r$   r:   r'   r'   r(   Úget_state_dictY   s    zWeightsEnum.get_state_dict)r    c                 C   s   | j j› d| j› �S )Nr2   )r7   r*   Ú_name_©r&   r'   r'   r(   Ú__repr__\   s    zWeightsEnum.__repr__c                 C   s   | j jS r;   )Úvaluer   r>   r'   r'   r(   r   _   s    zWeightsEnum.urlc                 C   s   | j jS r;   )r@   r   r>   r'   r'   r(   r   c   s    zWeightsEnum.transformsc                 C   s   | j jS r;   )r@   r   r>   r'   r'   r(   r   g   s    zWeightsEnum.metaN)r*   r+   r,   r-   Úclassmethodr   r9   r   r.   r<   r?   Úpropertyr   r   r   r'   r'   r'   r(   r   D   s   	


)Únamer    c           	      C   sÖ   z|   d¡\}}W n$ tk
r6   td| › d�ƒ‚Y nX d tjt j  d¡dd… ¡}t |¡}|gdd„ t 	|tj
¡D ƒ }d}|D ],}|j |d¡}|dk	rˆt|tƒrˆ|} q¶qˆ|dkrÎtd|› d	�ƒ‚|| S )
zá
    Gets the weights enum value by its full name. Example: "ResNet50_Weights.IMAGENET1K_V1"

    Args:
        name (str): The name of the weight enum entry.

    Returns:
        WeightsEnum: The requested weight enum.
    r2   zInvalid weight name provided: 'ú'.Néÿÿÿÿc                 S   s$   g | ]}|d  j  d¡r|d  ‘qS )é   z__init__.py)Ú__file__Úendswith)Ú.0Úxr'   r'   r(   Ú
<listcomp>}   s   þzget_weight.<locals>.<listcomp>zThe weight enum 'z0' for the specific method couldn't be retrieved.)ÚsplitÚ
ValueErrorÚjoinÚsysÚmodulesr*   Ú	importlibÚimport_moduleÚinspectÚ
getmembersÚismoduleÚ__dict__ÚgetÚ
issubclassr   )	rC   Z	enum_nameZ
value_nameZbase_module_nameÚbase_moduleZmodel_modulesÚweights_enumÚmZpotential_classr'   r'   r(   r   l   s$    
 

þc                 C   s   t | tƒrt| ƒn| }t|ƒS )a  
    Returns the weights enum class associated to the given model.

    Args:
        name (callable or str): The model builder function or the name under which it is registered.

    Returns:
        weights_enum (WeightsEnum): The weights enum class associated with the model.
    )r!   r.   r   Ú_get_enum_from_fn)rC   Úmodelr'   r'   r(   r   �   s    
©Úfnr    c                 C   s„   t | ƒ}d|jkrtdƒ‚t | ƒjd j}d}t|tƒrHt|tƒrH|}n(|jD ] }t|tƒrNt|tƒrN|} qpqN|dkr€tdƒ‚|S )zã
    Internal method that gets the weight enum of a specific model builder method.

    Args:
        fn (Callable): The builder method used to create the model.
    Returns:
        WeightsEnum: The requested weight enum.
    Úweightsz-The method is missing the 'weights' argument.NzjThe WeightsEnum class for the specific method couldn't be retrieved. Make sure the typing info is correct.)	r   Ú
parametersrM   Ú
annotationr!   r4   rX   r   Ú__args__)r_   ÚsigÚannrZ   Útr'   r'   r(   r\   ž   s     	

ÿr\   ÚM)Úbound.c                    s(   t dtf t dtf dœ‡ fdd„}|S )N.r^   c                    s6   ˆ d k	rˆ n| j }|tkr*td|› d�ƒ‚| t|< | S )Nz/An entry is already registered under the name 'rD   )r*   ÚBUILTIN_MODELSrM   )r_   Úkey©rC   r'   r(   ÚwrapperÅ   s
    zregister_model.<locals>.wrapper)r   rg   )rC   rl   r'   rk   r(   Úregister_modelÄ   s    $rm   )ÚmoduleÚincludeÚexcluder    c                    sŽ   ‡ fdd„t  ¡ D ƒ}|rPtƒ }t|tƒr0|g}|D ]}|tt ||¡ƒB }q4n|}|r†t|tƒrh|g}|D ]}|tt ||¡ƒ }qlt|ƒS )a§  
    Returns a list with the names of registered models.

    Args:
        module (ModuleType, optional): The module from which we want to extract the available models.
        include (str or Iterable[str], optional): Filter(s) for including the models from the set of all models.
            Filters are passed to `fnmatch <https://docs.python.org/3/library/fnmatch.html>`__ to match Unix shell-style
            wildcards. In case of many filters, the results is the union of individual filters.
        exclude (str or Iterable[str], optional): Filter(s) applied after include_filters to remove models.
            Filter are passed to `fnmatch <https://docs.python.org/3/library/fnmatch.html>`__ to match Unix shell-style
            wildcards. In case of many filters, the results is removal of all the models that match any individual filter.

    Returns:
        models (list): A list with the names of available models.
    c                    s4   h | ],\}}ˆ d ks,|j  dd¡d ˆ jkr|’qS )Nr2   rF   r   )r+   Úrsplitr*   )rI   ÚkÚv©rn   r'   r(   Ú	<setcomp>ã   s      zlist_models.<locals>.<setcomp>)ri   ÚitemsÚsetr!   r.   ÚfnmatchÚfilterÚsorted)rn   ro   rp   Z
all_modelsÚmodelsZinclude_filterZexclude_filterr'   rt   r(   r   Ï   s     
ÿ

c                 C   s<   |   ¡ } zt|  }W n" tk
r6   td| › �ƒ‚Y nX |S )zÏ
    Gets the model name and returns the model builder method.

    Args:
        name (str): The name under which the model is registered.

    Returns:
        fn (Callable): The model builder method.
    zUnknown model )Úlowerri   ÚKeyErrorrM   )rC   r_   r'   r'   r(   r   ÷   s    
)rC   Úconfigr    c                 K   s   t | ƒ}|f |ŽS )a&  
    Gets the model name and configuration and returns an instantiated model.

    Args:
        name (str): The name under which the model is registered.
        **config (Any): parameters passed to the model builder method.

    Returns:
        model (nn.Module): The initialized model.
    )r   )rC   r~   r_   r'   r'   r(   r   	  s    )N)NNN)+rx   rQ   rS   rO   Údataclassesr   Úenumr   Ú	functoolsr   r   Útypesr   Útypingr   r   r	   r
   r   r   r   r   r   r   r   Ztorchr   Z_internally_replaced_utilsr   Ú__all__r   r   r.   r   r   r\   ÚModulerg   ri   rm   r   r   r   r'   r'   r'   r(   Ú<module>   s@   4/($!0   ýü(