U
    ­Ê¹jî%  ã                   @   s  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 dddg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Ú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                	       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kr&td|› �ƒ‚|d k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__© ú]/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torchvision/models/mobilenetv2.pyr$      s:    
ÿú	ôÿzInvertedResidual.__init__©Úxr   c                 C   s"   | j r||  |¡ S |  |¡S d S ©N)r)   r/   ©r0   r6   r3   r3   r4   Úforward<   s    zInvertedResidual.forward)N)Ú__name__Ú
__module__Ú__qualname__r'   r   r   r   ÚModuler$   r   r9   Ú__classcell__r3   r3   r1   r4   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krt}|dkr,tj}d}d}	|dkr†ddddgddddgddd	dgdd
ddgddd	dgddd	dgddddgg}t|ƒdks¢t|d ƒdkr°td|› �ƒ‚t|| |ƒ}t|	t	d|ƒ |ƒ| _
td	|d|tjd�g}
|D ]X\}}}}t|| |ƒ}t|ƒD ]4}|dk�r"|nd}|
 ||||||d�¡ |}�qqî|
 t|| j
d|tjd�¡ tj|
Ž | _t tj|d�t | j
|¡¡| _|  ¡ D ]¢}t|tjƒ�rØtjj|jdd� |jdk	�r8tj |j¡ n`t|tjtjfƒ�r
tj |j¡ tj |j¡ n.t|tjƒ�r˜tj |jdd¡ tj |j¡ �q˜d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	   é   é@   é   é`   é    i@  r   zGinverted_residual_setting should be non-empty or a 4-element list, got r@   )r   r   r    )r   r   r   )ÚpZfan_out)Úmodeg{®Gáz„?)r#   r$   r   r   r   r&   Úlenr%   r   ÚmaxÚlast_channelr
   r+   Úranger*   r.   ÚfeaturesZDropoutZLinearÚ
classifierÚmodulesÚ
isinstancer-   ÚinitZkaiming_normal_Úweightr"   Zzeros_Z	GroupNormZones_Znormal_)r0   rC   rD   rE   rF   rG   r   rH   Zinput_channelrV   rX   ÚtÚcÚnÚsZoutput_channelÚir   Úmr1   r3   r4   r$   D   sp    







øÿÿ
    ÿÿ
þzMobileNetV2.__init__r5   c                 C   s2   |   |¡}tj |d¡}t |d¡}|  |¡}|S )N©r   r   r   )rX   r   Z
functionalZadaptive_avg_pool2dÚtorchÚflattenrY   r8   r3   r3   r4   Ú_forward_impl£   s
    

zMobileNetV2._forward_implc                 C   s
   |   |¡S r7   )rg   r8   r3   r3   r4   r9   ­   s    zMobileNetV2.forward)r?   r@   NrA   NNrB   )r:   r;   r<   r'   Úfloatr   r   r   r   r=   r$   r   rg   r9   r>   r3   r3   r1   r4   r   C   s&          ø÷_
ièz5 rd   )Z
num_paramsZmin_sizeÚ
categoriesc                
   @   sr   e Zd Zedeedd�edddddœid	d
ddœ–d�Zedeeddd�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-1KgÕxé&1øQ@gü©ñÒM’V@)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éè   )rk   Zresize_sizezHhttps://github.com/pytorch/vision/issues/3995#new-recipe-with-reg-tuningg`åÐ"Û	R@gøSã¥›´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:   r;   r<   r   r   r   Ú_COMMON_METAÚIMAGENET1K_V1ZIMAGENET1K_V2ÚDEFAULTr3   r3   r3   r4   r   ¸   s>   
þÿõýþÿõýZ
pretrained)ÚweightsT)rs   Úprogress)rs   rt   Úkwargsr   c                 K   sR   t  | ¡} | dk	r(t|dt| jd ƒƒ tf |Ž}| dk	rN| | 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:
    NrC   ri   T)rt   Z
check_hash)r   Úverifyr   rT   rn   r   Zload_state_dictZget_state_dict)rs   rt   ru   Úmodelr3   r3   r4   r   â   s    

)#Ú	functoolsr   Útypingr   r   r   r   re   r   r   Zops.miscr
   Ztransforms._presetsr   Úutilsr   Z_apir   r   r   Ú_metar   Z_utilsr   r   r   Ú__all__r=   r   r   rp   r   rq   Úboolr   r3   r3   r3   r4   Ú<module>   s6   
0oý* ÿ  þ