o
    €o™ií!  ã                   @  s  U d dl mZ d dlmZmZ d dlZd dlmZmZ d dl	m
Z
 ddlmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZ i Zded< ded< ded< ded< dddddg d¢dœdddg d¢ddd œd!d"d#d$d%d&dd'd(d)œ	dddd*d+gdd,œd-œZG d.d/„ d/eƒZdS )0é    )Úannotations)ÚAnyÚOptionalN)ÚModuleÚTensor)Úresizeé   )Úbuild_backbone)ÚFinePreprocessÚLocalFeatureTransformer)ÚCoarseMatching)ÚFineMatching)ÚPositionEncodingSinezdict[str, str]Úurlsz;http://cmp.felk.cvut.cz/~mishkdmy/models/loftr_outdoor.ckptÚoutdoorzAhttp://cmp.felk.cvut.cz/~mishkdmy/models/loftr_indoor_ds_new.ckptÚ
indoor_newz:http://cmp.felk.cvut.cz/~mishkdmy/models/loftr_indoor.ckptÚindoorÚ	ResNetFPN)é   é   é   Té€   )r   éÄ   é   )Úinitial_dimÚ
block_dimsr   r   )ÚselfÚcrossr   r   r   r   r   r   ÚlinearF)Úd_modelÚd_ffnÚnheadÚlayer_namesÚ	attentionÚtemp_bug_fixgš™™™™™É?r   Údual_softmaxgš™™™™™¹?é   g      ð?gš™™™™™Ù?éÈ   )	ÚthrÚ	border_rmÚ
match_typeÚdsmax_temperatureÚ	skh_itersÚskh_init_bin_scoreÚskh_prefilterÚtrain_coarse_percentÚtrain_pad_num_gt_minr   r   )r   r    r!   r"   r#   )Úbackbone_typeÚ
resolutionÚfine_window_sizeÚfine_concat_coarse_featÚ	resnetfpnÚcoarseÚmatch_coarseÚfinec                      s@   e Zd ZdZdefd‡ fd	d
„Zddd„Zd‡ fdd„Z‡  ZS )ÚLoFTRa¯  Module, which finds correspondences between two images.

    This is based on the original code from paper "LoFTR: Detector-Free Local
    Feature Matching with Transformers". See :cite:`LoFTR2021` for more details.

    If the distance matrix dm is not provided, :py:func:`torch.cdist` is used.

    Args:
        config: Dict with initialization parameters. Do not pass it, unless you know what you are doing`.
        pretrained: Download and set pretrained weights to the model. Options: 'outdoor', 'indoor'.
                    'outdoor' is trained on the MegaDepth dataset and 'indoor'
                    on the ScanNet.

    Returns:
        Dictionary with image correspondences and confidence scores.

    Example:
        >>> img1 = torch.rand(1, 1, 320, 200)
        >>> img2 = torch.rand(1, 1, 128, 128)
        >>> input = {"image0": img1, "image1": img2}
        >>> loftr = LoFTR('outdoor')
        >>> out = loftr(input)

    r   Ú
pretrainedúOptional[str]Úconfigúdict[str, Any]ÚreturnÚNonec                   sê   t ƒ  ¡  || _|dkrd| jd d< t|ƒ| _t|d d |d d d| _t|d ƒ| _t	|d ƒ| _
t|ƒ| _t|d ƒ| _tƒ | _|| _|d uro|t ¡ vr[td	t ¡ › ƒ‚tjjt| t d
¡d}|  |d ¡ |  ¡  d S )Nr   Tr6   r$   r   )r$   r7   r8   z$pretrained should be None or one of Úcpu)Úmap_locationÚ
state_dict)ÚsuperÚ__init__r<   r	   Úbackboner   Úpos_encodingr   Úloftr_coarser   Úcoarse_matchingr
   Úfine_preprocessÚ
loftr_finer   Úfine_matchingr:   r   ÚkeysÚ
ValueErrorÚtorchÚhubÚload_state_dict_from_urlÚdeviceÚload_state_dictÚeval)r   r:   r<   Úpretrained_dict©Ú	__class__© úN/home/ubuntu/.local/lib/python3.10/site-packages/kornia/feature/loftr/loftr.pyrD   `   s(   

ÿ
zLoFTR.__init__Údataúdict[str, Tensor]c                 C  sn  |d   d¡|d jdd… |d jdd… dœ}|d |d krG|  tj|d |d gdd	¡\}}| |d
 ¡| |d
 ¡\}}\}}n|  |d ¡|  |d ¡\}}\}}| |jdd… |jdd… |jdd… |jdd… dœ¡ |  |¡ dddd¡}|j\}	}
}}| 	|	d|¡}|  |¡ dddd¡}|j\}}}}| 	|d|¡}d }}d|v rÀt
|d |d dd d¡}d|v rÒt
|d |d dd d¡}|  ||||¡\}}| j|||||d |  |||||¡\}}|  d¡dkr|  ||¡\}}|  |||¡ dddddœ}i }| ¡ D ]\}}|| }t|tƒr)|||< qtd|› dt|ƒ› ƒ‚|S )a$  Run forward.

        Args:
            data: dictionary containing the input data in the following format:

        Keyword Args:
            image0: left image with shape :math:`(N, 1, H1, W1)`.
            image1: right image with shape :math:`(N, 1, H2, W2)`.
            mask0 (optional): left image mask. '0' indicates a padded position :math:`(N, H1, W1)`.
            mask1 (optional): right image mask. '0' indicates a padded position :math:`(N, H2, W2)`.

        Returns:
            - ``keypoints0``, matching keypoints from image0 :math:`(NC, 2)`.
            - ``keypoints1``, matching keypoints from image1 :math:`(NC, 2)`.
            - ``confidence``, confidence score [0, 1] :math:`(NC)`.
            - ``batch_indexes``, batch indexes for the keypoints and lafs :math:`(NC)`.

        Úimage0r   r   NÚimage1)ÚbsÚhw0_iÚhw1_ir^   r_   )Údimr]   )Úhw0_cÚhw1_cÚhw0_fÚhw1_fr&   r   éÿÿÿÿÚmask0ra   Únearest)ÚinterpolationéþÿÿÿÚmask1rb   )Úmask_c0Úmask_c1Ú
