+
    åûj#  ã                  ó„   € ^ RI Ht ^ RIHt ^ RIt^ RIHtHt ^ RIHtH	t	H
t
 R
R R llt ! R R	]P                  4      tR# )é    )Úannotations)ÚOptionalN)ÚTensorÚnn)ÚKORNIA_CHECKÚKORNIA_CHECK_IS_TENSORÚKORNIA_CHECK_SHAPEc               ó(   € V ^8„  d   QhRRRRRRRR/# )é   Úpredr   ÚtargetÚweightúOptional[Tensor]Úreturn© )Úformats   "ÚXC:\Ai_Coding\apps\studio\ComfyUI\.venv\Lib\site-packages\kornia\losses\lovasz_softmax.pyÚ__annotate__r      s0   € ÷ p'ñ p'˜fð p'¨fð p'Ð>Nð p'ÐZ`ñ p'ó    c                ó  € \        V . RO4       \        V. RO4       V P                  ^,          ^8”  g   \        RV P                   24      hV P                  ^ ,          VP                  ^ ,          8X  g   V P                  ^ ,          ^8X  d"   V P                  RR VP                  RR 8X  g&   \        RV P                   RVP                   24      hV P                  VP                  8X  g&   \        RV P                   RVP                   24      hV P                  ^,          pVeŸ   \	        VR4       \        VP                  ^ ,          V8H  ;'       d    VP                  4       V8H  RVP                   RV P                   24       \        VP                  V P                  8H  R	VP                   RV P                   24       V P                  ^R
7      pVP                  ^R
7      pVP                  w  rgpVP                  ^4      p	\        P                  P                  P                  VP                  \        P                  4      VR7      P                  ^ ^^4      P                  V P                   4      p
Wš,
          P#                  4       p\        P$                  ! V^RR7      w  rÍ\        P&                  ! V P                   \        P(                  4      pV
P+                  ^V4      P                  V4      pVP-                  ^RR7      pVVP/                  ^4      ,
          pVRV,
          P/                  ^4      ,           pRVV,          ,
          pV^8”  d   VR,          VRRR13,          ,
          VR&   VV,          pVP-                  ^4      P1                  ^ 4      pV P                   pVe+   VV,          p\        P&                  ! VVP                   4      pVP1                  4       pVP                  V4      # )u�	  Compute the multi-class LovÃ¡sz-softmax IoU loss of ``(B, C, H, W)`` logits against ``(B, H, W)`` class indices.

``loss = lovasz_softmax_loss(pred, target)`` optimises the intersection over union (IoU) of every class: the
softmax of ``pred`` over the classes (:math:`X`) against the one-hot ``target`` (:math:`Y`). It averages over the
classes the LovÃ¡sz extension of the Jaccard loss of each class [1], evaluated at the errors :math:`|X - Y|`.

.. math::

    \Delta_J = 1 - \frac{|X \cap Y|}{|X \cup Y|}

Reference:
    [1] https://arxiv.org/pdf/1705.08790.pdf

Convention:
    - ``pred`` holds logits ``(B, C, H, W)`` with ``C >= 2`` and the softmax over dim 1 is taken inside;
      ``target`` holds class labels ``(B, H, W)`` in ``[0, C)`` with the spatial size of ``pred``. The batch
      sizes match, or ``pred`` holds one image, which is scored against every target. There is no
      ``ignore_index`` and the labels are not validated: a label of ``-100`` is not ignored, and on the CPU in
      eager mode it raises a ``RuntimeError`` from torch.
    - Each image is scored on its own and the losses are averaged over the batch; there is no ``reduction``. An
      image's loss is the mean over all ``C`` classes: a class absent from the image enters it with that image's
      largest probability for the class.
    - ``weight`` multiplies the term of each class and the mean still divides by ``C``, so a uniform weight of 2
      doubles the loss, where ``dice_loss(average='macro')`` divides by the summed weights of the classes it
      averages.

Args:
    pred: logits ``(B, C, H, W)`` with ``C >= 2``.
    target: class indices ``(B, H, W)`` in ``[0, C)``.
    weight: class weights ``(C,)``.

