+
    åûj�.  ã                  óØ   a € 0 t $ ^ RIHt ^ RIt^ RIHu Ht ^ RIHt ^ RIH	t	H
t
 ^RIHt ^RIHt ^RIHt R]	! R	R
4      R.R]	! R	R4      R./tR]R&    ! R R]P&                  4      tR# )é    )ÚannotationsN)Únn)Úhf_urlÚload_state_dict_from_url)ÚUnet)Úheatmap_to_keypoints)ÚDISKFeaturesÚdepthÚdiskzdepth-save.pthzGhttps://raw.githubusercontent.com/cvlab-epfl/disk/master/depth-save.pthÚepipolarzepipolar-save.pthzJhttps://raw.githubusercontent.com/cvlab-epfl/disk/master/epipolar-save.pthzdict[str, list[str]]Úurlsc                  ól   a € ] tR t^,tRtRR V 3R llltR R ltRR R llt]RR R	 ll4       t	R
t
V ;t# )ÚDISKas  Detect integer-pixel keypoints and describe them with the dense maps of one U-Net (DISK).

``disk = DISK.from_pretrained("depth")`` loads trained weights in evaluation mode.
``features = disk(images, n=2048)`` takes RGB images ``(B, 3, H, W)`` and returns one :class:`DISKFeatures`
per image: up to ``n`` integer-pixel keypoints ``(N, 2)``, scores ``(N,)`` and L2-normalised descriptors
``(N, 128)``. Image dimensions must be multiples of 16 unless ``pad_if_not_divisible=True``.
Use :class:`ALIKED` for subpixel keypoints or :class:`DeDoDe` for a fixed keypoint budget per image.

See :cite:`tyszkiewicz2020disk` for details.

.. image:: _static/img/disk_outdoor_depth.jpg

Convention:
    - ``DISK()`` creates random weights in training mode. For matching, construct LAFs with
      :func:`~kornia.feature.laf_from_center_scale_ori` on ``keypoints[None]`` and pass them with the descriptors
      to :class:`LightGlueMatcher` in ``"disk"`` mode.
    - ``images`` are RGB, ``(B, 3, H, W)``, which the U-Net sees as given, without normalisation. With the
      default U-Net, a common positive gain of the input, such as :math:`[0, 255]` instead of :math:`[0, 1]`,
      changes the outputs only through the instance norms' ``eps``, which weighs more the lower the image's
      contrast. At ordinary contrast the maps barely change, though a keypoint can still swap where scores nearly
      tie or at the ``n`` cut; at low contrast the keypoints and descriptors change substantially. With the
      ``depth`` weights and ``n=2048``, the :math:`[0, 1]` and :math:`[0, 255]` versions of Oxford graf, wall and
      bark ``img1`` differ in 0 to 2 keypoints, in 2 to 6 % of them with the intensities multiplied by 0.1, and
      in about 30 % multiplied by 0.01. ``H`` and ``W`` must be multiples of 16, unless
      ``pad_if_not_divisible=True`` zero-pads the bottom and right, in which case the maps are cropped back to the
      image before the suppression and no keypoint lies in the padding. The image must have the network's
      dtype: a float16, bfloat16 or float64 image raises on a float32 model, so cast the model first,
      ``disk.to(images.dtype)``.
    - Keypoints are the integer ``(x, y)`` pixels of the strict heatmap
      maxima (see the note), using the pixel convention of :func:`~kornia.feature.laf_from_center_scale_ori`
      (see :ref:`Local features <local-features-conventions>`), listed in raster order (row by row, then column
      by column) rather than by score, in float32 for a float16 or bfloat16 network and otherwise in the dtype
      of the network's output. ``detection_scores`` are the heatmap values there, in the network's dtype, and
      each descriptor is the dense descriptor at the keypoint's pixel, L2-normalised;
      :meth:`heatmap_and_dense_descriptors` returns the dense descriptors without normalisation.
    - ``score_threshold`` keeps the scores strictly above it. ``n`` is an upper bound on the maxima above
      ``score_threshold`` in each image; the ``n`` argument of :meth:`forward` says which of them are kept when
      there are more.

Args:
    desc_dim: The dimension of the descriptor.
    unet: The U-Net to use. If None, a default U-Net is used. Kornia doesn't provide the training code for DISK
          so this is only useful when using a custom checkpoint trained using the code released with the paper.
          The unet should take as input a torch.Tensor of shape ``(B, C, H, W)`` and output
          a torch.Tensor of shape ``(B, desc_dim + 1, H, W)``: the descriptors, then the heatmap.

