
    3jj                        % S SK Jr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  S SKJr  SSKJrJrJr   " S	 S
\	5      rSSS\" 5       S.r\\S'   Sr " S S\R,                  5      r " S S\R,                  5      r " S S\R2                  5      r      S#S\S\S\S\S\S\S\R2                  4S jjr " S S \R,                  5      r " S! S"\5      rg)$    )ListOptionalN)nn)	TypedDict)SpatialGradient)pyrdown   )Detector_configMultiResolutionDetectorget_default_detector_configc                   B    \ rS rSr% Sr\\S'   \\S'   \\S'   \\S'   Srg)	KeyNet_conf   zDefine the configuration schema for the KeyNet feature detector.

Attributes:
    num_filters: The number of filters in the convolutional layers.
    num_levels: The number of levels in the image pyramid.
num_filters
num_levelskernel_sizeDetector_conf N)	__name__
__module____qualname____firstlineno____doc__int__annotations__r
   __static_attributes__r       O/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/feature/keynet.pyr   r      s      O""r   r            )r   r   r   r   keynet_default_configzXhttps://github.com/axelBarroso/Key.Net-Pytorch/raw/main/model/weights/keynet_pytorch.pthc                   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
$ )	_FeatureExtractor9   zNHelper class for KeyNet.

It loads both, the handcrafted and learnable blocks
returnc                 ^   > [         TU ]  5         [        5       U l        [	        5       U l        g N)super__init___HandcraftedBlockhc_block_LearnableBlocklb_blockself	__class__s    r   r*   _FeatureExtractor.__init__?   s"    )+')r   xc                 J    U R                  U5      nU R                  U5      nU$ r(   r,   r.   )r0   r3   x_hcx_lbs       r   forward_FeatureExtractor.forwardE   s#    }}Q}}T"r   r5   r&   Nr   r   r   r   r   r*   torchTensorr8   r   __classcell__r1   s   @r   r$   r$   9   s-    
* %,,  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
$ )	r+   K   zaHelper class for KeyNet, it defines the handcrafted filters within the Key.Net handcrafted block.r&   c                 D   > [         TU ]  5         [        SS5      U l        g )Nsobelr	   )r)   r*   r   spatial_gradientr/   s    r   r*   _HandcraftedBlock.__init__N   s     / ;r   r3   c                    U R                  U5      nUS S 2S S 2SS S 2S S 24   US S 2S S 2SS S 2S S 24   pCU R                  U5      nUS S 2S S 2SS S 2S S 24   US S 2S S 2SS S 2S S 24   pvU R                  U5      S S 2S S 2SS S 2S S 24   n[        R                  " X4UR                  5       UR                  5       X4-  XwR                  5       XhXh-  /
S5      n	U	$ )Nr   r	   )rD   r<   catsquare)
r0   r3   rC   dxdysobel_dxdxxdxydyyhc_featss
             r   r8   _HandcraftedBlock.forwardR   s    %%a(q!Q1}%uQ1a]';B((,Aq!QM*HQ1a],CS ##B'1aA699bbiik299;jjl\_fifoprstr   )rD   r:   r;   r?   s   @r   r+   r+   K   s+    k< %,,  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$ )r-   a   zMHelper class for KeyNet.

It defines the learnable blocks within the Key.Net
in_channelsr&   Nc                 ~   > [         TU ]  5         [        U5      U l        [        5       U l        [        5       U l        g r(   )r)   r*   _KeyNetConvBlockconv0conv1conv2)r0   rS   r1   s     r   r*   _LearnableBlock.__init__g   s.    %k2
%'
%'
r   r3   c                 d    U R                  U R                  U R                  U5      5      5      nU$ r(   )rX   rW   rV   )r0   r3   s     r   r8   _LearnableBlock.forwardn   s&    JJtzz$**Q-01r   )rV   rW   rX   )
   )r   r   r   r   r   r   r*   r<   r=   r8   r   r>   r?   s   @r   r-   r-   a   s@    
