
    3j8                        % S SK JrJrJrJrJr  S SKrS SKJr  0 r\\	\	4   \
S'   S\S'   S\S'   SS	\S
\S\S\R                  4S jjrSS	\S
\S\S\S\S\R                  4S jjr " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      r " S S\R                  5      rg)    )CallableDictListOptionalTypeN)nnurlszMhttp://ptak.felk.cvut.cz/personal/rozumden/defmo_saved_models/encoder_best.ptdefmo_encoderzOhttp://ptak.felk.cvut.cz/personal/rozumden/defmo_saved_models/rendering_best.ptdefmo_rendering	in_planes
out_planesstridereturnc                 0    [         R                  " XSUSS9$ )z1x1 convolution.   F)kernel_sizer   biasr   Conv2d)r   r   r   s      N/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/feature/defmo.pyconv1x1r       s    99Y&uUU    groupsdilationc                 8    [         R                  " U USUUUSUS9$ )z3x3 convolution with padding.   F)r   r   paddingr   r   r   r   )r   r   r   r   r   s        r   conv3x3r   %   s+    99	 	r   c                      ^  \ rS rSr% SrSr\\S'         SS\S\S\S	\\	R                     S
\S\S\S\\S\	R                  4      SS4U 4S jjjrS\R                  S\R                  4S jrSrU =r$ )
Bottleneck3   a  Implement the Bottleneck building block for ResNet.

This block follows the ResNet V1.5 design where the stride is applied to the 3x3 convolution
instead of the first 1x1 convolution to better preserve spatial information.

Args:
    inplanes: The number of input channels.
    planes: The number of intermediate channels.
    stride: The stride size for the convolution. Default: 1.
    downsample: An optional module to downsample the input identity. Default: None.
    groups: The number of blocked connections from input channels to output channels. Default: 1.
    base_width: The width of each group. Default: 64.
    dilation: The spacing between kernel elements. Default: 1.
    norm_layer: The normalization layer to use. Default: None.
   	expansionNinplanesplanesr   
downsampler   
base_widthr   
norm_layer.r   c	                   > [         T
U ]  5         Uc  [        R                  n[	        X&S-  -  5      U-  n	[        X5      U l        U" U	5      U l        [        XX5U5      U l	        U" U	5      U l
        [        XU R                  -  5      U l        U" X R                  -  5      U l        [        R                  " SS9U l        X@l        X0l        g )Ng      P@Tinplace)super__init__r   BatchNorm2dintr   conv1bn1r   conv2bn2r#   conv3bn3ReLUrelur&   r   )selfr$   r%   r   r&   r   r'   r   r(   width	__class__s             r   r-   Bottleneck.__init__L   s     	JF4/01F:X-
e$U68D
e$UT^^$;<
f~~56GGD)	$r   xc                    UnU R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU R	                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU R                  b  U R                  U5      nX2-  nU R                  U5      nU$ N)r0   r1   r7   r2   r3   r4   r5   r&   )r8   r<   identityouts       r   forwardBottleneck.forwardf   s    jjmhhsmiinjjohhsmiinjjohhsm??&q)Hiin
r   )	r1   r3   r5   r0   r2   r4   r&   r7   r   )r   Nr   @   r   N)__name__
__module____qualname____firstlineno____doc__r#   r/   __annotations__r   r   Moduler   r-   torchTensorrA   __static_attributes____classcell__r:   s   @r   r    r    3   s    , Is *.9=  	
 RYY'    Xc299n56 
 4 %,,  r   r    c                   V  ^  \ rS rSrSr      SS\\   S\\   S\S\	S\S	\S
\
\\	      S\
\S\R                  4      SS4U 4S jjjr SS\\   S\S\S\S\	S\R                  4S jjrS\R$                  S\R$                  4S jrS\R$                  S\R$                  4S jrSrU =r$ )ResNet}   a+  Implement the ResNet architecture for feature extraction.

