o
    oiP                     @  s   d dl mZ d dlmZmZ d dlmZ d dlZd dlm	Z	 d dl
mZ d dlmZ ddddZeG dd dZG dd de	e ZdS )    )annotations)	dataclassfield)LiteralN)	ModelBasebackbone)Tensorb1   
model_typeLiteral['b1', 'b2', 'b3']
resolutionLiteral[224, 256, 288]returnstrc              	   C  s   d|  d| d|  d| d	S )z)Return the base URL of the model weights.z4https://huggingface.co/kornia/efficientvit_imagenet__rz/resolve/main/z-rz.pt r   r   r   r   ]/home/ubuntu/.local/lib/python3.10/site-packages/kornia/contrib/models/efficient_vit/model.py_get_base_url   s   r   c                   @  s2   e Zd ZU dZeedZded< edd
dZ	dS )EfficientViTConfiga  Configuration to construct EfficientViT model.

    Model weights can be loaded from a checkpoint URL or local path.
    The model weights are hosted on HuggingFace's model hub: https://huggingface.co/kornia.

    Args:
        checkpoint: URL or local path of model weights.

    )default_factoryr   
checkpointr   r   r   r   r   c                 C  s   | t ||ddS )zReturn a configuration object from a pre-trained model.

        Args:
            model_type: model type, one of :obj:`"b1"`, :obj:`"b2"`, :obj:`"b3"`.
            resolution: input resolution, one of :obj:`224`, :obj:`256`, :obj:`288`.

        r   )r   )r   )clsr   r   r   r   r   from_pretrained1   s   z"EfficientViTConfig.from_pretrainedN)r   r   r   r   r   r   )
__name__
__module____qualname____doc__r   r   r   __annotations__classmethodr   r   r   r   r   r   #   s
   
 
r   c                      s:   e Zd ZdZd fddZedd
dZdddZ  ZS )EfficientViTzEfficientViT backbone model.r   8vit.EfficientViTBackbone | vit.EfficientViTLargeBackboner   Nonec                   s   t    || _d S )N)super__init__r   )selfr   	__class__r   r   r&   B   s   

zEfficientViT.__init__configr   c                 C  s   zt jj| jdd}d|v r|d n|}W n ty&   td| j ddw | jdd }|d	d
 }|dvrBtd| dttd|  }|j	|dd t
|dS )zBuild the EfficientViT model from a configuration object.

        Args:
            config: EfficientViT configuration object. See :class:`EfficientViTConfig`.

        Returns:
            EfficientViT: the EfficientViT model.

        cpu)map_location
state_dictzUnable to load the model from .N/-r   )b0r
   b2b3l0l1l2l3zUnknown model type: efficientvit_backbone_F)strictr   )torchhubload_state_dict_from_urlr   RuntimeErrorsplit
ValueErrorgetattrvitload_state_dictr"   )r*   
model_file	file_namer   modelr   r   r   from_configF   s   
zEfficientViT.from_configimagesr	   c                 C  s   |  |}|S )zExtract features from the input images.

        Args:
            images: input images tensor of shape :math:`(B, C, H, W)`.

        Returns:
            Dict[str, Tensor]: a dictionary containing the features.

        r   )r'   rH   featsr   r   r   forwardd   s   

zEfficientViT.forward)r   r#   r   r$   )r*   r   r   r"   )rH   r	   r   r	   )	r   r   r   r   r&   staticmethodrG   rJ   __classcell__r   r   r(   r   r"   ?   s    r"   )r
   r   )r   r   r   r   r   r   )
__future__r   dataclassesr   r   typingr   r;   kornia.contrib.models.baser   #kornia.contrib.models.efficient_vitr   rB   kornia.corer	   r   r   r"   r   r   r   r   <module>   s   