Note:
    Keypoints are the strict local maxima of the heatmap: :func:`~kornia.geometry.subpix.nms2d` keeps a position
    only if its score is greater than every other score in its full ``window_size`` window. Tied maxima, and the
    positions near the border whose window runs off the image (see :func:`~kornia.geometry.subpix.nms2d`), are
    never keypoints. This departs on purpose from the reference implementation (cvlab-epfl/disk). Its max-pool NMS
    clips the window at the border, so it judges a border position against part of its window. Of several equal
    maxima it keeps the one the max-pool reports first, which depends on an order PyTorch does not specify. With
    the pretrained ``depth`` weights on Oxford graf and boat img1, every keypoint of the reference that kornia
    leaves out lies in that border strip. The reference's ``n`` keeps the scores strictly above the
    ``(n + 1)``-th largest, which drops the weakest maximum, with every maximum tied with it, of an image with
    ``n`` maxima or fewer, and every maximum tied across the cut. kornia's ``n`` is an upper bound, so such an
    image keeps every maximum, and a tie at the cut is filled in raster order.

See Also:
    - :class:`DISKFeatures`: the per-image result of :meth:`forward`.
    - :class:`ALIKED`: learned sub-pixel keypoints for RGB images of any size, also as padded LAF tensors.
    - :class:`XFeat`: learned keypoints with 64-D descriptors, and its own mutual-nearest-neighbour and refined
      matching of image pairs.
    - :class:`DeDoDe`: separate learned detector and descriptor networks, with ``n`` keypoints in every image.
    - :class:`LocalFeature`: a classical detector and LAF descriptor in one call, returning padded LAF tensors
      instead of a list per image.
    - :func:`laf_from_center_scale_ori`: turns the keypoints of one image into the LAFs that
      :class:`LightGlueMatcher` and :class:`LAFDescriptor` take.
    - :class:`LightGlueMatcher`: matches DISK's descriptors and keypoints with the ``"disk"`` weights.

Example:
    >>> disk = DISK.from_pretrained('depth')
    >>> images = torch.rand(1, 3, 256, 256)
    >>> features = disk(images)

c               ó$   € V ^8„  d   QhRRRRRR/# )é   Údesc_dimÚintÚunetznn.Module | NoneÚreturnÚNone© )Úformats   "ÚTC:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\feature\disk\disk.pyÚ__annotate__ÚDISK.__annotate__{   s"   € ÷ ñ  ð Ð2Bð Èdñ ó    c           
     	ó|   <€ \         SV `  4        Wn        Vf   \        ^^. RO^@^@^@V^,           .R7      pW n        R # )N)Úin_featuresÚsizeÚdownÚup)é   é    é@   r$   r$   )ÚsuperÚ__init__r   r   r   )Úselfr   r   Ú	__class__s   &&&€r   r&   ÚDISK.__init__{   s@   ø€ Ü‰ÑÔà ŒàŠ<Ü A¨AÒ4HÈbÐRTÐVXÐZbÐefÕZfÐMgÔhˆDØŽ	r   c               ó    € V ^8„  d   QhRRRR/# )r   Úimagesútorch.Tensorr   z!tuple[torch.Tensor, torch.Tensor]r   )r   s   "r   r   r   „   s   € ÷ %ñ %°Lð %ÐEfñ %r   c                ó2  € V P                  V4      pVP                  ^,          V P                  ^,           8w  d.   \        RVP                  ^,           RV P                   R24      hVRRV P                  13,          pVRV P                  R13,          pWC3# )ad  Return the heatmap and the dense descriptors.

.. image:: _static/img/DISK.png

Args:
    images: The image to detect features in. Shape ``(B, 3, H, W)``.

Returns:
    A tuple of dense detection scores and descriptors.
    Shapes are ``(B, 1, H, W)`` and ``(B, D, H, W)``, where
    ``D`` is the descriptor dimension; the descriptors are not normalised.

zU-Net output has z& channels, but expected self.desc_dim=z + 1.:NNNN)r   Úshaper   Ú
ValueError)r'   r+   Úunet_outputÚdescriptorsÚheatmapss   &&   r   Úheatmap_and_dense_descriptorsÚ"DISK.heatmap_and_dense_descriptors„   sš   € ð —i‘i Ó'ˆà×Ñ˜QÕ 4§=¡=°1Õ#4Ô4ÜØ# K×$5Ñ$5°aÕ$8Ð#9Ð9_Ð`d×`mÑ`mÐ_nÐnsÐtóð ð " ! _ t§}¡} _Ð"4Õ5ˆØ˜q $§-¡-¡/Ð1Õ2ˆàÐ$Ð$r   c               ó0   € V ^8„  d   QhRRRRRRRRR	R
RR/# )r   r+   r,   Únz
int | NoneÚwindow_sizer   Úscore_thresholdÚfloatÚpad_if_not_divisibleÚboolr   zlist[DISKFeatures]r   )r   s   "r   r   r   ž   sF   € ÷ +ñ +àð+ð ð+ð ð	+ð
 ð+ð #ð+ð 