This implementation provides a flexible backbone used as the encoder in the DeFMO framework.

Args:
    block: The block type to use, typically :class:`Bottleneck`.
    layers: A list containing the number of blocks in each of the four stages.
    num_classes: The number of output classes. Default: 1000.
    zero_init_residual: Whether to initialize the last batch norm in each residual branch to zero. Default: False.
    groups: The number of groups for the convolution. Default: 1.
    width_per_group: The width of each group. Default: 64.
    replace_stride_with_dilation: A list of booleans indicating
        if stride should be replaced by dilation in each stage. Default: None.
    norm_layer: The normalization layer to use. Default: None.
Nblocklayersnum_classeszero_init_residualr   width_per_groupreplace_stride_with_dilationr(   .r   c	           	      2  > [         T
U ]  5         Uc  [        R                  nXl        SU l        SU l        Uc  / SQn[        U5      S:w  a  [        SU 35      eXPl	        X`l
        [        R                  " SU R
                  SSSSS	9U l        U" U R
                  5      U l        [        R                  " S
S9U l        [        R                   " SSSS9U l        U R%                  USUS   5      U l        U R%                  USUS   SUS   S9U l        U R%                  USUS   SUS   S9U l        U R%                  USUS   SUS   S9U l        [        R.                  " S5      U l        [        R2                  " SUR4                  -  U5      U l        U R9                  5        H  n	[;        U	[        R                  5      (       a+  [        R<                  R?                  U	R@                  SSS9  MM  [;        U	[        R                  [        RB                  45      (       d  M~  [        R<                  RE                  U	R@                  S5        [        R<                  RE                  U	RF                  S5        M     U(       a  U R9                  5        H  n	[;        U	[H        5      (       d  M  [;        U	RJ                  R@                  [L        RN                  5      (       d  MO  [        R<                  RE                  U	RJ                  R@                  S5        M     g g )NrC   r   )FFFr   zFreplace_stride_with_dilation should be None or a 3-element tuple, got       Fr   r   r   r   Tr*   )r   r   r   r      )r   dilate   i   )r   r   fan_outr7   )modenonlinearity)(r,   r-   r   r.   _norm_layerr$   r   len
ValueErrorr   r'   r   r0   r1   r6   r7   	MaxPool2dmaxpool_make_layerlayer1layer2layer3layer4AdaptiveAvgPool2davgpoolLinearr#   fcmodules
isinstanceinitkaiming_normal_weight	GroupNorm	constant_r   r    r5   rK   rL   )r8   rS   rT   rU   rV   r   rW   rX   r(   mr:   s             r   r-   ResNet.__init__   sk    	J%'/ ,A(+,1XYuXvw  )YYq$--QqRSZ_`
dmm,GGD)	||!QG&&ub&)<&&uc6!9QOklmOn&o&&uc6!9QOklmOn&o&&uc6!9QOklmOn&o++F3))C%//1;?A!RYY''''yv'VA=>>!!!((A.!!!&&!,   \\^a,,AEELL%,,1W1WGG%%aeellA6 $ r   r%   blocksr   r^   c                    U R                   nS nU R                  nU(       a  U =R                  U-  sl        SnUS:w  d  U R                  X!R                  -  :w  aJ  [        R
                  " [        U R                  X!R                  -  U5      U" X!R                  -  5      5      n/ n	U	R                  U" U R                  X$XpR                  U R                  X5      5        X!R                  -  U l        [        SU5       HE  n
U	R                  U" U R                  UU R                  U R                  U R                  US95        MG     [        R
                  " U	6 $ )Nr   )r   r'   r   r(   )rc   r   r$   r#   r   
