
    YhA                        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
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" 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/        j4        e-j/        j4        e.j/        j4        d:          Z5dS );    )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                   z     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                 V    t                                          ||d||d           d S )N)r
   r
   r
   Fin_channelsout_channelskernel_sizer!   r"   biassuper__init__selfr   r   r    r!   r"   	__class__s         q/var/www/tools.fuzzalab.pt/emblema-extractor/venv/lib/python3.11/site-packages/torchvision/models/video/resnet.pyr,   zConv3DSimple.__init__   sB     	!#! 	 	
 	
 	
 	
 	
    c                     | | | fS N r!   s    r0   get_downsample_stridez"Conv3DSimple.get_downsample_stride(       vv%%r1   Nr   r   
__name__
__module____qualname__intr   r,   staticmethodtupler6   __classcell__r/   s   @r0   r   r      s        pq
 

*-
:B3-
X[
jm
	
 
 
 
 
 
 &c &eCcM.B & & & \& & & & &r1   r   c                   l     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"   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                     d S )	Nr   r
   r
   r   r   Fr(   r!   r"   r)   Tinplacer
   r   r   r+   r,   nnConv3dBatchNorm3dReLUr-   s         r0   r,   zConv2Plus1D.__init__.   s    I%66*GW-   N9%%GD!!!I:9faQR^^eghjk]lsx  	
 	
 	
 	
 	
r1   c                     | | | fS r3   r4   r5   s    r0   r6   z!Conv2Plus1D.get_downsample_stride?   r7   r1   r   r   )	r:   r;   r<   r=   r,   r>   r?   r6   r@   rA   s   @r0   rC   rC   -   s        
 
# 
3 
3 
PS 
be 
nr 
 
 
 
 
 
" &c &eCcM.B & & & \& & & & &r1   rC   c                   z     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"   r#   c           	      b    t                                          ||dd||fd||fd           d S )NrE   r   r   Fr%   r*   r-   s         r0   r,   zConv3DNoTemporal.__init__E   sP     	!#!vv&) 	 	
 	
 	
 	
 	
r1   c                     d| | fS Nr   r4   r5   s    r0   r6   z&Conv3DNoTemporal.get_downsample_strideR   s    &&  r1   r8   r9   rA   s   @r0   rR   rR   D   s        pq
 

*-
:B3-
X[
jm
	
 
 
 
 
 
 !c !eCcM.B ! ! ! \! ! ! ! !r1   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 )
BasicBlockr   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          | _        || _	        || _
        d S )Nr
   TrG   )r+   r,   rK   
