a
    áñhá  ã                   @   s¸   d dl Z d dlmZ d dlmZ d dlZd dlZddlm	Z	m
Z
 ejd Zg d¢ZdddddœZG d	d
„ d
ejƒZG dd„ de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é   )Ú_replace_reluÚquantize_modelztorchvision.models.shufflenetv2)ÚQuantizableShuffleNetV2Úshufflenet_v2_x0_5Úshufflenet_v2_x1_0Úshufflenet_v2_x1_5Úshufflenet_v2_x2_0zQhttps://download.pytorch.org/models/quantized/shufflenetv2_x1_fbgemm-db332c57.pth)zshufflenetv2_x0.5_fbgemmzshufflenetv2_x1.0_fbgemmzshufflenetv2_x1.5_fbgemmzshufflenetv2_x2.0_fbgemmc                       s$   e Zd Z‡ fdd„Zdd„ Z‡  ZS )ÚQuantizableInvertedResidualc                    s&   t t| ƒj|i |¤Ž tj ¡ | _d S ©N)Úsuperr   Ú__init__ÚnnZ	quantizedZFloatFunctionalÚcat©ÚselfÚargsÚkwargs©Ú	__class__© úl/home/yuhw/wc/uboone-dl-vtx/venv/lib/python3.9/site-packages/torchvision/models/quantization/shufflenetv2.pyr      s    z$QuantizableInvertedResidual.__init__c                 C   sh   | j dkr8|jddd�\}}| jj||  |¡fdd�}n | jj|  |¡|  |¡fdd�}t |d¡}|S )Nr   é   )Zdim)ZstrideÚchunkr   Úbranch2Úbranch1Úshufflenetv2Zchannel_shuffle)r   ÚxÚx1Zx2Úoutr   r   r   Úforward   s    
 z#QuantizableInvertedResidual.forward)Ú__name__Ú
__module__Ú__qualname__r   r!   Ú__classcell__r   r   r   r   r      s   r   c                       s,   e Zd Z‡ fdd„Zdd„ Zdd„ Z‡  ZS )r   c                    s6   t t| ƒj|dti|¤Ž tj ¡ | _tj ¡ | _	d S )NZinverted_residual)
r   r   r   r   ÚtorchÚquantizationZ	QuantStubÚquantZDeQuantStubÚdequantr   r   r   r   r   *   s    z QuantizableShuffleNetV2.__init__c                 C   s"   |   |¡}|  |¡}|  |¡}|S r   )r(   Z_forward_implr)   )r   r   r   r   r   r!   /   s    


zQuantizableShuffleNetV2.forwardc                 C   s¬   | j  ¡ D ](\}}|dv r
tjj|g d¢gdd� q
|  ¡ D ]j}t|ƒtkr<t|j	j  ¡ ƒdkr€tjj|j	ddgg d¢gdd� tjj|j
g d¢d	d
gg d¢gdd� q<dS )a   Fuse conv/bn/relu modules in shufflenetv2 model

        Fuse conv+bn+relu/ conv+relu/conv+bn modules to prepare for quantization.
        Model is modified in place.  Note that this operation does not change numerics
        and the model after modification is in floating point
        )Zconv1Zconv5)Ú0Ú1Ú2T)Zinplacer   r*   r+   )r,   Ú3Ú4r-   r.   )Ú5Ú6Ú7N)Z_modulesÚitemsr&   r'   Zfuse_modulesÚmodulesÚtyper   Úlenr   r   )r   ÚnameÚmr   r   r   Ú
fuse_model5   s    ÿýz"QuantizableShuffleNetV2.fuse_model)r"   r#   r$   r   r!   r8   r%   r   r   r   r   r   )   s   r   c           
      O   st   t |i |¤Ž}t|ƒ |r*d}t||ƒ n|dv s6J ‚|rp|rPt| d |  }n
tj|  }t||d�}	| |	¡ |S )NZfbgemm)TFÚ_)Úprogress)r   r   r   Úquant_model_urlsr   Z
model_urlsr   Zload_state_dict)
ÚarchÚ
pretrainedr:   Úquantizer   r   ÚmodelÚbackendZ	model_urlZ
state_dictr   r   r   Ú_shufflenetv2M   s    
ÿ
rA   FTc                 K   s"   t d| ||g d¢g d¢fi |¤ŽS )at  
    Constructs a ShuffleNetV2 with 0.5x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design"
    <https://arxiv.org/abs/1807.11164>`_.

    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
    zshufflenetv2_x0.5©é   é   rC   )é   é0   é`   éÀ   é   ©rA   ©r=   r:   r>   r   r   r   r   r   e   s
    

ÿÿr   c                 K   s"   t d| ||g d¢g d¢fi |¤ŽS )at  
    Constructs a ShuffleNetV2 with 1.0x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design"
    <https://arxiv.org/abs/1807.11164>`_.

    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
    zshufflenetv2_x1.0rB   )rE   ét   éè   iÐ  rI   rJ   rK   r   r   r   r   s   s
    

ÿÿr   c                 K   s"   t d| ||g d¢g d¢fi |¤ŽS )at  
    Constructs a ShuffleNetV2 with 1.5x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design"
    <https://arxiv.org/abs/1807.11164>`_.

    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
    zshufflenetv2_x1.5rB   )rE   é°   i`  iÀ  rI   rJ   rK   r   r   r   r	   �   s
    

ÿÿr	   c                 K   s"   t d| ||g d¢g d¢fi |¤ŽS )at  
    Constructs a ShuffleNetV2 with 2.0x output channels, as described in
    `"ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design"
    <https://arxiv.org/abs/1807.11164>`_.

    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
    zshufflenetv2_x2.0rB   )rE   éô   iè  iÐ  i   rJ   rK   r   r   r   r
   �   s
    

ÿÿr
   )FTF)FTF)FTF)FTF)r&   Ztorch.nnr   Ztorchvision.models.utilsr   Ztorchvision.models.shufflenetv2ZtorchvisionÚsysÚutilsr   r   r3   r   Ú__all__r;   ZInvertedResidualr   ZShuffleNetV2r   rA   r   r   r	   r
   r   r   r   r   Ú<module>   s&   
û	$