Sequentialr   appendr   r'   range)r8   rS   r%   rz   r   r^   r(   r&   previous_dilationrT   _s              r   rh   ResNet._make_layer   s    %%

 MMMMV#MFQ;$--6OO+CCv'?H*U[^m^mUmJnJ vz;;Yj	

 0q&!AMMMM;;#!]])	 " }}f%%r   r<   c                    U R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU R	                  U5      nU R                  U5      nU R                  U5      nU R                  U5      nU R                  U5      n[        R                  " US5      nU R                  U5      nU$ )Nr   )r0   r1   r7   rg   ri   rj   rk   rl   rn   rK   flattenrp   r8   r<   s     r   _forward_implResNet._forward_impl   s    JJqMHHQKIIaLLLOKKNKKNKKNKKNLLOMM!QGGAJr   c                 $    U R                  U5      $ r>   )r   r   s     r   rA   ResNet.forward   s    !!!$$r   )rc   rn   r'   r1   r0   r   rp   r   r$   ri   rj   rk   rl   rg   r7   )i  Fr   rC   NN)r   F)rD   rE   rF   rG   rH   r   r    r   r/   boolr   r   r   rJ   r-   r|   rh   rK   rL   r   rA   rM   rN   rO   s   @r   rQ   rQ   }   s$   (  #(!=A9=47J47 S	47 	47
 !47 47 47 '/tDz&:47 Xc299n5647 
47 47n bg!&*%!&/2!&<?!&IL!&Z^!&	!&Fu||  $% %%,, % %r   rQ   c                   j   ^  \ rS rSrSrSU 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	EncoderDeFMO   a  Implement the Encoder module for the Deblurring Fast Moving Objects (DeFMO) model.

The encoder extracts latent features from the concatenation of the blurred input image and
the estimated background. It uses a modified ResNet-50 backbone to accept 6-channel inputs.


Shape:
    - Input: (B, 6, H, W) where 6 represents the concatenated blurred image and background.
    - Output: A list of feature maps from different stages of the ResNet backbone.
r   c           	      h  > [         TU ]  5         [        [        / SQ5      n[        R
                  " [        UR                  5       5      S S 6 n[        R
                  " [        UR                  5       5      SS 6 n[        R                  " SSSSS	S
S9US'   [        R
                  " X#5      U l	        g )N)r   r"      r   r   r"      r   rC   )rZ   rZ   )r[   r[   )r   r   Fr\   r   )
r,   r-   rQ   r    r   r|   listchildrenr   net)r8   modelmodelc1modelc2r:   s       r   r-   EncoderDeFMO.__init__	  s    z<0--enn&6!7!;<--enn&6!7!!<=YYq"&QW^cd
==2r   
input_datac                 $    U R                  U5      $ r>   r   )r8   r   s     r   rA   EncoderDeFMO.forward  s    xx
##r   r   r   NrD   rE   rF   rG   rH   r-   rK   rL   rA   rM   rN   rO   s   @r   r   r      s-    	3$%,, $5<< $ $r   r   c                   j   ^  \ rS rSrSrSU 4S jjrS\R                  S\R                  4S jrSr	U =r
$ )	RenderingDeFMOi  a  Implement the Rendering module for the Deblurring Fast Moving Objects (DeFMO) model.

This module acts as a decoder that transforms the latent features from the encoder into
a temporal sequence of sharp sub-frames, recovering the object's appearance and motion.

Shape:
    - Input: Latent feature representation from the :class:`EncoderDeFMO`.
    - Output: (B, T, 4, H, W) where T is the number of sub-frames and 4 represents RGBA channels.
