Ë
    Fêñi)8  ã                   ó®   — d dl Z d dlZd dlZd dlmZ d dlmZmZ d dlm	Z	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 dde
de
deded	ed
ede
dededededefd„Z G d„ d«      Zy)é    N)ÚLOGGER)Úbatch_probiouÚbox_iou)Ú	xywh2xyxyÚ
conf_thresÚ	iou_thresÚagnosticÚmulti_labelÚmax_detÚncÚmax_time_imgÚmax_nmsÚmax_whÚrotatedÚend2endÚreturn_idxsc           	      óþ
  — d|cxk  rdk  sn J d|› d�«       ‚d|cxk  rdk  sn J d|› d�«       ‚t        | t        t        f«      r| d   } |�!t        j                  || j
                  ¬«      }| j                  d   d	k(  s|rP| D �cg c]  }||dd…d
f   |kD     d| ‘Œ }}|�-|D �cg c]"  }||dd…dd	…f   |k(  j                  d«         ‘Œ$ }}|S | j                  d   }|xs | j                  d   d
z
  }| j                  d   |z
  d
z
  }d
|z   }| dd…d
|…f   j                  d«      |kD  }t        j                  | j                  d   | j
                  ¬«      j                  |d«      d   }d|	|z  z   }||dkD  z  }| j                  dd«      } |st        | ddd
…f   «      | ddd
…f<   t        j                  «       }t        j                  dd	|z   f| j
                  ¬«      g|z  }t        j                  d| j
                  ¬«      g|z  }t        t!        | |«      «      D �]D  \  }\  }}||   }||   }|r||   }|r­t#        ||   «      rŸ|s�||   }t        j                  t#        |«      ||z   d
z   f|j
                  ¬«      }t        |dd…dd…f   «      |dd…dd
…f<   d|t%        t#        |«      «      |dd…df   j'                  «       d
z   f<   t        j(                  ||fd«      }|j                  d   sŒÚ|j+                  d
||fd«      \  }} }!|rat        j,                  | |kD  «      \  }"}#t        j(                  ||"   ||"d
|#z   df   |#dd…df   j/                  «       |!|"   fd«      }|rb||"   }n\| j1                  dd¬«      \  }$}#|$j3                  d«      |kD  }t        j(                  ||$|#j/                  «       |!fd«      |   }|r||   }|�)|dd…dd	…f   |k(  j                  d«      }||   }|r||   }|j                  d   }%|%s�Œð|%|
kD  r(|dd…d
f   j5                  d¬«      d|
 }||   }|r||   }|dd…dd	…f   |rdn|z  }&|dd…d
f   }'|rVt        j(                  |dd…dd…f   |&z   |dd…dd
…f   |dd…dd…f   fd¬«      }(t6        j9                  |(|'|t:        ¬«      }"nY|dd…dd
…f   |&z   }(dt<        j>                  v r"ddl })|)jB                  jE                  |(|'|«      }"nt6        jE                  |(|'|«      }"|"d| }"||"   ||<   |r||"   j3                  d«      ||<   t        j                  «       |z
  |kD  s�Œ+tG        jH                  d|d›d�«        n |r||fS |S c c}w c c}w )ad  Perform non-maximum suppression (NMS) on prediction results.

    Applies NMS to filter overlapping bounding boxes based on confidence and IoU thresholds. Supports multiple detection
    formats including standard boxes, rotated boxes, and masks.

    Args:
        prediction (torch.Tensor): Predictions with shape (batch_size, num_classes + 4 + num_masks, num_boxes)
            containing boxes, classes, and optional masks.
        conf_thres (float): Confidence threshold for filtering detections. Valid values are between 0.0 and 1.0.
        iou_thres (float): IoU threshold for NMS filtering. Valid values are between 0.0 and 1.0.
        classes (list[int], optional): List of class indices to consider. If None, all classes are considered.
        agnostic (bool): Whether to perform class-agnostic NMS.
        multi_label (bool): Whether each box can have multiple labels.
        labels (list[torch.Tensor]): A priori labels for each image.
        max_det (int): Maximum number of detections to keep per image.
        nc (int): Number of classes. Indices after this are considered masks.
        max_time_img (float): Maximum time in seconds for processing one image.
        max_nms (int): Maximum number of boxes for NMS.
        max_wh (int): Maximum box width and height in pixels.
        rotated (bool): Whether to handle Oriented Bounding Boxes (OBB).
        end2end (bool): Whether the model is end-to-end and doesn't require NMS.
        return_idxs (bool): Whether to return the indices of kept detections.

    Returns:
        (list[torch.Tensor] | tuple[list[torch.Tensor], list[torch.Tensor]]): List of detections per image with shape
            (num_boxes, 6 + num_masks) containing (x1, y1, x2, y2, confidence, class, mask1, mask2, ...). If
            return_idxs=True, returns a tuple of (output, keepi) where keepi contains indices of kept detections.
    r   é   zInvalid Confidence threshold z&, valid values are between 0.0 and 1.0zInvalid IoU N©Údeviceéÿÿÿÿé   é   é   ).Ng       @éþÿÿÿ.)r   r   g      ð?T)Úkeepdim©Ú
