
    3j                        S SK Jr  S SKJr  S SKrS SKJs  Jr  S SKJr  S SK	J
r
    S	             S
S jjr " S S\R                  5      rg)    )annotations)OptionalN)nn)mask_ignore_pixelsc           	     B   [        U [        R                  5      (       d  [        S[	        U 5       35      e[        U R                  5      S:X  d  [        SU R                   35      e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[        R                  " U S	S
9n[        X5      u  pUR                  S	UR                  S	5      5      nUb>  UR                  S	5      R                  U R                  S9n	X-  nU	R!                  S5      n
O=U R                  u  pp[        R"                  " U4X-  U R                  U R                  S9n
UR!                  S5      n[        R$                  " UX-
  [        R&                  " XU-   5      SS9R)                  U5      nUR+                  U5      nSUR-                  5       -
  $ )uC  Criterion that computes Tversky Coefficient loss.

According to :cite:`salehi2017tversky`, we compute the Tversky Coefficient as follows:

.. math::

    \text{S}(P, G, \alpha; \beta) =
      \frac{|PG|}{|PG| + \alpha |P \setminus G| + \beta |G \setminus P|}

Where:
   - :math:`P` and :math:`G` are the predicted and ground truth binary
     labels.
   - :math:`\alpha` and :math:`\beta` control the magnitude of the
     penalties for FPs and FNs, respectively.

Note:
   - :math:`\alpha = \beta = 0.5` => dice coeff
   - :math:`\alpha = \beta = 1` => tanimoto coeff
   - :math:`\alpha + \beta = 1` => F beta coeff

Args:
    pred: logits tensor with shape :math:`(N, C, H, W)` where C = number of classes.
    target: labels tensor with shape :math:`(N, H, W)` where each value
      is in range :math:`0 ≤ targets[i] ≤ C-1`.
    alpha: the first coefficient in the denominator.
    beta: the second coefficient in the denominator.
    eps: scalar for numerical stability.
    ignore_index: labels with this value are ignored in the loss computation.

Return:
    the computed loss.

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

z%pred type is not a torch.Tensor. Got    z,Invalid pred shape, we expect BxNxHxW. Got: Nz.pred and target shapes must be the same. Got: z and z1pred and target must be in the same device. Got:    )dim)dtype)r
         )r   deviceg      ?)value)
isinstancetorchTensor	TypeErrortypelenshape
ValueErrorr   Fsoftmaxr   gather	unsqueezetor   sumfulladdcmul	full_likeadd_divmean)predtargetalphabetaepsignore_index	pred_softtarget_maskp_truemtotalB_HWintersectiondenominatorscores                     O/home/wildlama/miniconda3/lib/python3.13/site-packages/kornia/losses/tversky.pytversky_lossr8       s   ` dELL))?T
|LMMtzz?aG

|TUU::bc?fll23//I$**UZ[a[g[gZhijj;;&--'LT[[MY^_e_l_l^mnoo 		$A&I,VBFa!1!1!!45F!!!$''djj'9i ZZ
a

A4djjM::i(L --t|,	
 d3i  [)E    c                  >   ^  \ rS rSrSrSSU 4S jjjrSS jrSrU =r$ )	TverskyLossx   u  Criterion that computes Tversky Coefficient loss.

According to :cite:`salehi2017tversky`, we compute the Tversky Coefficient as follows:

.. math::

    \text{S}(P, G, \alpha; \beta) =
      \frac{|PG|}{|PG| + \alpha |P \setminus G| + \beta |G \setminus P|}

Where:
   - :math:`P` and :math:`G` are the predicted and ground truth binary
     labels.
   - :math:`\alpha` and :math:`\beta` control the magnitude of the
     penalties for FPs and FNs, respectively.

Note:
   - :math:`\alpha = \beta = 0.5` => dice coeff
   - :math:`\alpha = \beta = 1` => tanimoto coeff
   - :math:`\alpha + \beta = 1` => F beta coeff

Args:
    alpha: the first coefficient in the denominator.
    beta: the second coefficient in the denominator.
    eps: scalar for numerical stability.
    ignore_index: labels with this value are ignored in the loss computation.

Shape:
    - Pred: :math:`(N, C, H, W)` where C = number of classes.
    - Target: :math:`(N, H, W)` where each value is
      :math:`0 ≤ targets[i] ≤ C-1`.

Examples:
    >>> N = 5  # num_classes
    >>> criterion = TverskyLoss(alpha=0.5, beta=0.5)
    >>> 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                R   > [         TU ]  5         Xl        X l        X0l        X@l        g N)super__init__r'   r(   r)   r*   )selfr'   r(   r)   r*   	__class__s        r7   r@   TverskyLoss.__init__   s#    !
	+7r9   c                p    [        XU R                  U R                  U R                  U R                  5      $ r>   )r8   r'   r(   r)   r*   )rA   r%   r&   s      r7   forwardTverskyLoss.forward   s'    D$**dii4K\K\]]r9   )r'   r(   r)   r*   g:0yE>i)
r'   floatr(   rH   r)   rH   r*   Optional[int]returnNone)r%   torch.Tensorr&   rL   rJ   rL   )	__name__
__module____qualname____firstlineno____doc__r@   rE   __static_attributes____classcell__)rB   s   @r7   r;   r;   x   s    'R8 8^ ^r9   r;   rG   )r%   rL   r&   rL   r'   rH   r(   rH   r)   rH   r*   rI   rJ   rL   )
__future__r   typingr   r   torch.nn.functionalr   
functionalr   kornia.losses._utilsr   r8   Moduler;    r9   r7   <module>r[      s   $ #      3 "&U
UU U 	U
 
U  U Up2^")) 2^r9   