§
    ‚ŠtjL  ã                   ó®  — d Z ddlmZ ddlZddlmZ ddlmZ ddlmZ ddl	m
Z
 dd	lmZmZmZ  ej        e¦  «        Zd
ej        dej        dedededeej        ej        f         fd„Zd
ej        dej        dedeej        ej        f         fd„Zdej        dedej        fd„Z ed¬¦  «        e G d„ de¦  «        ¦   «         ¦   «         Z G d„ dej        ¦  «        Z G d„ dej        ¦  «        Z G d„ dej        ¦  «        Z G d„ d ej        ¦  «        Ze G d!„ d"e¦  «        ¦   «         Z ed#¬¦  «         G d$„ d%e¦  «        ¦   «         Zd%d"gZ dS )&zPyTorch SuperPoint model.é    )Ú	dataclassN)Únn)ÚPreTrainedModel)ÚBaseModelOutputWithNoAttention)ÚSuperPointConfigé   )ÚModelOutputÚauto_docstringÚloggingÚ	keypointsÚscoresÚborderÚheightÚwidthÚreturnc                 ó¼   — | dd…df         |k    | dd…df         ||z
  k     z  }| dd…df         |k    | dd…df         ||z
  k     z  }||z  }| |         ||         fS )zPRemoves keypoints (and their associated scores) that are too close to the borderNr   é   © )r   r   r   r   r   Úmask_hÚmask_wÚmasks           úp/var/www/html/CA-Chatbot/venv/lib/python3.11/site-packages/transformers/models/superpoint/modeling_superpoint.pyÚremove_keypoints_from_bordersr   %   s€   € ð ˜˜˜˜1˜Œo Ò'¨I°a°a°a¸°d¬O¸vÈ¹Ò,OÑP€FØ˜˜˜˜1˜Œo Ò'¨I°a°a°a¸°d¬O¸uÀv¹~Ò,NÑO€FØ�F‰?€DØ�TŒ?˜F 4œLÐ(Ð(ó    Úkc                 óx   — |t          | ¦  «        k    r| |fS t          j        ||d¬¦  «        \  }}| |         |fS )z(Keeps the k keypoints with highest scorer   )Údim)ÚlenÚtorchÚtopk)r   r   r   Úindicess       r   Útop_k_keypointsr"   /   sH   € à�C�	‰NŒNÒÐØ˜&Ð Ð Ý”j ¨°Ð2Ñ2Ô2�O€FˆGØ�WÔ˜vÐ%Ð%r   Ú
nms_radiusc                 ól  ‡— ‰dk     rt          d¦  «        ‚ˆfd„}t          j        | ¦  «        }|  || ¦  «        k    }t          d¦  «        D ]Q} ||                     ¦   «         ¦  «        dk    }t          j        ||| ¦  «        }| ||¦  «        k    }||| z  z  }ŒRt          j        || |¦  «        S )z)Applies non-maximum suppression on scoresr   z'Expected positive values for nms_radiusc                 óV   •— t           j                             | ‰dz  dz   d‰¬¦  «        S )Né   r   ©Úkernel_sizeÚstrideÚpadding)r   Ú
functionalÚ
max_pool2d)Úxr#   s    €r   Úmax_poolzsimple_nms.<locals>.max_pool<   s-   ø€ ÝŒ}×'Ò'¨°zÀA±~ÈÑ7IÐRSÐ]gÐ'ÑhÔhÐhr   r&   )Ú
ValueErrorr   Ú
zeros_likeÚrangeÚfloatÚwhere)	r   r#   r.   ÚzerosÚmax_maskÚ_Ú	supp_maskÚsupp_scoresÚnew_max_masks	    `       r   Ú
simple_nmsr:   7   sà   ø€ à�A‚~€~ÝÐBÑCÔCÐCðið ið ið ið iõ Ô˜VÑ$Ô$€EØ˜˜ &Ñ)Ô)Ò)€HÝ�1‰XŒXð <ð <ˆØ�H˜XŸ^š^Ñ-Ô-Ñ.Ô.°Ò2ˆ	Ý”k )¨U°FÑ;Ô;ˆØ" h h¨{Ñ&;Ô&;Ò;ˆØ˜|°	¨zÑ:Ñ;ˆˆÝŒ;�x ¨Ñ/Ô/Ð/r   aù  
    Base class for outputs of image point description models. Due to the nature of keypoint detection, the number of
    keypoints is not fixed and can vary from image to image, which makes batching non-trivial. In the batch of images,
    the maximum number of keypoints is set as the dimension of the keypoints, scores and descriptors tensors. The mask
    tensor is used to indicate which values in the keypoints, scores and descriptors tensors are keypoint information
    and which are padding.
    )Úcustom_introc                   óÔ   — e Zd ZU dZdZej        dz  ed<   dZej	        dz  ed<   dZ
