# LICENSE HEADER MANAGED BY add-license-header
#
# Copyright 2018 Kornia Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from typing import Dict, Literal, Optional, Tuple

import torch
import torch.nn.functional as F
from torch import nn

from kornia.core.check import KORNIA_CHECK_SHAPE
from kornia.core.download import hf_url, load_state_dict_from_url
from kornia.enhance.normalize import Normalize

from .dedode_models import DeDoDeDescriptor, DeDoDeDetector, get_descriptor, get_detector
from .utils import dedode_denormalize_pixel_coordinates, sample_keypoints

urls: Dict[str, Dict[str, str]] = {
    "detector": {
        "L-upright": [
            hf_url("dedode", "dedode_detector_L.pth"),
            "https://github.com/Parskatt/DeDoDe/releases/download/dedode_pretrained_models/dedode_detector_L.pth",
        ],
        "L-C4": [
            hf_url("dedode", "dedode_detector_C4.pth"),
            "https://github.com/georg-bn/rotation-steerers/releases/download/release-2/dedode_detector_C4.pth",
        ],
        "L-SO2": [
            hf_url("dedode", "dedode_detector_SO2.pth"),
            "https://github.com/georg-bn/rotation-steerers/releases/download/release-2/dedode_detector_SO2.pth",
        ],
        "L-C4-v2": [
            hf_url("dedode", "dedode_detector_L_v2.pth"),
            "https://github.com/Parskatt/DeDoDe/releases/download/v2/dedode_detector_L_v2.pth",
        ],
    },
    "descriptor": {
        "B-upright": [
            hf_url("dedode", "dedode_descriptor_B.pth"),
            "https://github.com/Parskatt/DeDoDe/releases/download/dedode_pretrained_models/dedode_descriptor_B.pth",
        ],
        "B-C4": [
            hf_url("dedode", "B_C4_Perm_descriptor_setting_C.pth"),
            "https://github.com/georg-bn/rotation-steerers/releases/download/release-2/B_C4_Perm_descriptor_setting_C.pth",
        ],
        "B-SO2": [
            hf_url("dedode", "B_SO2_Spread_descriptor_setting_C.pth"),
            "https://github.com/georg-bn/rotation-steerers/releases/download/release-2/B_SO2_Spread_descriptor_setting_C.pth",
        ],
        "G-upright": [
            hf_url("dedode", "dedode_descriptor_G.pth"),
            "https://github.com/Parskatt/DeDoDe/releases/download/dedode_pretrained_models/dedode_descriptor_G.pth",
        ],
        "G-C4": [
            hf_url("dedode", "G_C4_Perm_descriptor_setting_C.pth"),
            "https://github.com/georg-bn/rotation-steerers/releases/download/release-2/G_C4_Perm_descriptor_setting_C.pth",
        ],
        "G-SO2": [
            hf_url("dedode", "G_SO2_Spread_descriptor_setting_C.pth"),
            "https://github.com/georg-bn/rotation-steerers/releases/download/release-2/G_SO2_Spread_descriptor_setting_C.pth",
        ],
    },
}