ñ+r   c                ó0  € VP                   ^ ,          pV'       ds   VP                   R,          w  rxV^,          ^ 8”  d   ^V^,          ,
          M^ p	V^,          ^ 8”  d   ^V^,          ,
          M^ p
\        P                  ! V^ V
^ V	3RR7      pV P                  V4      w  r¼V'       d!   VRRX1RX13,          pVRRV1RV13,          p\	        W²W4R7      p. p\        V4       F/  pVP                  Wß,          P                  WÏ,          4      4       K1  	  V# )a¸  Detect features in an image, returning keypoint locations, descriptors and detection scores.

Args:
    images: The image to detect features in. Shape ``(B, 3, H, W)``.
    n: The maximum number of keypoints per image, counted among the maxima above ``score_threshold``. An
       image with ``n`` such maxima or fewer keeps all of them; with more, its ``n`` strongest, where maxima
       tied at the ``n``-th score are taken in raster order. If None, all keypoints are returned.
    window_size: The size of the non-maxima suppression window used to filter detections.
    score_threshold: Detections must score strictly above it to be returned.
                     See :py:class:`DISKFeatures` for details.
    pad_if_not_divisible: if True, an input whose height or width is not a multiple of 16 is zero-padded at
        the bottom and right to the next multiple of 16.

Returns:
    A list of length :math:`B` containing the detected features.

:r   NNç        )Úvalue.N)r6   r7   r8   )r.   ÚFÚpadr3   r   ÚrangeÚappendÚmerge_with_descriptors)r'   r+   r6   r7   r8   r:   ÚBÚhÚwÚpd_hÚpd_wr2   r1   Ú	keypointsÚfeaturesÚis   &&&&&&          r   ÚforwardÚDISK.forwardž   sø   € ð2 �L‰L˜�OˆßØ—<‘< Õ#‰DˆAØ"# b¥&¨1¤*�2˜˜B�–;°!ˆDØ"# b¥&¨1¤*�2˜˜B�–;°!ˆDÜ—U’U˜6 A t¨Q°Ð#5¸SÔAˆFà $× BÑ BÀ6Ó JÑˆßØ  R a R¨¨!¨ Õ,ˆHØ% c¨2¨A¨2¨r°¨r kÕ2ˆKä(¨ÀKÔqˆ	àˆÜ�q–ˆAØ�O‰O˜I�L×?Ñ?ÀÅÓOÖPñ ð ˆr   c               ó$   € V ^8„  d   QhRRRRRR/# )r   Ú
checkpointÚstrÚdeviceztorch.device | Noner   r   r   )r   s   "r   r   r   Ì   s#   € ÷ ñ ¨ð Ð@Sð Ð_cñ r   c                ó  € V\         9  d   \        RV 24      hVf   \        P                  ! R4      p\	        \         V,          VR7      pV ! 4       P                  V4      pVP                  VR,          4       VP                  4        V# )a.  Load a pretrained model.

Depth model was trained using depth map supervision and is slightly more precise but biased to detect keypoints
only where SfM depth is available. Epipolar model was trained using epipolar geometry supervision and
is less precise but detects keypoints everywhere where they are matchable. The difference is especially
pronounced on thin structures and on edges of objects.

Args:
    checkpoint: The checkpoint to load. One of 'depth' or 'epipolar'.
    device: The device to load the model to.

Returns:
    The pretrained model.

zUnknown pretrained model: Úcpu)Úmap_locationÚ	extractor)r   r/   ÚtorchrQ   r   ÚtoÚload_state_dictÚeval)ÚclsrO   rQ   Úpretrained_dictÚmodels   &&&  r   Úfrom_pretrainedÚDISK.from_pretrainedË   su   € ð" œTÔ!ÜÐ9¸*¸ÐFÓGÐGàŠ>Ü—\’\ %Ó(ˆFÜ2´4¸
Õ3CÐRXÔYˆá“e—h‘h˜vÓ&ˆØ×Ñ˜o¨kÕ:Ô;Ø�
‰
ŒØˆr   )r   r   )é€   N)Né   r=   F)r
   N)Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__r&   r3   rL   Úclassmethodr]   Ú__static_attributes__Ú__classcell__)r(   s   @r   r   r   ,   s3   ø† ñL÷\ò õ%÷4+ðZ öó ör   r   )Ú__conditional_annotations__Ú
__future__r   rV   Útorch.nn.functionalr   Ú
functionalr?   Úkornia.core.downloadr   r   Ú_unetsr   Údetectorr   Ústructsr	   r   Ú__annotations__ÚModuler   )ri   s   @r   Ú<module>rs      sw   øð÷$ #Ð "ã ß Ð Ý ç Aå Ý *Ý !ð
 ÙˆvÐ'Ó(ØQðð ÙˆvÐ*Ó+ØTðð	€Ðó 	ôzˆ2�9‰9ö zr   