
    hA                        d dl mZ d dlmZ d dlmZmZmZmZ d dl	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 g dZ G d de
j6                        Z G d de
j:                        Z G d de
j6                        Z G d de
j@                        Z! G d de
j@                        Z" G d de
j:                        Z# G d de
j:                        Z$ G d de
j@                        Z%de&ee!e"f      dee&eeeef         d e'e(   d!ed"e
j@                  f   d#ee   d$e)d%ed&e%fd'Z*d(ed)d*d+Z+ G d, d-e      Z, G d. d/e      Z- G d0 d1e      Z. e        ed2e,j^                  f3      dd4d5d#ee,   d$e)d%ed&e%fd6              Z0 e        ed2e-j^                  f3      dd4d5d#ee-   d$e)d%ed&e%fd7              Z1 e        ed2e.j^                  f3      dd4d5d#ee.   d$e)d%ed&e%fd8              Z2d	d9lm3Z3  e3e,j^                  jh                  e-j^                  jh                  e.j^                  jh                  d:      Z5y);    )Sequence)partial)AnyCallableOptionalUnionN)Tensor   )VideoClassification)_log_api_usage_once   )register_modelWeightsWeightsEnum)_KINETICS400_CATEGORIES)_ovewrite_named_paramhandle_legacy_interface)VideoResNetR3D_18_WeightsMC3_18_WeightsR2Plus1D_18_Weightsr3d_18mc3_18r2plus1d_18c                   h     e Zd Z	 d
dededee   dededdf fdZededeeeef   fd	       Z xZ	S )Conv3DSimpleN	in_planes
out_planes	midplanesstridepaddingreturnc                 0    t         |   ||d||d       y )N)r
   r
   r
   Fin_channelsout_channelskernel_sizer    r!   biassuper__init__selfr   r   r   r    r!   	__class__s         ]/var/www/html/eduruby.in/venv/lib/python3.12/site-packages/torchvision/models/video/resnet.pyr+   zConv3DSimple.__init__   s)     	!#! 	 	
    c                     | | | fS N r    s    r/   get_downsample_stridez"Conv3DSimple.get_downsample_stride(       vv%%r0   N   r8   
__name__
__module____qualname__intr   r+   staticmethodtupler5   __classcell__r.   s   @r/   r   r      sl    pq

*-
:B3-
X[
jm
	
 &c &eCcM.B & &r0   r   c                   `     e Zd Zd
dedededededdf fdZededeeeef   fd	       Z xZS )Conv2Plus1Dr   r   r   r    r!   r"   Nc                     t         |   t        j                  ||dd||fd||fd      t        j                  |      t        j
                  d      t        j                  ||d|ddf|ddfd             y )	Nr8   r
   r
   r8   r   Fr'   r    r!   r(   Tinplacer
   r8   r8   r*   r+   nnConv3dBatchNorm3dReLUr,   s         r/   r+   zConv2Plus1D.__init__.   s    II%66*GW- NN9%GGD!II:9faQR^^eghjk]lsx	
r0   c                     | | | fS r2   r3   r4   s    r/   r5   z!Conv2Plus1D.get_downsample_stride?   r6   r0   r8   r8   )	r:   r;   r<   r=   r+   r>   r?   r5   r@   rA   s   @r/   rC   rC   -   sb    
# 
3 
3 
PS 
be 
nr 
" &c &eCcM.B & &r0   rC   c                   h     e Zd Z	 d
dededee   dededdf fdZededeeeef   fd	       Z xZ	S )Conv3DNoTemporalNr   r   r   r    r!   r"   c           	      <    t         |   ||dd||fd||fd       y )NrE   r8   r   Fr$   r)   r,   s         r/   r+   zConv3DNoTemporal.__init__E   s7     	!#!vv&) 	 	
r0   c                     d| | fS Nr8   r3   r4   s    r/   r5   z&Conv3DNoTemporal.get_downsample_strideR   s    &&  r0   r7   r9   rA   s   @r/   rR   rR   D   sl    pq

*-
:B3-
X[
jm
	
 !c !eCcM.B ! !r0   rR   c                        e Zd ZdZ	 	 ddedededej                  f   dedeej                     d	df fd
Z	de
d	e
fdZ xZS )
BasicBlockr8   Ninplanesplanesconv_builder.r    
downsampler"   c                    ||z  dz  dz  dz  |dz  dz  d|z  z   z  }t         |           t        j                   |||||      t        j                  |      t        j
                  d            | _        t        j                   ||||      t        j                  |            | _        t        j
                  d      | _        || _	        || _
        y )Nr
   TrG   )r*   r+   rK   
SequentialrM   rN   conv1conv2relur[   r    r-   rX   rY   rZ   r    r[   r   r.   s          r/   r+   zBasicBlock.__init__[   s     &*Q.21q8H1v:8UV	]]69f=r~~f?UWYW^W^gkWl

 ]]<	#JBNN[aLbc
GGD)	$r0   xc                     |}| j                  |      }| j                  |      }| j                  | j                  |      }||z  }| j                  |      }|S r2   )r^   r_   r[   r`   r-   rb   residualouts       r/   forwardzBasicBlock.forwardn   sT    jjmjjo??&q)Hxiin
r0   r8   Nr:   r;   r<   	expansionr=   r   rK   Moduler   r+   r	   rg   r@   rA   s   @r/   rW   rW   W   sx    I *.  sBII~.	
  RYY' 
