
    kh%                        d dl mZ d dlmZmZmZmZ d dlZd dlmZm	Z	 ddl
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ej6                        Z G d dej6                        ZddedZ G d de      Z e        edej@                  f      ddddee   de!dedefd              Z"y)     )partial)AnyCallableListOptionalN)nnTensor   )Conv2dNormActivation)ImageClassification)_log_api_usage_once   )register_modelWeightsWeightsEnum)_IMAGENET_CATEGORIES)_make_divisible_ovewrite_named_paramhandle_legacy_interface)MobileNetV2MobileNet_V2_Weightsmobilenet_v2c                   p     e Zd Z	 ddededededeedej                  f      ddf fd	Zd
e	de	fdZ
 xZS )InvertedResidualNinpoupstrideexpand_ratio
norm_layer.returnc                 4   t         |           || _        |dvrt        d|       |t        j
                  }t        t        ||z              }| j                  dk(  xr ||k(  | _        g }|dk7  r-|j                  t        ||d|t        j                               |j                  t        |||||t        j                        t	        j                  ||dddd       ||      g       t	        j                  | | _        || _        |dkD  | _        y )	N)r   r
   z#stride should be 1 or 2 instead of r   kernel_sizer   activation_layer)r   groupsr   r$   r   F)bias)super__init__r   
ValueErrorr   BatchNorm2dintrounduse_res_connectappendr   ReLU6extendConv2d
Sequentialconvout_channels_is_cn)	selfr   r   r   r   r   
hidden_dimlayers	__class__s	           Z/var/www/teggl/fontify/venv/lib/python3.12/site-packages/torchvision/models/mobilenetv2.pyr(   zInvertedResidual.__init__   s    	B6(KLLJs\123
#{{a/>C3J"$1MM$S*!PZmomumuv 	 %!%)%'XX 		*c1a?3	
  MM6*	qj    xc                 d    | j                   r|| j                  |      z   S | j                  |      S N)r-   r3   r6   r<   s     r:   forwardzInvertedResidual.forward<   s,    tyy|##99Q<r;   r>   )__name__
__module____qualname__r+   r   r   r   Moduler(   r	   r@   __classcell__r9   s   @r:   r   r      sg    sw&!&! &!*-&!=@&!NVW_`cegenen`nWoNp&!	&!P   F  r;   r   c                        e Zd Z	 	 	 	 	 	 	 ddededeeee         dedeedej                  f      deedej                  f      d	ed
df fdZ
ded
efdZded
efdZ xZS )r   Nnum_classes
width_multinverted_residual_settinground_nearestblock.r   dropoutr    c                 r   t         |           t        |        |t        }|t        j
                  }d}d}	|g dg dg dg dg dg d	g d
g}t        |      dk(  st        |d         dk7  rt        d|       t        ||z  |      }t        |	t        d|      z  |      | _
        t        d|d|t        j                        g}