Returns:
    a scalar with the computed loss, in the dtype of ``pred`` promoted with the dtype of ``weight``.
    The Jaccard weights and the sum over pixels are computed in float32 for a float16 or bfloat16 ``pred``.

See Also:
    - :class:`~kornia.losses.LovaszSoftmaxLoss`: the module form.
    - :func:`~kornia.losses.lovasz_hinge_loss`: the binary loss, from one logit channel.
    - :func:`~kornia.losses.dice_loss`: optimises the Dice overlap and drops ignored pixels.

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 = lovasz_softmax_loss(pred, target)
    >>> output.backward()

z8Invalid pred shape, we expect BxNxHxW, with N > 1. Got: Nz.pred and target shapes must be the same. Got: z and z1pred and target must be in the same device. Got: zweight must be Tensor or None.zBweight must hold one value per class of pred: got weight of shape z for pred of shape z1weight and pred must be in the same device. Got: )Ú	start_dim)Únum_classesT)ÚdimÚ
descending)Úkeepdimg      ð?.)ÚBÚNÚHÚW)r   r   r   éþÿÿÿ).:é   NNéÿÿÿÿ)r	   ÚshapeÚ
ValueErrorÚdevicer   r   ÚnumelÚflattenÚsoftmaxÚtorchr   Ú
functionalÚone_hotÚtoÚint64ÚpermuteÚdtypeÚabsÚsortÚpromote_typesÚfloat32ÚgatherÚsumÚcumsumÚmean)r   r   r   Únum_of_classesÚpred_flattenÚtarget_flattenÚ_ÚCr   Ú	pred_softÚ
foregroundÚerrorsÚerrors_sortedÚpermutationsÚaccumulation_dtypeÚtarget_sortedÚtarget_sorted_sumÚintersectionÚunionÚgradientÚweighted_errorsÚloss_per_classÚoutput_dtypeÚ
final_losss   &&&                     r   Úlovasz_softmax_lossrL      sy  € ôb �tÒ1Ô2ä�všÔ/à�:‰:�a�=˜1ÔÜÐSÐTX×T^ÑT^ÐS_Ð`ÓaÐað �Z‰Z˜�]˜fŸl™l¨1�oÔ-°·±¸AµÀ!Ô1CÈÏÉÐTVÐTWÈÐ\b×\hÑ\hÐikÐilÐ\mÔImÜÐIÈ$Ï*É*ÈÐUZÐ[a×[gÑ[gÐZhÐiÓjÐjà�;‰;˜&Ÿ-™-Ô'ÜÐLÈTÏ[É[ÈMÐY^Ð_e×_lÑ_lÐ^mÐnÓoÐoà—Z‘Z •]€NàÒÜ˜vÐ'GÔHÜØ�\‰\˜!�_ Ñ.×SÐS°6·<±<³>À^Ñ3SØPÐQW×Q]ÑQ]ÐP^Ð^qØ�z‰zˆlðô	
ô
 	Ø�M‰M˜TŸ[™[Ñ(Ø?ÀÇÁ¸ÈeÐTX×T_ÑT_ÐS`Ðaô	
ð  Ÿ<™<°!˜<Ó4€LØ#Ÿ^™^°a˜^Ó8€Nð × Ñ �G€Aˆ!ð %×,Ñ,¨QÓ/€Iô 	�‰×Ñ×#Ñ# N×$5Ñ$5´e·k±kÓ$BÐPQÐ#ÓR×ZÑZÐ[\Ð^_ÐabÓc×fÑfÐgk×gqÑgqÓrð ð  Õ,×1Ñ1Ó3€FÜ"'§*¢*¨V¸ÀtÔ"LÑ€Mô ×,Ò,¨T¯Z©Z¼¿¹ÓGÐØ×%Ñ% a¨Ó6×9Ñ9Ð:LÓM€MØ%×)Ñ)¨!°TÐ)Ó:ÐØ$ }×';Ñ';¸AÓ'>Õ>€LØ  }Õ!4× <Ñ <¸QÓ ?Õ?€EØ�\ EÕ)Õ)€HØˆ1„uØ$ WÕ-°¸¸c¸r¸c¸Õ0BÕBˆ�ÑØ# hÕ.€OØ$×(Ñ(¨Ó+×0Ñ0°Ó3€NØ—:‘:€LØÒØ'¨&Õ0ˆÜ×*Ò*¨<¸¿¹ÓFˆØ'×,Ñ,Ó.€JØ�=‰=˜Ó&Ð&r   c                  óB   a € ] tR t^’tRtRR V 3R llltR R ltRtV ;t# )ÚLovaszSoftmaxLossuÒ  Compute the multi-class LovÃ¡sz-softmax IoU loss of ``(B, C, H, W)`` logits against class indices (module form).