r   c                   > [         TU ]  5         SU l        [        R                  " [        R
                  " SSSSSSS9[        R                  " SSS	S
S
S9[        R                  " S
S9[        SS5      [        R                  " S5      [        SS5      [        R                  " S5      [        SS5      [        R                  " S5      [        R
                  " SSSSSSS9[        R                  " S5      [        R
                  " SSSSSS
S9[        R                  " S
S9[        R
                  " SSSSSS
S95      nXl
        [        R                  " SSU R                  5      U l        g )N   i  i   r   r   Fr\   gh㈵>g?T)epsmomentumaffinetrack_running_statsr*   r_   r[   rC      r"   r   )r,   r-   	tsr_stepsr   r|   r   r.   r6   r    PixelShuffler   rK   linspacetimes)r8   r   r:   s     r   r-   RenderingDeFMO.__init__   s    IIdDa15QNN4US[_`GGD!tS!OOAsBOOAr2OOAIIb"!AquMOOAIIa!QTJGGD!IIa!QTJ
  ^^Aq$..9
r   latentc                    U R                   R                  UR                  5      R                  S5      R	                  UR
                  S   S5      n/ n[        UR
                  S   5       H  nU[        [        UR
                  S   5      5      U4   R                  S5      R                  S5      R                  S5      R	                  SSUR
                  S   UR
                  S   5      n[        R                  " XQ4S5      nU R                  U5      nUR                  U5        M     [        R                  " US5      R                  5       n[        R                  " US S 2S S 2S S24   5      US S 2S S 2S S24'   U$ )Nr   r   r[   r   r"   )r   todevice	unsqueezerepeatshaper~   r   rK   catr   r}   stack
contiguoussigmoid)	r8   r   r   renderskit_tensorlatentiresultrenders_stackeds	            r   rA   RenderingDeFMO.forward6  s0   

fmm,66q9@@aRSTA'Bd5Q012562221fll1ov||A?  ii 2A6GXXg&FNN6" (  ++gq1<<>$)MM/!Q(2K$L1bqb!r   )r   r   r   r   r   rO   s   @r   r   r     s-    :,ell u||  r   r   c                   v   ^  \ rS rSrSrS
S\SS4U 4S jjjrS\R                  S\R                  4S jr	S	r
U =r$ )DeFMOiI  aq  nn.Module that disentangle a fast-moving object from the background and performs deblurring.

This is based on the original code from paper "DeFMO: Deblurring and Shape Recovery
    of Fast Moving Objects". See :cite:`DeFMO2021` for more details.

Args:
    pretrained: Download and set pretrained weights to the model. Default: false.

Returns:
    Temporal super-resolution without background.
Shape:
    - Input: (B, 6, H, W)
    - Output: (B, S, 4, H, W)

Examples:
    >>> import kornia
    >>> input = torch.rand(2, 6, 240, 320)
    >>> defmo = kornia.feature.DeFMO()
    >>> tsr_nobgr = defmo(input) # 2x24x4x240x320


pretrainedr   Nc                   > [         TU ]  5         [        5       U l        [	        5       U l        U(       a  [        R                  R                  [        S   [        R                  " S5      S9nU R                  R                  USS9  [        R                  R                  [        S   [        R                  " S5      S9nU R
                  R                  USS9  U R                  5         g )Nr
   cpu)map_locationT)strictr   )r,   r-   r   encoderr   	renderingrK   hubload_state_dict_from_urlr	   r   load_state_dicteval)r8   r   pretrained_dictpretrained_dict_renr:   s       r   r-   DeFMO.__init__`  s    #~') #ii@@_%ELL4G A O LL(((F"'))"D"D&'ell56I #E # NN**+>t*L		r   r   c                 J    U R                  U5      nU R                  U5      nU$ r>   r   r   )r8   r   r   x_outs       r   rA   DeFMO.forwardq  s#    j)v&r   r   )F)rD   rE   rF   rG   rH   r   r-   rK   rL   rA   rM   rN   rO   s   @r   r   r   I  s@    ,4 D  "%,, 5<<  r   r   )r   )r   r   r   )typingr   r   r   r   r   rK   r   r	   strrI   r/   r   r   r   rJ   r    rQ   r   r   r    r   r   <module>r      s   $ 8 7  d38n g_ k Vs V VS V V
s  S c Y\ egenen G GT}%RYY }%@$299 $01RYY 1h+BII +r   