ej        dz  ed<   dZej        dz  ed<   dZej        dz  ed<   dZeej                 dz  ed<   dS )	Ú#SuperPointKeypointDescriptionOutputax  
    loss (`torch.FloatTensor` of shape `(1,)`, *optional*):
        Loss computed during training.
    keypoints (`torch.FloatTensor` of shape `(batch_size, num_keypoints, 2)`):
        Relative (x, y) coordinates of predicted keypoints in a given image.
    scores (`torch.FloatTensor` of shape `(batch_size, num_keypoints)`):
        Scores of predicted keypoints.
    descriptors (`torch.FloatTensor` of shape `(batch_size, num_keypoints, descriptor_size)`):
        Descriptors of predicted keypoints.
    mask (`torch.BoolTensor` of shape `(batch_size, num_keypoints)`):
        Mask indicating which values in keypoints, scores and descriptors are keypoint information.
    hidden_states (`tuple(torch.FloatTensor)`, *optional*, returned when `output_hidden_states=True` is passed or
    when `config.output_hidden_states=True`):
        Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, +
        one for the output of each stage) of shape `(batch_size, sequence_length, hidden_size)`. Hidden-states
        (also called feature maps) of the model at the output of each stage.
    NÚlossr   r   Údescriptorsr   Úhidden_states)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r>   r   ÚFloatTensorÚ__annotations__r   Ú	IntTensorr   r?   r   Ú
BoolTensorr@   Útupler   r   r   r=   r=   I   s²   € € € € € € ðð ð$ &*€Dˆ%Ô
˜dÑ
"Ð)Ð)Ñ)Ø(,€IˆuŒ Ñ%Ð,Ð,Ñ,Ø'+€FˆEÔ Ñ$Ð+Ð+Ñ+Ø,0€K�Ô" TÑ)Ð0Ð0Ñ0Ø$(€Dˆ%Ô
˜TÑ
!Ð(Ð(Ñ(Ø59€M�5˜Ô*Ô+¨dÑ2Ð9Ð9Ñ9Ð9Ð9r   r=   c                   ó\   ‡ — e Zd Z	 ddededededdf
ˆ fd„Zd	ej        dej        fd
„Z	ˆ xZ
S )ÚSuperPointConvBlockFÚconfigÚin_channelsÚout_channelsÚadd_poolingr   Nc                 ó2  •— t          ¦   «                              ¦   «          t          j        ||ddd¬¦  «        | _        t          j        ||ddd¬¦  «        | _        t          j        d¬¦  «        | _        |rt          j        dd¬¦  «        nd | _	        d S )Nr   r   r'   T©Úinplacer&   ©r(   r)   )
ÚsuperÚ__init__r   ÚConv2dÚconv_aÚconv_bÚReLUÚreluÚ	MaxPool2dÚpool)ÚselfrL   rM   rN   rO   Ú	__class__s        €r   rU   zSuperPointConvBlock.__init__o   s¥   ø€ õ 	‰Œ×ÒÑÔÐÝ”iØØØØØð
ñ 
ô 
ˆŒõ ”iØØØØØð
ñ 
ô 
ˆŒõ ”G DÐ)Ñ)Ô)ˆŒ	Ø=HÐR•B”L¨Q°qÐ9Ñ9Ô9Ð9ÈdˆŒ	ˆ	ˆ	r   r@   c                 óÞ   — |                       |                      |¦  «        ¦  «        }|                       |                      |¦  «        ¦  «        }| j        �|                      |¦  «        }|S ©N)rZ   rW   rX   r\   )r]   r@   s     r   ÚforwardzSuperPointConvBlock.forward„   s[   € ØŸ	š	 $§+¢+¨mÑ"<Ô"<Ñ=Ô=ˆØŸ	š	 $§+¢+¨mÑ"<Ô"<Ñ=Ô=ˆØŒ9Ð Ø ŸIšI mÑ4Ô4ˆMØÐr   )F)rA   rB   rC   r   ÚintÚboolrU   r   ÚTensorra   Ú__classcell__©r^   s   @r   rK   rK   n   sŸ   ø€ € € € € àafðSð SØ&ðSØ58ðSØHKðSØZ^ðSà	ðSð Sð Sð Sð Sð Sð* U¤\ð °e´lð ð ð ð ð ð ð ð r   rK   c                   óX   ‡ — e Zd ZdZdeddfˆ fd„Z	 	 ddedz  d	edz  deez  fd
„Z	ˆ xZ
S )ÚSuperPointEncoderzž
    SuperPoint encoder module. It is made of 4 convolutional layers with ReLU activation and max pooling, reducing the
     dimensionality of the image.
    rL   r   Nc           	      ó>  •— t          ¦   «                              ¦   «          d| _        g }|                     t	          || j        |j        d         d¬¦  «        ¦  «         t          dt          |j        ¦  «        dz
  ¦  «        D ]A}|                     t	          ||j        |dz
           |j        |         d¬¦  «        ¦  «         ŒB|                     t	          ||j        d         |j        d         d¬¦  «        ¦  «         t          j	        |¦  «        | _
        d S )Nr   r   T)rO   éþÿÿÿéÿÿÿÿF)rT   rU   Ú	input_dimÚappendrK   Úencoder_hidden_sizesr1   r   r   Ú
