
    h%                     |   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
 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j4                        Z G d dej4                        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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	           \/var/www/html/eduruby.in/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,   r2   r5   r;   s     r9   forwardzInvertedResidual.forward<   s,    tyy|##99Q<r:   r=   )__name__
__module____qualname__r*   r   r   r   Moduler'   r   r?   __classcell__r8   s   @r9   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	   )rP   rN      r	   )rP   @      r	   )rP   `   rR   r   )rP      rR   r	   )rP   i@  r   r   r   rT   zGinverted_residual_setting should be non-empty or a 4-element list, got       ?rR   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-   r1   featuresDropoutLinear
classifiermodules
isinstancer0   initkaiming_normal_weightr%   zeros_	GroupNormones_normal_)r5   rG   rH   rI   rJ   rK   r   rL   input_channelr]   r_   tcnsoutput_channelir   mr8   s                      r9   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flattenrb   r>   s     r9   _forward_implzMobileNetV2._forward_impl   sK     MM!MM--a8MM!QOOAr:   c                 $    | j                  |      S r=   )rz   r>   s     r9   r?   zMobileNetV2.forward   s    !!!$$r:   )i  rW   N   NNg?)r@   rA   rB   r*   floatr   listr   r   rC   r'   r   rz   r?   rD   rE   s   @r9   r   r   C   s      ?C489=]']' ]' $,DcO#<	]'
 ]' bii01]' Xc299n56]' ]' 
]'~v & % %F %r:   r   iz5 ru   )
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)
r@   rA   rB   r   r   r   _COMMON_METAIMAGENET1K_V1IMAGENET1K_V2DEFAULT r:   r9   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:
    rG   r   T)r   
check_hashr   )r   verifyr   r[   r   r   load_state_dictget_state_dict)r   r   r   models       r9   r   r      sk    0 #))'2GfmSl9S5TU!&!Eg44hSW4XYLr:   )"	functoolsr   typingr   r   r   rx   r   r   ops.miscr
   transforms._presetsr   utilsr   _apir   r   r   _metar   _utilsr   r   r   __all__rC   r   r   r   r   r   boolr   r   r:   r9   <module>r      s     * *   + 5 ' 6 6 ' S S B- ryy - `k%")) k%^ &'; 'T ,0D0R0R!ST15 -. AE X[   U  r:   