o
    oi=                     @   s   U d dl mZ d dlZd dlmZ d dlmZmZmZmZm	Z	 i Z
eeef ed< de
d< de
d	< d
e
d< G dd deZG dd deZG dd deZdS )    )DictN)nn)Module	ParameterTensortensorzerosurlszXhttps://github.com/ducha-aiki/Key.Net-Pytorch/raw/main/model/HyNet/weights/HyNet_LIB.pthlibertyzWhttps://github.com/ducha-aiki/Key.Net-Pytorch/raw/main/model/HyNet/weights/HyNet_ND.pth	notredamezXhttps://github.com/ducha-aiki/Key.Net-Pytorch/raw/main/model/HyNet/weights/HyNet_YOS.pthyosemitec                       sn   e Zd ZdZ				ddedededed	ed
df fddZdddZd
e	fddZ
ded
efddZ  ZS )FilterResponseNorm2da  Feature Response Normalization layer from 'Filter Response Normalization Layer: Eliminating Batch Dependence
    in the Training of Deep Neural Networks', see :cite:`FRN2019` for more details.

    .. math::
        y =  \gamma \times \frac{x}{\sqrt{\mathrm{E}[x^2]} + |\epsilon|} + \beta


    Args:
        num_features: number of channels
        eps: normalization constant
        is_bias: use bias
        is_scale: use scale
        drop_rate: dropout rate,
        is_eps_leanable: if eps is learnable

    Returns:
        torch.Tensor: Normalized features

    Shape:
        - Input: :math:`(B, \text{num_features}, H, W)`
        - Output: :math:`(B, \text{num_features}, H, W)`

    ư>TFnum_featuresepsis_biasis_scaleis_eps_leanablereturnNc                    s   t    || _|| _|| _|| _|| _tt	d|dddd| _
ttd|dddd| _|r9ttddd| _n	| dt|g |   d S )N   Trequires_gradr   )super__init__r   init_epsr   r   r   r   torchonesweightr   biasr   r   register_bufferreset_parameters)selfr   r   r   r   r   	__class__ H/home/ubuntu/.local/lib/python3.10/site-packages/kornia/feature/hynet.pyr   8   s   
zFilterResponseNorm2d.__init__c                 C   s<   t j| j t j| j | jrt j| j| j	 d S d S N)
r   initones_r   zeros_r   r   	constant_r   r   r!   r$   r$   r%   r    P   s
   z%FilterResponseNorm2d.reset_parametersc                 C      dj di | jS )Nz+num_features={num_features}, eps={init_eps}r$   format__dict__r+   r$   r$   r%   
extra_reprV      zFilterResponseNorm2d.extra_reprxc                 C   sT   | djddgdd}|t|| j   }| jr | j| }| jr(|| j	 }|S )N      T)dimkeepdim)
powmeanr   rsqrtr   absr   r   r   r   )r!   r2   nu2r$   r$   r%   forwardY   s   

zFilterResponseNorm2d.forward)r   TTFr   N)__name__
__module____qualname____doc__intfloatboolr   r    strr0   r   r<   __classcell__r$   r$   r"   r%   r      s,    
r   c                       sT   e Zd ZdZdeddf fddZdddZdefd	d
Zde	de	fddZ
  ZS )TLUa  TLU layer from 'Filter Response Normalization Layer: Eliminating Batch Dependence in the Training of Deep
    Neural Networks, see :cite:`FRN2019` for more details. :math:`{\tau}` is learnable per channel.

    .. math::
        y = \max(x, {\tau})

    Args:
        num_features: number of channels

    Returns:
        torch.Tensor

    Shape:
        - Input: :math:`(B, \text{num_features}, H, W)`
        - Output: :math:`(B, \text{num_features}, H, W)`

    r   r   Nc                    s8   t    || _ttd|dd dd| _|   dS )z:max(y, tau) = max(y - tau, 0) + tau = ReLU(y - tau) + tau.r   Tr   N)r   r   r   r   r   r   taur    )r!   r   r"   r$   r%   r   {   s   
zTLU.__init__c                 C   s   t j| jd d S )N)r   r'   r*   rH   r+   r$   r$   r%   r       s   zTLU.reset_parametersc                 C   r,   )Nznum_features={num_features}r$   r-   r+   r$   r$   r%   r0      r1   zTLU.extra_reprr2   c                 C   s   t || jS r&   )r   maxrH   r!   r2   r$   r$   r%   r<      s   zTLU.forwardr=   )r>   r?   r@   rA   rB   r   r    rE   r0   r   r<   rF   r$   r$   r"   r%   rG   h   s    
rG   c                       sb   e Zd ZdZdZ						dded	ed
ededededdf fddZde	de	fddZ
  ZS )HyNeta9  Module, which computes HyNet descriptors of given grayscale patches of 32x32.

    This is based on the original code from paper
    "HyNet: Learning Local Descriptor with Hybrid Similarity Measure and Triplet Loss".
    See :cite:`hynet2020` for more details.

    Args:
        pretrained: Download and set pretrained weights to the model.
        is_bias: use bias in TLU layers
        is_bias_FRN:  use bias in FRN layers
        dim_desc: descriptor dimensionality,
        drop_rate: dropout rate,
        eps_l2_norm: to avoid div by zero

    Returns:
        HyNet descriptor of the patches.

    Shape:
        - Input: :math:`(B, 1, 32, 32)`
        - Output: :math:`(B, 128)`

    Examples:
        >>> input = torch.rand(16, 1, 32, 32)
        >>> hynet = HyNet()
        >>> descs = hynet(input) # 16x128

        FT   333333?绽|=
pretrainedr   is_bias_FRNdim_desc	drop_rateeps_l2_normr   Nc                    s  t    || _|| _|| _ttd|dtdtj	dddd|dtd|dtd| _
ttj	dddd|dtd|dtd| _ttj	ddddd|dtd|dtd| _ttj	dddd|dtd|dtd| _ttj	dd	ddd|dtd	|dtd	| _ttj	d	d	dd|dtd	|dtd	| _tt| jtj	d	| jd
ddtj| jdd| _td| j d| j dd| _|rtjjtd tdd}| j|dd |   d S )Nr   )r   rM   r4   )kernel_sizepaddingr   @   r3   )rV   striderW   r   rN      F)rV   r   )affineg       @g      ?g        r
   cpu)map_locationT)strict)r   r   rU   rS   rT   r   
Sequentialr   rG   Conv2dlayer1layer2layer3layer4layer5layer6DropoutBatchNorm2dlayer7LocalResponseNorm	desc_normr   hubload_state_dict_from_urlr	   deviceload_state_dicteval)r!   rQ   r   rR   rS   rT   rU   pretrained_dictr"   r$   r%   r      s\   
	







zHyNet.__init__r2   c                 C   sl   |  |}| |}| |}| |}| |}| |}| |}| || j }|	|
dd}|S )Nr   rI   )ra   rb   rc   rd   re   rf   ri   rk   rU   viewsizerK   r$   r$   r%   r<      s   






zHyNet.forward)FTTrN   rO   rP   )r>   r?   r@   rA   
patch_sizerD   rB   rC   r   r   r<   rF   r$   r$   r"   r%   rL      s2    ?rL   )typingr   r   r   kornia.corer   r   r   r   r   r	   rE   __annotations__r   rG   rL   r$   r$   r$   r%   <module>   s   I%