# 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

import torch
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.feature._layout import _patch_conv_layout
from kornia.feature._normalization import _DescriptorSequential

urls: Dict[str, str | list[str]] = {}
urls["liberty"] = [
    hf_url("hynet", "HyNet_LIB.pth"),
    "https://github.com/ducha-aiki/Key.Net-Pytorch/raw/main/model/HyNet/weights/HyNet_LIB.pth",
]  # pylint: disable
urls["notredame"] = [
    hf_url("hynet", "HyNet_ND.pth"),
    "https://github.com/ducha-aiki/Key.Net-Pytorch/raw/main/model/HyNet/weights/HyNet_ND.pth",
]  # pylint: disable
urls["yosemite"] = [
    hf_url("hynet", "HyNet_YOS.pth"),
    "https://github.com/ducha-aiki/Key.Net-Pytorch/raw/main/model/HyNet/weights/HyNet_YOS.pth",
]  # pylint: disable


class FilterResponseNorm2d(nn.Module):
    r"""Normalise each channel map by its root mean square, without batch statistics (FRN).

    ``frn = FilterResponseNorm2d(C)``, then ``y = frn(x)`` takes feature maps ``(B, C, H, W)`` and returns them in the
    same shape, each map divided by the root of its mean square plus ``|eps|``, then, by default, scaled and shifted per
    channel. It subtracts no mean and uses no batch statistics, so it behaves the same in training and eval mode and for
    any batch size. :class:`HyNet` applies it, followed by :class:`TLU`, after every convolution but the last, where
    :class:`HardNet` uses BatchNorm and ReLU, and on the raw patch, which HardNet standardises instead. A float16 or
    bfloat16 input is normalised in float32, and the output dtype is the promotion of the input's and the layer's, so a
    float16 input to a float32 layer returns float32. This is the Filter 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 \frac{x}{\sqrt{\nu^2 + |\epsilon|}} + \beta

    Convention:
        - :math:`\nu^2` is the mean of :math:`x^2` over the ``H x W`` map of each sample and channel, so the output
          does not depend on the rest of the batch; no mean is subtracted. :math:`\gamma` (``weight``) and
          :math:`\beta` (``bias``) are per-channel parameters initialised to 1 and 0, applied only when ``is_scale``
          and ``is_bias`` are set.

    Args:
        num_features: number of channels
        eps: normalization constant
        is_bias: use bias
        is_scale: use scale
        is_eps_leanable: if eps is learnable

    Returns:
        torch.Tensor: Normalized features

    Shape:
        - Input: ``(B, num_features, H, W)``
        - Output: ``(B, num_features, H, W)``

    See Also:
        - :class:`TLU`: the learned threshold that follows each of these layers in HyNet.
        - :class:`HyNet`: the patch descriptor built from these layers.

    """

    def __init__(
        self,
        num_features: int,
        eps: float = 1e-6,
        is_bias: bool = True,
        is_scale: bool = True,
        is_eps_leanable: bool = False,
    ) -> None:
        super().__init__()

        self.num_features = num_features
        self.init_eps = eps
        self.is_eps_leanable = is_eps_leanable
        self.is_bias = is_bias
        self.is_scale = is_scale

        self.weight = nn.Parameter(torch.ones(1, num_features, 1, 1), requires_grad=True)
        self.bias = nn.Parameter(torch.zeros(1, num_features, 1, 1), requires_grad=True)
        if is_eps_leanable:
            self.eps = nn.Parameter(torch.tensor([eps]), requires_grad=True)
        else:
            self.register_buffer("eps", torch.tensor([eps]))
        self.reset_parameters()

    def reset_parameters(self) -> None:
        """Reset learnable parameters to their initial values.

        Returns:
            None. The module parameters are reset in place.
        """
        nn.init.ones_(self.weight)
        nn.init.zeros_(self.bias)
        if self.is_eps_leanable:
            nn.init.constant_(self.eps, self.init_eps)

    def extra_repr(self) -> str:
        """Return a compact text summary for module printing.

        Returns:
            Readable string summarizing the module configuration.
        """
        return "num_features={num_features}, eps={init_eps}".format(**self.__dict__)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """Apply filter response normalization to the input tensor.

        Args:
            x: Input tensor with shape ``(B, C, H, W)``.

        Returns:
            Normalized tensor with the same shape as the input.
        """
        input_dtype = x.dtype
        half = input_dtype in (torch.float16, torch.bfloat16)
        if half:
            # Pretrained HyNet produces activations whose squares exceed the float16 range, and its scale and bias
            # cancel too many bits for either half dtype.
            x = x.float()
        # Compute the mean norm of activations per channel.
        nu2 = x.pow(2).mean(dim=[2, 3], keepdim=True)

        # Perform FRN.
        x = x * torch.rsqrt(nu2 + self.eps.abs())

        # Scale and Bias
        if self.is_scale:
            x = self.weight * x
        if self.is_bias:
            x = x + self.bias
        if half:
            # Scale and bias stay in float32 too: in pretrained HyNet they cancel (weight 3.70, bias -3.40 in the
            # first layer), so rounding before them loses several bits.
            x = x.to(torch.promote_types(input_dtype, self.eps.dtype))
        return x


