
    3j0                        S SK r S SKJr  S SKrS SKJs  Jr  S SKJr  S SKJ	r	  S SK
JrJr  S SKJr  S\S\S	\S
\R                   4S jrSS\S
\R                   4S jjrS\S\S
\\\\4   4S jr " S S\R(                  5      r     SS\R                   S\S\S\S\S\S
\R                   4S jjr " S S\R(                  5      rg)    N)Tuple)nn)KORNIA_CHECK_SHAPE)get_gaussian_kernel2dspatial_gradient)pikdkykxreturnc                 J   X-  U-  nSnX4::  ap  [        [        S5      (       d  0 [        l        [        R                  nUR                  U5      nUc  [        R
                  " U5      nXeU'   UR                  X0X5      $ [        R
                  " U5      R                  X0X5      $ )z"Return neigh2channels conv kernel.i   
_eye_cache)hasattr_get_reshape_kernelr   gettorcheyeview)r	   r
   r   numel_MAX_CACHEDcacheress          Q/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/feature/siftdesc.pyr   r      s    2E K*L99-/*#..ii;))E"C%Lxx2** yy$$U77    ksizec                     [        U 5      S-  nU[        R                  " U 5      R                  5       S-   U-
  R                  5       -
  n[        R                  " X"5      US-  -  nU$ )zReturn a weighted pooling kernel for SIFT descriptor.

Args:
    ksize: kernel_size.

Returns:
    the pooling kernel with shape :math:`(ksize, ksize)`.

       @g      ?   )floatr   arangeabsger)r   ks_2xc2kernels       r   get_sift_pooling_kernelr&   5   s]     ,$D
%,,u%++-3d:??A
ACYYs D!G,FMr   
patch_sizenum_spatial_binsc                     S[        XS-   -  5      -  nX-  nUS-  nU SU-  -   US-
  -
  S-
  U-  S-   nXQ:w  a  [        SU  SU S35      eX#U4$ )zReturn a tuple with SIFT parameters.

Args:
    patch_size: the given patch size.
    num_spatial_bins: the ggiven number of spatial bins.

Returns:
    ksize, stride, pad.

r         zPatch size zC is incompatible with             requested number of spatial bins z{             for SIFT descriptor. Usually it happens when patch size is too small            for num_spatial_bins specified)int
ValueError)r'   r(   r   stridepadout_sizes         r   get_sift_bin_ksize_stride_padr1   E   s     S!';<==E0FzC!c')UQY7!;FJH#* & ! "44
 	

 #r   c                      ^  \ rS rSrSrS\4S jr     SS\S\S\S\S	\	SS
4U 4S jjjr
S\R                  4S jrS\R                  4S jrS\R                  S\R                  4S jrSrU =r$ )SIFTDescriptor]   u  nn.Module which computes SIFT descriptors of given patches.

Args:
    patch_size: Input patch size in pixels.
    num_ang_bins: Number of angular bins.
    num_spatial_bins: Number of spatial bins.
    clipval: clipping value to reduce single-bin dominance
    rootsift: if ``True``, RootSIFT (Arandjelović et. al, 2012) is computed.

Returns:
    SIFT descriptor of the patches with shape.

Shape:
    - Input: :math:`(B, 1, \text{num_spatial_bins}, \text{num_spatial_bins})`
    - Output: :math:`(B, \text{num_ang_bins * num_spatial_bins ** 2})`

Example:
    >>> input = torch.rand(23, 1, 32, 32)
    >>> SIFT = SIFTDescriptor(32, 8, 4)
    >>> descs = SIFT(input) # 23x128

r   c                     U R                   R                   SU R                   SU R                   SU R                   SU R
                   SU R                   S3$ )N(num_ang_bins=, num_spatial_bins=z, patch_size=, rootsift=
, clipval=))	__class____name__num_ang_binsr(   r'   rootsiftclipvalselfs    r   __repr__SIFTDescriptor.__repr__u   sk    ~~&&' ( --. /  $ 5 56 7//* + '||nA'	
r   r'   r=   r(   r>   r?   Nc           
         > [         T	U ]  5         SU l        X l        X0l        XPl        X@l        Xl        U R                  n[        U5      [        R                  " S5      -  n[        Xf4Xw4S5      U l        [        X5      u  U l        U l        U l        [#        U R                  S9R                  5       n[$        R&                  " SSUR)                  S5      UR)                  S5      4U R                  U R                  4U R                   U R                   4SS9U l        U R*                  R,                  R.                  R1                  UR3                  SSUR)                  S5      UR)                  S5      5      5        g )	N绽|=r   Tr   r*   r   F)kernel_sizer.   paddingbias)super__init__epsr=   r(   r?   r>   r'   r   mathsqrtr   gkr1   	bin_ksize
bin_strider/   r&   r   Conv2dsizepkweightdatacopy_reshape)
rA   r'   r=   r(   r>   r?   kssigmanwr;   s
            r   rK   SIFTDescriptor.__init__   s"    	( 0 $//Ry499S>1'5.$G6ST^6q3$($4>>:@@B))RWWQZ0OOT__5XXtxx(
 	!!"**Q2771:rwwqz"JKr   c                 J    U R                   R                  R                  5       $ N)rT   rU   detachr@   s    r   get_pooling_kernel!SIFTDescriptor.get_pooling_kernel   s    ww~~$$&&r   c                 6    U R                   R                  5       $ r^   )rO   r_   r@   s    r   get_weighting_kernel#SIFTDescriptor.get_weighting_kernel   s    ww~~r   inputc                    [        USSU R                   U R                   /5        UR                  S   nU R                  R	                  UR
                  5      R	                  UR                  5      U l        [        US5      nUS S 2S S 2S4   nUS S 2S S 2S4   n[        R                  " XD-  XU-  -   U R                  -   5      n[        R                  " XTU R                  -   5      S[        -  -   nX`R                  R                  U5      R                  U5      R	                  UR                  5      -  n[!        U R"                  5      U-  S[        -  -  n[        R$                  " U5      n	X-
  n
XR"                  -  nUS-   U R"                  -  nSU
-
  U-  nX-  n[        R&                  " [)        SU R"                  5       Vs/ s HS  nU R                  X:H  R	                  UR
                  5      U-  X:H  R	                  UR
                  5      U-  -   5      PMU     snS5      nUR+                  US5      n[,        R.                  " US	S
9n[        R0                  " US[!        U R2                  5      5      n[,        R.                  " US	S
9nU R4                  (       a6  [        R                  " [,        R.                  " USS
9U R                  -   5      nU$ s  snf )NB1r   diffr*   r         ?r   pg        )r   r'   shaperT   todtypedevicer   r   rN   rL   atan2r   rO   	expand_astype_asr   r=   floorcatranger   F	normalizeclampr?   r>   )rA   re   rg   gradsgxgymagorio_bigbo0_big_wo1_big_bo0_bigbo1_bigwo0_bigwo1_bigiang_binss                    r   forwardSIFTDescriptor.forward   sW   53/@dooEV"XYQ''**U[[),,U\\: /1a7^1a7^jj27*TXX56kk"488m,sRx7GG%%c*2237::3::FFd''(3.#(;;;u%#...Q;$"3"33>S(.99 q$"3"344A ))%++6@GLCTCTUZU`U`CadkCkkl4 
 ==B';;x1-;;xeDLL.AB;;x1-==zz!++h!"<txx"GHHs   	AK)rP   rQ   r?   rL   rO   r=   r(   r/   r'   rT   r>   )      r+   T皙?)r<   
__module____qualname____firstlineno____doc__strrB   r,   boolr   rK   r   Tensorr`   rc   r   __static_attributes____classcell__r;   s   @r   r3   r3   ]   s    .
# 
  !LL L 	L
 L L 
L LB'ELL ' ell  #U\\ #ell # #r   r3   re   r=   r>   r?   c                 (    [        XX4U5      " U 5      $ )zXCompute the sift descriptor.

See
:class: `~kornia.feature.SIFTDescriptor` for details.
)r3   )re   r'   r=   r(   r>   r?   s         r   sift_describer      s     *4DPWXY^__r   c                      ^  \ rS rSrSrS\4S jr       SS\S\S\S\S	\	S
\S\SS4U 4S jjjr
S\R                  4S jrS\R                  S\R                  4S jrSrU =r$ )DenseSIFTDescriptor   ud  nn.Module, which computes SIFT descriptor densely over the image.

Args:
    num_ang_bins: Number of angular bins. (8 is default)
    num_spatial_bins: Number of spatial bins per descriptor (4 is default).
You might want to set odd number and relevant padding to keep feature map size
    spatial_bin_size: Size of a spatial bin in pixels (4 is default)
    clipval: clipping value to reduce single-bin dominance
    rootsift: (bool) if True, RootSIFT (Arandjelović et. al, 2012) is computed
    stride: default 1
    padding: default 0

Returns:
    torch.Tensor: DenseSIFT descriptor of the image

Shape:
    - Input: (B, 1, H, W)
    - Output: (B, num_ang_bins * num_spatial_bins ** 2, (H+padding)/stride, (W+padding)/stride)

Examples::
    >>> input =  torch.rand(2, 1, 200, 300)
    >>> SIFT = DenseSIFTDescriptor()
    >>> descs = SIFT(input) # 2x128x194x294

r   c                     U R                   R                   SU R                   SU R                   SU R                   SU R
                   SU R                   SU R                   S3$ )Nr6   r7   z, spatial_bin_size=r8   z	, stride=r9   r:   )r;   r<   r=   r(   spatial_bin_sizer>   r.   r?   r@   s    r   rB   DenseSIFTDescriptor.__repr__   s{    ~~&&' ( --. /  $ 5 56 7  $ 5 56 7 'kk] #||nA'	
r   r=   r(   r   r>   r?   r.   rH   Nc                   > [         TU ]  5         SU l        Xl        X l        X0l        XPl        X@l        X`l        Xpl	        [        U R
                  S9R                  5       nU R                  SUR                  SSUR                  S5      UR                  S5      5      5        [        R                   " SSUR                  S5      UR                  S5      4SSUR                  S5      S-  UR                  S5      S-  4S	9n	U	R"                  R$                  R'                  U R(                  5        Xl        [-        XU5      R                  5       n
U R                  S
U
5        [        R                   " UXS-  -  X"4U R                  U R                  4SU R                  U R                  4S	9nUR"                  R$                  R'                  U R.                  5        Xl        U R(                  R3                  5       U l        g )NrE   rF   _bin_pooling_kernel_weightr*   r   )r*   r*   Fr   )rG   r.   rI   rH   _poolingconv_weight)rJ   rK   rL   r=   r(   r   r?   r>   r.   r/   r&   r   register_bufferrX   rS   r   rR   rU   rV   rW   r   bin_pooling_kernelr   r   PoolingConvr_   _pooling_kernel)rA   r=   r(   r   r>   r?   r.   rH   r[   r   Pwr   r;   s               r   rK   DenseSIFTDescriptor.__init__  s    	( 0 0  %4+@+@AGGI92::aBGGTUJXZX_X_`aXb;cdYYRWWQZ0WWQZ1_bggajAo6
 	!!&&,,T-L-LM"4 AQRXXZ2B7iiQ..)<KK-XXtxx(
 	%%d&>&>?&  $>>EEGr   c                     U R                   $ r^   )r   r@   s    r   r`   &DenseSIFTDescriptor.get_pooling_kernel4  s    ###r   re   c                    [        U/ SQ5        UR                  5       u  p#pEU R                  R                  UR                  5      R                  UR
                  5      U l        U R                  R                  UR                  5      R                  UR
                  5      U l        [        US5      nUS S 2S S 2S4   nUS S 2S S 2S4   n[        R                  " Xw-  X-  -   U R                  -   5      n	[        R                  " XU R                  -   5      S[        -  -   n
[        U R                  5      U
-  S[        -  -  n[        R                  " U5      nX-
  nXR                  -  nUS-   U R                  -  nSU-
  U	-  nX-  n[        R                   " [#        SU R                  5       Vs/ s HU  nU R                  UU:H  R                  UR                  5      U-  UU:H  R                  UR                  5      U-  -   5      PMW     snS5      nU R                  U5      n[$        R&                  " USSS9R)                  S[        U R*                  5      5      n[$        R&                  " USSS9nU R,                  (       a6  [        R                  " [$        R&                  " USS	9U R                  -   5      nU$ s  snf )
N)rg   rh   HWri   r   r*   r   rj   r   )dimrm   rl   )r   rS   r   ro   rp   rq   r   r   r   rN   rL   rr   r   r   r=   ru   rv   rw   rx   ry   clamp_r?   r>   )rA   re   _B_CH_W_Hr{   r|   r}   r~   r   r   r   r   r   r   r   r   r   r   out_no_normouts                         r   r   DenseSIFTDescriptor.forward8  sO   5"67**,"&"9"9"<"<U[["I"L"LU\\"Z++..u{{;>>u||L /1a7^1a7^jj27*TXX56kk"488m,sRx7d''(3.#(;;;u%#...Q;$"3"33>S(.99
 q$"3"34	 5A ''\%%ekk2W<1?P?PQVQ\Q\?]`g?gg 5	 
 &&x0kk+1299!U4<<=PQkk#1*==**Q[[2TXX=>C
s   3AK )r   r   r   r?   rL   r=   r(   r/   r>   r   r.   )r   r+   r+   Tr   r*   r*   )r<   r   r   r   r   r   rB   r,   r   r   rK   r   r   r`   r   r   r   r   s   @r   r   r      s    4	
# 	
  ! !0H0H 0H 	0H
 0H 0H 0H 0H 
0H 0Hd$ELL $#U\\ #ell # #r   r   )   r   )rM   typingr   r   torch.nn.functionalr   
functionalrx   kornia.core.checkr   kornia.filtersr   r   kornia.geometry.conversionsr   r,   r   r   r&   r1   Moduler3   r   r   r   r    r   r   <module>r      s  $       0 B *8C 8S 8c 8ell 8.3   c S USVX[]`S`Ma 0lRYY lb `<<`` ` 	`
 ` ` \\` ")) r   