ModuleListÚconv_blocks)r]   rL   rp   Úir^   s       €r   rU   zSuperPointEncoder.__init__’   s7  ø€ Ý‰Œ×ÒÑÔÐàˆŒàˆØ×ÒÝ ¨¬¸Ô8SÐTUÔ8VÐdhÐiÑiÔiñ	
ô 	
ð 	
õ �q�#˜fÔ9Ñ:Ô:¸QÑ>Ñ?Ô?ð 	ð 	ˆAØ×ÒÝ#Ø˜FÔ7¸¸A¹Ô>ÀÔ@[Ð\]Ô@^Ðlpðñ ô ñô ð ð ð
 	×ÒÝØ˜Ô3°BÔ7¸Ô9TÐUWÔ9XÐfkðñ ô ñ	
ô 	
ð 	
õ
 œ=¨Ñ5Ô5ˆÔÐÐr   FTÚoutput_hidden_statesÚreturn_dictc                 ó¨   — |rdnd }| j         D ]} ||¦  «        }|r||fz   }Œ|}|st          d„ ||fD ¦   «         ¦  «        S t          ||¬¦  «        S )Nr   c              3   ó   K  — | ]}|®|V — Œ	d S r`   r   ©Ú.0Úvs     r   ú	<genexpr>z,SuperPointEncoder.forward.<locals>.<genexpr>¶   s"   è è € ÐQÐQ˜qÀ1À=˜À=À=À=À=ÐQÐQr   )Úlast_hidden_stater@   )rp   rI   r   )r]   Úinputrr   rs   Úall_hidden_statesÚ
conv_blockÚoutputs          r   ra   zSuperPointEncoder.forward¨   s›   € ð #7Ð@˜B˜B¸DÐàÔ*ð 	Að 	AˆJØ�J˜uÑ%Ô%ˆEØ#ð AØ$5¸¸Ñ$@Ð!øØˆØð 	RÝÐQÐQ VÐ->Ð$?ÐQÑQÔQÑQÔQÐQå-Ø$Ø+ð
ñ 
ô 
ð 	
r   )FT)rA   rB   rC   rD   r   rU   rc   rI   r   ra   re   rf   s   @r   rh   rh   Œ   s£   ø€ € € € € ðð ð
6Ð/ð 6°Dð 6ð 6ð 6ð 6ð 6ð 6ð2 -2Ø#'ð	
ð 
ð # T™kð
ð ˜D‘[ð	
ð
 
Ð/Ñ	/ð
ð 
ð 
ð 
ð 
ð 
ð 
ð 
r   rh   c                   óÌ   ‡ — e Zd ZdZdeddfˆ fd„Zdej        deej        ej        f         fd„Z	dej        dej        fd„Z
d	ej        deej        ej        f         fd
„Zˆ xZS )ÚSuperPointInterestPointDecodera  
    The SuperPointInterestPointDecoder uses the output of the SuperPointEncoder to compute the keypoint with scores.
    The scores are first computed by a convolutional layer, then a softmax is applied to get a probability distribution
    over the 65 possible keypoint classes. The keypoints are then extracted from the scores by thresholding and
    non-maximum suppression. Post-processing is then applied to remove keypoints too close to the image borders as well
    as to keep only the k keypoints with highest score.
    rL   r   Nc                 ó¾  •— t          ¦   «                              ¦   «          |j        | _        |j        | _        |j        | _        |j        | _        t          j        d¬¦  «        | _        t          j	        dd¬¦  «        | _
        t          j        |j        d         |j        ddd¬¦  «        | _        t          j        |j        |j        ddd	¬¦  «        | _        d S ©
NTrQ   r&   rS   rk   r   r   r'   r   )rT   rU   Úkeypoint_thresholdÚmax_keypointsr#   Úborder_removal_distancer   rY   rZ   r[   r\   rV   rn   Údecoder_hidden_sizeÚconv_score_aÚkeypoint_decoder_dimÚconv_score_b©r]   rL   r^   s     €r   rU   z'SuperPointInterestPointDecoder.__init__Ç   sÖ   ø€ Ý‰Œ×ÒÑÔÐØ"(Ô";ˆÔØ#Ô1ˆÔØ Ô+ˆŒØ'-Ô'EˆÔ$å”G DÐ)Ñ)Ô)ˆŒ	Ý”L¨Q°qÐ9Ñ9Ô9ˆŒ	ÝœIØÔ'¨Ô+ØÔ&ØØØð
ñ 
ô 
ˆÔõ œIØÔ&¨Ô(CÐQRÐ[\Ðfgð
ñ 
ô 
ˆÔÐÐr   Úencodedc                 ód   — |                       |¦  «        }|                      |¦  «        \  }}||fS r`   )Ú_get_pixel_scoresÚ_extract_keypoints)r]   r‹   r   r   s       r   ra   z&SuperPointInterestPointDecoder.forwardÛ   s8   € Ø×'Ò'¨Ñ0Ô0ˆØ ×3Ò3°FÑ;Ô;Ñˆ	�6à˜&Ð Ð r   c                 óà  — |                       |                      |¦  «        ¦  «        }|                      |¦  «        }t          j                             |d¦  «        dd…dd…f         }|j        \  }}}}|                     dddd¦  «                             |||dd¦  «        }|                     ddddd¦  «                             ||dz  |dz  ¦  «        }t          || j
        ¦  «        }|S )	zKBased on the encoder output, compute the scores for each pixel of the imager   Nrk   r   r&   r   é   é   )rZ   r‡   r‰   r   r+   ÚsoftmaxÚshapeÚpermuteÚreshaper:   r#   )r]   r‹   r   Ú
