U
    Ð‹Ñh5}  ã                   @   s^  d dl 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 d dlZd dlZd dlm  mZ d dlmZmZ d dlmZmZmZ d dlmZ d dlmZmZ d d	lmZmZ d d
l m!Z! d dl"m#Z#m$Z$ d dl%m&Z& dddgZ'ee(e(f e(e(e(ee(e(f dœdd„Z)ee(e(f e(eee(e(f  dœdd„Z*e(e(ejdœdd„Z+G dd„ dej,ƒZ-G dd„ dej,ƒZ.G dd„ dej,ƒZ/G dd „ d ej,ƒZ0G d!d"„ d"ej,ƒZ1G d#d$„ d$ej,ƒZ2G d%d&„ d&ej,ƒZ3G d'd(„ d(ej,ƒZ4G d)d„ dej,ƒZ5d5e(ee( ee( e6e(e(e	e e7ee5d+œ
d,d-„Z8G d.d„ deƒZ9eƒ ed/e9j:fd0�dd1d2œe	e9 e7ee5d3œd4d„ƒƒZ;dS )6é    N)ÚOrderedDict)Úpartial)ÚAnyÚCallableÚListÚOptionalÚSequenceÚTuple)ÚnnÚTensor)Úregister_modelÚWeightsÚWeightsEnum)Ú_IMAGENET_CATEGORIES)Ú_ovewrite_named_paramÚhandle_legacy_interface)ÚConv2dNormActivationÚSqueezeExcitation)ÚStochasticDepth)ÚImageClassificationÚInterpolationMode)Ú_log_api_usage_onceÚMaxVitÚMaxVit_T_WeightsÚmaxvit_t)Ú
input_sizeÚkernel_sizeÚstrideÚpaddingÚreturnc                 C   s8   | d | d|  | d | d | d|  | d fS )Nr   é   é   © )r   r   r   r   r"   r"   úG/tmp/user/2202/pip-unpacked-wheel-mjxxo1pc/torchvision/models/maxvit.pyÚ_get_conv_output_shape   s    þr$   )r   Ún_blocksr   c                 C   s<   g }t | dddƒ}t|ƒD ]}t |dddƒ}| |¡ q|S )zQUtil function to check that the input size is correct for a MaxVit configuration.é   r    r!   )r$   ÚrangeÚappend)r   r%   ZshapesZblock_input_shapeÚ_r"   r"   r#   Ú_make_block_input_shapes    s    r*   )ÚheightÚwidthr   c                 C   sÞ   t  t  t  | ¡t  |¡g¡¡}t  |d¡}|d d …d d …d f |d d …d d d …f  }| ddd¡ ¡ }|d d …d d …df  | d 7  < |d d …d d …df  |d 7  < |d d …d d …df  d| d 9  < | d¡S )Nr!   r    r   éÿÿÿÿ)ÚtorchÚstackZmeshgridZarangeÚflattenÚpermuteÚ
contiguousÚsum)r+   r,   ZcoordsZcoords_flatZrelative_coordsr"   r"   r#   Ú_get_relative_position_index*   s     ,""&r4   c                       s\   e Zd ZdZdeeeeeedejf edejf eddœ	‡ fdd„Z	e
e
dœd	d
„Z‡  ZS )ÚMBConva=  MBConv: Mobile Inverted Residual Bottleneck.

    Args:
        in_channels (int): Number of input channels.
        out_channels (int): Number of output channels.
        expansion_ratio (float): Expansion ratio in the bottleneck.
        squeeze_ratio (float): Squeeze ratio in the SE Layer.
        stride (int): Stride of the depthwise convolution.
        activation_layer (Callable[..., nn.Module]): Activation function.
        norm_layer (Callable[..., nn.Module]): Normalization function.
        p_stochastic_dropout (float): Probability of stochastic depth.
    ç        .N)	Úin_channelsÚout_channelsÚexpansion_ratioÚsqueeze_ratior   Úactivation_layerÚ
norm_layerÚp_stochastic_dropoutr   c	                    s*  t ƒ  ¡  |  |dkp||k}	|	rdtj||dddd�g}
|dkrVtjd|dd�g|
 }
