a
    h%                     @   s  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 dejZG dd dejZddedZG dd deZe edejfddddee e eedddZ!dS )    )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                	       sL   e Zd Zd	eeeeeedejf  dd fddZe	e	dddZ
  ZS )
InvertedResidualN.)inpoupstrideexpand_ratio
norm_layerreturnc                    s   t    || _|dvr&td| |d u r4tj}tt|| }| jdkoT||k| _g }|dkr~|	t
||d|tjd |t
|||||tjdtj||ddddd||g tj| | _|| _|dk| _d S )	N)r   r   z#stride should be 1 or 2 instead of r   Z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convZout_channelsZ_is_cn)selfr   r   r   r   r   Z
hidden_dimZlayers	__class__ L/var/www/auris/lib/python3.9/site-packages/torchvision/models/mobilenetv2.pyr#      s:    
	zInvertedResidual.__init__xr   c                 C   s"   | j r|| | S | |S d S N)r(   r.   r/   r5   r2   r2   r3   forward<   s    zInvertedResidual.forward)N)__name__
__module____qualname__r&   r   r   r   Moduler#   r   r8   __classcell__r2   r2   r0   r3   r      s    (r   c                
       sz   e Zd Zdeeeeee   eeedej	f  eedej	f  edd fdd	Z
eed
ddZeed
ddZ  ZS )r           ?N   皙?.)num_classes
width_multinverted_residual_settinground_nearestblockr   dropoutr   c                    s"  t    t|  |du rt}|du r,tj}d}d}	|du rjg dg dg dg dg dg d	g d
g}t|dkst|d dkrtd| t|| |}t|	t	d| || _
td|d|tjdg}
|D ]V\}}}}t|| |}t|D ]2}|dkr|nd}|
||||||d |}qq|
t|| j
d|tjd tj|
 | _ttj|dt| j
|| _|  D ]}t|tjrtjj|jdd |jdurtj|j n`t|tjtjfrtj|j tj|j n.t|tjrztj|jdd tj|j qzd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   )rJ   rH      r   )rJ   @      r   )rJ   `   rL   r   )rJ      rL   r   )rJ   i@  r   r   r   rN   zGinverted_residual_setting should be non-empty or a 4-element list, got r?   rL   r   )r   r   r   r   )r   r   r   )pZfan_out)modeg{Gz?)r"   r#   r   r   r   r%   lenr$   r   maxlast_channelr	   r*   ranger)   r-   featuresZDropoutZLinear
classifiermodules
isinstancer,   initZkaiming_normal_Zweightr!   Zzeros_Z	GroupNormZones_Znormal_)r/   rB   rC   rD   rE   rF   r   rG   Zinput_channelrU   rW   tcnsZoutput_channelir   mr0   r2   r3   r#   D   sh    

zMobileNetV2.__init__r4   c                 C   s2   |  |}tj|d}t|d}| |}|S )Nr   r   r   )rW   r   Z
functionalZadaptive_avg_pool2dtorchflattenrX   r7   r2   r2   r3   _forward_impl   s
    

zMobileNetV2._forward_implc                 C   s
   |  |S r6   )re   r7   r2   r2   r3   r8      s    zMobileNetV2.forward)r>   r?   Nr@   NNrA   )r9   r:   r;   r&   floatr   listr   r   r<   r#   r   re   r8   r=   r2   r2   r0   r3   r   C   s&          _
r   iz5 rb   )Z
num_paramsZmin_size
categoriesc                
   @   sz   e Zd Zedeeddi e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id	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.)ZrecipeZ_metricsZ_ops
_file_sizeZ_docs)urlZ
transformsmetaz=https://download.pytorch.org/models/mobilenet_v2-7ebf99e0.pth   )rj   Z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)
r9   r:   r;   r   r   r
   _COMMON_METAIMAGENET1K_V1ZIMAGENET1K_V2DEFAULTr2   r2   r2   r3   r      sF   
r   Z
pretrained)weightsT)rr   progress)rr   rs   kwargsr   c                 K   sV   t | } | dur(t|dt| jd  tf i |}| durR|| j|d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:
    NrB   rh   T)rs   Z
check_hash)r   verifyr   rS   rm   r   Zload_state_dictZget_state_dict)rr   rs   rt   modelr2   r2   r3   r      s    
r   )"	functoolsr   typingr   r   r   rc   r   r   Zops.miscr	   Ztransforms._presetsr
   utilsr   Z_apir   r   r   Z_metar   _utilsr   r   r   __all__r<   r   r   ro   r   rp   boolr   r2   r2   r2   r3   <module>   s0   0o*