batch_sizer6   r   r   s          r   r�   z0SuperPointInterestPointDecoder._get_pixel_scoresá   sä   € à—’˜4×,Ò,¨WÑ5Ô5Ñ6Ô6ˆØ×"Ò" 6Ñ*Ô*ˆÝ”×&Ò& v¨qÑ1Ô1°!°!°!°S°b°S°&Ô9ˆØ'-¤|Ñ$ˆ
�A�v˜uØ—’  1 a¨Ñ+Ô+×3Ò3°JÀÈÈqÐRSÑTÔTˆØ—’  1 a¨¨AÑ.Ô.×6Ò6°zÀ6ÈAÁ:ÈuÐWXÉyÑYÔYˆÝ˜F D¤OÑ4Ô4ˆØˆr   r   c                 ó¦  — |j         \  }}}t          j        |d         | j        k    ¦  «        }|d         t	          |                     ¦   «         ¦  «                 }t          ||| j        |dz  |dz  ¦  «        \  }}| j        dk    rt          ||| j        ¦  «        \  }}t          j
        |dg¦  «                             |j        ¦  «        }||fS )zÍ
        Based on their scores, extract the pixels that represent the keypoints that will be used for descriptors computation.
        The keypoints are in the form of relative (x, y) coordinates.
        r   r�   r   )r“   r   Únonzerorƒ   rI   Útr   r…   r„   r"   ÚflipÚtoÚdtype)r]   r   r6   r   r   r   s         r   rŽ   z1SuperPointInterestPointDecoder._extract_keypointsì   sÓ   € ð
 "œ<Ñˆˆ6�5õ ”M &¨¤)¨dÔ.EÒ"EÑFÔFˆ	Ø˜”�5 §¢¡¤Ñ/Ô/Ô0ˆõ :Ø�v˜tÔ;¸VÀa¹ZÈÐQRÉñ
ô 
Ñˆ	�6ð
 Ô Ò"Ð"Ý /°	¸6À4ÔCUÑ VÔ VÑˆI�võ ”J˜y¨1¨#Ñ.Ô.×1Ò1°&´,Ñ?Ô?ˆ	à˜&Ð Ð r   )rA   rB   rC   rD   r   rU   r   rd   rI   ra   r�   rŽ   re   rf   s   @r   r€   r€   ¾   sà   ø€ € € € € ðð ð