& F r0   rW   c                        e Zd ZdZ	 	 ddedededej                  f   dedeej                     d	df fd
Z	de
d	e
fdZ xZS )
Bottleneck   NrX   rY   rZ   .r    r[   r"   c                    t         |           ||z  dz  dz  dz  |dz  dz  d|z  z   z  }t        j                  t        j                  ||dd      t        j
                  |      t        j                  d            | _        t        j                   |||||      t        j
                  |      t        j                  d            | _        t        j                  t        j                  ||| j                  z  dd      t        j
                  || j                  z              | _
        t        j                  d      | _        || _        || _        y )Nr
   r8   F)r'   r(   TrG   )r*   r+   rK   r]   rL   rM   rN   r^   r_   rj   conv3r`   r[   r    ra   s          r/   r+   zBottleneck.__init__   s    	&*Q.21q8H1v:8UV	 ]]IIhAEBBNNSYDZ\^\c\clp\q

 ]]F;R^^F=SUWU\U\eiUj


 ]]IIfft~~515QNN6DNN23

 GGD)	$r0   rb   c                     |}| j                  |      }| j                  |      }| j                  |      }| j                  | j                  |      }||z  }| j	                  |      }|S r2   )r^   r_   rp   r[   r`   rd   s       r/   rg   zBottleneck.forward   sa    jjmjjojjo??&q)Hxiin
r0   rh   ri   rA   s   @r/   rm   rm   |   sx    I *.  sBII~.	
  RYY' 
< F r0   rm   c                   $     e Zd ZdZd fdZ xZS )	BasicStemz$The default conv-batchnorm-relu stemc           
          t         |   t        j                  dddddd      t        j                  d      t        j
                  d	             y )
Nr
   @   )r
      rv   r8   r   r   rE   FrF   TrG   rJ   r-   r.   s    r/   r+   zBasicStem.__init__   s?    IIa9i^cdNN2GGD!	
r0   r"   Nr:   r;   r<   __doc__r+   r@   rA   s   @r/   rs   rs      s    .
 
r0   rs   c                   $     e Zd ZdZd fdZ xZS )R2Plus1dStemzRR(2+1)D stem is different than the default one as it uses separated 3D convolutionc                 .   t         |   t        j                  dddddd      t        j                  d      t        j
                  d	      t        j                  dd
dddd      t        j                  d
      t        j
                  d	             y )Nr
   -   )r8   rv   rv   rw   )r   r
   r
   FrF   TrG   ru   rI   r8   r8   r8   )r8   r   r   rJ   rx   s    r/   r+   zR2Plus1dStem.__init__   sn    IIa9i^cdNN2GGD!IIb")Iy_deNN2GGD!	
r0   ry   rz   rA   s   @r/   r}   r}      s    \
 
r0   r}   c                        e Zd Z	 	 ddeeeef      deeeee	e
f         dee   dedej                  f   dededd	f fd
ZdedefdZ	 ddeeeef      deeee	e
f      dedededej(                  fdZ xZS )r   blockconv_makerslayersstem.num_classeszero_init_residualr"   Nc                    t         |           t        |        d| _         |       | _        | j                  ||d   d|d   d      | _        | j                  ||d   d|d   d      | _        | j                  ||d   d|d   d      | _        | j                  ||d   d	|d   d      | _	        t        j                  d
      | _        t        j                  d	|j                  z  |      | _        | j!                         D ]a  }t#        |t        j$                        rdt        j&                  j)                  |j*                  dd       |j,                  Wt        j&                  j/                  |j,                  d       t#        |t        j0                        rUt        j&                  j/                  |j*                  d       t        j&                  j/                  |j,                  d       t#        |t        j                        st        j&                  j3                  |j*                  dd       t        j&                  j/                  |j,                  d       d |r[| j!                         D ]G  }t#        |t4              st        j&                  j/                  |j6                  j*                  d       I yy)a^  Generic resnet video generator.

        Args:
            block (Type[Union[BasicBlock, Bottleneck]]): resnet building block
            conv_makers (List[Type[Union[Conv3DSimple, Conv3DNoTemporal, Conv2Plus1D]]]): generator
                function for each layer
            layers (List[int]): number of blocks per layer
            stem (Callable[..., nn.Module]): module specifying the ResNet stem.
            num_classes (int, optional): Dimension of the final FC layer. Defaults to 400.
            zero_init_residual (bool, optional): Zero init bottleneck residual BN. Defaults to False.
        ru   r   r8   r4      r      r
   i   r   fan_outr`   )modenonlinearityNg{Gz?)r*   r+   r   rX   r   _make_layerlayer1layer2layer3layer4rK   AdaptiveAvgPool3davgpoolLinearrj   fcmodules