tj|
Ž | _n
t ¡ | _t|| ƒ}t|| ƒ}|ršt	|dd�| _
n
t ¡ | _
tƒ }||ƒ|d	< t||ddd
||d d�|d< t||d|d|||d d�	|d< t||tjd�|d< tj||ddd�|d< t |¡| _d S )Nr!   T)r   r   Úbiasr    r&   ©r   r   r   Úrow©ÚmodeZpre_normr   )r   r   r   r;   r<   ÚinplaceZconv_a)r   r   r   r;   r<   ÚgroupsrC   Zconv_b)Z
activationZsqueeze_excitation)r7   r8   r   r>   Zconv_c)ÚsuperÚ__init__r
   ÚConv2dZ	AvgPool2dÚ
SequentialÚprojÚIdentityÚintr   Ústochastic_depthr   r   r   ZSiLUÚlayers)Úselfr7   r8   r9   r:   r   r;   r<   r=   Zshould_projrI   Zmid_channelsZsqz_channelsZ_layers©Ú	__class__r"   r#   rF   C   sP    


ø

÷
zMBConv.__init__©Úxr   c                 C   s"   |   |¡}|  |  |¡¡}|| S )zÍ
        Args:
            x (Tensor): Input tensor with expected layout of [B, C, H, W].
        Returns:
            Tensor: Output tensor with expected layout of [B, C, H / stride, W / stride].
        )rI   rL   rM   ©rN   rR   Úresr"   r"   r#   Úforward€   s    
zMBConv.forward)r6   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rK   Úfloatr   r
   ÚModulerF   r   rU   Ú__classcell__r"   r"   rO   r#   r5   5   s    ÷ö=r5   c                       sL   e Zd ZdZeeeddœ‡ fdd„Zejdœdd„Zeed	œd
d„Z	‡  Z
S )Ú$RelativePositionalMultiHeadAttentionzÔRelative Positional Multi-Head Attention.

    Args:
        feat_dim (int): Number of input features.
        head_dim (int): Number of features per head.
        max_seq_len (int): Maximum sequence length.
    N)Úfeat_dimÚhead_dimÚmax_seq_lenr   c                    sð   t ƒ  ¡  || dkr*td|› d|› �ƒ‚|| | _|| _tt |¡ƒ| _|| _	t
 || j| j d ¡| _|d | _t
 | j| j |¡| _t
j tjd| j d d| j d  | jftjd�¡| _|  d	t| j| jƒ¡ tj
jj| jd
d� d S )Nr   z
feat_dim: z  must be divisible by head_dim: r&   g      à¿r    r!   )ZdtypeÚrelative_position_indexç{®Gáz”?©Zstd)rE   rF   Ú
ValueErrorÚn_headsr_   rK   ÚmathÚsqrtÚsizer`   r
   ÚLinearÚto_qkvÚscale_factorÚmergeZ	parameterÚ	Parameterr.   ÚemptyZfloat32Úrelative_position_bias_tableZregister_bufferr4   ÚinitZtrunc_normal_)rN   r^   r_   r`   rO   r"   r#   rF   •   s    


