§
    ’©ºj‚  ã                   óŒ  — d dl mZmZ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ded	ed
ed         deeef         fd„Z	 ddee         deded
ed         ddf
d„Z	 	 	 ddededee         ded
ed         deeef         fd„Zdededeeed         f         defd„Z	 	 	 	 ddededee         deded
ed         defd„ZdS )é    )ÚOptionalÚTupleÚUnionN)ÚTensor)ÚLiteral)Ú_segmentation_inputs_format©Ú_safe_divideúone-hotÚpredsÚtargetsÚinput_format©r   ÚindexÚmixedÚreturnc                 óÖ  — |dk    r| |fS |                       ¦   «         dk    r)|                      d¦  «                             d¦  «        } n-|                       ¦   «         dk    r|                      d¦  «        } |                      ¦   «         dk    r)|                     d¦  «                             d¦  «        }n-|                      ¦   «         dk    r|                     d¦  «        }| |fS )z Reshape tensors to 3D if needed.r   é   r   é   )ÚdimÚ	unsqueeze)r   r   r   s      ú}/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/torchmetrics/functional/segmentation/mean_iou.pyÚ_mean_iou_reshape_argsr      s×   € ð �yÒ Ð Ø�gˆ~Ðà‡y‚y�{„{�aÒÐØ—’ Ñ"Ô"×,Ò,¨QÑ/Ô/ˆˆØ	�Š‰Œ˜Ò	Ð	Ø—’ Ñ"Ô"ˆà‡{‚{�}„}˜ÒÐØ×#Ò# AÑ&Ô&×0Ò0°Ñ3Ô3ˆˆØ	�Š‰Œ˜!Ò	Ð	Ø×#Ò# AÑ&Ô&ˆØ�'ˆ>Ðó    Únum_classesÚinclude_backgroundÚ	per_classc                 ó4  — |dv r| €t          d¦  «        ‚| �| 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S )z%Validate the arguments of the metric.)r   NzGArgument `num_classes` must be provided when `input_format` is 'index'.r   zNExpected argument `num_classes` must be `None` or 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 `input_format` to be one of 'one-hot', 'index', 'mixed', but got )Ú
ValueErrorÚ
isinstanceÚbool)r   r   r   r   s       r   Ú_mean_iou_validate_argsr#   .   sè   € ð �yÐ Ð  [Ð%8ÝÐbÑcÔcÐcØÐ ;°!Ò#3Ð#3ÝØkÐ]hÐkÐkÐkñ
ô 
ð 	
õ Ð(­$Ñ/Ô/ð uÝÐsÐ^pÐsÐsÐsÑtÔtÐtÝ�i¥Ñ&Ô&ð cÝÐaÐU^ÐaÐaÐaÑbÔbÐbØÐ8Ð8Ð8ÝØqÐbnÐqÐqÐqñ
ô 
ð 	
ð 9Ð8r   FÚtargetc                 ó<  — t          | ||¦  «        \  } }t          | ||||¦  «        \  } }t          t          d| j        ¦  «        ¦  «        }t          j        | |z  |¬¦  «        }t          j        ||¬¦  «        }t          j        | |¬¦  «        }||z   |z
  }	||	fS )zFUpdate the intersection and union counts for the mean IoU computation.r   ©r   )r   r   ÚlistÚrangeÚndimÚtorchÚsum)
