o
    h?                     @   s  d dl mZ d dlmZ d dlmZmZmZ d dl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 d
dlmZmZmZ d
dlmZ d
dlmZmZmZ g dZG dd dZG dd de	jZ G dd de	jZ!	d3de"de#de$de$def
ddZ%de&e de'd ee d!e$ded"e!fd#d$Z(d%ed&Z)G d'd( d(eZ*G d)d* d*eZ+e ed+e*j,fd,dd-d.d ee* d!e$ded"e!fd/d0Z-e ed+e+j,fd,dd-d.d ee+ d!e$ded"e!fd1d2Z.dS )4    )Sequence)partial)AnyCallableOptionalN)nnTensor   )Conv2dNormActivationSqueezeExcitation)ImageClassification)_log_api_usage_once   )register_modelWeightsWeightsEnum)_IMAGENET_CATEGORIES)_make_divisible_ovewrite_named_paramhandle_legacy_interface)MobileNetV3MobileNet_V3_Large_WeightsMobileNet_V3_Small_Weightsmobilenet_v3_largemobilenet_v3_smallc                   @   sP   e Zd Zdedededededededed	efd
dZeded	efddZ	dS )InvertedResidualConfiginput_channelskernelexpanded_channelsout_channelsuse_se
activationstridedilation
width_multc
           
      C   sP   |  ||	| _|| _|  ||	| _|  ||	| _|| _|dk| _|| _|| _d S )NHS)	adjust_channelsr   r   r   r   r    use_hsr"   r#   )
selfr   r   r   r   r    r!   r"   r#   r$    r)   d/home/www/facesmatcher.com/frenv_anti/lib/python3.10/site-packages/torchvision/models/mobilenetv3.py__init__   s   

zInvertedResidualConfig.__init__channelsc                 C   s   t | | dS )N   )r   )r,   r$   r)   r)   r*   r&   0   s   z&InvertedResidualConfig.adjust_channelsN)
__name__
__module____qualname__intboolstrfloatr+   staticmethodr&   r)   r)   r)   r*   r      s,    	

r   c                	       s^   e Zd Zeeejdfdededej	f dedej	f f fddZ
ded	efd
dZ  ZS )InvertedResidual)Zscale_activationcnf
norm_layer.se_layerc                    s*  t    d|j  krdkstd td|jdko"|j|jk| _g }|jr,tj	ntj
}|j|jkrC|t|j|jd||d |jdkrJdn|j}|t|j|j|j||j|j||d |jrut|jd d}|||j| |t|j|jd|d d tj| | _|j| _|jdk| _d S )Nr   r	   zillegal stride valuekernel_sizer8   activation_layer)r;   r"   r#   groupsr8   r<      r-   )superr+   r"   
ValueErrorr   r   use_res_connectr'   r   	HardswishZReLUr   appendr
   r#   r   r    r   
SequentialblockZ_is_cn)r(   r7   r8   r9   layersr<   r"   Zsqueeze_channels	__class__r)   r*   r+   7   sT   
zInvertedResidual.__init__inputreturnc                 C   s   |  |}| jr||7 }|S N)rE   rA   )r(   rI   resultr)   r)   r*   forwardo   s   
zInvertedResidual.forward)r.   r/   r0   r   SElayerr   ZHardsigmoidr   r   Moduler+   r   rM   __classcell__r)   r)   rG   r*   r6   5   s    8r6   c                       s   e Zd Z				ddee dededeedej	f  d	eedej	f  d
e
deddf fddZdedefddZdedefddZ  ZS )r     N皙?inverted_residual_settinglast_channelnum_classesrE   .r8   dropoutkwargsrJ   c              
      s  t    t|  |stdt|trtdd |D s!td|du r't}|du r3t	t
jddd}g }|d	 j}	|td
|	d
d|t
jd |D ]
}
|||
| qJ|d j}d| }|t||d|t
jd t
j| | _t
d| _t
t
||t
jddt
j|ddt
||| _|  D ]L}t|t
jrt
jj|jdd |jdurt
j|j qt|t
jt
jfrt
j |j t
j|j qt|t
jrt
j!|jd	d t
j|j qdS )a.  
        MobileNet V3 main class

        Args:
            inverted_residual_setting (List[InvertedResidualConfig]): Network structure
            last_channel (int): The number of channels on the penultimate layer
            num_classes (int): Number of classes
            block (Optional[Callable[..., nn.Module]]): Module specifying inverted residual building block for mobilenet
            norm_layer (Optional[Callable[..., nn.Module]]): Module specifying the normalization layer to use
            dropout (float): The droupout probability
        z1The inverted_residual_setting should not be emptyc                 S   s   g | ]}t |tqS r)   )
isinstancer   ).0sr)   r)   r*   
<listcomp>   s    z(MobileNetV3.__init__.<locals>.<listcomp>zDThe inverted_residual_setting should be List[InvertedResidualConfig]NgMbP?g{Gz?)epsZmomentumr      r	   )r;   r"   r8   r<      r   r:   T)inplace)pr`   Zfan_out)mode)"r?   r+   r   r@   rX   r   all	TypeErrorr6   r   r   ZBatchNorm2dr   rC   r
   rB   r   rD   featuresZAdaptiveAvgPool2davgpoolZLinearZDropout
classifiermodulesZConv2dinitZkaiming_normal_weightZbiasZzeros_Z	GroupNormZones_Znormal_)r(   rS   rT   rU   rE   r8   rV   rW   rF   Zfirstconv_output_channelsr7   Zlastconv_input_channelsZlastconv_output_channelsmrG   r)   r*   r+   w   sx   