keypoints0Ú
keypoints1Ú
confidenceÚbatch_indexes)Úmkpts0_fÚmkpts1_fÚmconfÚb_idszExpected Tensor for item `z
`. Gotcha )ÚsizeÚshaperE   rN   ÚcatÚsplitÚupdaterF   ÚpermuteÚreshaper   ÚflattenrG   rH   rI   rJ   rK   ÚitemsÚ
isinstancer   Ú	TypeErrorÚtype)r   rY   Ú_dataÚfeats_cÚfeats_fÚfeat_c0Úfeat_c1Úfeat_f0Úfeat_f1ÚnÚ_hÚ_wÚcÚn1Ú_h1Ú_w1Úc1rk   rl   Úfeat_f0_unfoldÚfeat_f1_unfoldÚrename_keysÚoutÚkÚvÚ_drW   rW   rX   Úforwardy   sX   ý$(&üÿüzLoFTR.forwardrB   Úargsr   Úkwargsc                   sN   t | ¡ ƒD ]}| d¡r| |¡|| ddd¡< qtƒ j|g|¢R i |¤ŽS )Nzmatcher.Ú r   )ÚlistrL   Ú
startswithÚpopÚreplacerC   rR   )r   rB   r˜   r™   r”   rU   rW   rX   rR   Ð   s
   
€zLoFTR.load_state_dict)r:   r;   r<   r=   r>   r?   )rY   rZ   r>   rZ   )rB   r=   r˜   r   r™   r   r>   r   )	Ú__name__Ú
__module__Ú__qualname__Ú__doc__Údefault_cfgrD   r—   rR   Ú__classcell__rW   rW   rU   rX   r9   F   s
    
Wr9   )Ú
__future__r   Útypingr   r   rN   Úkornia.corer   r   Úkornia.geometryr   rE   r	   Úloftr_moduler
   r   Úutils.coarse_matchingr   Úutils.fine_matchingr   Úutils.position_encodingr   r   Ú__annotations__r£   r9   rW   rW   rW   rX   Ú<module>   sN   ú	÷ç