r   r$   r   r   r   Úreduce_axisÚintersectionÚ
target_sumÚpred_sumÚunions
             r   Ú_mean_iou_updater1   E   s¨   € õ +¨5°&¸,ÑGÔG�M€Eˆ6å/°°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Ø˜Ñ! LÑ0€EØ˜ÐÐr   r-   r0   Úzero_division)ÚwarnÚnanc                 ó&   — t          | ||¬¦  «        S )zCompute the mean IoU metric.©r2   r	   )r-   r0   r2   s      r   Ú_mean_iou_computer7   Y   s   € õ ˜ e¸=ÐIÑIÔIÐIr   Tc                 ó
  — t          ||||¦  «         t          | ||||¦  «        \  }}t          ||d¬¦  «        }|dk    }	|r|                     d¦  «        n,|                     d¬¦  «        |	                     d¬¦  «        z  S )aý	  Calculates the mean Intersection over Union (mIoU) for semantic segmentation.

    Returns -1 if class is completely absent both from predictions and ground truth labels.

    Args:
        preds: Predictions from model
        target: Ground truth values
        num_classes: Number of classes
            (required when input_format="index", optional when input_format="one-hot" or "mixed")
        include_background: Whether to include the background class in the computation
        per_class: Whether to compute the IoU for each class separately, else average over all classes
        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 mean IoU score

    Example:
        >>> import torch
        >>> from torch import randint
        >>> from torchmetrics.functional.segmentation import mean_iou
        >>> # 4 samples, 5 classes, 16x16 prediction
        >>> preds = randint(0, 2, (4, 5, 16, 16), generator=torch.Generator().manual_seed(42))
        >>> # 4 samples, 5 classes, 16x16 target
        >>> target = randint(0, 2, (4, 5, 16, 16), generator=torch.Generator().manual_seed(43))
        >>> mean_iou(preds, target)
        tensor([0.3323, 0.3336, 0.3397, 0.3435])
        >>> mean_iou(preds, target, include_background=False, num_classes=5)
        tensor([0.3250, 0.3258, 0.3307, 0.3398])
        >>> mean_iou(preds, target, include_background=True, num_classes=5, per_class=True)
        tensor([[0.3617, 0.3128, 0.3366, 0.3242, 0.3263],
                [0.3646, 0.2893, 0.3297, 0.3073, 0.3770],
                [0.3756, 0.3168, 0.3505, 0.3400, 0.3155],
                [0.3579, 0.3317, 0.3797, 0.3523, 0.2957]])
        >>> # re-initialize tensors for ``input_format="index"``
        >>> preds = randint(0, 2, (4, 16, 16), generator=torch.Generator().manual_seed(42))
        >>> target = randint(0, 2, (4, 16, 16), generator=torch.Generator().manual_seed(43))
        >>> mean_iou(preds, target, num_classes=5, input_format = "index")
        tensor([0.3617, 0.3128, 0.3047, 0.3499])
        >>> mean_iou(preds, target, num_classes=5, per_class=True, input_format="index")
        tensor([[ 0.3617,  0.3617, -1.0000, -1.0000, -1.0000],
                [ 0.3128,  0.3128, -1.0000, -1.0000, -1.0000],
                [ 0.2727,  0.3366, -1.0000, -1.0000, -1.0000],
                [ 0.3756,  0.3242, -1.0000, -1.0000, -1.0000]])

    r4   r6   r   g      ð¿éÿÿÿÿr&   )r#   r1   r7   Ú
nan_to_numÚnansumr+   )
r   r$   r   r   r   r   r-   r0   ÚscoresÚvalid_classess
             r   Úmean_iour>   b   s–   € õn ˜KÐ);¸YÈÑUÔUÐUÝ*¨5°&¸+ÐGYÐ[gÑhÔhÑ€L�%Ý˜|¨UÀ%ÐHÑHÔH€FØ˜A’I€MØ&/Ðfˆ6×Ò˜TÑ"Ô"Ð"°V·]²]Àr°]Ñ5JÔ5JÈ]×M^ÒM^ÐceÐM^ÑMfÔMfÑ5fÐfr   )r   )NFr   )NTFr   )Útypingr   r   r   r*   r   Útyping_extensionsr   Ú*torchmetrics.functional.segmentation.utilsr   Útorchmetrics.utilities.computer
   r   Úintr"   r#   Útupler1   Úfloatr7   r>   © r   r   ú<module>rG      sh  ðð *Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ð )Ð )à €€€Ø Ð Ð Ð Ð Ð Ø %Ð %Ð %Ð %Ð %Ð %à RÐ RÐ RÐ RÐ RÐ RØ 7Ð 7Ð 7Ð 7Ð 7Ð 7ð :Cðð Øðàðð Ð5Ô6ðð ˆ6�6ˆ>Ôð	ð ð ð ð2 :Cð	
ð 
Ø˜#”ð
àð
ð ð
ð Ð5Ô6ð	
ð
 
ð
ð 
ð 
ð 
ð4 "&Ø$Ø9Bðð Øðàðð ˜#”ðð ð	ð
 Ð5Ô6ðð ˆ6�6ˆ>Ôðð ð ð ð(JØðJàðJð ˜ ¨Ô 6Ð6Ô7ðJð ð	Jð Jð Jð Jð "&Ø#ØØ9Bð;gð ;gØð;gàð;gð ˜#”ð;gð ð	;gð
 ð;gð Ð5Ô6ð;gð ð;gð ;gð ;gð ;gð ;gð ;gr   