Ð/ð 
°Dð 
ð 
ð 
ð 
ð 
ð 
ð(!˜uœ|ð !°°e´lÀEÄLÐ6PÔ0Qð !ð !ð !ð !ð	¨¬ð 	¸%¼,ð 	ð 	ð 	ð 	ð!¨¬ð !¸%ÀÄÈeÌlÐ@ZÔ:[ð !ð !ð !ð !ð !ð !ð !ð !r   r€   c                   óŠ   ‡ — e Zd ZdZdeddfˆ fd„Zdej        dej        dej        fd„Ze	dd
e
dej        fd„¦   «         Zˆ xZS )ÚSuperPointDescriptorDecoderag  
    The SuperPointDescriptorDecoder uses the outputs of both the SuperPointEncoder and the
    SuperPointInterestPointDecoder to compute the descriptors at the keypoints locations.

    The descriptors are first computed by a convolutional layer, then normalized to have a norm of 1. The descriptors
    are then interpolated at the keypoints locations.
    rL   r   Nc                 ó^  •— t          ¦   «                              ¦   «          t          j        d¬¦  «        | _        t          j        dd¬¦  «        | _        t          j        |j        d         |j	        ddd¬¦  «        | _
        t          j        |j	        |j        ddd	¬¦  «        | _        d S r‚   )rT   rU   r   rY   rZ   r[   r\   rV   rn   r†   Úconv_descriptor_aÚdescriptor_decoder_dimÚconv_descriptor_brŠ   s     €r   rU   z$SuperPointDescriptorDecoder.__init__  s¬   ø€ Ý‰Œ×ÒÑÔÐå”G DÐ)Ñ)Ô)ˆŒ	Ý”L¨Q°qÐ9Ñ9Ô9ˆŒ	Ý!#¤ØÔ'¨Ô+ØÔ&ØØØð"
ñ "
ô "
ˆÔõ "$¤ØÔ&ØÔ)ØØØð"
ñ "
ô "
ˆÔÐÐr   r‹   r   c                 óJ  — |                       |                      |                      |¦  «        ¦  «        ¦  «        }t          j                             |dd¬¦  «        }|                      |d         |d         d         d¦  «        d         }t          j        |dd¦  «        }|S )zXBased on the encoder output and the keypoints, compute the descriptors for each keypointr&   r   ©Úpr   Nr   r�   )	r¢   rZ   r    r   r+   Ú	normalizeÚ_sample_descriptorsr   Ú	transpose)r]   r‹   r   r?   s       r   ra   z#SuperPointDescriptorDecoder.forward#  s‘   € à×,Ò,¨T¯YªY°t×7MÒ7MÈgÑ7VÔ7VÑ-WÔ-WÑXÔXˆÝ”m×-Ò-¨k¸QÀAÐ-ÑFÔFˆà×.Ò.¨y¸¬ÀÈAÄÈtÔ@TÐVWÑXÔXÐYZÔ[ˆõ ”o k°1°aÑ8Ô8ˆàÐr   r�   Úscalec                 ó´  — |j         \  }}}}| |dz  z
  dz   } t          j        ||z  |dz  z
  dz
  ||z  |dz  z
  dz
  gg¦  «        }|                     | ¦  «        }| |z  } | dz  dz
  } ddi}|                      |ddd¦  «        } t          j        j        || fddi|¤Ž}|                     ||d¦  «        }t
          j         	                    |dd¬	¦  «        }|S )
