a
    d                     @   s   U d dl mZmZ d dlZd dlmZ i Zeeef ed< ded< ded< ded	< G d
d dej	Z
G dd dej	ZG dd dej	ZdS )    )CallableDictN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.pthZ	notredamezXhttps://github.com/ducha-aiki/Key.Net-Pytorch/raw/main/model/HyNet/weights/HyNet_YOS.pthZyosemitec                       sT   e Zd ZdZdeeeeed fddZdd	 Zd
d Z	e
je
jd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_leanablec                    s   t    || _|| _|| _|| _|| _tjj	t
d|dddd| _tjj	t
d|dddd| _|rtjj	t
ddd| _n| dt
|g |   d S )N   TZrequires_gradr	   )super__init__r   init_epsr   r
   r   nn	parameter	ParametertorchonesweightzerosbiasZtensorr	   Zregister_bufferreset_parameters)selfr   r	   r
   r   r   	__class__ ]/var/www/html/stable-diffusion-webui/venv/lib/python3.9/site-packages/kornia/feature/hynet.pyr   *   s    	
zFilterResponseNorm2d.__init__c                 C   s8   t j| j t j| j | jr4t j| j| j	 d S N)
r   initZones_r   Zzeros_r   r   	constant_r	   r   r   r   r   r   r   C   s    z%FilterResponseNorm2d.reset_parametersc                 C   s   dj f i | jS )Nz+num_features={num_features}, eps={init_eps}format__dict__r#   r   r   r   
extra_reprI   s    zFilterResponseNorm2d.extra_reprxreturnc                 C   sT   | djddgdd}|t|| j   }| jr@| j| }| jrP|| j	 }|S )N      T)ZdimZkeepdim)
powmeanr   Zrsqrtr	   absr   r   r
   r   )r   r)   Znu2r   r   r   forwardL   s    

zFilterResponseNorm2d.forward)r   TTF)__name__
__module____qualname____doc__intfloatboolr   r   r'   r   Tensorr0   __classcell__r   r   r   r   r      s       r   c                       sJ   e Zd ZdZed fddZdd Zdd Zej	ej	d	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   c                    s<   t    || _tjjtd|dd dd| _| 	  dS )z9max(y, tau) = max(y - tau, 0) + tau = ReLU(y - tau) + taur   Tr   N)
r   r   r   r   r   r   r   r   taur   )r   r   r   r   r   r   m   s    
 zTLU.__init__c                 C   s   t j| jd d S )N)r   r!   r"   r;   r#   r   r   r   r   t   s    zTLU.reset_parametersc                 C   s   dj f i | jS )Nznum_features={num_features}r$   r#   r   r   r   r'   x   s    zTLU.extra_reprr(   c                 C   s   t || jS r    )r   maxr;   r   r)   r   r   r   r0   {   s    zTLU.forward)r1   r2   r3   r4   r5   r   r   r'   r   r8   r0   r9   r   r   r   r   r:   [   s
   r:   c                       sJ   e Zd ZdZdZdeeeeeed fd	d
Ze	j
e	j
dddZ  ZS )HyNeta8  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 dimentionality,
        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_normc           	         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| _|rdd }tjjtd |d}| j|dd |   d S )Nr   )r
   r@   r,   )kernel_sizepaddingr   @   r+   )rI   ZstriderJ   r   rA      F)rI   r   )Zaffineg       @g      ?g        c                 S   s   | S r    r   )Zstoragelocr   r   r   <lambda>       z HyNet.__init__.<locals>.<lambda>r   )Zmap_locationT)strict)r   r   rH   rF   rG   r   Z
Sequentialr   r:   ZConv2dlayer1layer2layer3layer4layer5layer6ZDropoutZBatchNorm2dlayer7ZLocalResponseNorm	desc_normr   ZhubZload_state_dict_from_urlr   Zload_state_dicteval)	r   rD   r
   rE   rF   rG   rH   Zstorage_fcnZpretrained_dictr   r   r   r      s`    	








zHyNet.__init__r(   c                 C   sl   |  |}| |}| |}| |}| |}| |}| |}| || j }|	|
dd}|S )Nr   r<   )rQ   rR   rS   rT   rU   rV   rW   rX   rH   viewsizer>   r   r   r   r0      s    






zHyNet.forward)FTTrA   rB   rC   )r1   r2   r3   r4   Z
patch_sizer7   r5   r6   r   r   r8   r0   r9   r   r   r   r   r?      s"         Ar?   )typingr   r   r   Ztorch.nnr   r   str__annotations__Moduler   r:   r?   r   r   r   r   <module>   s$   I$