SequentialrM   rN   conv1conv2relur[   r!   r.   rX   rY   rZ   r!   r[   r    r/   s          r0   r,   zBasicBlock.__init__[   s     &*Q.21q8H1v:8UV	]L69f==r~f?U?UWYW^gkWlWlWl
 

 ]<<	#J#JBN[aLbLbcc
GD)))	$r1   xc                     |}|                      |          }|                     |          }| j        |                     |          }||z  }|                     |          }|S r3   )r^   r_   r[   r`   r.   rb   residualouts       r0   forwardzBasicBlock.forwardn   s[    jjmmjjoo?&q))Hxiinn
r1   r   Nr:   r;   r<   	expansionr=   r   rK   Moduler   r,   r	   rg   r@   rA   s   @r0   rW   rW   W   s        I *.   sBI~.	
  RY' 
     & F        r1   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   r   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          | _        || _        || _        d S )Nr
   r   F)r(   r)   TrG   )r+   r,   rK   r]   rL   rM   rN   r^   r_   rj   conv3r`   r[   r!   ra   s          r0   r,   zBottleneck.__init__   sI    	&*Q.21q8H1v:8UV	 ]IhAEBBBBNSYDZDZ\^\clp\q\q\q
 

 ]LF;;R^F=S=SUWU\eiUjUjUj
 


 ]Ifft~515QQQN6DN233
 

 GD)))	$r1   rb   c                     |}|                      |          }|                     |          }|                     |          }| j        |                     |          }||z  }|                     |          }|S r3   )r^   r_   rp   r[   r`   rd   s       r0   rg   zBottleneck.forward   sj    jjmmjjoojjoo?&q))Hxiinn
r1   rh   ri   rA   s   @r0   rm   rm   |   s        I *.   sBI~.	
  RY' 
     < F        r1   rm   c                   $     e Zd ZdZd fdZ xZS )	BasicStemz$The default conv-batchnorm-relu stemr#   Nc           
          t                                          t          j        dddddd          t          j        d          t          j        d	                     d S )
Nr
   @   )r
      rv   r   r   r   rE   FrF   TrG   rJ   r.   r/   s    r0   r,   zBasicStem.__init__   sa    Ia9i^cdddN2GD!!!	
 	
 	
 	
 	
r1   r#   Nr:   r;   r<   __doc__r,   r@   rA   s   @r0   rs   rs      sC        ..
 
 
 
 
 
 
 
 
 
r1   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 convolutionr#   Nc                 H   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	                     d S )Nr
   -   )r   rv   rv   rw   )r   r
   r
   FrF   TrG   ru   rI   r   r   r   )r   r   r   rJ   rx   s    r0   r,   zR2Plus1dStem.__init__   s    Ia9i^cdddN2GD!!!Ib")Iy_deeeN2GD!!!	
 	
 	
 	
 	
r1   ry   rz   rA   s   @r0   r}   r}      sC        \\
 
 
 
 
 
 
 
 
 
r1   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     Fblockconv_makerslayersstem.num_classeszero_init_residualr#   Nc                    t                                                       t          |            d| _         |            | _        |                     ||d         d|d         d          | _        |                     ||d         d|d         d          | _        |                     ||d         d|d         d          | _        |                     ||d         d	|d         d          | _	        t          j        d
          | _        t          j        d	|j        z  |          | _        |                                 D ];}t#          |t          j                  rTt          j                            |j        dd           |j        %t          j                            |j        d           qt#          |t          j                  rKt          j                            |j        d           t          j                            |j        d           t#          |t          j                  rKt          j                            |j        dd           t          j                            |j        d           =|rV|                                 D ]C}t#          |t4                    r*t          j                            |j        j        d           BdS dS )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   r   r5      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	           r0   r,   zVideoResNet.__init__   sr   ( 	D!!!DFF	&&uk!nb&)TU&VV&&uk!nc6!9UV&WW&&uk!nc6!9UV&WW&&uk!nc6!9UV&WW+I66)C%/1;??  
	- 
	-A!RY'' 	-''yv'VVV6%G%%afa000Ar~.. -!!!(A...!!!&!,,,,Ary)) -!T222!!!&!,,, 	7\\^^ 7 7a,, 7G%%aelA666	7 	77 7r1   rb   c                 V   |                      |          }|                     |          }|                     |          }|                     |          }|                     |          }|                     |          }|                    d          }|                     |          }|S rU   )r   r   r   r   r   r   flattenr   )r.   rb   s     r0   rg   zVideoResNet.forward   s    IIaLLKKNNKKNNKKNNKKNNLLOOIIaLLGGAJJr1   r   rZ   rY   blocksr!   c           	         d }|dk    s| j         ||j        z  k    rh|                    |          }t          j        t          j        | j         ||j        z  d|d          t          j        ||j        z                      }g }|                     || j         ||||                     ||j        z  | _         t          d|          D ]'}	|                     || j         ||                     (t          j        | S )Nr   F)r(   r!   r)   )	rX   rj   r6   rK   r]   rL   rM   appendrange)
r.   r   rZ   rY   r   r!   r[   	ds_strider   is
             r0   r   zVideoResNet._make_layer
  s    
Q;;$-6EO+CCC$::6BBI	$-%/)AqYbinooov788 J eeDM6<TTUUU0q&!! 	F 	FAMM%%v|DDEEEE}f%%r1   )r   F)r   )r:   r;   r<   typer   rW   rm   r   r   rR   rC   listr=   r   rK   rk   boolr,   r	   rg   r]   r   r@   rA   s   @r0   r   r      sa        #(27 27E*j01227 d57G)T#UVW27 S		27
 sBI~&27 27 !27 
27 27 27 27 27 27h F    * & &E*j012& 5/?!LMN& 	&
 & & 
& & & & & & & &r1   r   r   r   r   r   .weightsprogresskwargsr#   c                     |)t          |dt          |j        d                              t          | |||fi |}|*|                    |                    |d                     |S )Nr   
categoriesT)r   
check_hash)r   lenmetar   load_state_dictget_state_dict)r   r   r   r   r   r   r   models           r0   _video_resnetr   $  sy     fmSl9S5T5TUUU{FDCCFCCEg44hSW4XXYYYLr1   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            
       b    e Zd Z ed eedd          i eddddd	id
dd          ZeZdS )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DEFAULTr4   r1   r0   r   r   C  s        WE7.*R\]]]

"##! ! !
 
 
  N  GGGr1   r   c            
       b    e Zd Z ed eedd          i eddddd	id
dd          ZeZdS )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   r4   r1   r0   r   r   W  s        WE7.*R\]]]

"##! !  
 
 
  N  GGGr1   r   c            
       b    e Zd Z ed eedd          i eddddd	id
dd          ZeZdS )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   r4   r1   r0   r   r   k  s        WJ7.*R\]]]

"##! ! !
 
 
  N  GGGr1   r   
pretrained)r   T)r   r   c                     t                               |           } 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      r0   r   r     sU    0 ##G,,G	    r1   c                     t                               |           } 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      r0   r   r     s^    0 ##G,,G	*+a//    r1   c                     t                               |           } 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      r0   r   r     sU    0 "((11G	    r1   )
_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_urlsr4   r1   r0   <module>r      s[   $ $ $ $ $ $       1 1 1 1 1 1 1 1 1 1 1 1             6 6 6 6 6 6 ( ( ( ( ( ( 7 7 7 7 7 7 7 7 7 7 + + + + + + C C C C C C C C  & & & & &29 & & &&& & & & &"- & & &.! ! ! ! !ry ! ! !&" " " " " " " "J. . . . . . . .b
 
 
 
 
 
 
 

 
 
 
 
2= 
 
 
[& [& [& [& [&") [& [& [&|j*,-.$u\3C[%PQRS I 3	>
"	
 k"      * )[	S     [   (    [   (    +   ( ,0M!NOOO26      x/  $  Y\  al       PO  F ,0M!NOOO26      x/  $  Y\  al       PO  F ,0C0R!STTT<@SW      H%89  D  cf  kv       UT  H        Z /3 /3*9=  


r1   