o
    )iI                     @   s  d dl Z d dlmZ d dlmZ d dlmZmZmZm	Z	m
Z
 d dlZd dlm  mZ d dlmZ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 g dZedddgZee	e de_ eZ!G dd dej"Z#G dd dej"Z$G dd dej"Z%G dd dej"Z&G dd dej"Z'G dd dej"Z(G dd dej"Z)G d d! d!ej"Z*G d"d# d#eZ+e ed$e+j,fd%dd&d'd(e	e+ d)e-d*ed+e#fd,d-Z.dS ).    N)
namedtuple)partial)AnyCallableListOptionalTuple)nnTensor   )ImageClassification)_log_api_usage_once   )register_modelWeightsWeightsEnum)_IMAGENET_CATEGORIES)_ovewrite_named_paramhandle_legacy_interface)
Inception3InceptionOutputs_InceptionOutputsInception_V3_Weightsinception_v3r   logits
aux_logits)r   r   c                       s   e Zd Z						ddededed	eeed
ej	f   dee de
ddf fddZdedefddZdedeeee f fddZejjdedee defddZdedefddZ  ZS )r     TFN      ?num_classesr   transform_inputinception_blocks.init_weightsdropoutreturnc                    s~  t    t|  |d u rttttttt	g}|d u r"t
dt d}t|dkr1tdt| |d }|d }|d }	|d }
|d	 }|d
 }|d }|| _|| _|ddddd| _|dddd| _|ddddd| _tjddd| _|dddd| _|dddd| _tjddd| _|ddd| _|ddd| _|ddd| _|	d| _|
ddd| _|
ddd| _|
ddd| _ |
ddd| _!d | _"|r|d|| _"|d| _#|d| _$|d| _%t&d| _'tj(|d| _)t*d|| _+|r;| , D ]D}t-|tj.st-|tj*r#t/|drt0|j1nd}t2jj3j4|j5d |d!dd" qt-|tj6r:tj37|j5d tj37|j8d qd S d S )#NzThe default weight initialization of inception_v3 will be changed in future releases of torchvision. If you wish to keep the old behavior (which leads to long initialization times due to scipy/scipy#11299), please set init_weights=True.T   z2length of inception_blocks should be 7 instead of r   r   r                   kernel_sizestrider+   @   r+   paddingP      )pool_features   i         )channels_7x7   i   i   r   r   )pstddevg?g        )meanZstdab)9super__init__r   BasicConv2d
InceptionA
InceptionB
InceptionC
InceptionD
InceptionEInceptionAuxwarningswarnFutureWarninglen
ValueErrorr   r   Conv2d_1a_3x3Conv2d_2a_3x3Conv2d_2b_3x3r	   Z	MaxPool2dmaxpool1Conv2d_3b_1x1Conv2d_4a_3x3maxpool2Mixed_5bMixed_5cMixed_5dMixed_6aMixed_6bMixed_6cMixed_6dMixed_6e	AuxLogitsMixed_7aMixed_7bMixed_7cZAdaptiveAvgPool2davgpoolZDropoutr"   Linearfcmodules
isinstanceConv2dhasattrfloatr;   torchinitZtrunc_normal_weightBatchNorm2dZ	constant_bias)selfr   r   r   r    r!   r"   
conv_blockZinception_aZinception_bZinception_cZinception_dZinception_eZinception_auxmr;   	__class__ k/var/www/html/eduruby.in/lip-sync/lip-sync-env/lib/python3.10/site-packages/torchvision/models/inception.pyrA      sp   
	



