
    3jI                         % S SK Jr  S SKrS SKJr  0 r\\\4   \S'   S\S'   S\S'   S	\S
'    " S S\R                  5      r " S S\R                  5      r	 " S S\R                  5      r
g)    )DictN)nn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                      ^  \ rS rSrSr    SS\S\S\S\S\SS	4U 4S
 jjjrSS jr	S\
4S jrS\R                  S\R                  4S jrSrU =r$ )FilterResponseNorm2d   ax  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)`

num_featuresepsis_biasis_scaleis_eps_leanablereturnNc                   > [         TU ]  5         Xl        X l        XPl        X0l        X@l        [        R                  " [        R                  " SUSS5      SS9U l        [        R                  " [        R                  " SUSS5      SS9U l        U(       a/  [        R                  " [        R                  " S5      SS9U l        O'U R!                  S[        R                  " U/5      5        U R#                  5         g )N   Trequires_gradr   )super__init__r   init_epsr   r   r   r   	Parametertorchonesweightzerosbiastensorr   register_bufferreset_parameters)selfr   r   r   r   r   	__class__s         N/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/feature/hynet.pyr   FilterResponseNorm2d.__init__6   s     	(. ll5::aq!#DTXYLLQa!CSWX	||ELLO4HDH  cU(;<    c                 4   [         R                  R                  U R                  5        [         R                  R	                  U R
                  5        U R                  (       a5  [         R                  R                  U R                  U R                  5        g g N)
r   initones_r   zeros_r   r   	constant_r   r   r"   s    r$   r!   %FilterResponseNorm2d.reset_parametersN   sT    
dkk"
tyy!GGdhh6  r&   c                 :    SR                   " S0 U R                  D6$ )Nz+num_features={num_features}, eps={init_eps} format__dict__r-   s    r$   
extra_reprFilterResponseNorm2d.extra_reprT   s    <CCTdmmTTr&   xc                 ,   UR                  S5      R                  SS/SS9nU[        R                  " X R                  R                  5       -   5      -  nU R                  (       a  U R                  U-  nU R                  (       a  XR                  -   nU$ )N      T)dimkeepdim)
powmeanr   rsqrtr   absr   r   r   r   )r"   r6   nu2s      r$   forwardFilterResponseNorm2d.forwardW   so    eeAhmmAm5 C((,,.011 ==aA<<IIAr&   )r   r   r   r   r   r   r   r   )gư>TTFr   N)__name__
__module____qualname____firstlineno____doc__intfloatboolr   r!   strr4   r   TensorrA   __static_attributes____classcell__r#   s   @r$   r
   r
      s    6  %     	 
     
   07UC U %,,  r&   r
   c                      ^  \ rS rSrSrS\SS4U 4S jjrSS jrS\4S jr	S	\
R                  S\
R                  4S
 jrSrU =r$ )TLUf   a  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                    > [         TU ]  5         Xl        [        R                  " [
        R                  " SUSS5      * SS9U l        U R                  5         g)z:max(y, tau) = max(y - tau, 0) + tau = ReLU(y - tau) + tau.r   Tr   N)	r   r   r   r   r   r   r   taur!   )r"   r   r#   s     r$   r   TLU.__init__y   sE    (<<A|Q!B BRVWr&   c                 X    [         R                  R                  U R                  S5        g )N)r   r)   r,   rU   r-   s    r$   r!   TLU.reset_parameters   s    
$((B'r&   c                 :    SR                   " S0 U R                  D6$ )Nznum_features={num_features}r0   r1   r-   s    r$   r4   TLU.extra_repr   s    ,33DdmmDDr&   r6   c                 B    [         R                  " XR                  5      $ r(   )r   maxrU   r"   r6   s     r$   rA   TLU.forward   s    yyHH%%r&   )r   rU   rC   )rD   rE   rF   rG   rH   rI   r   r!   rL   r4   r   rM   rA   rN   rO   rP   s   @r$   rR   rR   f   sN    $ S  T  (EC E& &%,, & &r&   rR   c                      ^  \ rS rSrSrSr      SS\S\S\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$ )HyNet   a  nn.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

    
pretrainedr   is_bias_FRNdim_desc	drop_rateeps_l2_normr   Nc                   > [         TU ]  5         X`l        X@l        XPl        [
        R                  " [        SUS9[        S5      [
        R                  " SSSSUS9[        SUS9[        S5      5      U l
        [
        R                  " [
        R                  " SSSSUS9[        SUS9[        S5      5      U l        [
        R                  " [
        R                  " SSSSSUS9[        SUS9[        S5      5      U l        [
        R                  " [
        R                  " SSSSUS9[        SUS9[        S5      5      U l        [
        R                  " [
        R                  " SS	SSSUS9[        S	US9[        S	5      5      U l        [
        R                  " [
        R                  " S	S	SSUS9[        S	US9[        S	5      5      U l        [
        R                  " [
        R                   " U R                  5      [
        R                  " S	U R                  S
SS9[
        R"                  " U R                  SS95      U l        [
        R&                  " SU R                  -  SU R                  -  SS5      U l        U(       aI  [*        R,                  R/                  [0        S   [*        R2                  " S5      S9nU R5                  USS9  U R7                  5         g )Nr   )r   rc   r9   )kernel_sizepaddingr   @   r8   )rj   striderk   r         F)rj   r   )affineg       @g      ?g        r   cpu)map_locationT)strict)r   r   rh   rf   rg   r   
Sequentialr
   rR   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"   rd   r   re   rf   rg   rh   pretrained_dictr#   s	           r$   r   HyNet.__init__   s>    	& "mm K8FIIaAGD [9G
 mmIIb"!QWE [9G
 mmIIb"!AqwO [9G
 mmIIb"!QWE [9G

 mmIIb#1QP k:H
 mmIIc3AqwG k:H
 mmJJt~~&IIc4==aeDNN4==7
 --a$--.?t}}ATVY[^_#ii@@i_d_k_klq_r@sO   >		r&   r6   c                 p   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                  XR                  -   5      nUR                  UR                  S5      S5      nU$ )Nr   rX   )rv   rw   rx   ry   rz   r{   r~   r   rh   viewsizer^   s     r$   rA   HyNet.forward   s    KKNKKNKKNKKNKKNKKNKKNNN1///0FF166!9b!r&   )r   rf   rg   rh   rv   rw   rx   ry   rz   r{   r~   )FTTrn   g333333?g|=)rD   rE   rF   rG   rH   
patch_sizerK   rI   rJ   r   r   rM   rA   rN   rO   rP   s   @r$   ra   ra      s    8 J ! "== = 	=
 = = = 
= =~
 
%,, 
 
r&   ra   )typingr   r   r   r   rL   __annotations__Moduler
   rR   ra   r0   r&   r$   <module>r      su   $   d38n lYm[ mZ F299 FR"&")) "&JhBII hr&   