|D ]M  \  }}}}t        ||z  |      }t        |      D ])  }|dk(  r|nd}|
j                   ||||||             |}+ O |
j                  t        || j                  d|t        j                               t	        j                  |
 | _        t	        j                  t	        j"                  |      t	        j$                  | j                  |            | _        | j)                         D ]l  }t+        |t        j,                        rbt        j.                  j1                  |j2                  d       |j4                  Vt        j.                  j7                  |j4                         t+        |t        j
                  t        j8                  f      rSt        j.                  j;                  |j2                         t        j.                  j7                  |j4                         t+        |t        j$                        st        j.                  j=                  |j2                  dd       t        j.                  j7                  |j4                         o y)aw  
        MobileNet V2 main class

        Args:
            num_classes (int): Number of classes
            width_mult (float): Width multiplier - adjusts number of channels in each layer by this amount
            inverted_residual_setting: Network structure
            round_nearest (int): Round the number of channels in each layer to be a multiple of this number
            Set to 1 to turn off rounding
            block: Module specifying inverted residual building block for mobilenet
            norm_layer: Module specifying the normalization layer to use
            dropout (float): The droupout probability

        N    i   )r      r   r   )      r
   r
   )rQ   rO      r
   )rQ   @      r
   )rQ   `   rS   r   )rQ      rS   r
   )rQ   i@  r   r   r   rU   zGinverted_residual_setting should be non-empty or a 4-element list, got       ?rS   r
   )r   r   r$   r   )r   r   r"   )pfan_out)modeg{Gz?)r'   r(   r   r   r   r*   lenr)   r   maxlast_channelr   r/   ranger.   r2   featuresDropoutLinear
classifiermodules
isinstancer1   initkaiming_normal_weightr&   zeros_	GroupNormones_normal_)r6   rH   rI   rJ   rK   rL   r   rM   input_channelr^   r`   tcnsoutput_channelir   mr9   s                      r:   r(   zMobileNetV2.__init__D   s   0 	D!=$EJ$, 	)% ()Q.#6OPQ6R2SWX2XYZsYtu 
 (
(BMR+L3sJ;O,OQ^_ M!
egememn%
 4 	/JAq!Q,Q^]KN1X /1f!m^VZ[hr st ./	/ 	 t00aJikiqiq	
 x0 --JJ!IId''5
  
	'A!RYY'''y'A66%GGNN166*A=>ahh'qvv&Aryy)!T2qvv&
	'r;   r<   c                     | j                  |      }t        j                  j                  |d      }t	        j
                  |d      }| j                  |      }|S )Nr   r   r   )r`   r   
functionaladaptive_avg_pool2dtorchflattenrc   r?   s     r:   _forward_implzMobileNetV2._forward_impl   sK     MM!MM--a8MM!QOOAr;   c                 $    | j                  |      S r>   )r{   r?   s     r:   r@   zMobileNetV2.forward   s    !!!$$r;   )i  rX   N   NNg?)rA   rB   rC   r+   floatr   r   r   r   rD   r(   r	   r{   r@   rE   rF   s   @r:   r   r   C   s      ?C489=]']' ]' $,DcO#<	]'
 ]' bii01]' Xc299n56]' ]' 
]'~v & % %F %r;   r   iz5 rv   )
num_paramsmin_size
categoriesc                       e Zd Z ed eed      i edddddid	d
dd      Z ed eedd      i edddddid	ddd      ZeZ	y)r   z=https://download.pytorch.org/models/mobilenet_v2-b0353104.pth   )	crop_sizezQhttps://github.com/pytorch/vision/tree/main/references/classification#mobilenetv2zImageNet-1Kgx&1Q@gMV@)zacc@1zacc@5g$C?g\(+@zXThese weights reproduce closely the results of the paper using a simple training recipe.)recipe_metrics_ops
_file_size_docs)url
transformsmetaz=https://download.pytorch.org/models/mobilenet_v2-7ebf99e0.pth   )r   resize_sizezHhttps://github.com/pytorch/vision/issues/3995#new-recipe-with-reg-tuningg`"	R@gS㥛V@gV-2+@a$  
                These weights improve 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)
rA   rB   rC   r   r   r   _COMMON_METAIMAGENET1K_V1IMAGENET1K_V2DEFAULT r;   r:   r   r      s    K.#>

i##   s
M" K.#3O

`##   
M* Gr;   r   
pretrained)weightsT)r   progressr   r   kwargsr    c                     t         j                  |       } | #t        |dt        | j                  d                t        di |}| "|j                  | j                  |d             |S )a  MobileNetV2 architecture from the `MobileNetV2: Inverted Residuals and Linear
    Bottlenecks <https://arxiv.org/abs/1801.04381>`_ paper.

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

    .. autoclass:: torchvision.models.MobileNet_V2_Weights
        :members:
    rH   r   T)r   
check_hashr   )r   verifyr   r\   r   r   load_state_dictget_state_dict)r   r   r   models       r:   r   r      sk    0 #))'2GfmSl9S5TU!&!Eg44hSW4XYLr;   )#	functoolsr   typingr   r   r   r   ry   r   r	   ops.miscr   transforms._presetsr   utilsr   _apir   r   r   _metar   _utilsr   r   r   __all__rD   r   r   r   r   r   boolr   r   r;   r:   <module>r      s     0 0   + 5 ' 6 6 ' S S B- ryy - `k%")) k%^ &'; 'T ,0D0R0R!ST15 -. AE X[   U  r;   