,ÿz-RelativePositionalMultiHeadAttention.__init__©r   c                 C   s@   | j  d¡}| j|  | j| jd¡}| ddd¡ ¡ }| d¡S )Nr-   r    r   r!   )ra   Úviewro   r`   r1   r2   Z	unsqueeze)rN   Z
bias_indexZrelative_biasr"   r"   r#   Úget_relative_positional_bias±   s    zARelativePositionalMultiHeadAttention.get_relative_positional_biasrQ   c                 C   s  |j \}}}}| j| j }}|  |¡}tj|ddd�\}	}
}|	 |||||¡ ddddd¡}	|
 |||||¡ ddddd¡}
| |||||¡ ddddd¡}|
| j }
t 	d|	|
¡}|  
¡ }tj|| dd�}t 	d	||¡}| ddddd¡ ||||¡}|  |¡}|S )
z»
        Args:
            x (Tensor): Input tensor with expected layout of [B, G, P, D].
        Returns:
            Tensor: Output tensor with expected layout of [B, G, P, D].
        r&   r-   )Zdimr   r!   r    é   z!B G H I D, B G H J D -> B G H I Jz!B G H I J, B G H J D -> B G H I D)Úshapere   r_   rj   r.   ÚchunkÚreshaper1   rk   Zeinsumrs   ÚFZsoftmaxrl   )rN   rR   ÚBÚGÚPÚDÚHZDHZqkvÚqÚkÚvZdot_prodZpos_biasÚoutr"   r"   r#   rU   ·   s    
   

z,RelativePositionalMultiHeadAttention.forward)rV   rW   rX   rY   rK   rF   r.   r   rs   rU   r\   r"   r"   rO   r#   r]   Œ   s   
ûr]   c                       s>   e Zd ZdZeeddœ‡ fdd„Zejejdœdd„Z‡  Z	S )	ÚSwapAxeszPermute the axes of a tensor.N)ÚaÚbr   c                    s   t ƒ  ¡  || _|| _d S ©N)rE   rF   rƒ   r„   )rN   rƒ   r„   rO   r"   r#   rF   Ø   s    
zSwapAxes.__init__rQ   c                 C   s   t  || j| j¡}|S r…   )r.   Zswapaxesrƒ   r„   rS   r"   r"   r#   rU   Ý   s    zSwapAxes.forward)
rV   rW   rX   rY   rK   rF   r.   r   rU   r\   r"   r"   rO   r#   r‚   Õ   s   r‚   c                       s8   e Zd ZdZddœ‡ fdd„Zeeedœdd„Z‡  ZS )	ÚWindowPartitionzB
    Partition the input tensor into non-overlapping windows.
    Nrq   c                    s   t ƒ  ¡  d S r…   ©rE   rF   ©rN   rO   r"   r#   rF   ç   s    zWindowPartition.__init__)rR   Úpr   c                 C   sf   |j \}}}}|}| |||| ||| |¡}| dddddd¡}| ||| ||  || |¡}|S )zï
        Args:
            x (Tensor): Input tensor with expected layout of [B, C, H, W].
            p (int): Number of partitions.
        Returns:
            Tensor: Output tensor with expected layout of [B, H/P, W/P, P*P, C].
        r   r    rt   r&   é   r!   ©ru   rw   r1   )rN   rR   r‰   ry   ÚCr}   ÚWr{   r"   r"   r#   rU   ê   s     zWindowPartition.forward©	rV   rW   rX   rY   rF   r   rK   rU   r\   r"   r"   rO   r#   r†   â   s   r†   c                       s<   e Zd ZdZddœ‡ fdd„Zeeeeedœdd„Z‡  ZS )	ÚWindowDepartitionzo
    Departition the input tensor of non-overlapping windows into a feature volume of layout [B, C, H, W].
    Nrq   c                    s   t ƒ  ¡  d S r…   r‡   rˆ   rO   r"   r#   rF     s    zWindowDepartition.__init__)rR   r‰   Úh_partitionsÚw_partitionsr   c                 C   s`   |j \}}}}|}	|| }
}| ||
||	|	|¡}| dddddd¡}| |||
|	 ||	 ¡}|S )ar  
        Args:
            x (Tensor): Input tensor with expected layout of [B, (H/P * W/P), P*P, C].
            p (int): Number of partitions.
            h_partitions (int): Number of vertical partitions.
            w_partitions (int): Number of horizontal partitions.
        Returns:
            Tensor: Output tensor with expected layout of [B, C, H, W].
        r   rŠ   r!   r&   r    rt   r‹   )rN   rR   r‰   r�   r‘   ry   rz   ZPPrŒ   r{   ZHPZWPr"   r"   r#   rU     s    

zWindowDepartition.forwardrŽ   r"   r"   rO   r#   r�   ü   s   r�   c                       sh   e Zd ZdZeeeeeeef eedej	f edej	f e
e
e
ddœ‡ fdd„Zeedœdd	„Z‡  ZS )
ÚPartitionAttentionLayera®  
    Layer for partitioning the input tensor into non-overlapping windows and applying attention to each window.

    Args:
        in_channels (int): Number of input channels.
        head_dim (int): Dimension of each attention head.
        partition_size (int): Size of the partitions.
        partition_type (str): Type of partitioning to use. Can be either "grid" or "window".
        grid_size (Tuple[int, int]): Size of the grid to partition the input tensor into.
        mlp_ratio (int): Ratio of the  feature size expansion in the MLP layer.
        activation_layer (Callable[..., nn.Module]): Activation function to use.
        norm_layer (Callable[..., nn.Module]): Normalization function to use.
        attention_dropout (float): Dropout probability for the attention layer.
        mlp_dropout (float): Dropout probability for the MLP layer.
        p_stochastic_dropout (float): Probability of dropping out a partition.
    .N)r7   r_   Úpartition_sizeÚpartition_typeÚ	grid_sizeÚ	mlp_ratior;   r<   Úattention_dropoutÚmlp_dropoutr=   r   c              	      s(  t ƒ  ¡  || | _|| _|d | | _|| _|| _|dkrDtdƒ‚|dkr^|| j | _| _	n| j| | _| _	t
ƒ | _tƒ | _|dkr�tddƒnt ¡ | _|dkr¬tddƒnt ¡ | _t ||ƒt|||d ƒt |	¡¡| _t t |¡t ||| ¡|ƒ t || |¡t |
¡¡| _t|d	d
�| _d S )Nr   )ÚgridÚwindowz0partition_type must be either 'grid' or 'window'rš   r™   éþÿÿÿéýÿÿÿr    r@   rA   )rE   rF   re   r_   Zn_partitionsr”   r•   rd   r‰   Úgr†   Úpartition_opr�   Údepartition_opr‚   r
   rJ   Úpartition_swapÚdepartition_swaprH   r]   ZDropoutÚ
