o
    oi)                     @   s<  U d dl mZmZ d dlZd dlm  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 dd	lmZmZmZ G d
d de	Zddde dZeed< dZG dd deZG dd deZG dd dejZ						d&dededededededejfd d!ZG d"d# d#eZ G d$d% d%eZ!dS )'    )ListOptionalN)nn)	TypedDict)ModuleTensorconcatenate)SpatialGradient)pyrdown   )Detector_configMultiResolutionDetectorget_default_detector_configc                   @   s.   e Zd ZU eed< eed< eed< eed< dS )KeyNet_confnum_filters
num_levelskernel_sizeDetector_confN)__name__
__module____qualname__int__annotations__r    r   r   I/home/ubuntu/.local/lib/python3.10/site-packages/kornia/feature/keynet.pyr       s
   
 r            )r   r   r   r   keynet_default_configzXhttps://github.com/axelBarroso/Key.Net-Pytorch/raw/main/model/weights/keynet_pytorch.pthc                       4   e Zd ZdZd	 fddZdedefddZ  ZS )
_FeatureExtractorzVHelper class for KeyNet.

    It loads both, the handcrafted and learnable blocks
    returnNc                    s   t    t | _t | _d S N)super__init___HandcraftedBlockhc_block_LearnableBlocklb_blockself	__class__r   r   r$   9   s   
z_FeatureExtractor.__init__xc                 C   s   |  |}| |}|S r"   )r&   r(   )r*   r-   x_hcx_lbr   r   r   forward?   s   

z_FeatureExtractor.forwardr!   Nr   r   r   __doc__r$   r   r0   __classcell__r   r   r+   r   r    3   s    r    c                       r   )
r%   zaHelper class for KeyNet, it defines the handcrafted filters within the Key.Net handcrafted block.r!   Nc                    s   t    tdd| _d S )Nsobelr   )r#   r$   r	   spatial_gradientr)   r+   r   r   r$   H   s   
z_HandcraftedBlock.__init__r-   c                 C   s  |  |}|d d d d dd d d d f |d d d d dd d d d f }}|  |}|d d d d dd d d d f |d d d d dd d d d f }}|  |}|d d d d dd d d d f }	t|||d |d || ||d ||	||	 g
d}
|
S )Nr   r   g       @)r6   r   )r*   r-   r5   dxdysobel_dxdxxdxysobel_dydyyhc_featsr   r   r   r0   L   s   
F
F
"2z_HandcraftedBlock.forwardr1   r2   r   r   r+   r   r%   E   s    r%   c                       s>   e Zd ZdZddeddf fddZdedefd	d
Z  ZS )r'   zUHelper class for KeyNet.

    It defines the learnable blocks within the Key.Net
    
   in_channelsr!   Nc                    s(   t    t|| _t | _t | _d S r"   )r#   r$   _KeyNetConvBlockconv0conv1conv2)r*   r@   r+   r   r   r$   a   s   

z_LearnableBlock.__init__r-   c                 C   s   |  | | |}|S r"   )rD   rC   rB   )r*   r-   r   r   r   r0   h   s   z_LearnableBlock.forward)r?   )	r   r   r   r3   r   r$   r   r0   r4   r   r   r+   r   r'   [   s    r'      r@   out_channelsr   stridepaddingdilationr!   c              
   C   s,   t t | |||||t |t jddS )zUCreate KeyNet Conv Block.

    Default learnable convolutional block for KeyNet.
    Tinplace)r   
SequentialConv2dBatchNorm2dReLU)r@   rF   r   rG   rH   rI   r   r   r   rA   m   s
   
rA   c                       sF   e Zd ZdZdefdededdf fddZd	edefd
dZ	  Z
S )KeyNetad  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)`

    F
pretrainedkeynet_confr!   Nc                    s   t    |d }|d | _|d }|d }t | _ttj|| j d||dtjdd| _	|rEt
jjtt
d	d
}| j|d dd |   d S )Nr   r   r   rE   r   )r@   rF   r   rH   TrJ   cpu)map_location
state_dict)strict)r#   r$   r   r    feature_extractorr   rL   rM   rO   	last_convtorchhubload_state_dict_from_url
KeyNet_URLdeviceload_state_dicteval)r*   rQ   rR   r   r   rH   pretrained_dictr+   r   r   r$      s    


zKeyNet.__init__r-   c                 C   st   |j }| |g}td| jD ] }t|dd}| |}tj||d |d fdd}|| q| t	|d}|S )zX - input image.r   g333333?)factorrE   r   bilinear)sizemode)
shaperW   ranger   r
   FinterpolateappendrX   r   )r*   r-   shape_imfeats_feats_iscoresr   r   r   r0      s   
zKeyNet.forward)r   r   r   r3   r   boolr   r$   r   r0   r4   r   r   r+   r   rP      s     rP   c                       sN   e Zd ZdZddeddfdedededee	 d	ee	 d
df fddZ
  ZS )KeyNetDetectora  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.

    Fi   NrQ   num_featuresrR   
ori_module
aff_moduler!   c                    s&   t ||}t |||d || d S )Nr   )rP   r#   r$   )r*   rQ   rq   rR   rr   rs   modelr+   r   r   r$      s   
zKeyNetDetector.__init__)r   r   r   r3   r   ro   r   r   r   r   r$   r4   r   r   r+   r   rp      s(    rp   )r   r   r   r   rE   r   )"typingr   r   rY   torch.nn.functionalr   
functionalrg   typing_extensionsr   kornia.corer   r   r   kornia.filtersr	   kornia.geometry.transformr
   scale_space_detectorr   r   r   r   r   r   r\   r    r%   rL   r'   r   rA   rP   rp   r   r   r   r   <module>   sT   		
8