
    ڧg%                        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j                  Z G d dej                  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"dS )     )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                   r     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                 p   t                                                       || _        |dvrt          d|           |t          j        }t          t          ||z                      }| j        dk    o||k    | _        g }|dk    r1|	                    t          ||d|t          j                             |                    t          |||||t          j                  t	          j        ||dddd           ||          g           t	          j        | | _        || _        |dk    | _        d S )	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/html/ai-engine/env/lib/python3.11/site-packages/torchvision/models/mobilenetv2.pyr(   zInvertedResidual.__init__   sW    	K6KKLLLJs\12233
#{a/>C3J"$1MM$S*!PZmomuvvv   	 %!%)%'X   	*c1a???
3	
 	
 	
  M6*	qj    xc                 j    | j         r||                     |          z   S |                     |          S N)r-   r3   r6   r<   s     r:   forwardzInvertedResidual.forward<   s2     	 tyy||##99Q<<r;   r>   )__name__
__module____qualname__r+   r   r   r   Moduler(   r	   r@   __classcell__r9   s   @r:   r   r      s        sw&! &!&! &!*-&!=@&!NVW_`cegen`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                    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k    rt          d|           t          ||z  |          }t          |	t          d|          z  |          | _
        t          d|d|t          j                  g}
|D ][\  }}}}t          ||z  |          }t          |          D ]1}|dk    r|nd}|
                     ||||||                     |}2\|
                    t          || j
        d|t          j                             t	          j        |
 | _        t	          j        t	          j        |          t	          j        | j
        |                    | _        |                                 D ]B}t+          |t          j                  rRt          j                            |j        d           |j        $t          j                            |j                   ot+          |t          j        t          j        f          rIt          j                            |j                   t          j                            |j                   t+          |t          j                  rJt          j                            |j        dd           t          j                            |j                   DdS )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
   )rU   rS      r
   )rU   @      r
   )rU   `   rW   r   )rU      rW   r
   )rU   i@  r   r   r   rY   zGinverted_residual_setting should be non-empty or a 4-element list, got rI   rW   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   rL   rM   rN   rO   rP   r   rQ   input_channelra   rc   tcnsoutput_channelir   mr9   s                      r:   r(   zMobileNetV2.__init__D   s>   0 	D!!!=$EJ$, 	)% ())Q..#6OPQ6R2S2SWX2X2XuZsuu  
 (
(BMRR+L3sJ;O;O,OQ^__ M!
egemnnn%
 4 	/ 	/JAq!Q,Q^]KKN1XX / /1ff!m^VZ[hr s s sttt ./
 	 t0aJikiq  	
 	
 	
 x0 -J!!!Id'55
 
  
	' 
	'A!RY'' 	'''y'AAA6%GNN16***A=>> 'ah'''qv&&&&Ary)) '!T222qv&&&
	' 
	'r;   r<   c                     |                      |          }t          j                            |d          }t	          j        |d          }|                     |          }|S )Nr   r   r   )rc   r   
functionaladaptive_avg_pool2dtorchflattenrf   r?   s     r:   _forward_implzMobileNetV2._forward_impl   sS     MM!M--a88M!QOOAr;   c                 ,    |                      |          S r>   )r~   r?   s     r:   r@   zMobileNetV2.forward   s    !!!$$$r;   )rH   rI   NrJ   NNrK   )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#<	]'
 ]' bi01]' Xc29n56]' ]' 
]' ]' ]' ]' ]' ]'~v &    % %F % % % % % % % %r;   r   iz5 ry   )
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	dS )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        GK7.#>>>

i##     s
 
 
  M" GK7.#3OOO

`##     
 
 
  M* GGGr;   r   
pretrained)weightsT)r   progressr   r   kwargsr    c                     t                               |           } | )t          |dt          | j        d                              t          di |}| *|                    |                     |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:
    NrL   r   T)r   
check_hashr   )r   verifyr   r_   r   r   load_state_dictget_state_dict)r   r   r   models       r:   r   r      s    0 #))'22GfmSl9S5T5TUUU!!&!!Eg44hSW4XXYYYLr;   )#	functoolsr   typingr   r   r   r   r|   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 0 0 0 0 0 0 0 0 0 0          + + + + + + 5 5 5 5 5 5 ' ' ' ' ' ' 6 6 6 6 6 6 6 6 6 6 ' ' ' ' ' ' S S S S S S S S S S B
A
A-  -  -  -  - ry -  -  - `k% k% k% k% k%") k% k% k%^ & ' ' ' ' '; ' ' 'T ,0D0R!STTT15     -. AE X[       UT      r;   