attn_layerÚ	LayerNormri   Ú	mlp_layerr   Ústochastic_dropout)rN   r7   r_   r“   r”   r•   r–   r;   r<   r—   r˜   r=   rO   r"   r#   rF   ,  s8    

û	û	z PartitionAttentionLayer.__init__rQ   c                 C   s¾   | j d | j | j d | j  }}t | j d | j dkoL| j d | j dkd | j | j¡¡ |  || j¡}|  |¡}||  |  |¡¡ }||  |  	|¡¡ }|  
|¡}|  || j||¡}|S )z»
        Args:
            x (Tensor): Input tensor with expected layout of [B, C, H, W].
        Returns:
            Tensor: Output tensor with expected layout of [B, C, H, W].
        r   r!   z[Grid size must be divisible by partition size. Got grid size of {} and partition size of {})r•   r‰   r.   Z_assertÚformatrž   r    r¥   r¢   r¤   r¡   rŸ   )rN   rR   ZghZgwr"   r"   r#   rU   f  s    
"& ÿþ

zPartitionAttentionLayer.forward)rV   rW   rX   rY   rK   Ústrr	   r   r
   r[   rZ   rF   r   rU   r\   r"   r"   rO   r#   r’     s   
ð:r’   c                       sn   e Zd ZdZeeeeeedejf edejf eeeeeee	eef ddœ‡ fdd„Z
eedœdd	„Z‡  ZS )
ÚMaxVitLayera  
    MaxVit layer consisting of a MBConv layer followed by a PartitionAttentionLayer with `window` and a PartitionAttentionLayer with `grid`.

    Args:
        in_channels (int): Number of input channels.
        out_channels (int): Number of output channels.
        expansion_ratio (float): Expansion ratio in the bottleneck.
        squeeze_ratio (float): Squeeze ratio in the SE Layer.
        stride (int): Stride of the depthwise convolution.
        activation_layer (Callable[..., nn.Module]): Activation function.
        norm_layer (Callable[..., nn.Module]): Normalization function.
        head_dim (int): Dimension of the attention heads.
        mlp_ratio (int): Ratio of the MLP layer.
        mlp_dropout (float): Dropout probability for the MLP layer.
        attention_dropout (float): Dropout probability for the attention layer.
        p_stochastic_dropout (float): Probability of stochastic depth.
        partition_size (int): Size of the partitions.
        grid_size (Tuple[int, int]): Size of the input feature grid.
    .N)r7   r8   r:   r9   r   r<   r;   r_   r–   r˜   r—   r=   r“   r•   r   c                    s„   t ƒ  ¡  tƒ }t||||||||d�|d< t|||d||	|tj||
