§
    ’©ºj¶  ã                   ó8  — d dl mZ d dlZd dlmZ d dlmZ d dlmZ d dlm	Z	 de
ded	ed
ed         ded         ddfd„Z	 	 ddedede
ded
ed         ded         deeef         fd„Zddeded	edefd„Z	 	 	 	 ddedede
ded	ed
ed         ded         defd„ZdS )é    )ÚTupleN)ÚTensor)ÚLiteral)Ú_segmentation_inputs_format)Ú_safe_divideÚnum_classesÚinclude_backgroundÚ	per_classÚweight_type©ÚsquareÚsimpleÚlinearÚinput_format©úone-hotÚindexÚmixedÚreturnc                 ó^  — t          | t          ¦  «        r| dk    rt          d| › d�¦  «        ‚t          |t          ¦  «        st          d|› d�¦  «        ‚t          |t          ¦  «        st          d|› d�¦  «        ‚|dvrt          d|› d�¦  «        ‚|dvrt          d	|› d�¦  «        ‚d
S )z%Validate the arguments of the metric.r   zDExpected argument `num_classes` must be a positive integer, but got ú.zBExpected argument `include_background` must be a boolean, but got z9Expected argument `per_class` must be a boolean, but got r   zSExpected argument `weight_type` to be one of 'square', 'simple', 'linear', but got r   zSExpected argument `input_format` to be one of 'one-hot', 'index', 'mixed', but got N)Ú
isinstanceÚintÚ
ValueErrorÚbool)r   r	   r
   r   r   s        ú…/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/segmentation/generalized_dice.pyÚ_generalized_dice_validate_argsr      sý   € õ �k¥3Ñ'Ô'ð p¨;¸!Ò+;Ð+;ÝÐnÐ`kÐnÐnÐnÑoÔoÐoÝÐ(­$Ñ/Ô/ð uÝÐsÐ^pÐsÐsÐsÑtÔtÐtÝ�i¥Ñ&Ô&ð cÝÐaÐU^ÐaÐaÐaÑbÔbÐbØÐ8Ð8Ð8ÝØpÐbmÐpÐpÐpñ
ô 
ð 	
ð Ð8Ð8Ð8ÝØqÐbnÐqÐqÐqñ
ô 
ð 	
ð 9Ð8ó    r   r   ÚpredsÚtargetc                 óø  — t          | ||||¦  «        \  } }t          t          d|j        ¦  «        ¦  «        }t	          j        | |z  |¬¦  «        }t	          j        ||¬¦  «        }t	          j        | |¬¦  «        }	||	z   }
|dk    rd|z  }n=|dk    rt	          j        |¦  «        }n"|dk    r	d|dz  z  }nt          d|› d�¦  «        ‚|j        }| 	                    ¦   «         }t	          j
        |¦  «        }d	||<   t	          j        |d	¦  «        j                             |d	         d
¦  «        j         	                    ¦   «         }||         ||<   |                     |¦  «        }d|z  |z  }|
|z  }||fS )z8Update the state with the current prediction and target.é   )Údimr   g      ð?r   r   zSExpected argument `weight_type` to be one of 'simple', 'linear', 'square', but got r   r   é   g       @)r   ÚlistÚrangeÚndimÚtorchÚsumÚ	ones_liker   ÚshapeÚflattenÚisinfÚmaxÚvaluesÚrepeatÚTÚreshape)r   r    r   r	   r   r   Úreduce_axisÚintersectionÚ
target_sumÚpred_sumÚcardinalityÚweightsÚw_shapeÚweights_flattenÚinfsÚw_maxÚ	numeratorÚdenominators                     r   Ú_generalized_dice_updater?   0   sŸ  € õ 0°°vÐ?QÐS^Ð`lÑmÔm�M€Eˆ6å•u˜Q ¤Ñ,Ô,Ñ-Ô-€KÝ”9˜U V™^°Ð=Ñ=Ô=€LÝ”˜6 {Ð3Ñ3Ô3€JÝŒy˜ KÐ0Ñ0Ô0€HØ˜xÑ'€KØ�hÒÐØ˜
Ñ"ˆˆØ	˜Ò	 Ð	 Ý”/ *Ñ-Ô-ˆˆØ	˜Ò	 Ð	 Ø˜ Q™Ñ'ˆˆåØpÐbmÐpÐpÐpñ
ô 
ð 	
ð Œm€GØ—o’oÑ'Ô'€OÝŒ;�Ñ'Ô'€DØ€O�DÑÝŒI�g˜qÑ!Ô!Ô(×/Ò/°¸´
¸AÑ>Ô>Ô@×HÒHÑJÔJ€EØ! $œK€O�DÑØ×%Ò% gÑ.Ô.€Gà�lÑ" WÑ,€IØ Ñ'€KØ�kÐ!Ð!r   Tr=   r>   c                 óz   — |s*t          j        | d¦  «        } t          j        |d¦  «        }t          | |¦  «        S )z#Compute the generalized dice score.r$   )r(   r)   r   )r=   r>   r
   s      r   Ú_generalized_dice_computerA   X   s<   € àð 0Ý”I˜i¨Ñ+Ô+ˆ	Ý”i ¨QÑ/Ô/ˆÝ˜	 ;Ñ/Ô/Ð/r   Fc                 óx   — t          |||||¦  «         t          | |||||¦  «        \  }}t          |||¦  «        S )a?	  Compute the Generalized Dice Score for semantic segmentation.

    Args:
        preds: Predictions from model
        target: Ground truth values
        num_classes: Number of classes
        include_background: Whether to include the background class in the computation
        per_class: Whether to compute the score for each class separately, else average over all classes
        weight_type: Type of weight factor to apply to the classes. One of ``"square"``, ``"simple"``, or ``"linear"``
        input_format: What kind of input the function receives.
            Choose between ``"one-hot"`` for one-hot encoded tensors, ``"index"`` for index tensors
            or ``"mixed"`` for one one-hot encoded and one index tensor

    Returns:
        The Generalized Dice Score

    Example (with one-hot encoded tensors):
        >>> from torch import randint
        >>> from torchmetrics.functional.segmentation import generalized_dice_score
        >>> preds = randint(0, 2, (4, 5, 16, 16))  # 4 samples, 5 classes, 16x16 prediction
        >>> target = randint(0, 2, (4, 5, 16, 16))  # 4 samples, 5 classes, 16x16 target
        >>> generalized_dice_score(preds, target, num_classes=5)
        tensor([0.4830, 0.4935, 0.5044, 0.4880])
        >>> generalized_dice_score(preds, target, num_classes=5, per_class=True)
        tensor([[0.4724, 0.5185, 0.4710, 0.5062, 0.4500],
                [0.4571, 0.4980, 0.5191, 0.4380, 0.5649],
                [0.5428, 0.4904, 0.5358, 0.4830, 0.4724],
                [0.4715, 0.4925, 0.4797, 0.5267, 0.4788]])

    Example (with index tensors):
        >>> from torch import randint
        >>> from torchmetrics.functional.segmentation import generalized_dice_score
        >>> preds = randint(0, 5, (4, 16, 16))  # 4 samples, 5 classes, 16x16 prediction
        >>> target = randint(0, 5, (4, 16, 16))  # 4 samples, 5 classes, 16x16 target
        >>> generalized_dice_score(preds, target, num_classes=5, input_format="index")
        tensor([0.1991, 0.1971, 0.2350, 0.2216])
        >>> generalized_dice_score(preds, target, num_classes=5, per_class=True, input_format="index")
        tensor([[0.1714, 0.2500, 0.1304, 0.2524, 0.2069],
                [0.1837, 0.2162, 0.0962, 0.2692, 0.1895],
                [0.3866, 0.1348, 0.2526, 0.2301, 0.2083],
                [0.1978, 0.2804, 0.1714, 0.1915, 0.2783]])

    )r   r?   rA   )	r   r    r   r	   r
   r   r   r=   r>   s	            r   Úgeneralized_dice_scorerC   `   sT   € õh $ KÐ1CÀYÐP[Ð]iÑjÔjÐjÝ5Øˆv�{Ð$6¸À\ñô Ñ€Iˆ{õ % Y°¸YÑGÔGÐGr   )r   r   )T)TFr   r   )Útypingr   r(   r   Útyping_extensionsr   Ú*torchmetrics.functional.segmentation.utilsr   Útorchmetrics.utilities.computer   r   r   r   r?   rA   rC   © r   r   ú<module>rI      s  ðð Ð Ð Ð Ð Ð à €€€Ø Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %à RÐ RÐ RÐ RÐ RÐ RØ 7Ð 7Ð 7Ð 7Ð 7Ð 7ð
Øð
àð
ð ð
ð Ð5Ô6ð	
ð
 Ð5Ô6ð
ð 
ð
ð 
ð 
ð 
ð: :BØ9Bð%"ð %"Øð%"àð%"ð ð%"ð ð	%"ð
 Ð5Ô6ð%"ð Ð5Ô6ð%"ð ˆ6�6ˆ>Ôð%"ð %"ð %"ð %"ðP0ð 0¨ð 0¸fð 0ÐQUð 0Ðagð 0ð 0ð 0ð 0ð  $ØØ9AØ9Bð8Hð 8HØð8Hàð8Hð ð8Hð ð	8Hð
 ð8Hð Ð5Ô6ð8Hð Ð5Ô6ð8Hð ð8Hð 8Hð 8Hð 8Hð 8Hð 8Hr   