zInception3.__init__xc                 C   s|   | j r<t|d d df dd d }t|d d df dd d }t|d d df dd d	 }t|||fd}|S )
Nr   r   gZd;O?gQgy&1?gI+r   g?gMbȿ)r   ri   Z	unsqueezecat)rn   ru   Zx_ch0Zx_ch1Zx_ch2rs   rs   rt   _transform_input_   s      zInception3._transform_inputc                 C   s  |  |}| |}| |}| |}| |}| |}| |}| |}| |}| 	|}| 
|}| |}| |}| |}| |}d }| jd urZ| jrZ| |}| |}| |}| |}| |}| |}t|d}| |}||fS Nr   )rN   rO   rP   rQ   rR   rS   rT   rU   rV   rW   rX   rY   rZ   r[   r\   r]   trainingr^   r_   r`   ra   r"   ri   flattenrc   rn   ru   auxrs   rs   rt   _forwardg   s6   






















zInception3._forwardr|   c                 C   s   | j r| jrt||S |S N)ry   r   r   r{   rs   rs   rt   eager_outputs   s   
zInception3.eager_outputsc                 C   sR   |  |}| |\}}| jo| j}tj r#|std t	||S | 
||S )Nz3Scripted Inception3 always returns Inception3 Tuple)rw   r}   ry   r   ri   jitZis_scriptingrI   rJ   r   r   )rn   ru   r|   Zaux_definedrs   rs   rt   forward   s   



zInception3.forward)r   TFNNr   )__name__
__module____qualname__intboolr   r   r   r	   Modulerh   rA   r
   rw   r   r}   ri   r   Zunusedr   r   r   __classcell__rs   rs   rq   rt   r      s6    B6r   c                
       h   e Zd Z	ddededeedejf  ddf fddZd	e	de
e	 fd
dZd	e	de	fddZ  ZS )rC   Nin_channelsr3   ro   .r#   c                    s   t    |d u rt}||ddd| _||ddd| _|ddddd| _||ddd| _|ddd	dd| _|ddd	dd| _|||dd| _	d S )
Nr.   r   r-   0   r'   r   r/   `   r%   )
r@   rA   rB   	branch1x1branch5x5_1branch5x5_2branch3x3dbl_1branch3x3dbl_2branch3x3dbl_3branch_pool)rn   r   r3   ro   rq   rs   rt   rA      s   
zInceptionA.__init__ru   c                 C   sh   |  |}| |}| |}| |}| |}| |}tj|dddd}| |}||||g}|S Nr%   r   r+   r,   r0   )	r   r   r   r   r   r   F
avg_pool2dr   )rn   ru   r   Z	branch5x5branch3x3dblr   outputsrs   rs   rt   r}      s   






zInceptionA._forwardc                 C      |  |}t|dS rx   r}   ri   rv   rn   ru   r   rs   rs   rt   r         
zInceptionA.forwardr~   r   r   r   r   r   r   r	   r   rA   r
   r   r}   r   r   rs   rs   rq   rt   rC      s    rC   c                       b   e Zd Zddedeedejf  ddf fddZde	de
e	 fd	d
Zde	de	fddZ  ZS )rD   Nr   ro   .r#   c                    s`   t    |d u rt}||dddd| _||ddd| _|ddddd	| _|ddddd| _d S )
N  r%   r   r*   r.   r   r-   r   r/   )r@   rA   rB   	branch3x3r   r   r   rn   r   ro   rq   rs   rt   rA      s   
zInceptionB.__init__ru   c                 C   sF   |  |}| |}| |}| |}tj|ddd}|||g}|S Nr%   r   r*   )r   r   r   r   r   
max_pool2d)rn   ru   r   r   r   r   rs   rs   rt   r}      s   




zInceptionB._forwardc                 C   r   rx   r   r   rs   rs   rt   r      r   zInceptionB.forwardr~   r   rs   rs   rq   rt   rD      s    *
rD   c                
       r   )rE   Nr   r7   ro   .r#   c                    s   t    |d u rt}||ddd| _|}|||dd| _|||ddd| _||dddd| _|||dd| _|||ddd| _|||ddd| _	|||ddd| _
||dddd| _||ddd| _d S )	Nr2   r   r-   r   r$   r   r%   r/   r$   r   r%   r   )r@   rA   rB   r   branch7x7_1branch7x7_2branch7x7_3branch7x7dbl_1branch7x7dbl_2branch7x7dbl_3branch7x7dbl_4branch7x7dbl_5r   )rn   r   r7   ro   Zc7rq   rs   rt   rA      s   
zInceptionC.__init__ru   c                 C   s   |  |}| |}| |}| |}| |}| |}| |}| |}| |}t	j
|dddd}| |}||||g}|S r   )r   r   r   r   r   r   r   r   r   r   r   r   )rn   ru   r   Z	branch7x7Zbranch7x7dblr   r   rs   rs   rt   r}   	  s   









zInceptionC._forwardc                 C   r   rx   r   r   rs   rs   rt   r     r   zInceptionC.forwardr~   r   rs   rs   rq   rt   rE      s    rE   c                       r   )rF   Nr   ro   .r#   c                    s   t    |d u rt}||ddd| _|ddddd| _||ddd| _|dddd	d
| _|ddddd
| _|ddddd| _d S )Nr2   r   r-   @  r%   r   r*   r   r   r/   r   r   )	r@   rA   rB   branch3x3_1branch3x3_2branch7x7x3_1branch7x7x3_2branch7x7x3_3branch7x7x3_4r   rq   rs   rt   rA   "  s   
zInceptionD.__init__ru   c                 C   sZ   |  |}| |}| |}| |}| |}| |}tj|ddd}|||g}|S r   )r   r   r   r   r   r   r   r   )rn   ru   r   Zbranch7x7x3r   r   rs   rs   rt   r}   .  s   