|d�|d< t|||d||	|tj||
|d�|d< t |¡| _d S )N)r7   r8   r9   r:   r   r;   r<   r=   ZMBconvrš   )r7   r_   r“   r”   r•   r–   r;   r<   r—   r˜   r=   Zwindow_attentionr™   Zgrid_attention)	rE   rF   r   r5   r’   r
   r£   rH   rM   )rN   r7   r8   r:   r9   r   r<   r;   r_   r–   r˜   r—   r=   r“   r•   rM   rO   r"   r#   rF   —  sN    
ø
õ
õ
zMaxVitLayer.__init__rQ   c                 C   s   |   |¡}|S ©z�
        Args:
            x (Tensor): Input tensor of shape (B, C, H, W).
        Returns:
            Tensor: Output tensor of shape (B, C, H, W).
        ©rM   )rN   rR   r"   r"   r#   rU   Ø  s    
zMaxVitLayer.forward)rV   rW   rX   rY   rK   rZ   r   r
   r[   r	   rF   r   rU   r\   r"   r"   rO   r#   r¨   ‚  s$   
ìAr¨   c                       sr   e Zd ZdZeeeeedejf edejf eeeeee	eef ee
e ddœ‡ fdd„Zeedœdd	„Z‡  ZS )
ÚMaxVitBlocka(  
    A MaxVit block consisting of `n_layers` MaxVit layers.

     Args:
        in_channels (int): Number of input channels.
        out_channels (int): Number of output channels.
        expansion_ratio (float): Expansion ratio in the bottleneck.
        squeeze_ratio (float): Squeeze ratio in the SE Layer.
        activation_layer (Callable[..., nn.Module]): Activation function.
        norm_layer (Callable[..., nn.Module]): Normalization function.
        head_dim (int): Dimension of the attention heads.
        mlp_ratio (int): Ratio of the MLP layer.
        mlp_dropout (float): Dropout probability for the MLP layer.
        attention_dropout (float): Dropout probability for the attention layer.
        p_stochastic_dropout (float): Probability of stochastic depth.
        partition_size (int): Size of the partitions.
        input_grid_size (Tuple[int, int]): Size of the input feature grid.
        n_layers (int): Number of layers in the block.
        p_stochastic (List[float]): List of probabilities for stochastic depth for each layer.
    .N)r7   r8   r:   r9   r<   r;   r_   r–   r˜   r—   r“   Úinput_grid_sizeÚn_layersÚp_stochasticr   c                    s¬   t ƒ  ¡  t|ƒ|ks,td|› d|› d�ƒ‚t ¡ | _t|dddd�| _t	|ƒD ]V\}}|dkrddnd}|  jt
|dkr||n||||||||||	|
|| j|d	�g7  _qPd S )
Nz'p_stochastic must have length n_layers=z, got p_stochastic=Ú.r&   r    r!   r?   r   )r7   r8   r:   r9   r   r<   r;   r_   r–   r˜   r—   r“   r•   r=   )rE   rF   Úlenrd   r
   Ú
ModuleListrM   r$   r•   Ú	enumerater¨   )rN   r7   r8   r:   r9   r<   r;   r_   r–   r˜   r—   r“   r¬   r­   r®   Úidxr‰   r   rO   r"   r#   rF   ù  s2    

òÿzMaxVitBlock.__init__rQ   c                 C   s   | j D ]}||ƒ}q|S r©   rª   )rN   rR   Zlayerr"   r"   r#   rU   ,  s    