isinstancerL   initkaiming_normal_weightr(   	constant_rM   normal_rm   bn3)	r-   r   r   r   r   r   r   mr.   s	           r/   r+   zVideoResNet.__init__   s   ( 	D!F	&&uk!nb&)TU&V&&uk!nc6!9UV&W&&uk!nc6!9UV&W&&uk!nc6!9UV&W++I6))C%//1;?  
	-A!RYY'''yv'V66%GG%%affa0Ar~~.!!!((A.!!!&&!,Aryy)!T2!!!&&!,
	- \\^ 7a,GG%%aeellA67 r0   rb   c                    | j                  |      }| j                  |      }| j                  |      }| j                  |      }| j	                  |      }| j                  |      }|j                  d      }| j                  |      }|S rU   )r   r   r   r   r   r   flattenr   )r-   rb   s     r/   rg   zVideoResNet.forward   so    IIaLKKNKKNKKNKKNLLOIIaLGGAJr0   rZ   rY   blocksr    c           	      6   d }|dk7  s| j                   ||j                  z  k7  rv|j                  |      }t        j                  t        j
                  | j                   ||j                  z  d|d      t        j                  ||j                  z              }g }|j                   || j                   ||||             ||j                  z  | _         t        d|      D ]%  }	|j                   || j                   ||             ' t        j                  | S )Nr8   F)r'   r    r(   )	rX   rj   r5   rK   r]   rL   rM   appendrange)
r-   r   rZ   rY   r   r    r[   	ds_strider   is
             r/   r   zVideoResNet._make_layer
  s     
Q;$--6EOO+CC$::6BI		$--%//)AqYbinov78J eDMM6<TU0q&! 	FAMM%v|DE	F }}f%%r0   )i  F)r8   )r:   r;   r<   typer   rW   rm   r   r   rR   rC   listr=   r   rK   rk   boolr+   r	   rg   r]   r   r@   rA   s   @r/   r   r      s    #(27E*j01227 d57G)T#UVW27 S		27
 sBII~&27 27 !27 
27h F * &E*j012& 5/?!LMN& 	&
 & & 
&r0   r   r   r   r   r   .weightsprogresskwargsr"   c                     |#t        |dt        |j                  d                t        | |||fi |}|"|j	                  |j                  |d             |S )Nr   
categoriesT)r   
check_hash)r   lenmetar   load_state_dictget_state_dict)r   r   r   r   r   r   r   models           r/   _video_resnetr   $  sc     fmSl9S5TU{FDCFCEg44hSW4XYLr0   rP   zKhttps://github.com/pytorch/vision/tree/main/references/video_classificationzThe weights reproduce closely the accuracy of the paper. The accuracies are estimated on video-level with parameters `frame_rate=15`, `clips_per_video=5`, and `clip_len=16`.)min_sizer   recipe_docsc            
       T    e Zd Z ed eedd      i eddddd	id
dd      ZeZy)r   z7https://download.pytorch.org/models/r3d_18-b3b3357e.pthp   r   r      	crop_sizeresize_sizeiP5Kinetics-400gO@g-T@zacc@1zacc@5gK7YD@g"_@
num_params_metrics_ops
_file_sizeurl
transformsr   N	r:   r;   r<   r   r   r   _COMMON_METAKINETICS400_V1DEFAULTr3   r0   r/   r   r   C  sT    E.*R\]

"##! !
N  Gr0   r   c            
       T    e Zd Z ed eedd      i eddddd	id
dd      ZeZy)r   z7https://download.pytorch.org/models/mc3_18-a90a0ba3.pthr   r   r   iPu r   g{GO@gQU@r   gClE@gtVF@r   r   Nr   r3   r0   r/   r   r   W  sT    E.*R\]

"##!  
N  Gr0   r   c            
       T    e Zd Z ed eedd      i eddddd	id
dd      ZeZy)r   z<https://download.pytorch.org/models/r2plus1d_18-91a641e6.pthr   r   r   ir   gʡP@g33333U@r   gOnBD@g1Z^@r   r   Nr   r3   r0   r/   r   r   k  sT    J.*R\]

"##! !
N  Gr0   r   
pretrained)r   T)r   r   c                 r    t         j                  |       } t        t        t        gdz  g dt
        | |fi |S )a  Construct 18 layer Resnet3D model.

    .. betastatus:: video module

    Reference: `A Closer Look at Spatiotemporal Convolutions for Action Recognition <https://arxiv.org/abs/1711.11248>`__.

    Args:
        weights (:class:`~torchvision.models.video.R3D_18_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.video.R3D_18_Weights`
            below for more details, and possible values. By default, no
            pre-trained weights are used.
        progress (bool): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.video.resnet.VideoResNet`` base class.
            Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/video/resnet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.video.R3D_18_Weights
        :members:
    rn   r   r   r   r   )r   verifyr   rW   r   rs   r   r   r   s      r/   r   r     sD    0 ##G,G	  r0   c                     t         j                  |       } t        t        t        gt
        gdz  z   g dt        | |fi |S )a  Construct 18 layer Mixed Convolution network as in

    .. betastatus:: video module

    Reference: `A Closer Look at Spatiotemporal Convolutions for Action Recognition <https://arxiv.org/abs/1711.11248>`__.

    Args:
        weights (:class:`~torchvision.models.video.MC3_18_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.video.MC3_18_Weights`
            below for more details, and possible values. By default, no
            pre-trained weights are used.
        progress (bool): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.video.resnet.VideoResNet`` base class.
            Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/video/resnet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.video.MC3_18_Weights
        :members:
    r
   r   )r   r   r   rW   r   rR   rs   r   s      r/   r   r     sM    0 ##G,G	*+a//  r0   c                 r    t         j                  |       } t        t        t        gdz  g dt
        | |fi |S )a  Construct 18 layer deep R(2+1)D network as in

    .. betastatus:: video module

    Reference: `A Closer Look at Spatiotemporal Convolutions for Action Recognition <https://arxiv.org/abs/1711.11248>`__.

    Args:
        weights (:class:`~torchvision.models.video.R2Plus1D_18_Weights`, optional): The
            pretrained weights to use. See
            :class:`~torchvision.models.video.R2Plus1D_18_Weights`
            below for more details, and possible values. By default, no
            pre-trained weights are used.
        progress (bool): If True, displays a progress bar of the download to stderr. Default is True.
        **kwargs: parameters passed to the ``torchvision.models.video.resnet.VideoResNet`` base class.
            Please refer to the `source code
            <https://github.com/pytorch/vision/blob/main/torchvision/models/video/resnet.py>`_
            for more details about this class.

    .. autoclass:: torchvision.models.video.R2Plus1D_18_Weights
        :members:
    rn   r   )r   r   r   rW   rC   r}   r   s      r/   r   r     sD    0 "((1G	  r0   )
_ModelURLs)r   r   r   )6collections.abcr   	functoolsr   typingr   r   r   r   torch.nnrK   torchr	   transforms._presetsr   utilsr   _apir   r   r   _metar   _utilsr   r   __all__rL   r   r]   rC   rR   rk   rW   rm   rs   r}   r   r   r   r=   r   r   r   r   r   r   r   r   r   r   r   r   
model_urlsr3   r0   r/   <module>r      s   $  1 1   6 ( 7 7 + C&299 &&&"-- &.!ryy !&" "J. .b
 

2== 
[&")) [&|j*,-.$u\3C[%PQRS I 3		>
"	
 k"   * )[	S[ ([ (+ ( ,0M0M!NO26  x/  $  Y\  al   P  F ,0M0M!NO26  x/  $  Y\  al   P  F ,0C0R0R!ST<@SW  H%89  D  cf  kv   U  H    //33 //33*99==
r0   