3
Á3W^   ã               @   sº   d dl Z d dlZd dljZddlmZ dddddgZd	dd
ddœZdZG dd„ dej	ƒZ
dd„ Zd dd„Zdd„ ZG dd„ dejj	ƒZdd„ Zd!dd„Zd"dd„Zd#dd„Zd$dd„ZdS )%é    Né   )Úload_state_dict_from_urlÚMNASNetÚ
mnasnet0_5Úmnasnet0_75Ú
mnasnet1_0Ú
mnasnet1_3zIhttps://download.pytorch.org/models/mnasnet0.5_top1_67.592-7c6cb539b9.pthzIhttps://download.pytorch.org/models/mnasnet1.0_top1_73.512-f206786ef8.pth)r   r   r   r   gZõ¹ÚŠýï?c                   s&   e Zd Zd‡ fdd„	Zdd„ Z‡  ZS )Ú_InvertedResidualçš™™™™™¹?c                sÀ   t t| ƒjƒ  |dkst‚|dks&t‚|| }||ko<|dk| _tjtj||ddd�tj||d�tj	dd	�tj||||d ||dd
�tj||d�tj	dd	�tj||ddd�tj||d�ƒ| _
d S )Nr   é   é   é   F)Úbias)ÚmomentumT)Úinplace)ÚpaddingÚstrideÚgroupsr   )r   r   )r   r   )Úsuperr	   Ú__init__ÚAssertionErrorÚapply_residualÚnnÚ
SequentialÚConv2dÚBatchNorm2dÚReLUÚlayers)ÚselfÚin_chÚout_chÚkernel_sizer   Zexpansion_factorÚbn_momentumZmid_ch)Ú	__class__© úA/tmp/pip-build-7wfi2gtz/torchvision/torchvision/models/mnasnet.pyr      s    


z_InvertedResidual.__init__c             C   s"   | j r| j|ƒ| S | j|ƒS d S )N)r   r   )r   Úinputr$   r$   r%   Úforward.   s    z_InvertedResidual.forward)r
   )Ú__name__Ú
__module__Ú__qualname__r   r'   Ú__classcell__r$   r$   )r#   r%   r	      s   r	   c       
   
   C   sb   |dkst ‚t| |||||d�}g }x,td|ƒD ]}	|jt|||d||d�ƒ q0W tj|f|žŽ S )z( Creates a stack of inverted residuals. r   )r"   )r   r	   ÚrangeÚappendr   r   )
r   r    r!   r   Z
exp_factorZrepeatsr"   ÚfirstÚ	remainingÚ_r$   r$   r%   Ú_stack5   s    r1   çÍÌÌÌÌÌì?c             C   sR   d|  k odk n  st ‚t|t| |d  ƒ| | ƒ}|||  krJ|S || S )zÜ Asymmetric rounding to make `val` divisible by `divisor`. With default
    bias, will round up, unless the number is no more than 10% greater than the
    smaller divisible value, i.e. (83, 8) -> 80, but (84, 8) -> 88. g        g      ð?r   )r   ÚmaxÚint)ÚvalZdivisorZround_up_biasÚnew_valr$   r$   r%   Ú_round_to_multiple_ofD   s    r7   c                s   ‡ fdd„| D ƒS )z_ Scales tensor depths as in reference MobileNet code, prefers rouding up
    rather than down. c                s   g | ]}t |ˆ  d ƒ‘qS )é   )r7   )Ú.0Údepth)Úalphar$   r%   ú
<listcomp>P   s    z!_scale_depths.<locals>.<listcomp>r$   )Údepthsr;   r$   )r;   r%   Ú_scale_depthsM   s    r>   c                   s2   e Zd ZdZd
‡ fdd„	Zdd„ Zdd	„ Z‡  ZS )r   z× MNASNet, as described in https://arxiv.org/pdf/1807.11626.pdf.
    >>> model = MNASNet(1000, 1.0)
    >>> x = torch.rand(1, 3, 224, 224)
    >>> y = model(x)
    >>> y.dim()
    1
    >>> y.nelement()
    1000
    éè  çš™™™™™É?c                sœ  t t| ƒjƒ  tddddddg|ƒ}tjdddd	d
dd�tjdtd�tjdd�tjdddd	d	ddd�tjdtd�tjdd�tjddd	dd	dd�tjdtd�t	d|d dd
ddtƒt	|d |d	 dd
ddtƒt	|d	 |d
 dd