class TLU(nn.Module):
    r"""Floor each channel at a learned threshold, the TLU activation that follows FRN in HyNet.

    ``tlu = TLU(C)``, then ``y = tlu(x)`` takes feature maps ``(B, C, H, W)`` and returns
    ``torch.max(x, tau)``, the input floored at a learned threshold ``tau`` per channel, in the same shape. It is the
    activation that the FRN paper pairs with :class:`FilterResponseNorm2d`, which subtracts no mean, in place of a
    ReLU. The output dtype is the promotion of the input's and the layer's. This is the TLU layer from 'Filter
    Response Normalization Layer: Eliminating Batch Dependence in the Training of Deep Neural Networks', see
    :cite:`FRN2019` for more details.

    .. math::
        y = \max(x, {\tau})

    Convention:
        - :math:`\tau` (``tau``) is a ``(1, num_features, 1, 1)`` parameter, one threshold per channel, initialised
          to -1.

    Args:
        num_features: number of channels

    Returns:
        torch.Tensor

    Shape:
        - Input: ``(B, num_features, H, W)``
        - Output: ``(B, num_features, H, W)``

    See Also:
        - :class:`FilterResponseNorm2d`: the normalisation that each of these layers follows in HyNet.
        - :class:`HyNet`: the patch descriptor built from these layers.

    """

    def __init__(self, num_features: int) -> None:
        """max(y, tau) = max(y - tau, 0) + tau = ReLU(y - tau) + tau."""
        super().__init__()
        self.num_features = num_features
        self.tau = nn.Parameter(-torch.ones(1, num_features, 1, 1), requires_grad=True)
        self.reset_parameters()

    def reset_parameters(self) -> None:
        """Reset learnable parameters to their initial values.

        Returns:
            None. The module parameters are reset in place.
        """
        # nn.init.zeros_(self.tau)
        nn.init.constant_(self.tau, -1)

    def extra_repr(self) -> str:
        """Return a compact text summary for module printing.

        Returns:
            Readable string summarizing the module configuration.
        """
        return "num_features={num_features}".format(**self.__dict__)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """Apply the thresholded linear unit to the input tensor.

        Args:
            x: Input tensor.

        Returns:
            Tensor with the same shape as the input, thresholded by ``tau``.
        """
        return torch.max(x, self.tau)