z-Interpolate descriptors at keypoint locationsr&   g      à?r   Úalign_cornersTrk   ÚmodeÚbilinearr¤   )
r“   r   Útensorr›   Úviewr   r+   Úgrid_sampler•   r¦   )	r   r?   r©   r–   Únum_channelsr   r   ÚdivisorÚkwargss	            r   r§   z/SuperPointDescriptorDecoder._sample_descriptors/  s  € ð 3>Ô2CÑ/ˆ
�L &¨%Ø ¨¡	Ñ)¨CÑ/ˆ	Ý”, %¨%¡-°%¸!±)Ñ";¸cÑ"AÀVÈeÁ^ÐV[Ð^_ÑV_ÑE_ÐbeÑEeÐ gÐhÑiÔiˆØ—*’*˜YÑ'Ô'ˆØ�WÑˆ	Ø ‘M AÑ%ˆ	Ø! 4Ð(ˆà—N’N :¨q°"°aÑ8Ô8ˆ	Ý”mÔ/°¸YÐbÐbÈZÐbÐ[aÐbÐbˆà!×)Ò)¨*°lÀBÑGÔGˆÝ”m×-Ò-¨k¸QÀAÐ-ÑFÔFˆØÐr   )r�   )rA   rB   rC   rD   r   rU   r   rd   ra   Ústaticmethodrb   r§   re   rf   s   @r   rž   rž     sº   ø€ € € € € ðð ð
Ð/ð 
°Dð 
ð 
ð 
ð 
ð 
ð 
ð(
˜uœ|ð 
¸¼ð 
ÈÌð 
ð 
ð 
ð 
ð ðð ¸3ð ÀuÄ|ð ð ð ñ „\ðð ð ð ð r   rž   c                   óN   — e Zd ZU eed<   dZdZdZdZde	j
        de	j
        fd„ZdS )	ÚSuperPointPreTrainedModelrL   Ú
superpointÚpixel_values)ÚimageFr   c                 óF   — |dd…ddd…dd…f         dd…ddd…dd…f         S )aG  
        Assuming pixel_values has shape (batch_size, 3, height, width), and that all channels values are the same,
        extract the first channel value to get a tensor of shape (batch_size, 1, height, width) for SuperPoint. This is
        a workaround for the issue discussed in :
        https://github.com/huggingface/transformers/pull/25786#issuecomment-1730176446

        Args:
            pixel_values: torch.FloatTensor of shape (batch_size, 3, height, width)

        Returns:
            pixel_values: torch.FloatTensor of shape (batch_size, 1, height, width)

        Nr   r   )r]   r¸   s     r   Ú extract_one_channel_pixel_valuesz:SuperPointPreTrainedModel.extract_one_channel_pixel_valuesJ  s<   € ð ˜A˜A˜A˜q ! ! ! Q Q Q˜JÔ'¨¨¨¨4°°°°A°A°A¨Ô6Ð6r   N)rA   rB   rC   r   rF   Úbase_model_prefixÚmain_input_nameÚinput_modalitiesÚsupports_gradient_checkpointingr   rE   r»   r   r   r   r¶   r¶   B  sb   € € € € € € àÐÐÑØ$ÐØ$€OØ!ÐØ&+Ð#ð7¸UÔ=Nð 7ÐSXÔSdð 7ð 7ð 7ð 7ð 7ð 7r   r¶   z@
    SuperPoint model outputting keypoints and descriptors.
    c                   óŒ   ‡ — e Zd ZdZdeddfˆ fd„Ze	 	 	 ddej        dej	        dz  de