(C ( ( ( %,,  r   r-   rS   out_channelsr   stridepaddingdilationr&   c           
          [         R                  " [         R                  " XX#XE5      [         R                  " U5      [         R                  " SS95      $ )zMCreate KeyNet Conv Block.

Default learnable convolutional block for KeyNet.
Tinplace)r   
SequentialConv2dBatchNorm2dReLU)rS   r]   r   r^   r_   r`   s         r   rU   rU   s   s=     ==
		+['T
|$
 r   c                      ^  \ rS rSrSrSS\S\\   SS4U 4S jjjrS\	R                  S\	R                  4S	 jrS
rU =r$ )KeyNet   a0  Key.Net model definition -- local feature detector (response function).

This is based on the original code
from paper "Key.Net: Keypoint Detection by Handcrafted and Learned CNN Filters". See :cite:`KeyNet2019` for
more details.

.. image:: _static/img/KeyNet.png

Args:
    pretrained: Download and set pretrained weights to the model.
    keynet_conf: Dict with initialization parameters. Do not pass it, unless you know what you are doing`.

Returns:
    KeyNet response score.

Shape:
    - Input: :math:`(B, 1, H, W)`
    - Output: :math:`(B, 1, H, W)`

N
pretrainedkeynet_confr&   c           	        > [         TU ]  5         Uc  [        nUS   nUS   U l        US   nUS-  n[	        5       U l        [        R                  " [        R                  " X0R                  -  SXES9[        R                  " SS95      U l
        U(       aI  [        R                  R                  [        [        R                  " S	5      S
9nU R!                  US   SS9  U R#                  5         g )Nr   r   r      r	   )rS   r]   r   r_   Trb   cpu)map_location
state_dict)strict)r)   r*   r"   r   r$   feature_extractorr   rd   re   rg   	last_convr<   hubload_state_dict_from_url
KeyNet_URLdeviceload_state_dicteval)r0   rk   rl   r   r   r_   pretrained_dictr1   s          r   r*   KeyNet.__init__   s    /K!-0%l3!-0"!2!4II'//9Wb GGD!	
 #ii@@Z_ZfZfglZm@nO  !>t L		r   r3   c                 r   UR                   S   UR                   S   p2U R                  U5      /n[        SU R                  5       HG  n[	        USS9nU R                  U5      n[
        R                  " XbU4SSS9nUR                  U5        MI     U R                  [        R                  " US5      5      nU$ )	zX - input image.rn   r    r	   g333333?)factorbilinearF)sizemodealign_corners)shapers   ranger   r   Finterpolateappendrt   r<   rG   )r0   r3   hwfeats_feats_iscoress           r   r8   KeyNet.forward   s    wwqz1771:1%)%;%;A%>$?q$//*A#&A,,Q/GmmGa&zY^_GLL!	 +
 		% 34r   )rs   rt   r   )FN)r   r   r   r   r   boolr   r   r*   r<   r=   r8   r   r>   r?   s   @r   ri   ri      sM    *4 h{>S _c  .
 
%,, 
 
r   ri   c                      ^  \ rS rSrSr       SS\S\S\\   S\\	R                     S\\	R                     S	\S
\SS4U 4S jjjrSrU =r$ )KeyNetDetector   aN  Multi-scale feature detector based on KeyNet.

This is based on the original code from paper
"Key.Net: Keypoint Detection by Handcrafted and Learned CNN Filters".
See :cite:`KeyNet2019` for more details.

.. image:: _static/img/keynet.jpg

Args:
    pretrained: Download and set pretrained weights to the model.
    num_features: Number of features to detect.
    keynet_conf: Dict with initialization parameters. Do not pass it, unless you know what you are doing`.
    ori_module: for local feature orientation estimation. Default: :class:`~kornia.feature.PassLAF`,
       which does nothing. See :class:`~kornia.feature.LAFOrienter` for details.
    aff_module: for local feature affine shape estimation. Default: :class:`~kornia.feature.PassLAF`,
        which does nothing. See :class:`~kornia.feature.LAFAffineShapeEstimator` for details.

Nrk   num_featuresrl   
ori_module
aff_modulecompile_modelscore_thresholdr&   c           	      X   > Uc  [         n[        X5      n[        T	U ]  XUS   XEXg5        g )Nr   )r"   ri   r)   r*   )
r0   rk   r   rl   r   r   r   r   modelr1   s
            r   r*   KeyNetDetector.__init__   s6     /Kz/_!=zWd	
r   r   )Fi   NNNFg        )r   r   r   r   r   r   r   r   r   r   Modulefloatr*   r   r>   r?   s   @r   r   r      s    * ! -1*.*.#!$

 
 k*	

 RYY'
 RYY'
 
 
 

 
r   r   )r   r   r!   r	   rn   r	   )typingr   r   r<   torch.nn.functionalr   
functionalr   typing_extensionsr   kornia.filtersr   kornia.geometry.transformr   scale_space_detectorr
   r   r   r   r"   r   rw   r   r$   r+   rd   r-   r   rU   ri   r   r   r   r   <module>r      s	  $ " !     ' * - g g#) #  02& {  h
		 $		 ,bmm &   	
   ]]&7RYY 7t#
, #
r   