ddtƒt	|d
 |d dd	dd
tƒt	|d |d dd
ddtƒt	|d |d dd	dd	tƒtj|d dd	dd	dd�tjdtd�tjdd�g}tj
|Ž | _tj
tj|dd�tjd|ƒƒ| _| jƒ  d S )Né   é(   éP   é`   éÀ   i@  r   é    r   r   F)r   r   r   )r   T)r   )r   r   r   r   é   r   r   é   é   i   )Úpr   )r   r   r   r>   r   r   r   Ú_BN_MOMENTUMr   r1   r   r   ZDropoutÚLinearÚ
classifierÚ_initialize_weights)r   r;   Znum_classesZdropoutr=   r   )r#   r$   r%   r   ^   s.    

zMNASNet.__init__c             C   s"   | j |ƒ}|jddgƒ}| j|ƒS )Nr   r   )r   ZmeanrM   )r   Úxr$   r$   r%   r'   }   s    
zMNASNet.forwardc             C   s¤   xž| j ƒ D ]’}t|tjƒrHtjj|jddd� |jd k	rœtjj|jƒ q
t|tj	ƒrrtjj
|jƒ tjj|jƒ q
t|tjƒr
tjj|jdƒ tjj|jƒ q
W d S )NZfan_outZrelu)ÚmodeZnonlinearityg{®Gáz„?)ÚmodulesÚ
isinstancer   r   ÚinitZkaiming_normal_Zweightr   Zzeros_r   Zones_rL   Znormal_)r   Úmr$   r$   r%   rN   ƒ   s    
zMNASNet._initialize_weights)r?   r@   )r(   r)   r*   Ú__doc__r   r'   rN   r+   r$   r$   )r#   r%   r   S   s   	c             C   s@   | t kst |  d kr"tdj| ƒƒ‚t |  }|jt||d�ƒ d S )Nz,No checkpoint is available for model type {})Úprogress)Ú_MODEL_URLSÚ
ValueErrorÚformatZload_state_dictr   )Z
model_nameÚmodelrV   Zcheckpoint_urlr$   r$   r%   Ú_load_pretrained’   s
    r[   FTc             K   s   t d|Ž}| rtd||ƒ |S )aK  MNASNet with depth multiplier of 0.5 from
    `"MnasNet: Platform-Aware Neural Architecture Search for Mobile"
    <https://arxiv.org/pdf/1807.11626.pdf>`_.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    ç      à?r   )r\   )r   r[   )Ú
pretrainedrV   ÚkwargsrZ   r$   r$   r%   r   š   s    
c             K   s   t d|Ž}| rtd||ƒ |S )aL  MNASNet with depth multiplier of 0.75 from
    `"MnasNet: Platform-Aware Neural Architecture Search for Mobile"
    <https://arxiv.org/pdf/1807.11626.pdf>`_.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    ç      è?r   )r_   )r   r[   )r]   rV   r^   rZ   r$   r$   r%   r   ¨   s    
c             K   s   t d|Ž}| rtd||ƒ |S )aK  MNASNet with depth multiplier of 1.0 from
    `"MnasNet: Platform-Aware Neural Architecture Search for Mobile"
    <https://arxiv.org/pdf/1807.11626.pdf>`_.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    ç      ð?r   )r`   )r   r[   )r]   rV   r^   rZ   r$   r$   r%   r   ¶   s    
c             K   s   t d|Ž}| rtd||ƒ |S )aK  MNASNet with depth multiplier of 1.3 from
    `"MnasNet: Platform-Aware Neural Architecture Search for Mobile"
    <https://arxiv.org/pdf/1807.11626.pdf>`_.
    Args:
        pretrained (bool): If True, returns a model pre-trained on ImageNet
        progress (bool): If True, displays a progress bar of the download to stderr
    çÍÌÌÌÌÌô?r   )ra   )r   r[   )r]   rV   r^   rZ   r$   r$   r%   r   Ä   s    
g 0U0*©3?)r2   )FT)FT)FT)FT)ÚmathZtorchZtorch.nnr   Úutilsr   Ú__all__rW   rK   ÚModuler	   r1   r7   r>   r   r[   r   r   r   r   r$   r$   r$   r%   Ú<module>   s&   

	?