descendingé   )Údim)Úiou_funcÚtorchvisionzNMS time limit z.3fz
s exceeded)%Ú
isinstanceÚlistÚtupleÚtorchÚtensorr   ÚshapeÚanyÚamaxÚarangeÚexpandÚ	transposer   ÚtimeÚzerosÚ	enumerateÚzipÚlenÚrangeÚlongÚcatÚsplitÚwhereÚfloatÚmaxÚviewÚargsortÚTorchNMSÚfast_nmsr   ÚsysÚmodulesr"   ÚopsÚnmsr   Úwarning)*Ú
predictionr   r   Úclassesr	   r
   Úlabelsr   r   r   r   r   r   r   r   ÚpredÚoutputÚbsÚextraÚmiÚxcÚxindsÚ
time_limitÚtÚkeepiÚxiÚxÚxkÚfiltÚlbÚvÚboxÚclsÚmaskÚiÚjÚconfÚnÚcÚscoresÚboxesr"   s*                                             úW/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/ultralytics/utils/nms.pyÚnon_max_suppressionra      só  € ð\ �
Ô˜aÔÐsÐ#@ÀÀÐLrÐ!sÓsÐØ�	Ô˜QÔÐ` ,¨y¨kÐ9_Ð `Ó`ÐÜ�*œt¤U˜mÔ,Ø ‘]ˆ
ØÐÜ—,‘,˜w¨z×/@Ñ/@ÔAˆà×Ñ˜Ñ˜qÒ ¡GØFPÖQ¸d�$�tšA˜q˜D‘z JÑ.Ñ/°°Ò9ÐQˆÐQØÐØIOÖPÀ�d˜D¢ A a C ™L¨GÑ3×8Ñ8¸Ó;Ó<ÐPˆFÐPØˆà	×	Ñ	˜!Ñ	€BØ	Ò	(�
× Ñ  Ñ# aÑ'€BØ×Ñ˜QÑ "Ñ$ qÑ(€EØ	
ˆR‰€BØ	’A�q˜�t�GÑ	×	!Ñ	! !Ó	$ zÑ	1€BÜ�L‰L˜×)Ñ)¨"Ñ-°j×6GÑ6GÔH×OÑOÐPRÐTVÓWÐXaÑb€Eð �| bÑ(Ñ(€JØ�2˜‘6Ñ€Kà×%Ñ% b¨"Ó-€JÙÜ'¨
°3¸¸¸°7Ñ(;Ó<ˆ
�3˜˜˜�7Ñä�	‰	‹€AÜ�k‰k˜1˜a %™i˜.°×1BÑ1BÔCÐDÀrÑI€FÜ�[‰[˜¨
×(9Ñ(9Ô:Ð;¸bÑ@€EÜ ¤ Z°Ó!7Ó8ó I‰ˆ‰GˆQ�ð �"‰vˆØˆd‰GˆÙØ�D‘ˆBñ ”c˜& ™*”o©gØ˜‘ˆBÜ—‘œS ›W b¨5¡j°1¡nÐ5¸a¿h¹hÔGˆAÜ  ¢A q¨ s F¡Ó,ˆAŠa��!�ˆe‰HØ58ˆAŒe”C˜“G‹n˜b¢ A ™hŸm™m›o°Ñ1Ð1Ñ2Ü—	‘	˜1˜a˜& !Ó$ˆAð �w‰w�qŠzØð Ÿ™ ! R¨ °Ó3‰ˆˆS�$áÜ—;‘;˜s ZÑ/Ó0‰DˆAˆqÜ—	‘	˜3˜q™6 1 Q¨¨A©¨t ^Ñ#4°aº¸4¸±j×6FÑ6FÓ6HÈ$ÈqÉ'ÐRÐTUÓVˆAÙØ˜‘U‘à—g‘g˜a¨�gÓ.‰GˆD�!Ø—9‘9˜R“= :Ñ-ˆDÜ—	‘	˜3  a§g¡g£i°Ð6¸Ó:¸4Ñ@ˆAÙØ˜‘X�ð ÐØ’a˜˜1˜�f‘I Ñ(×-Ñ-¨aÓ0ˆDØ�$‘ˆAÙØ˜‘X�ð �G‰G�A‰JˆÙÙØˆwŠ;Ø’Q˜�T‘7—?‘?¨d�?Ó3°H°WÐ=ˆDØ�$‘ˆAÙØ˜‘X�àŠa��1�ˆf‰I™h™¨FÑ3ˆØ’1�a�4‘ˆÙÜ—I‘I˜q¢ B Q B ™x¨!™|¨Qªq°!°A°#¨v©Y¸º!¸R¹S¸&¹	ÐBÈÔKˆEÜ×!Ñ! %¨°Ä]Ð!ÓS‰Aà’a˜˜!˜�e‘H˜q‘LˆEà¤§¡Ñ+Û"à—O‘O×'Ñ'¨¨v°yÓA‘ä—L‘L ¨°	Ó:�ØˆhˆwˆKˆà�q‘Tˆˆr‰
ÙØ˜1™Ÿ
™
 2›ˆE�"‰IÜ�I‰I‹K˜!‰O˜zÔ)Ü�N‰N˜_¨Z¸Ð,<¸JÐGÔHÙðSIñV *ˆF�Eˆ?Ð5¨vÐ5ùòG RùâPs   ÂU5Â('U:c                   óZ  — e Zd ZdZededfdej                  dej                  dede	de	dej                  fd	„«       Z
edej                  dej                  dedej                  fd
„«       Ze	 ddej                  dej                  dej                  dede	dej                  fd„«       Zy)r<   aÕ  Ultralytics custom NMS implementation optimized for YOLO.

    This class provides static methods for performing non-maximum suppression (NMS) operations on bounding boxes,
    including standard NMS, fast NMS, and batched NMS for multi-class scenarios.

    Methods:
        fast_nms: Fast-NMS using upper triangular matrix operations.
        nms: Optimized NMS with early termination that matches torchvision behavior exactly.
        batched_nms: Batched NMS for class-aware suppression.

    Examples:
        Perform standard NMS on boxes and scores
        >>> boxes = torch.tensor([[0, 0, 10, 10], [5, 5, 15, 15]])
        >>> scores = torch.tensor([0.9, 0.8])
        >>> keep = TorchNMS.nms(boxes, scores, 0.5)
    Tr_   r^   Úiou_thresholdÚuse_triuÚ
exit_earlyÚreturnc                 ó^  — | j                  «       dk(  r2|r0t        j                  dt        j                  | j                  ¬«      S t        j
                  |d¬«      }| |   }  || | «      }|rP|j                  d¬«      }t        j                  ||k\  j                  d«      dk  «      j                  d«      }||   S | j                  d   }	t        j                  |	| j                  ¬	«      j                  dd«      j                  d|	«      }