zMaxVitBlock.forward)rV   rW   rX   rY   rK   rZ   r   r
   r[   r	   r   rF   r   rU   r\   r"   r"   rO   r#   r«   ã  s$   
ë3r«   c                       s˜   e Zd ZdZdejddddddfeeef eeee ee ee	e
edejf  edejf e	e	ee	e	eddœ‡ fd	d
„Zeedœdd„Zdd„ Z‡  ZS )r   ay  
    Implements MaxVit Transformer from the `MaxViT: Multi-Axis Vision Transformer <https://arxiv.org/abs/2204.01697>`_ paper.
    Args:
        input_size (Tuple[int, int]): Size of the input image.
        stem_channels (int): Number of channels in the stem.
        partition_size (int): Size of the partitions.
        block_channels (List[int]): Number of channels in each block.
        block_layers (List[int]): Number of layers in each block.
        stochastic_depth_prob (float): Probability of stochastic depth. Expands to a list of probabilities for each layer that scales linearly to the specified value.
        squeeze_ratio (float): Squeeze ratio in the SE Layer. Default: 0.25.
        expansion_ratio (float): Expansion ratio in the MBConv bottleneck. Default: 4.
        norm_layer (Callable[..., nn.Module]): Normalization function. Default: None (setting to None will produce a `BatchNorm2d(eps=1e-3, momentum=0.01)`).
        activation_layer (Callable[..., nn.Module]): Activation function Default: nn.GELU.
        head_dim (int): Dimension of the attention heads.
        mlp_ratio (int): Expansion ratio of the MLP layer. Default: 4.
        mlp_dropout (float): Dropout probability for the MLP layer. Default: 0.0.
        attention_dropout (float): Dropout probability for the attention layer. Default: 0.0.
        num_classes (int): Number of classes. Default: 1000.
    Ng      Ð?rt   r6   iè  .)r   Ústem_channelsr“   Úblock_channelsÚblock_layersr_   Ústochastic_depth_probr<   r;   r:   r9   r–   r˜   r—   Únum_classesr   c                    sÔ  t ƒ  ¡  t| ƒ d}|d kr.ttjddd�}t|t|ƒƒ}t|ƒD ]J\}}|d | dksl|d | dkrDt	d|› d|› d	|› d
|› d�	ƒ‚qDt 
t||dd||	dd d�t||ddd d dd�¡| _t|dddd�}|| _t ¡ | _|g|d d…  }|}t d|t|ƒ¡ ¡ }d}t|||ƒD ]X\}}}| j t|||
|||	||||||||||| … d�¡ | jd j}||7 }�q t 
t d¡t ¡ t |d ¡t |d |d ¡t ¡ tj|d |dd�¡| _|  ¡  d S )Nr&   gü©ñÒMbP?g{®Gáz„?)ZepsZmomentumr   r!   zInput size z
 of block z$ is not divisible by partition size zx. Consider changing the partition size or the input size.
Current configuration yields the following block input sizes: r¯   r    F)r   r<   r;   r>   rC   T)r   r<   r;   r>   r?   r-   )r7   r8   r:   r9   r<   r;   r_   r–   r˜   r—   r“   r¬   r­   r®   )r>   ) rE   rF   r   r   r
   ÚBatchNorm2dr*   r°   r²   rd   rH   r   Ústemr$   r“   r±   ÚblocksÚnpZlinspacer3   ÚtolistÚzipr(   r«   r•   ZAdaptiveAvgPool2dZFlattenr£   ri   ZTanhÚ
classifierÚ_init_weights)rN   r   r´   r“   rµ   r¶   r_   r·   r<   r;   r:   r9   r–   r˜   r—   r¸   Zinput_channelsZblock_input_sizesr³   Zblock_input_sizer7   r8   r®   Zp_idxZ
in_channelZout_channelZ
num_layersrO   r"   r#   rF   M  sŠ    
 ÿø
      ÿõ
òÿú	zMaxVit.__init__rQ   c                 C   s,   |   |¡}| jD ]}||ƒ}q|  |¡}|S r…   )rº   r»   r¿   )rN   rR   Úblockr"   r"   r#   rU   Ã  s
    



zMaxVit.forwardc                 C   s®   |   ¡ D ] }t|tjƒrDtjj|jdd� |jd k	r¨tj |j¡ qt|tj	ƒrrtj 
|jd¡ tj 
|jd¡ qt|tjƒrtjj|jdd� |jd k	rtj |j¡ qd S )Nrb   rc   r!   r   )ÚmodulesÚ
isinstancer
   rG   rp   Znormal_Zweightr>   Zzeros_r¹   Z	constant_ri   )rN   Úmr"   r"   r#   rÀ   Ê  s    