class HyNet(nn.Module):
    r"""Describe 32 x 32 grayscale patches with the learned HyNet CNN, built on FRN layers.

    ``hynet = HyNet()`` builds the network with its pretrained weights (trained on Liberty), in eval mode, and
    ``descs = hynet(patches)`` takes grayscale patches ``(B, 1, 32, 32)`` and returns unit-norm descriptors
    ``(B, dim_desc)``, ``(B, 128)`` by default. Its :class:`FilterResponseNorm2d` layers, each followed by
    :class:`TLU`, subtract no mean, and the first runs on the raw patch, so unlike :class:`HardNet` its descriptor
    changes with an intensity offset (see the Convention block). To describe LAFs on an image, pass it to
    :class:`LAFDescriptor`. 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.

    Convention:
        - It takes the ``(B, 1, 32, 32)`` patches of :class:`~kornia.feature.HardNet` and returns unit-norm rows, but
          its first layer is a :class:`~kornia.feature.FilterResponseNorm2d` on the raw patch, which divides by the root
          mean square and subtracts no mean, as in the reference implementation. A scale :math:`a x` with
          :math:`a > 0` therefore gives the same descriptor as long as the mean squares of the patch and of its scaled
          copy are both large compared with that layer's ``eps``, while an offset :math:`x + b` changes it: feed
          non-negative intensities such as ``[0, 1]`` or ``[0, 255]``, not mean-subtracted or standardised patches.

    Args:
        pretrained: Download and set pretrained weights to the model. The weights fit the default ``is_bias``,
            ``is_bias_FRN`` and ``dim_desc`` only, so pass ``pretrained=False`` with another value: the strict load
            raises ``RuntimeError`` for another ``is_bias`` or ``dim_desc``, and ``is_bias_FRN=False`` raises
            ``ValueError`` before downloading, since the weights would load but leave their trained FRN biases unused.
        is_bias: use bias in the convolution layers (the final 8 x 8 convolution has none either way)
        is_bias_FRN:  use bias in FRN layers
        dim_desc: descriptor dimensionality,
        drop_rate: dropout rate,
        eps_l2_norm: to avoid div by zero

    Returns:
        HyNet descriptor of the patches.

    Shape:
        - Input: ``(B, 1, 32, 32)``
        - Output: ``(B, dim_desc)``

    See Also:
        - :class:`HardNet`: a descriptor of the same patches with BatchNorm, which standardises each patch first.
        - :class:`SOSNet`: a descriptor of the same patches, trained with second-order similarity regularisation.
        - :class:`HardNet8`: the wider follow-up of HardNet, with a PCA to 128 dimensions.
        - :class:`FilterResponseNorm2d`: the normalisation layer of this network.
        - :class:`TLU`: the learned-threshold activation that follows each of its FRN layers.
        - :class:`LAFDescriptor`: runs a patch descriptor such as this one on a patch per LAF of an image.

    Note:
        With float16 weights, the final BatchNorm keeps its running statistics in float32.
        Convert the model to float16 before calling ``torch.jit.script``.

    Examples:
        >>> input = torch.rand(16, 1, 32, 32)
        >>> hynet = HyNet(pretrained=False).eval()
        >>> descs = hynet(input) # 16x128

    """

    patch_size = 32

    def __init__(
        self,
        pretrained: bool = True,
        is_bias: bool = True,
        is_bias_FRN: bool = True,
        dim_desc: int = 128,
        drop_rate: float = 0.3,
        eps_l2_norm: float = 1e-10,
    ) -> None:
        super().__init__()
        if pretrained and not is_bias_FRN:
            # The FRN biases exist with either flag, so the strict load cannot catch this mismatch.
            raise ValueError(
                "HyNet's pretrained weights use their trained FRN biases; pass pretrained=False to build it with "
                "is_bias_FRN=False."
            )
        self.eps_l2_norm = eps_l2_norm
        self.dim_desc = dim_desc
        self.drop_rate = drop_rate
        self.layer1 = nn.Sequential(
            FilterResponseNorm2d(1, is_bias=is_bias_FRN),
            TLU(1),
            nn.Conv2d(1, 32, kernel_size=3, padding=1, bias=is_bias),
            FilterResponseNorm2d(32, is_bias=is_bias_FRN),
            TLU(32),
        )

        self.layer2 = nn.Sequential(
            nn.Conv2d(32, 32, kernel_size=3, padding=1, bias=is_bias),
            FilterResponseNorm2d(32, is_bias=is_bias_FRN),
            TLU(32),
        )

        self.layer3 = nn.Sequential(
            nn.Conv2d(32, 64, kernel_size=3, stride=2, padding=1, bias=is_bias),
            FilterResponseNorm2d(64, is_bias=is_bias_FRN),
            TLU(64),
        )

        self.layer4 = nn.Sequential(
            nn.Conv2d(64, 64, kernel_size=3, padding=1, bias=is_bias),
            FilterResponseNorm2d(64, is_bias=is_bias_FRN),
            TLU(64),
        )
        self.layer5 = nn.Sequential(
            nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1, bias=is_bias),
            FilterResponseNorm2d(128, is_bias=is_bias_FRN),
            TLU(128),
        )

        self.layer6 = nn.Sequential(
            nn.Conv2d(128, 128, kernel_size=3, padding=1, bias=is_bias),
            FilterResponseNorm2d(128, is_bias=is_bias_FRN),
            TLU(128),
        )

        self.layer7 = _DescriptorSequential(
            nn.Dropout(self.drop_rate),
            nn.Conv2d(128, self.dim_desc, kernel_size=8, bias=False),
            nn.BatchNorm2d(self.dim_desc, affine=False),
        )

        self.desc_norm = nn.LocalResponseNorm(2 * self.dim_desc, 2.0 * self.dim_desc, 0.5, 0.0)
        # use torch.hub to load pretrained model
        if pretrained:
            pretrained_dict = load_state_dict_from_url(urls["liberty"], map_location=torch.device("cpu"))
            self.load_state_dict(pretrained_dict, strict=True)
            self.eval()

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """Compute HyNet descriptors from grayscale image patches.

        Args:
            x: Input grayscale patches with shape ``(B, 1, 32, 32)``.

        Returns:
            HyNet descriptors with shape ``(B, D)``.
        """
        KORNIA_CHECK_SHAPE(x, ["B", "1", "32", "32"])
        # The leading FilterResponseNorm2d and TLU are pointwise, so the layout reaches the first convolution.
        x = self.layer1(_patch_conv_layout(x))
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)
        x = self.layer5(x)
        x = self.layer6(x)
        x = self.layer7(x)
        # Half precision takes this step in float32 and casts back, for two reasons that between them
        # cover both half dtypes. (1) `desc_norm` divides by the descriptor's own L2 norm, and
        # `eps_l2_norm` is what keeps that division defined for a patch the network maps to exactly
        # zero -- which HyNet does with `is_bias=False`. 1e-10 is not representable in float16, where
        # it would flush to 0.0 and leave 0/0; bfloat16 keeps float32's exponent range, so the guard
        # survives there and this half of the argument does not apply to it. (2) `F.local_response_norm`
        # routes a 4-D input through `avg_pool3d`, which has no CPU kernel for *either* half dtype, so
        # the lift is what makes CPU half work at all. Note the lift is therefore wider than the one
        # `siftdesc.py` gives its own 1e-10 guards, which is float16-only for reason (1) alone.
        # float32 and float64 take the original expression unchanged, bit for bit.
        if x.dtype in (torch.float16, torch.bfloat16):
            x = self.desc_norm(x.float() + self.eps_l2_norm).to(x.dtype)
        else:
            x = self.desc_norm(x + self.eps_l2_norm)
        return x.flatten(1)