t        j                  |	| j                  ¬	«      j                  dd«      j                  |	d«      }|
|k  }||z  }||   }d|||k\  j                  d«      dk   <   |||<   t        j                  ||j                  d   «      j                  }||   S )
a™  Fast-NMS implementation from https://arxiv.org/pdf/1904.02689 using upper triangular matrix operations.

        Args:
            boxes (torch.Tensor): Bounding boxes with shape (N, 4) in xyxy format.
            scores (torch.Tensor): Confidence scores with shape (N,).
            iou_threshold (float): IoU threshold for suppression.
            use_triu (bool): Whether to use torch.triu operator for upper triangular matrix operations.
            iou_func (callable): Function to compute IoU between boxes.
            exit_early (bool): Whether to exit early if there are no boxes.

        Returns:
            (torch.Tensor): Indices of boxes to keep after NMS.

        Examples:
            Apply NMS to a set of boxes
            >>> boxes = torch.tensor([[0, 0, 10, 10], [5, 5, 15, 15]])
            >>> scores = torch.tensor([0.9, 0.8])
            >>> keep = TorchNMS.fast_nms(boxes, scores, 0.5)
        r   ©r   ©Údtyper   Tr   r   )Údiagonalr   r   )Únumelr&   ÚemptyÚint64r   r;   Útriu_ÚnonzeroÚsumÚsqueeze_r(   r+   r:   r,   ÚtopkÚindices)r_   r^   rc   rd   r!   re   Ú