zMaxVit._init_weights)rV   rW   rX   rY   r
   ZGELUr	   rK   r   rZ   r   r   r[   rF   r   rU   rÀ   r\   r"   r"   rO   r#   r   8  s:   %å
ävF)
r´   rµ   r¶   r·   r“   r_   ÚweightsÚprogressÚkwargsr   c              
   K   sš   |d k	rPt |dt|jd ƒƒ |jd d |jd d ks>t‚t |d|jd ƒ | dd¡}	tf | ||||||	dœ|—Ž}
|d k	r–|
 |j|d	d
�¡ |
S )Nr¸   Ú
categoriesÚmin_sizer   r!   r   ©éà   rË   )r´   rµ   r¶   r·   r_   r“   r   T)rÆ   Z
check_hash)r   r°   ÚmetaÚAssertionErrorÚpopr   Zload_state_dictZget_state_dict)r´   rµ   r¶   r·   r“   r_   rÅ   rÆ   rÇ   r   Úmodelr"   r"   r#   Ú_maxvitÙ  s&     ùørÐ   c                   @   sH   e Zd Zedeeddejd�edddddd	d
œiddddœd�Z	e	Z
dS )r   z9https://download.pytorch.org/models/maxvit_t-bc5ab103.pthrË   )Z	crop_sizeZresize_sizeÚinterpolationiÈË×rÊ   zLhttps://github.com/pytorch/vision/tree/main/references/classification#maxvitzImageNet-1KgÍÌÌÌÌìT@g‘í|?5.X@)zacc@1zacc@5g¬Zd;@gð§ÆK7±]@z½These weights reproduce closely the results of the paper using a similar training recipe.
            They were trained with a BatchNorm2D momentum of 0.99 instead of the more correct 0.01.)rÈ   Z
num_paramsrÉ   ZrecipeZ_metricsZ_opsÚ
_file_sizeZ_docs)ÚurlZ
transformsrÌ   N)rV   rW   rX   r   r   r   r   ZBICUBICr   ÚIMAGENET1K_V1ÚDEFAULTr"   r"   r"   r#   r     s.      ÿþÿóúZ
pretrained)rÅ   T)rÅ   rÆ   )rÅ   rÆ   rÇ   r   c                 K   s:   t  | ¡} tf dddddgddddgddd	| |d
œ|—ŽS )aŠ  
    Constructs a maxvit_t architecture from
    `MaxViT: Multi-Axis Vision Transformer <https://arxiv.org/abs/2204.01697>`_.

    Args:
        weights (:class:`~torchvision.models.MaxVit_T_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.MaxVit_T_Weights` below for
            more details, and possible values. By default, no pre-trained
            weights are used.
        progress (bool, optional): If True, displays a progress bar of the
            download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.maxvit.MaxVit``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/maxvit.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.MaxVit_T_Weights
        :members:
    é@   é€   é   i   r    rŠ   é    gš™™™™™É?é   )r´   rµ   r¶   r_   r·   r“   rÅ   rÆ   )r   ÚverifyrÐ   )rÅ   rÆ   rÇ   r"   r"   r#   r     s    


ø	÷)NF)<rf   Úcollectionsr   Ú	functoolsr   Útypingr   r   r   r   r   r	   Znumpyr¼   r.   Ztorch.nn.functionalr
   Z
functionalrx   r   Ztorchvision.models._apir   r   r   Ztorchvision.models._metar   Ztorchvision.models._utilsr   r   Ztorchvision.ops.miscr   r   Z torchvision.ops.stochastic_depthr   Ztorchvision.transforms._presetsr   r   Ztorchvision.utilsr   Ú__all__rK   r$   r*   r4   r[   r5   r]   r‚   r†   r�   r’   r¨   r«   r   rZ   ÚboolrÐ   r   rÔ   r   r"   r"   r"   r#   Ú<module>   sb    ý&&
WIhaU .  óð*