U
    (do                     @   s   d dl Z d dlZd dlZd dlmZmZ d dlmZ d dlmZm	Z	m
Z
mZ d dlmZ ddlmZ 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dddZdS )    N)	dataclassfields)	signature)AnyCallableDictcast)StrEnum   )load_state_dict_from_urlWeightsEnumWeights
get_weightc                   @   s2   e Zd ZU dZeed< eed< eeef ed< 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.
    urlZ
transformsmetaN)	__name__
__module____qualname____doc__str__annotations__r   r   r    r   r   ;/tmp/pip-unpacked-wheel-vx7f76es/torchvision/models/_api.pyr      s   
c                       sh   e Zd ZdZedddZeeedddZe	e
eef dd	d
ZedddZ fddZ  Z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.
    )valuec                 C   s
   || _ d S N)_value_)selfr   r   r   r   __init__0   s    zWeightsEnum.__init__)objreturnc                 C   sX   |d k	rTt |tkr.| || jd d}n&t|| sTtd| j d|jj d|S )N. z(Invalid Weight class provided; expected z but received )typer   from_strreplacer   
isinstance	TypeError	__class__)clsr   r   r   r   verify3   s    
zWeightsEnum.verify)progressr   c                 C   s   t | j|dS )N)r*   )r   r   )r   r*   r   r   r   get_state_dict>   s    zWeightsEnum.get_state_dict)r   c                 C   s   | j j d| j S )Nr    )r'   r   _name_)r   r   r   r   __repr__A   s    zWeightsEnum.__repr__c                    s6   t tD ] }|j|krt| j|  S qt |S r   )r   r   nameobject__getattribute__r   super__getattr__)r   r.   fr'   r   r   r2   D   s    
zWeightsEnum.__getattr__)r   r   r   r   r   r   classmethodr   r)   boolr   r   r+   r-   r2   __classcell__r   r   r4   r   r   &   s   	
)r.   r   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	rt|tr|} qq|dkrtd| d	||S )
z
    Gets the weight 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.
    r    zInvalid weight name provided: 'z'.Nc                 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   r#   )	r.   Z	enum_nameZ
value_nameZbase_module_namebase_moduleZmodel_modulesweights_enummZpotential_classr   r   r   r   L   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rtdt	t|S )az  
    Internal method that gets the weight enum of a specific model builder method.
    Might be removed after the handle_legacy_interface is removed.

    Args:
        fn (Callable): The builder method used to create the model.
        weight_name (str): The name of the weight enum entry of the specific 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   
parametersr@   
annotationr%   r"   rK   r   __args__r   )rO   sigannrM   tr   r   r   _get_enum_from_fnn   s     

rW   )rD   rF   rB   Zdataclassesr   r   r   typingr   r   r   r   Ztorchvision._utilsr	   Z_internally_replaced_utilsr   __all__r   r   r   r   rW   r   r   r   r   <module>   s   
&"