zMobileNetV3.__init__xc                 C   s.   |  |}| |}t|d}| |}|S )Nr   )re   rf   torchflattenrg   r(   rl   r)   r)   r*   _forward_impl   s
   


zMobileNetV3._forward_implc                 C   s
   |  |S rK   )rp   ro   r)   r)   r*   rM      s   
zMobileNetV3.forward)rQ   NNrR   )r.   r/   r0   listr   r1   r   r   r   rO   r4   r   r+   r   rp   rM   rP   r)   r)   rG   r*   r   v   s0    	[
r         ?Farchr$   reduced_taildilatedrW   c                 K   s  |rdnd}|r
dnd}t t|d}t tj|d}| dkr|dddddddd|ddd	d
dddd|d
ddd
dddd|d
ddddddd|dddddddd|dddddddd|dddddddd|dddddddd|dddddddd|dddddddd|dddddddd|dddddddd|dddd| ddd||d| dd| d| ddd||d| dd| d| ddd|g}	|d| }
|	|
fS | dkrZ|dddddddd|dddd
dddd|d
ddd
dddd|d
ddddddd|dddddddd|dddddddd|dddddddd|dddddddd|ddd d| ddd||d| dd!| d| ddd||d| dd!| d| ddd|g}	|d"| }
|	|
fS td#|  )$Nr	   r   )r$   r      r]   FZRE@      H      (   Tx      P   r%         i  p   i     i  i   r   X   `   0      i   i@  i   zUnsupported model type )r   r   r&   r@   )rs   r$   rt   ru   rW   Zreduce_dividerr#   Z
bneck_confr&   rS   rT   r)   r)   r*   _mobilenet_v3_conf   sN     
  r   rS   rT   weightsprogressrJ   c                 K   sP   |d urt |dt|jd  t| |fi |}|d ur&||j|dd |S )NrU   
categoriesT)r   Z
check_hash)r   lenmetar   Zload_state_dictZget_state_dict)rS   rT   r   r   rW   modelr)   r)   r*   _mobilenet_v3  s   r   )r   r   )Zmin_sizer   c                   @   s~   e Zd Zedeeddi edddddd	id
ddddZedeedddi edddddd	id
ddddZeZ	dS )r   zChttps://download.pytorch.org/models/mobilenet_v3_large-8738ca79.pth   	crop_sizeiS ^https://github.com/pytorch/vision/tree/main/references/classification#mobilenetv3-large--smallImageNet-1Kg R@g(\V@zacc@1zacc@5g-?gw/5@zJThese weights were trained from scratch by using a simple training recipe.Z
num_paramsZrecipeZ_metricsZ_ops
_file_sizeZ_docsurlZ
transformsr   zChttps://download.pytorch.org/models/mobilenet_v3_large-5c1a4163.pth   )r   Zresize_sizezHhttps://github.com/pytorch/vision/issues/3995#new-recipe-with-reg-tuninggK7R@gNbX9$W@gZd5@a/  
                These weights improve marginally upon the results of the original paper by using a modified version of
                TorchVision's `new training recipe
                <https://pytorch.org/blog/how-to-train-state-of-the-art-models-using-torchvision-latest-primitives/>`_.
            N)
r.   r/   r0   r   r   r   _COMMON_METAIMAGENET1K_V1ZIMAGENET1K_V2DEFAULTr)   r)   r)   r*   r   )  sL    
r   c                   @   sF   e Zd Zedeeddi edddddd	id
ddddZeZdS )r   zChttps://download.pytorch.org/models/mobilenet_v3_small-047dcff4.pthr   r   i& r   r   gnP@g}?5^U@r   gv/?g r#@z}
                These weights improve upon the results of the original paper by using a simple training recipe.
            r   r   N)	r.   r/   r0   r   r   r   r   r   r   r)   r)   r)   r*   r   U  s(    
r   Z
pretrained)r   T)r   r   c                 K   2   t | } tdi |\}}t||| |fi |S )a  
    Constructs a large MobileNetV3 architecture from
    `Searching for MobileNetV3 <https://arxiv.org/abs/1905.02244>`__.

    Args:
        weights (:class:`~torchvision.models.MobileNet_V3_Large_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.MobileNet_V3_Large_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.mobilenet.MobileNetV3``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/mobilenetv3.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.MobileNet_V3_Large_Weights
        :members:
    r   N)r   )r   verifyr   r   r   r   rW   rS   rT   r)   r)   r*   r   m     
r   c                 K   r   )a  
    Constructs a small MobileNetV3 architecture from
    `Searching for MobileNetV3 <https://arxiv.org/abs/1905.02244>`__.

    Args:
        weights (:class:`~torchvision.models.MobileNet_V3_Small_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.MobileNet_V3_Small_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.mobilenet.MobileNetV3``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/mobilenetv3.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.MobileNet_V3_Small_Weights
        :members:
    r   N)r   )r   r   r   r   r   r)   r)   r*   r     r   r   )rr   FF)/collections.abcr   	functoolsr   typingr   r   r   rm   r   r   Zops.miscr
   r   rN   Ztransforms._presetsr   utilsr   Z_apir   r   r   _metar   Z_utilsr   r   r   __all__r   rO   r6   r   r3   r4   r2   r   rq   r1   r   r   r   r   r   r   r   r)   r)   r)   r*   <module>   s    	Ak
1
,