
    h"                        S SK Jr  S SKJr  S SKJr  S SKrS SKJr  S SK	J
r
JrJr  S SKJr  S SKJrJr  S	r " S
 S\5      r " S S\5      rg)    )annotations)Path)AnyN)YOLODataset)ComposeFormatv8_transforms)DetectionValidator)colorstrops)RTDETRValidatorc                  N   ^  \ rS rSrSrSS.U 4S jjrS	U 4S jjrS
S jrSrU =r	$ )RTDETRDataset   a?  
Real-Time DEtection and TRacking (RT-DETR) dataset class extending the base YOLODataset class.

This specialized dataset class is designed for use with the RT-DETR object detection model and is optimized for
real-time detection and tracking tasks.

Attributes:
    augment (bool): Whether to apply data augmentation.
    rect (bool): Whether to use rectangular training.
    use_segments (bool): Whether to use segmentation masks.
    use_keypoints (bool): Whether to use keypoint annotations.
    imgsz (int): Target image size for training.

Methods:
    load_image: Load one image from dataset index.
    build_transforms: Build transformation pipeline for the dataset.

Examples:
    Initialize an RT-DETR dataset
    >>> dataset = RTDETRDataset(img_path="path/to/images", imgsz=640)
    >>> image, hw = dataset.load_image(0)
N)datac               *   > [         TU ]  " USU0UD6  g)a  
Initialize the RTDETRDataset class by inheriting from the YOLODataset class.

This constructor sets up a dataset specifically optimized for the RT-DETR (Real-Time DEtection and TRacking)
model, building upon the base YOLODataset functionality.

Args:
    *args (Any): Variable length argument list passed to the parent YOLODataset class.
    data (dict | None): Dictionary containing dataset information. If None, default values will be used.
    **kwargs (Any): Additional keyword arguments passed to the parent YOLODataset class.
r   N)super__init__)selfr   argskwargs	__class__s       W/home/james-whalen/.local/lib/python3.13/site-packages/ultralytics/models/rtdetr/val.pyr   RTDETRDataset.__init__*   s     	$4T4V4    c                   > [         TU ]  XS9$ )a  
Load one image from dataset index 'i'.

Args:
    i (int): Index of the image to load.
    rect_mode (bool, optional): Whether to use rectangular mode for batch inference.

Returns:
    im (torch.Tensor): The loaded image.
    resized_hw (tuple): Height and width of the resized image with shape (2,).

Examples:
    Load an image from the dataset
    >>> dataset = RTDETRDataset(img_path="path/to/images")
    >>> image, hw = dataset.load_image(0)
)i	rect_mode)r   
load_image)r   r   r   r   s      r   r   RTDETRDataset.load_image8   s    " w!A!;;r   c                4   U R                   (       a  U R                   (       a  U R                  (       d  UR                  OSUl        U R                   (       a  U R                  (       d  UR                  OSUl        U R                   (       a  U R                  (       d  UR                  OSUl        [        X R                  USS9nO[        / 5      nUR                  [        SSU R                  U R                  SUR                  UR                  S95        U$ )z
Build transformation pipeline for the dataset.

Args:
    hyp (dict, optional): Hyperparameters for transformations.

Returns:
    (Compose): Composition of transformation functions.
g        T)stretchxywh)bbox_format	normalizereturn_maskreturn_keypoint	batch_idx
mask_ratiomask_overlap)augmentrectmosaicmixupcutmixr	   imgszr   appendr   use_segmentsuse_keypointsr)   overlap_mask)r   hyp
transformss      r   build_transformsRTDETRDataset.build_transformsK   s     <<'+||DII3CJ%)\\$))		CI'+||DII3CJ&tZZdKJ !J" -- $ 2 2>> --
	
 r    )F)N)
__name__
__module____qualname____firstlineno____doc__r   r   r7   __static_attributes____classcell__)r   s   @r   r   r      s&    . $( 5 5<& r   r   c                  >    \ rS rSrSrSS jr    S	S jrS
S jrSrg)r   k   a  
RTDETRValidator extends the DetectionValidator class to provide validation capabilities specifically tailored for
the RT-DETR (Real-Time DETR) object detection model.

The class allows building of an RTDETR-specific dataset for validation, applies Non-maximum suppression for
post-processing, and updates evaluation metrics accordingly.

Attributes:
    args (Namespace): Configuration arguments for validation.
    data (dict): Dataset configuration dictionary.

Methods:
    build_dataset: Build an RTDETR Dataset for validation.
    postprocess: Apply Non-maximum suppression to prediction outputs.

Examples:
    Initialize and run RT-DETR validation
    >>> from ultralytics.models.rtdetr import RTDETRValidator
    >>> args = dict(model="rtdetr-l.pt", data="coco8.yaml")
    >>> validator = RTDETRValidator(args=args)
    >>> validator()