zInceptionD._forwardc                 C   r   rx   r   r   rs   rs   rt   r   ;  r   zInceptionD.forwardr~   r   rs   rs   rq   rt   rF   !  s    *rF   c                       r   )rG   Nr   ro   .r#   c                    s   t    |d u rt}||ddd| _||ddd| _|ddddd| _|dddd	d| _||d
dd| _|d
dddd| _|ddddd| _	|dddd	d| _
||ddd| _d S )Nr   r   r-   r   )r   r%   )r   r   r/   )r%   r   )r   r   i  r%   r2   )r@   rA   rB   r   r   branch3x3_2abranch3x3_2br   r   branch3x3dbl_3abranch3x3dbl_3br   r   rq   rs   rt   rA   A  s   
zInceptionE.__init__ru   c                 C   s   |  |}| |}| || |g}t|d}| |}| |}| || 	|g}t|d}t
j|dddd}| |}||||g}|S )Nr   r%   r   )r   r   r   r   ri   rv   r   r   r   r   r   r   r   )rn   ru   r   r   r   r   r   rs   rs   rt   r}   R  s    




zInceptionE._forwardc                 C   r   rx   r   r   rs   rs   rt   r   j  r   zInceptionE.forwardr~   r   rs   rs   rq   rt   rG   @  s    *rG   c                
       sR   e Zd Z	ddededeedejf  ddf fddZd	e	de	fd
dZ
  ZS )rH   Nr   r   ro   .r#   c                    sX   t    |d u rt}||ddd| _|dddd| _d| j_td|| _d| j_d S )Nr6   r   r-   r5   r'   g{Gz?MbP?)	r@   rA   rB   conv0conv1r;   r	   rb   rc   )rn   r   r   ro   rq   rs   rt   rA   p  s   
zInceptionAux.__init__ru   c                 C   sJ   t j|ddd}| |}| |}t |d}t|d}| |}|S )Nr'   r%   r*   r9   r   )r   r   r   r   Zadaptive_avg_pool2dri   rz   rc   rn   ru   rs   rs   rt   r   |  s   


zInceptionAux.forwardr~   )r   r   r   r   r   r   r	   r   rA   r
   r   r   rs   rs   rq   rt   rH   o  s    rH   c                       s@   e Zd Zdedededdf fddZdedefd	d
Z  ZS )rB   r   out_channelskwargsr#   Nc                    s8   t    tj||fddi|| _tj|dd| _d S )Nrm   Fr   )Zeps)r@   rA   r	   rf   convrl   bn)rn   r   r   r   rq   rs   rt   rA     s   
zBasicConv2d.__init__ru   c                 C   s"   |  |}| |}tj|ddS )NT)Zinplace)r   r   r   Zrelur   rs   rs   rt   r     s   

zBasicConv2d.forward)	r   r   r   r   r   rA   r
   r   r   rs   rs   rq   rt   rB     s    rB   c                   @   sD   e Zd Zedeedddddeddd	d
didddddZeZdS )r   zDhttps://download.pytorch.org/models/inception_v3_google-0cc3c7bd.pthi+  iV  )Z	crop_sizeZresize_sizeir)K   r   zRhttps://github.com/pytorch/vision/tree/main/references/classification#inception-v3zImageNet-1KgA`RS@g\W@)zacc@1zacc@5g'1@goY@z1These weights are ported from the original paper.)Z
num_paramsZmin_size
categoriesZrecipeZ_metricsZ_ops
_file_sizeZ_docs)urlZ
transformsmetaN)	r   r   r   r   r   r   r   IMAGENET1K_V1DEFAULTrs   rs   rs   rt   r     s&    r   Z
pretrained)weightsT)r   progressr   r   r   r#   c                 K   s   t | } |dd}| dur0d|vrt|dd t|dd t|dd t|dt| jd  td
i |}| durM|| j|dd	 |sMd|_	d|_
|S )ak  
    Inception v3 model architecture from
    `Rethinking the Inception Architecture for Computer Vision <http://arxiv.org/abs/1512.00567>`_.

    .. note::
        **Important**: In contrast to the other models the inception_v3 expects tensors with a size of
        N x 3 x 299 x 299, so ensure your images are sized accordingly.

    Args:
        weights (:class:`~torchvision.models.Inception_V3_Weights`, optional): The
            pretrained weights for the model. See
            :class:`~torchvision.models.Inception_V3_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.Inception3``
            base class. Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/inception.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.Inception_V3_Weights
        :members:
    r   TNr   r!   Fr   r   )r   Z
check_hashrs   )r   verifygetr   rL   r   r   Zload_state_dictZget_state_dictr   r]   )r   r   r   Zoriginal_aux_logitsmodelrs   rs   rt   r     s   
r   )/rI   collectionsr   	functoolsr   typingr   r   r   r   r   ri   Ztorch.nn.functionalr	   Z
functionalr   r
   Ztransforms._presetsr   utilsr   Z_apir   r   r   _metar   _utilsr   r   __all__r   __annotations__r   r   r   rC   rD   rE   rF   rG   rH   rB   r   r   r   r   rs   rs   rs   rt   <module>   s:     './.