dz  d	e
dz  deez  f
d
„¦   «         Zˆ xZS )ÚSuperPointForKeypointDetectionaº  
    SuperPoint model. It consists of a SuperPointEncoder, a SuperPointInterestPointDecoder and a
    SuperPointDescriptorDecoder. SuperPoint was proposed in `SuperPoint: Self-Supervised Interest Point Detection and
    Description <https://huggingface.co/papers/1712.07629>`__ by Daniel DeTone, Tomasz Malisiewicz, and Andrew Rabinovich. It
    is a fully convolutional neural network that extracts keypoints and descriptors from an image. It is trained in a
    self-supervised manner, using a combination of a photometric loss and a loss based on the homographic adaptation of
    keypoints. It is made of a convolutional encoder and two decoders: one for keypoints and one for descriptors.
    rL   r   Nc                 óø   •— t          ¦   «                              |¦  «         || _        t          |¦  «        | _        t          |¦  «        | _        t          |¦  «        | _        |  	                    ¦   «          d S r`   )
rT   rU   rL   rh   Úencoderr€   Úkeypoint_decoderrž   Údescriptor_decoderÚ	post_initrŠ   s     €r   rU   z'SuperPointForKeypointDetection.__init__j  sg   ø€ Ý‰Œ×Ò˜Ñ Ô Ð àˆŒå(¨Ñ0Ô0ˆŒÝ >¸vÑ FÔ FˆÔÝ"=¸fÑ"EÔ"EˆÔà�ŠÑÔÐÐÐr   r¸   Úlabelsrr   rs   c           	      ól  ‡ — d}|�t          d¦  «        ‚|�|n‰ j        j        }|�|n‰ j        j        }‰                      |¦  «        }|j        \  }}}	}
‰                      |||¬¦  «        }|d         }ˆ fd„|D ¦   «         }d„ |D ¦   «         }d„ |D ¦   «         }ˆ fd„t          ||¦  «        D ¦   «         }t          d	„ |D ¦   «         ¦  «        }t          j
        ||d
f|j        ¬¦  «        }t          j
        ||f|j        ¬¦  «        }t          j
        ||‰ j        j        f|j        ¬¦  «        }t          j
        ||f|j        t          j        ¬¦  «        }t          t          |||¦  «        ¦  «        D ]Y\  }\  }}}|||d|j        d         …f<   |||d|j        d         …f<   |||d|j        d         …f<   d||d|j        d         …f<   ŒZ|t          j        |
|	g|j        ¬¦  «        z  }|r|d         nd}|st!          d„ ||||||fD ¦   «         ¦  «        S t#          ||||||¬¦  «        S )aç  
        Examples:

        ```python
        >>> from transformers import AutoImageProcessor, SuperPointForKeypointDetection
        >>> import torch
        >>> from PIL import Image
        >>> import httpx
        >>> from io import BytesIO

        >>> url = "http://images.cocodataset.org/val2017/000000039769.jpg"
        >>> with httpx.stream("GET", url) as response:
        ...     image = Image.open(BytesIO(response.read()))

        >>> processor = AutoImageProcessor.from_pretrained("magic-leap-community/superpoint")
        >>> model = SuperPointForKeypointDetection.from_pretrained("magic-leap-community/superpoint")

        >>> inputs = processor(image, return_tensors="pt")
        >>> outputs = model(**inputs)
        ```Nz-SuperPoint does not support training for now.)rr   rs   r   c                 óF   •— g | ]}‰                      |d          ¦  «        ‘ŒS ©)N.)rÄ   )rw   rz   r]   s     €r   ú
<listcomp>z:SuperPointForKeypointDetection.forward.<locals>.<listcomp>§  s:   ø€ ð !
ð !
ð !
ØDUˆD×!Ò!Ð"3°IÔ">Ñ?Ô?ð!
ð !
ð !
r   c                 ó   — g | ]
}|d          ‘ŒS )r   r   ©rw   Úkeypoints_scoress     r   rË   z:SuperPointForKeypointDetection.forward.<locals>.<listcomp>«  s   € Ð\Ð\Ð\Ð2BÐ*¨1Ô-Ð\Ð\Ð\r   c                 ó   — g | ]
}|d          ‘ŒS )r   r   rÍ   s     r   rË   z:SuperPointForKeypointDetection.forward.<locals>.<listcomp>¬  s   € ÐYÐYÐYÐ/?Ð'¨Ô*ÐYÐYÐYr   c                 óZ   •— g | ]'\  }}‰                      |d          |d          ¦  «        ‘Œ(S rÊ   )rÅ   )rw   rz   r   r]   s      €r   rË   z:SuperPointForKeypointDetection.forward.<locals>.<listcomp>®  sI   ø€ ð 
ð 
ð 
á,Ð! 9ð ×#Ò#Ð$5°iÔ$@À)ÈIÔBVÑWÔWð
ð 
ð 
r   c              3   ó0   K  — | ]}|j         d          V — ŒdS )r   N)r“   )rw   r   s     r   ry   z9SuperPointForKeypointDetection.forward.<locals>.<genexpr>³  s)   è è € Ð#WÐ#W¸9 I¤O°AÔ$6Ð#WÐ#WÐ#WÐ#WÐ#WÐ#Wr   r&   )Údevice)rÒ   rœ   r   c              3   ó   K  — | ]}|®|V — Œ	d S r`   r   rv   s     r   ry   z9SuperPointForKeypointDetection.forward.<locals>.<genexpr>È  s(   è è € ÐqÐq˜qÐcdÐcp˜ÐcpÐcpÐcpÐcpÐqÐqr   )r>   r   r   r?   r   r@   )r/   rL   rr   rs   r»   r“   rÃ   ÚzipÚmaxr   r4   rÒ   r¡   rb   Ú	enumerater®   rI   r=   )r]   r¸   rÇ   rr   rs   r³   r>   r–   r6   r   r   Úencoder_outputsrz   Úlist_keypoints_scoresÚlist_keypointsÚlist_scoresÚlist_descriptorsÚmaximum_num_keypointsr   r   r?   r   rq   Ú
_keypointsÚ_scoresÚ_descriptorsr@   s   `                          r   ra   z&SuperPointForKeypointDetection.forwardu  s  ø€ ð: ˆØÐÝÐLÑMÔMÐMð %9Ð$DÐ Ð È$Ì+ÔJjð 	ð &1Ð%<�k�kÀ$Ä+ÔBYˆà×<Ò<¸\ÑJÔJˆà'3Ô'9Ñ$ˆ
�A�v˜uàŸ,š,ØØ!5Ø#ð 'ñ 
ô 
ˆð ,¨AÔ.Ðð!
ð !
ð !
ð !
ØYjð!
ñ !
ô !
Ðð ]Ð\ÐF[Ð\Ñ\Ô\ˆØYÐYÐCXÐYÑYÔYˆð
ð 
ð 
ð 
å03Ð4EÀ~Ñ0VÔ0Vð
ñ 
ô 
Ðõ
 !$Ð#WÐ#WÈÐ#WÑ#WÔ#WÑ WÔ WÐå”K Ð-BÀAÐ FÈ|ÔObÐcÑcÔcˆ	Ý”˜jÐ*?Ð@ÈÔI\Ð]Ñ]Ô]ˆÝ”kØÐ.°´Ô0RÐSØÔ&ð
ñ 
ô 
ˆõ Œ{˜JÐ(=Ð>À|ÔGZÕbgÔbkÐlÑlÔlˆå6?ÅÀNÐT_ÐaqÑ@rÔ@rÑ6sÔ6sð 	,ð 	,Ñ2ˆAÑ2�
˜G \Ø2<ˆI�aÐ.˜:Ô+¨AÔ.Ð.Ð.Ñ/Ø,3ˆF�1Ð(˜œ aÔ(Ð(Ð(Ñ)Ø6BˆK˜Ð2˜\Ô/°Ô2Ð2Ð2Ñ3Ø*+ˆD�Ð&�g”m AÔ&Ð&Ð&Ñ'Ð'ð ¥¤¨e°V¨_ÀYÔEUÐ VÑ VÔ VÑVˆ	à.BÐL˜¨Ô*Ð*ÈˆØð 	rÝÐqÐq T¨9°f¸kÈ4ÐQ^Ð$_ÐqÑqÔqÑqÔqÐqå2ØØØØ#ØØ'ð
ñ 
ô 
ð 	
r   )NNN)rA   rB   rC   rD   r   rU   r
   r   rE   Ú
LongTensorrc   rI   r=   ra   re   rf   s   @r   rÁ   rÁ   [  sÝ   ø€ € € € € ðð ð	Ð/ð 	°Dð 	ð 	ð 	ð 	ð 	ð 	ð ð +/Ø,0Ø#'ð[
ð [
àÔ'ð[
ð Ô  4Ñ'ð[
ð # T™kð	[
ð
 ˜D‘[ð[
ð 
Ð4Ñ	4ð[
ð [
ð [
ñ „^ð[
ð [
ð [
ð [
ð [
r   rÁ   )!rD   Údataclassesr   r   r   Útransformersr   Útransformers.modeling_outputsr   Ú7transformers.models.superpoint.configuration_superpointr   Úutilsr	   r
   r   Ú
get_loggerrA   Úloggerrd   rb   rI   r   r"   r:   r=   ÚModulerK   rh   r€   rž   r¶   rÁ   Ú__all__r   r   r   ú<module>rê      sD  ðð  Ð à !Ð !Ð !Ð !Ð !Ð !à €€€Ø Ð Ð Ð Ð Ð à (Ð (Ð (Ð (Ð (Ð (ðð ð ð ð ð ð UÐ TÐ TÐ TÐ TÐ Tðð ð ð ð ð ð ð ð ð ð 
ˆÔ	˜HÑ	%Ô	%€ð)ØŒ|ð)Ø%*¤\ð)Ø;>ð)ØHKð)ØTWð)à
ˆ5Œ<˜œÐ%Ô&ð)ð )ð )ð )ð&˜uœ|ð &°U´\ð &Àcð &ÈeÐTYÔT`ÐbgÔbnÐTnÔNoð &ð &ð &ð &ð0�u”|ð 0°ð 0¸¼ð 0ð 0ð 0ð 0ð$ €ððñ ô ð ð:ð :ð :ð :ð :¨+ñ :ô :ñ „ñô ð:ð6ð ð ð ð ˜"œ)ñ ô ð ð</
ð /
ð /
ð /
ð /
˜œ	ñ /
ô /
ð /
ðdE!ð E!ð E!ð E!ð E! R¤Yñ E!ô E!ð E!ðP9ð 9ð 9ð 9ð 9 "¤)ñ 9ô 9ð 9ðx ð7ð 7ð 7ð 7ð 7 ñ 7ô 7ñ „ð7ð0 €ððñ ô ð
q
ð q
ð q
ð q
ð q
Ð%>ñ q
ô q
ñô ð
q
ðh ,Ð-HÐ
I€€€r   