sorted_idxÚiousÚpickr\   Úrow_idxÚcol_idxÚ
upper_maskÚscores_s                 r`   r=   zTorchNMS.fast_nms»   s„  € ð8 �;‰;‹=˜AÒ¡*Ü—;‘;˜t¬5¯;©;¸u¿|¹|ÔLÐLä—]‘] 6°dÔ;ˆ
Ø�jÑ!ˆÙ˜˜uÓ%ˆÙØ—:‘: q�:Ó)ˆDä—=‘= $¨-Ñ"7×!<Ñ!<¸QÓ!?À1Ñ!DÓE×NÑNÈrÓRˆDð ˜$ÑÐð —‘˜A‘ˆAÜ—l‘l 1¨U¯\©\Ô:×?Ñ?ÀÀAÓF×MÑMÈbÐRSÓTˆGÜ—l‘l 1¨U¯\©\Ô:×?Ñ?ÀÀ2ÓF×MÑMÈaÐQSÓTˆGØ  7Ñ*ˆJØ˜*Ñ$ˆDà˜ZÑ(ˆGØ>?ˆG�t˜}Ñ,×1Ñ1°!Ó4¸Ñ9Ð:Ñ;Ø!(ˆF�:Ñä—:‘:˜g w§}¡}°QÑ'7Ó8×@Ñ@ˆDØ˜$ÑÐó    c                 ó¸  — | j                  «       dk(  r0t        j                  dt        j                  | j                  ¬«      S | j                  d«      \  }}}}||z
  ||z
  z  }|j                  dd¬«      }t        j                  |j                  «       t        j                  | j                  ¬«      }	d}
|j                  «       dkD  �r|d   }||	|
<   |
dz  }
|j                  «       dk(  rnè|dd }t        j                  ||   ||   «      }t        j                  ||   ||   «      }t        j                  ||   ||   «      }t        j                  ||   ||   «      }||z
  j                  d¬«      }||z
  j                  d¬«      }||z  }|j                  «       dk(  r|}Œñ|||   ||   z   |z
  z  }|||k     }|j                  «       dkD  r�Œ|	d|
 S )	a€  Optimized NMS with early termination that matches torchvision behavior exactly.

        Args:
            boxes (torch.Tensor): Bounding boxes with shape (N, 4) in xyxy format.
            scores (torch.Tensor): Confidence scores with shape (N,).
            iou_threshold (float): IoU threshold for suppression.

        Returns:
            (torch.Tensor): Indices of boxes to keep after NMS.

        Examples:
            Apply NMS to a set of boxes
            >>> boxes = torch.tensor([[0, 0, 10, 10], [5, 5, 15, 15]])
            >>> scores = torch.tensor([0.9, 0.8])
            >>> keep = TorchNMS.nms(boxes, scores, 0.5)
        r   rh   ri   r   Tr   N)Úmin)rl   r&   rm   rn   r   Úunbindr;   r/   ÚmaximumÚminimumÚclamp_rq   )r_   r^   rc   Úx1Úy1Úx2Úy2ÚareasÚorderÚkeepÚkeep_idxrY   ÚrestÚxx1Úyy1Úxx2Úyy2ÚwÚhÚinterÚious                        r`   rA   zTorchNMS.nmsï   sÎ  € ð$ �;‰;‹=˜AÒÜ—;‘;˜t¬5¯;©;¸u¿|¹|ÔLÐLð Ÿ™ a›‰ˆˆB��BØ�b‘˜R "™WÑ%ˆð —‘˜q¨T�Ó2ˆô �{‰{˜5Ÿ;™;›=´·±ÀEÇLÁLÔQˆØˆØ�k‰k‹m˜aÓØ�a‘ˆAØˆD�‰NØ˜‰MˆHà�{‰{‹} Ò!Øà˜˜�9ˆDÜ—-‘-  1¡ r¨$¡xÓ0ˆCÜ—-‘-  1¡ r¨$¡xÓ0ˆCÜ—-‘-  1¡ r¨$¡xÓ0ˆCÜ—-‘-  1¡ r¨$¡xÓ0ˆCð �s‘×"Ñ" qÐ"Ó)ˆAØ�s‘×"Ñ" qÐ"Ó)ˆAØ˜‘EˆEà�y‰y‹{˜aÒà�ØØ˜5 ™8 e¨D¡kÑ1°EÑ9Ñ:ˆCà˜ Ñ-Ñ.ˆEð3 �k‰k‹m˜aÔð6 �I�XˆÐr|   ÚidxsÚuse_fast_nmsc                 óN  — | j                  «       dk(  r0t        j                  dt        j                  | j                  ¬«      S | j                  «       }|j                  | «      |dz   z  }| |dd…df   z   }|rt        j                  |||«      S t        j                  |||«      S )a.  Batched NMS for class-aware suppression.

        Args:
            boxes (torch.Tensor): Bounding boxes with shape (N, 4) in xyxy format.
            scores (torch.Tensor): Confidence scores with shape (N,).
            idxs (torch.Tensor): Class indices with shape (N,).
            iou_threshold (float): IoU threshold for suppression.
            use_fast_nms (bool): Whether to use the Fast-NMS implementation.

        Returns:
            (torch.Tensor): Indices of boxes to keep after NMS.

        Examples:
            Apply batched NMS across multiple classes
            >>> boxes = torch.tensor([[0, 0, 10, 10], [5, 5, 15, 15]])
            >>> scores = torch.tensor([0.9, 0.8])
            >>> idxs = torch.tensor([0, 1])
            >>> keep = TorchNMS.batched_nms(boxes, scores, idxs, 0.5)
        r   rh   ri   r   N)