class DeDoDe(nn.Module):
    r"""Detect the n best keypoints of each RGB image and describe them with two networks (DeDoDe).

    ``dedode = DeDoDe.from_pretrained(detector_weights="L-C4-v2", descriptor_weights="B-upright")`` loads trained
    weights in evaluation mode. ``keypoints, scores, descriptions = dedode(images, n=10_000)`` takes RGB images
    ``(B, 3, H, W)`` in :math:`[0, 1]` and returns the best ``N = min(n, H W)`` keypoints per image.
    Outputs are ``(x, y)`` pixel coordinates ``(B, N, 2)`` with pixel centres at ``i + 0.5``, scores ``(B, N)``
    and unnormalised descriptors ``(B, N, 256)``. Use :class:`ALIKED` or :class:`DISK` when the keypoint count
    should depend on image content.

    See :cite:`edstedt2024dedode` for details.

    Convention:
        - :meth:`detect` and :meth:`describe` run the detector and descriptor separately. The default descriptor,
          ``G``, combines VGG19 with a frozen DINOv2 ViT-L/14 backbone and loads the backbone's weights in the
          constructor. Descriptor ``B`` uses VGG19 alone.
        - For :class:`LightGlueMatcher`, construct LAFs centred on the keypoints minus 0.5 and pass them with the
          descriptors in ``"dedodeb"`` or ``"dedodeg"`` mode, matching the descriptor variant.
        - :meth:`forward`, :meth:`detect` and :meth:`describe` take RGB images in the range :math:`[0, 1]` and apply
          the ImageNet normalization themselves; pass ``apply_imagenet_normalization=False`` for images that are
          already normalized. ``pad_if_not_divisible`` zero-pads the normalized image at the bottom and right to
          multiples of 14, so the padding is the ImageNet mean colour, and removes it from the maps again.
        - :meth:`forward` returns pixel ``i`` as ``i + 0.5``: subtract 0.5 for the pixel convention of
          :func:`~kornia.feature.laf_from_center_scale_ori` (see :ref:`Local features <local-features-conventions>`).
          :meth:`detect` returns it normalized, as ``(2 i + 1) / (W, H) - 1`` in :math:`[-1, 1]`
          (``align_corners=False``), the form :meth:`describe` takes; ``(k + 1) * (W, H) / 2 - 0.5`` converts such a
          ``k`` to kornia's frame. A description is read at the centre of pixel ``i``.
        - The keypoints of an image come in descending order of score, with no threshold and no padding. Keypoints,
          and the descriptions of :meth:`forward` and of ``describe(images, keypoints)``, are float32. The scoremap
          softmax and the keypoint sampling run in at least float32, so the scores are float32 for a half-precision
          model and float64 for a float64 one. The dense map of ``describe(images)`` follows the network dtype:
          ``amp_dtype`` on CUDA, the dtype of the model on CPU and MPS.
        - kornia detects and describes at the size of the input. The authors resize every image to 784 x 784, without
          keeping its aspect ratio, in ``detect_from_path`` and ``describe_keypoints_from_path``, and train the
          detector and the ``B`` descriptor at 512 x 512 (``G`` at 560 x 560). The 51-pixel coverage window of the
          keypoint sampling and the 14-pixel patches of ``G`` do not scale with the image. After resizing to
          784 x 784, the normalized keypoints of :meth:`detect` hold for the original image unchanged; multiply those
          of :meth:`forward` by ``(W / 784, H / 784)``.
        - ``amp_dtype`` is CUDA mixed precision in every component: on CUDA tensors the networks run under
          ``torch.autocast`` in ``amp_dtype`` (and the DINOv2 backbone of the ``G`` descriptor is cast to it). On CPU
          and MPS it has no effect, so the default argument runs there with float32 weights and images.

    Args:
        detector_model: The detector model kind. Available options are: ``"L"``.
        descriptor_model: The descriptor model kind. Available options are: ``"G"`` or ``"B"``.
        amp_dtype: The dtype of the automatic mixed precision used on CUDA; it has no effect on other devices.

    See Also:
        - :class:`ALIKED`: learned sub-pixel keypoints whose count follows each image's score map, for RGB images of
          any size.
        - :class:`DISK`: learned integer-pixel keypoints with 128-D descriptors from one U-Net, for RGB images whose
          sides are multiples of 16.
        - :class:`XFeat`: learned keypoints with 64-D descriptors, and its own mutual-nearest-neighbour and refined
          matching of image pairs.
        - :class:`LightGlueMatcher`: matches the descriptions and keypoints with the ``"dedodeb"`` weights for the
          ``B`` descriptor and the ``"dedodeg"`` weights for ``G``.

    Example:
        >>> dedode = DeDoDe.from_pretrained(detector_weights="L-C4-v2", descriptor_weights="B-upright")
        >>> images = torch.rand(1, 3, 256, 256)
        >>> keypoints, scores = dedode.detect(images)
        >>> descriptions = dedode.describe(images, keypoints = keypoints)
        >>> keypoints, scores, features = dedode(images) # alternatively do both

    """

    # TODO: implement steerers and mnn matchers
    def __init__(
        self,
        detector_model: Literal["L"] = "L",
        descriptor_model: Literal["G", "B"] = "G",
        amp_dtype: torch.dtype = torch.float16,
    ) -> None:
        super().__init__()
        self.detector: DeDoDeDetector = get_detector(detector_model, amp_dtype)
        self.descriptor: DeDoDeDescriptor = get_descriptor(descriptor_model, amp_dtype)
        self.normalizer = Normalize(torch.tensor([0.485, 0.456, 0.406]), std=torch.tensor([0.229, 0.224, 0.225]))

    def _preprocess(
        self, images: torch.Tensor, apply_imagenet_normalization: bool, pad_if_not_divisible: bool
    ) -> torch.Tensor:
        """Normalise, then zero-pad to a multiple of 14.

        The pad is applied after the normalisation so that it is the ImageNet mean colour, the neutral value the
        networks see, in every entry point.
        """
        if apply_imagenet_normalization:
            images = self.normalizer(images)
        if pad_if_not_divisible:
            H, W = images.shape[-2:]
            pd_h = 14 - H % 14 if H % 14 > 0 else 0
            pd_w = 14 - W % 14 if W % 14 > 0 else 0
            images = F.pad(images, (0, pd_w, 0, pd_h), value=0.0)
        return images

    def forward(
        self,
        images: torch.Tensor,
        n: Optional[int] = 10_000,
        apply_imagenet_normalization: bool = True,
        pad_if_not_divisible: bool = True,
    ) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
        """Detect and describe keypoints in the input images.

        Args:
            images: A torch.Tensor of shape ``(B, 3, H, W)`` containing the RGB input images in the range
                :math:`[0, 1]`.
            n: The number of keypoints to detect in each image, at most ``H W``; ``None`` keeps every pixel.
            apply_imagenet_normalization: Whether to apply ImageNet normalization to the input images. Pass
                ``False`` if they are already ImageNet-normalized.
            pad_if_not_divisible: Zero-pad the (normalised) image on the bottom and right so that H and W are
                multiples of 14; the pad is the ImageNet mean colour. The padding is removed from the scoremap, so
                keypoints are never reported in it.

        Returns:
            keypoints: A torch.Tensor of shape ``(B, N, 2)`` containing the detected keypoints in pixels, with pixel
                ``i`` at ``i + 0.5``, unlike :meth:`detect`, which normalises them.

            scores: A torch.Tensor of shape ``(B, N)`` containing the scores of the detected keypoints.

            descriptions: A torch.Tensor of shape ``(B, N, DIM)`` containing the descriptions
                of the detected keypoints. DIM is 256 for both B and G.

        """
        _B, _C, H, W = images.shape
        images = self._preprocess(images, apply_imagenet_normalization, pad_if_not_divisible)
        # ``images`` is already normalised and, when asked, padded: the calls below must do neither again.
        keypoints, scores = self.detect(
            images, n=n, apply_imagenet_normalization=False, pad_if_not_divisible=False, crop_h=H, crop_w=W
        )
        descriptions = self.describe(
            images, keypoints, apply_imagenet_normalization=False, pad_if_not_divisible=False, crop_h=H, crop_w=W
        )
        return dedode_denormalize_pixel_coordinates(keypoints, H, W), scores, descriptions

    @torch.no_grad()
    def detect(
        self,
        images: torch.Tensor,
        n: Optional[int] = 10_000,
        apply_imagenet_normalization: bool = True,
        pad_if_not_divisible: bool = True,
        crop_h: Optional[int] = None,
        crop_w: Optional[int] = None,
    ) -> Tuple[torch.Tensor, torch.Tensor]:
        """Detect keypoints in the input images.

        .. note::
            This method unconditionally sets the model to eval mode via ``self.train(False)``
            so that BatchNorm and Dropout behave deterministically. This is intentional: the
            detector is only used at inference time and its statistics must be frozen.

        Args:
            images: A torch.Tensor of shape ``(B, 3, H, W)`` containing the RGB input images in the range
                :math:`[0, 1]`.
            n: The number of keypoints to detect.
            apply_imagenet_normalization: Whether to apply ImageNet normalization to the input images. Pass
                ``False`` if they are already ImageNet-normalized.
            pad_if_not_divisible: Zero-pad the (normalised) image on the bottom and right so that H and W are
                multiples of 14; the pad is the ImageNet mean colour, as in :meth:`forward` and :meth:`describe`. The
                padding is removed from the scoremap, so keypoints are never reported in it.
            crop_h: The height of the crop to be used for detection. If None, the full image is used.
            crop_w: The width of the crop to be used for detection. If None, the full image is used.

        Returns:
            keypoints: A torch.Tensor of shape ``(B, N, 2)`` containing the detected keypoints,
            normalized to the range :math:`[-1, 1]`.
            scores: A torch.Tensor of shape ``(B, N)`` containing the scores of the detected keypoints.

        """
        KORNIA_CHECK_SHAPE(images, ["B", "3", "H", "W"])
        self.train(False)
        B, _C, H, W = images.shape
        images = self._preprocess(images, apply_imagenet_normalization, pad_if_not_divisible)
        logits = self.detector.forward(images)
        # Remove the padding, if any
        logits = logits[..., :H, :W]
        if crop_h is not None and crop_w is not None:
            logits = logits[..., :crop_h, :crop_w]
            H, W = crop_h, crop_w
        # at least float32: the probabilities are about 1 / (H W), below the smallest normal float16 for images above
        # 128 x 128, and bfloat16 keeps 8 significant bits of them
        logits = logits.to(torch.promote_types(logits.dtype, torch.float32))
        scoremap = logits.reshape(B, H * W).softmax(dim=-1).reshape(B, H, W)
        keypoints, confidence = sample_keypoints(scoremap, num_samples=n)
        return keypoints, confidence

    @torch.no_grad()
    def describe(
        self,
        images: torch.Tensor,
        keypoints: Optional[torch.Tensor] = None,
        apply_imagenet_normalization: bool = True,
        pad_if_not_divisible: bool = True,
        crop_h: Optional[int] = None,
        crop_w: Optional[int] = None,
    ) -> torch.Tensor:
        """Describe keypoints in the input images. If keypoints are not provided, returns the dense descriptors.

        .. note::
            This method unconditionally sets the model to eval mode via ``self.train(False)``
            so that BatchNorm and Dropout behave deterministically. This is intentional: the
            descriptor is only used at inference time and its statistics must be frozen.

        Args:
            images: A torch.Tensor of shape ``(B, 3, H, W)`` containing the RGB input images in the range
                :math:`[0, 1]`.
            keypoints: An optional torch.Tensor of shape ``(B, N, 2)`` containing the detected keypoints.
            apply_imagenet_normalization: Whether to apply ImageNet normalization to the input images. Pass
                ``False`` if they are already ImageNet-normalized.
            pad_if_not_divisible: Zero-pad the (normalised) image on the bottom and right so that H and W are
                multiples of 14; the pad is the ImageNet mean colour, as in :meth:`forward` and :meth:`detect`.
                Required when using the ``G`` descriptor backed by DINOv2 (patch size 14). The padding is removed from
                the descriptor map, so the map has the size of the input image.
            crop_h: The height of the crop to be used for description. If None, the full image is used.
            crop_w: The width of the crop to be used for description. If None, the full image is used.

        Returns:
            descriptions: A torch.Tensor of shape ``(B, N, DIM)`` containing the descriptions
                of the detected keypoints.
                If the dense descriptors are requested, the shape is ``(B, DIM, H, W)``.

        """
        KORNIA_CHECK_SHAPE(images, ["B", "3", "H", "W"])
        _B, _C, H, W = images.shape
        if keypoints is not None:
            KORNIA_CHECK_SHAPE(keypoints, ["B", "N", "2"])
        images = self._preprocess(images, apply_imagenet_normalization, pad_if_not_divisible)
        self.train(False)
        descriptions = self.descriptor.forward(images)
        if crop_h is not None and crop_w is not None:
            H, W = crop_h, crop_w
        # Remove the padding, so that the normalised keypoints of `detect` index the map of the input image.
        descriptions = descriptions[..., :H, :W]

        if keypoints is not None:
            return F.grid_sample(descriptions.float(), keypoints[:, None], mode="bilinear", align_corners=False)[
                :, :, 0
            ].mT
        return descriptions

    @classmethod
    def from_pretrained(
        cls,
        detector_weights: str = "L-C4-v2",
        descriptor_weights: str = "G-upright",
        amp_dtype: torch.dtype = torch.float16,
    ) -> nn.Module:
        r"""Load a pretrained model.

        Args:
            detector_weights: The weights to load for the detector.
                One of 'L-upright' (original paper, https://arxiv.org/abs/2308.08479),
                'L-C4', 'L-SO2' (from steerers, better for rotations, https://arxiv.org/abs/2312.02152),
                'L-C4-v2' (from dedode v2, better at rotations, less clustering, https://arxiv.org/abs/2404.08928).
                Default is 'L-C4-v2'.
            descriptor_weights: The weights to load for the descriptor.
                One of 'B-upright','G-upright' (original paper, https://arxiv.org/abs/2308.08479),
                'B-C4', 'B-SO2', 'G-C4', 'G-SO2' (from steerers, better for rotations, https://arxiv.org/abs/2312.02152).
                Default is 'G-upright'.
            amp_dtype: the dtype of the automatic mixed precision used on CUDA, one of torch.float16 or
                torch.float32. It has no effect on CPU or MPS, where every component runs in the dtype of the
                input and the model.
                Default is torch.float16.

        Returns:
            The pretrained model.

        """
        # The model architecture kind is encoded as the first character of the weight name,
        # e.g. "L-C4-v2" -> "L", "G-upright" -> "G". All current checkpoints follow this
        # convention intentionally so that new variants only need a new URL entry.
        model: DeDoDe = cls(
            detector_model=detector_weights[0],
            descriptor_model=descriptor_weights[0],
            amp_dtype=amp_dtype,
        )
        model.detector.load_state_dict(
            load_state_dict_from_url(urls["detector"][detector_weights], map_location=torch.device("cpu"))
        )
        model.descriptor.load_state_dict(
            load_state_dict_from_url(urls["descriptor"][descriptor_weights], map_location=torch.device("cpu"))
        )
        model.eval()
        return model