``criterion = LovaszSoftmaxLoss(weight)`` stores ``weight``, and ``loss = criterion(pred, target)`` is
:func:`~kornia.losses.lovasz_softmax_loss`. A plain tensor ``weight`` is stored as a non-persistent buffer,
so ``criterion.to(device)`` moves it and ``state_dict`` omits it.

Convention:
    See the Convention block of :func:`~kornia.losses.lovasz_softmax_loss`.

Args:
    weight: class weights ``(C,)``; ``None`` weights every class by 1.

Shape:
    - pred: logits ``(B, C, H, W)`` with ``C >= 2``.
    - target: class indices ``(B, H, W)`` in ``[0, C)``.
    - Output: a scalar.

See Also:
    - :func:`~kornia.losses.lovasz_softmax_loss`: the one-call form.
    - :class:`~kornia.losses.LovaszHingeLoss`: the binary loss, from one logit channel.
    - :class:`~kornia.losses.DiceLoss`: optimises the Dice overlap and drops ignored pixels.

Example:
    >>> N = 5  # num_classes
    >>> criterion = LovaszSoftmaxLoss()
    >>> 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               ó    € V ^8„  d   QhRRRR/# )r   r   r   r   ÚNoner   )r   s   "r   r   ÚLovaszSoftmaxLoss.__annotate__³   s   € ÷ Eñ EÐ/ð E¸4ñ Er   c                	óœ   <€ \         SV `  4        \        V\        P                  4      '       d	   Wn        R# V P                  R VRR7       R# )r   F)Ú
persistentN)ÚsuperÚ__init__Ú
isinstancer   Ú	Parameterr   Úregister_buffer)Úselfr   Ú	__class__s   &&€r   rU   ÚLovaszSoftmaxLoss.__init__³   s9   ø€ Ü‰ÑÔÜ�fœbŸl™l×+Ò+Ø ŽKà× Ñ  ¨6¸eÐ ÖDr   c               ó$   € V ^8„  d   QhRRRRRR/# )r   r   r   r   r   r   )r   s   "r   r   rQ   º   s&   € ÷ Qñ Q˜Fð Q¨Fð Q°vñ Qr   c                ó0   € \        WV P                  R7      # )a’  Compute multi-class Lovasz-Softmax loss.

Args:
    pred: Class logit tensor with shape ``(B, C, H, W)``, where
        :math:`C` is the number of classes.
    target: Integer class-label tensor with shape ``(B, H, W)``.

Returns:
    Scalar tensor containing the Lovasz-Softmax surrogate for the
    mean intersection-over-union objective, using ``self.weight`` when
    class weights are configured.
)r   r   r   )rL   r   )rY   r   r   s   &&&r   ÚforwardÚLovaszSoftmaxLoss.forwardº   s   € ô #¨ÀDÇKÁKÔPÐPr   )r   ©N)	Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__rU   r^   Ú__static_attributes__Ú__classcell__)rZ   s   @r   rN   rN   ’   s!   ø† ñ÷@Eò E÷Qó Qr   rN   r`   )Ú
__future__r   Útypingr   r)   r   r   Úkornia.core.checkr   r   r	   rL   ÚModulerN   r   r   r   Ú<module>rl      s2   ðõ$ #å ã ß ç VÑ V÷p'ôf5Q˜Ÿ	™	ö 5Qr   