o
    پi                     @   sb  d dl Z d dlmZ d dlmZmZmZmZ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 dd
lmZ ddlmZmZmZ g dZdedeee ef fddZd!dededefddZ								d"dededeeeeeef ef  deeeef  deeeef  deeeef  dee dee dee dedejfdd Z dS )#    N)Path)AnyDictOptionalTupleUnion)urlsplit)nn)set_layer_config   )load_checkpoint)load_model_config_from_hfload_model_config_from_path)PretrainedCfg)is_modelmodel_entrypointsplit_model_name_tag)parse_model_namesafe_model_namecreate_model
model_namereturnc                 C   sr   |  dr| dd} t| }|jdv sJ |jdkr!|j|jfS |jdkr,|j|jfS tj|jd } d| fS )z;Parse source and name from potentially prefixed model name.hf_hubhf-hub) r   	local-dirr   N)
startswithreplacer   schemepathossplit)r   parsed r$   H/home/ubuntu/.local/lib/python3.10/site-packages/timm/models/_factory.pyr      s   


r   Tremove_sourcec                 C   s*   dt dt fdd}|rt| d } || S )z)Return a filename / path safe model name.namer   c                 S   s   d dd | D dS )Nr   c                 s   s     | ]}|  r
|nd V  qdS )_N)isalnum).0cr$   r$   r%   	<genexpr>&   s    z5safe_model_name.<locals>.make_safe.<locals>.<genexpr>r(   )joinrstrip)r'   r$   r$   r%   	make_safe%   s   z"safe_model_name.<locals>.make_safer   )strr   )r   r&   r/   r$   r$   r%   r   #   s   r   F
pretrainedpretrained_cfgpretrained_cfg_overlaycheckpoint_path	cache_dir
scriptable
exportableno_jitkwargsc	                 K   s   dd |	  D }	t| \}
}|
rJ|rJ d|
dkr%t||d\}} }n|
dkr1t|\}} }nJ d|
 |rI|  D ]
\}}|	|| q>nt|\} }|rV|sV|}t| s`td	|  t| }t	|||d
 |d||||d|	}W d   n1 sw   Y  |rt
|| |S )a  Create a model.

    Lookup model's entrypoint function and pass relevant args to create a new model.

    Tip:
        **kwargs will be passed through entrypoint fn to ``timm.models.build_model_with_cfg()``
        and then the model class __init__(). kwargs values set to None are pruned before passing.

    Args:
        model_name: Name of model to instantiate.
        pretrained: If set to `True`, load pretrained ImageNet-1k weights.
        pretrained_cfg: Pass in an external pretrained_cfg for model.
        pretrained_cfg_overlay: Replace key-values in base pretrained_cfg with these.
        checkpoint_path: Path of checkpoint to load _after_ the model is initialized.
        cache_dir: Override model cache dir for Hugging Face Hub and Torch checkpoints.
        scriptable: Set layer config so that model is jit scriptable (not working for all models yet).
        exportable: Set layer config so that model is traceable / ONNX exportable (not fully impl/obeyed yet).
        no_jit: Set layer config so that model doesn't utilize jit scripted layers (so far activations only).

    Keyword Args:
        drop_rate (float): Classifier dropout rate for training.
        drop_path_rate (float): Stochastic depth drop rate for training.
        global_pool (str): Classifier global pooling type.

    Example:

    ```py
    >>> from timm import create_model

    >>> # Create a MobileNetV3-Large model with no pretrained weights.
    >>> model = create_model('mobilenetv3_large_100')

    >>> # Create a MobileNetV3-Large model with pretrained weights.
    >>> model = create_model('mobilenetv3_large_100', pretrained=True)
    >>> model.num_classes
    1000

    >>> # Create a MobileNetV3-Large model with pretrained weights and a new head with 10 classes.
    >>> model = create_model('mobilenetv3_large_100', pretrained=True, num_classes=10)
    >>> model.num_classes
    10

    >>> # Create a Dinov2 small model with pretrained weights and save weights in a custom directory.
    >>> model = create_model('vit_small_patch14_dinov2.lvd142m', pretrained=True, cache_dir="/data/my-models")
    >>> # Data will be stored at `/data/my-models/models--timm--vit_small_patch14_dinov2.lvd142m/`
    ```
    c                 S   s   i | ]\}}|d ur||qS )Nr$   )r*   kvr$   r$   r%   
<dictcomp>j   s    z create_model.<locals>.<dictcomp>zKpretrained_cfg should not be set when sourcing model from Hugging Face Hub.r   )r5   r   FzUnknown model_source zUnknown model (%s))r6   r7   r8   )r1   r2   r3   r5   Nr$   )itemsr   r   r   
setdefaultr   r   RuntimeErrorr   r
   r   )r   r1   r2   r3   r4   r5   r6   r7   r8   r9   model_sourcemodel_id
model_argsr:   r;   pretrained_tag	create_fnmodelr$   r$   r%   r   ,   sJ   >	
r   )T)FNNNNNNN)!r!   pathlibr   typingr   r   r   r   r   urllib.parser   torchr	   timm.layersr
   _helpersr   _hubr   r   _pretrainedr   	_registryr   r   r   __all__r0   r   boolr   Moduler   r$   r$   r$   r%   <module>   sX    	