rl   r&   rm   rn   r   r9   Útor<   r=   rA   )r_   r^   r”   rc   r•   Úmax_coordinateÚoffsetsÚboxes_for_nmss           r`   Úbatched_nmszTorchNMS.batched_nms+  s™   € ð6 �;‰;‹=˜AÒÜ—;‘;˜t¬5¯;©;¸u¿|¹|ÔLÐLð Ÿ™›ˆØ—'‘'˜%“. N°QÑ$6Ñ7ˆØ ª¨4¨Ñ 0Ñ0ˆñ ô ×Ñ˜m¨V°]ÓCð	
ô —‘˜m¨V°]ÓCð	
r|   N)F)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústaticmethodr   r&   ÚTensorr8   Úboolr=   rA   r›   © r|   r`   r<   r<   ©   s  „ ñð" ð
 ØØñ1 Ø�|‰|ð1 à—‘ð1 ð ð1 ð ð	1 ð ð1 ð 
�‰ò1 ó ð1 ðf ð9�5—<‘<ð 9¨¯©ð 9Àeð 9ÐPU×P\ÑP\ò 9ó ð9ðv ð #ñ&
Ø�|‰|ð&
à—‘ð&
ð �l‰lð&
ð ð	&
ð
 ð&
ð 
�‰ò&
ó ñ&
r|   r<   )g      Ð?gÍÌÌÌÌÌÜ?NFFr£   i,  r   gš™™™™™©?i0u  i   FFF)r>   r.   r&   Úultralytics.utilsr   Úultralytics.utils.metricsr   r   Úultralytics.utils.opsr   r8   r¢   Úintra   r<   r£   r|   r`   ú<module>r¨      sÚ   ðó Û ã å $ß <Ý +ð
 ØØØØØØØØØØØØØñY6àðY6ð ðY6ð
 ðY6ð ðY6ð ðY6ð 	ðY6ð ðY6ð ðY6ð ðY6ð ðY6ð ðY6ð óY6÷xi
ò i
r|   