
    kh                        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Zddededef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Z y)    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                 z   | j                  d      r| j                  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                  |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   parseds     P/var/www/teggl/fontify/venv/lib/python3.12/site-packages/timm/models/_factory.pyr   r      s    X&''(;
j!F==7777}} }}fkk))	+	%}}fkk))WW]]6;;/3
Z    remove_sourcec                 R    dt         dt         fd}|rt        |       d   }  ||       S )z)Return a filename / path safe model name.namer   c                 P    dj                  d | D              j                  d      S )Nr   c              3   D   K   | ]  }|j                         r|nd   yw)_N)isalnum).0cs     r&   	<genexpr>z5safe_model_name.<locals>.make_safe.<locals>.<genexpr>&   s     ?QAIIKqS0?s    r-   )joinrstrip)r*   s    r&   	make_safez"safe_model_name.<locals>.make_safe%   s"    ww?$??FFsKKr'   r   )strr   )r   r(   r4   s      r&   r   r   #   s5    L L L%j1"5
Z  r'   
pretrainedpretrained_cfgpretrained_cfg_overlaycheckpoint_path	cache_dir
scriptable
exportableno_jitkwargsc	           	      B   |	j                         D 
ci c]  \  }
}|	|
| }	}
}t        |       \  }}|rl|rJ d       |dk(  rt        ||      \  }} }n|dk(  rt        |      \  }} }n
J d|        |r?|j                         D ]  \  }
}|	j	                  |
|        nt        |      \  } }|r|s|}t        |       st        d| z        t        |       }t        |||      5   |d
||||d	|	}ddd       |rt        |       S c c}}
w # 1 sw Y   xY w)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/`
    ```
    NzKpretrained_cfg should not be set when sourcing model from Hugging Face Hub.r   )r:   r   zUnknown model_source zUnknown model (%s))r;   r<   r=   )r6   r7   r8   r:    )itemsr   r   r   
setdefaultr   r   RuntimeErrorr   r   r   )r   r6   r7   r8   r9   r:   r;   r<   r=   r>   kvmodel_sourcemodel_id
model_argspretrained_tag	create_fnmodels                     r&   r   r   ,   sn   |  &||~?tq!ad?F?-j9L(!p#pp!8# 6O#62NJ
 [(5P62NJ
 A1,@@5"((* (1!!!Q'( &:(%C"
N.+NJ/*<== ,I	ZJv	V 
 
!)#9	

 

 /LW @>
 
s   
DD)DD)T)FNNNNNNN)!r#   pathlibr   typingr   r   r   r   r   urllib.parser	   torchr
   timm.layersr   _helpersr   _hubr   r   _pretrainedr   	_registryr   r   r   __all__r5   r   boolr   Moduler   r@   r'   r&   <module>rX      sS   	  4 4 !  ( % H & G G B   x}c/A)B  "! !D !C ! !NR;?6:04%)%)!%iii !sDcNM'I!JKi !)c3h 8	i
 "%T	"23i E#t),-i TNi TNi i i YYir'   