Notes:
    For further details on the attributes and methods, refer to the parent DetectionValidator class.
Nc                    [        UU R                  R                  USU R                  SU R                  R                  =(       d    S[	        U S35      U R
                  S9	$ )af  
Build an RTDETR Dataset.

Args:
    img_path (str): Path to the folder containing images.
    mode (str, optional): `train` mode or `val` mode, users are able to customize different augmentations for
        each mode.
    batch (int, optional): Size of batches, this is for `rect`.

Returns:
    (RTDETRDataset): Dataset configured for RT-DETR validation.
FNz: )	img_pathr0   
batch_sizer+   r5   r,   cacheprefixr   )r   r   r0   rF   r   r   )r   rD   modebatchs       r   build_datasetRTDETRValidator.build_dataset   sT     ))//		))//)TtfBK(

 
	
r   c                   [        U[        [        45      (       d  US/nUS   R                  u  p#nUS   R	                  SUS-
  4SS9u  pVXPR
                  R                  -  n[        R                  " SUR                  S9/U-  n[        U5       H{  u  p[        R                  " U	5      n	Xh   R                  S5      u  p[        R                  " XS   US   /SS9nXR                  S	S
9   nXU R
                  R                   :     Xx'   M}     U Vs/ s H  oSS2SS24   USS2S4   USS2S4   S.PM!     sn$ s  snf )a;  
Apply Non-maximum suppression to prediction outputs.

Args:
    preds (torch.Tensor | list | tuple): Raw predictions from the model. If tensor, should have shape
        (batch_size, num_predictions, num_classes + 4) where last dimension contains bbox coords and class scores.

Returns:
    (list[dict[str, torch.Tensor]]): List of dictionaries for each image, each containing:
        - 'bboxes': Tensor of shape (N, 4) with bounding box coordinates
        - 'conf': Tensor of shape (N,) with confidence scores
        - 'cls': Tensor of shape (N,) with class indices
Nr      )dim)r      )device).NT)
descending   )bboxesconfcls)
isinstancelisttupleshapesplitr   r0   torchzerosrQ   	enumerater   	xywh2xyxymaxcatargsortrU   )r   predsbs_ndrT   scoresoutputsr   bboxscorerV   predxs                 r   postprocessRTDETRValidator.postprocess   s1     %$//DME!HNN	rqBF<))//!;;vfmm<=B (GA==&Dr*JE99d)$4c)nE2ND67Ddiinn45GJ ) PWWw!QU8Qq!tWQq!tWEwWWWs   &D>c                (   [        US   5      nUR                  nUR                  5       (       a  [        U5      OUnUS   R	                  5       nUSSS/4==   US   S   U R
                  R                  -  -  ss'   USSS/4==   US   S   U R
                  R                  -  -  ss'   [        R                  " U5      nUS	S	2S	S24==   US	S	2SS	24   S-  -  ss'   [        UR                  5       US
   R                  5       US   R                  5       5       Hl  u  pxn	U R                  R                  UUR                  U R                  [        U	5         U V
s/ s H  n
[        U
S5      PM     sn
[        US5      S.5        Mn     g	s  sn
f )a\  
Serialize YOLO predictions to COCO json format.

Args:
    predn (dict[str, torch.Tensor]): Predictions dictionary containing 'bboxes', 'conf', and 'cls' keys
        with bounding box coordinates, confidence scores, and class predictions.
    pbatch (dict[str, Any]): Batch dictionary containing 'imgsz', 'ori_shape', 'ratio_pad', and 'im_file'.
im_filerT   .r      	ori_shape      NrU   rV   rS   )image_id	file_namecategory_idri   rj   )r   stem	isnumericintcloner   r0   r   	xyxy2xywhziptolistjdictr1   name	class_mapround)r   prednpbatchpathrx   ru   boxbscrl   s              r   pred_to_jsonRTDETRValidator.pred_to_json   sd    F9%&yy $ 0 03t9dHo##%C!QKF;/2TYY__DDC!QKF;/2TYY__DDmmC ArrE
c!QR%j1n$
3::<v)=)=)?uATATAVWGA!JJ (!%#'>>#a&#9234!QU1a[!4"1a[ X 5s    Fr9   )valN)rc   z7torch.Tensor | list[torch.Tensor] | tuple[torch.Tensor]returnzlist[dict[str, torch.Tensor]])r   zdict[str, torch.Tensor]r   zdict[str, Any]r   None)	r:   r;   r<   r=   r>   rJ   rm   r   r?   r9   r   r   r   r   k   s*    4
2XLX	&XBr   r   )
__future__r   pathlibr   typingr   r\   ultralytics.datar   ultralytics.data.augmentr   r   r	   ultralytics.models.yolo.detectr
   ultralytics.utilsr   r   __all__r   r   r9   r   r   <module>r      sE    #    ( C C = +
VK Vro( or   