
    3j<                    h    S SK Jr  S SKrS SKJrJr  S SKJr  SS jr " S S\R                  5      r	g)	    )annotationsN)Tensornn)KORNIA_CHECK_SHAPEc                (   [        U / SQ5        [        U/ SQ5        U R                  SS UR                  SS :X  d%  [        SU R                   SUR                   35      eU R                  UR                  :X  d%  [        SU R                   SUR                   35      eU R	                  U R                  S   S	5      nUR	                  UR                  S   S	5      nUR                  u  pES
U-  S-
  nSX&-  -
  nUR                  SSS9u  p[        R                  " X@R                  S9R	                  S	S5      R                  SU5      R	                  S	5      n
X:U	R                  S	5      4   nUR                  XE5      nUR                  SSS9nXR                  S5      -
  nUSU-
  R                  S5      -   nSX-  -
  nUS:  a  USSS24   USSS	24   -
  USSS24'   UR                  5       U-  R                  S5      R                  5       nU$ )a  Criterion that computes a surrogate binary intersection-over-union (IoU) loss.

According to [2], we compute the IoU as follows:

.. math::

    \text{IoU}(x, class) = \frac{|X \cap Y|}{|X \cup Y|}

[1] approximates this fomular with a surrogate, which is fully differentable.

Where:
   - :math:`X` expects to be the scores of each class.
   - :math:`Y` expects to be the binary tensor with the class labels.

the loss, is finally computed as:

.. math::

    \text{loss}(x, class) = 1 - \text{IoU}(x, class)

Reference:
    [1] http://proceedings.mlr.press/v37/yub15.pdf
    [2] https://arxiv.org/pdf/1705.08790.pdf

.. note::
    This loss function only supports binary labels. For multi-class labels please
    use the Lovasz-Softmax loss.

Args:
    pred: logits tensor with shape :math:`(N, 1, H, W)`.
    target: labels tensor with shape :math:`(N, H, W)` with binary values.

Return:
    a scalar with the computed loss.

Example:
    >>> N = 1  # num_classes
    >>> pred = torch.randn(1, N, 3, 5, requires_grad=True)
    >>> target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
    >>> output = lovasz_hinge_loss(pred, target)
    >>> output.backward()

)B1HW)r   r
   r   Nz.pred and target shapes must be the same. Got: z and z1pred and target must be in the same device. Got: r   g       @g      ?   T)dim
descending)device)keepdim.)r   shape
ValueErrorr   reshapesorttorcharangerepeatviewsumcumsumrelumean)predtargetpred_flattentarget_flattenr   Nsignserrorserrors_sortedpermutationbatch_indextarget_sortedtarget_sorted_sumintersectionuniongradientlosss                    T/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/losses/lovasz_hinge.pylovasz_hinge_lossr0      s   X t12v/::bc?fll23//I$**UZ[a[g[gZhijj;;&--'LT[[MY^_e_l_l^mnoo  <<

1r:L#^^FLLOR@N DA . 3&E<''F!'t!DM,,q=EEb!LSSTUWXYaabdeK*8H8H8L+LMM!&&q,M - 1 1!T 1 B,/C/CA/FFL%})<(D(DQ(GGE\11H1u$S!"W-crc0BBab!&&(8388;@@BDK    c                  :   ^  \ rS rSrSrSU 4S jjrSS jrSrU =r$ )LovaszHingeLossk   a  Criterion that computes a surrogate binary intersection-over-union (IoU) loss.

According to [2], we compute the IoU as follows:

.. math::

    \text{IoU}(x, class) = \frac{|X \cap Y|}{|X \cup Y|}

[1] approximates this fomular with a surrogate, which is fully differentable.

Where:
   - :math:`X` expects to be the scores of each class.
   - :math:`Y` expects to be the binary tensor with the class labels.

the loss, is finally computed as:

.. math::

    \text{loss}(x, class) = 1 - \text{IoU}(x, class)

Reference:
    [1] http://proceedings.mlr.press/v37/yub15.pdf
    [2] https://arxiv.org/pdf/1705.08790.pdf

.. note::
    This loss function only supports binary labels. For multi-class labels please
    use the Lovasz-Softmax loss.

Args:
    pred: logits tensor with shape :math:`(N, 1, H, W)`.
    labels: labels tensor with shape :math:`(N, H, W)` with binary values.

Return:
    a scalar with the computed loss.

Example:
    >>> N = 1  # num_classes
    >>> criterion = LovaszHingeLoss()
    >>> pred = torch.randn(1, N, 3, 5, requires_grad=True)
    >>> target = torch.empty(1, 3, 5, dtype=torch.long).random_(N)
    >>> output = criterion(pred, target)
    >>> output.backward()

c                "   > [         TU ]  5         g )N)super__init__)self	__class__s    r/   r7   LovaszHingeLoss.__init__   s    r1   c                    [        XS9$ )N)r   r    )r0   )r8   r   r    s      r/   forwardLovaszHingeLoss.forward   s     d::r1    )returnNoner   r   r    r   r?   r   )	__name__
__module____qualname____firstlineno____doc__r7   r<   __static_attributes____classcell__)r9   s   @r/   r3   r3   k   s    +Z; ;r1   r3   rA   )

__future__r   r   r   r   kornia.core.checkr   r0   Moduler3   r>   r1   r/   <module>rL      s*   $ #   0K\2;bii 2;r1   