
    |h%                         d dl mZmZmZmZmZ d dlZd dlmZ d dl	m
Z
mZmZ d dlmZ d dlmZmZ dZ G d d	e      Z G d
 de      Zy)    )AnyDictListTupleUnionN)YOLODataset)ComposeFormatv8_transforms)DetectionValidator)colorstrops)RTDETRValidatorc                   <     e Zd ZdZdd fd
Zd fd	ZddZ xZS )	RTDETRDataseta  
    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        |   |d|i| y)aU  
        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       \/var/www/html/test/engine/venv/lib/python3.12/site-packages/ultralytics/models/rtdetr/val.pyr   zRTDETRDataset.__init__'   s     	$4T4V4    c                 &    t         |   ||      S )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   zRTDETRDataset.load_image5   s    " w!A!;;r   c                    | j                   r| j                   r| j                  s|j                  nd|_        | j                   r| j                  s|j                  nd|_        | j                   r| j                  s|j                  nd|_        t        | | j                  |d      }nt        g       }|j                  t        dd| j                  | j                  d|j                  |j                               |S )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_transformszRTDETRDataset.build_transformsH   s     <<'+||DII3CJ%)\\$))		CI'+||DII3CJ&tTZZdKJ !J" -- $ 2 2>> --
	
 r   )F)N)__name__
__module____qualname____doc__r   r   r6   __classcell__)r   s   @r   r   r      s    . $( 5<&r   r   c                   "   e Zd ZdZddZdeej                  eej                     e	ej                     f   dee
eej                  f      fdZdede
eef   de
eef   fd	Zd
e
eej                  f   de
eef   de
eej                  f   fdZy)r   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                     t        || j                  j                  |d| j                  d| j                  j                  xs dt	        | d      | j
                  	      S )a  
        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_pathr/   
batch_sizer*   r4   r+   cacheprefixr   )r   r   r/   r@   r   r   )r   r>   modebatchs       r   build_datasetzRTDETRValidator.build_dataset   sT     ))//		))//)TtfBK(

 
	
r   predsreturnc                    t        |t        t        f      s|dg}|d   j                  \  }}}|d   j	                  d|dz
  fd      \  }}|| j
                  j                  z  }t        j                  d|j                        g|z  }t        |      D ]  \  }}	t        j                  |	      }	||   j                  d      \  }
}t        j                  |	|
d   |d   gd      }||
j                  d	
         }||
| j
                  j                   kD     ||<    |D cg c]  }|ddddf   |dddf   |dddf   d! c}S c c}w )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   r/   torchzerosrL   	enumerater   	xywh2xyxymaxcatargsortrQ   )r   rE   bs_ndrP   scoresoutputsr   bboxscorerR   predxs                 r   postprocesszRTDETRValidator.postprocess   sE     %$/DME!HNN	ArqBF<$))//!;;vfmm<=B ( 	6GAt==&Dr*JE399dE)$4c)nE2ND67Dediinn45GAJ	6 PWW!1QU8Qq!tWQq!tWEWWWs   !$EsirC   c                 4   |d   |k(  }|d   |   j                  d      }|d   |   }|d   |   }|d   j                  dd }|d	   |   }t        |      r=t        j                  |      }|d
ddgfxx   |d   z  cc<   |d
ddgfxx   |d   z  cc<   |||||dS )a  
        Prepare a batch for validation by applying necessary transformations.

        Args:
            si (int): Batch index.
            batch (Dict[str, Any]): Batch data containing images and annotations.

        Returns:
            (Dict[str, Any]): Prepared batch with transformed annotations containing cls, bboxes,
                ori_shape, imgsz, and ratio_pad.
        r'   rR   rI   rP   	ori_shapeimg   N	ratio_pad.r         )rR   rP   rk   r/   rn   )squeezerV   lenr   r[   )	r   ri   rC   idxrR   rd   rk   r/   rn   s	            r   _prepare_batchzRTDETRValidator._prepare_batch   s     K B&El3''+Xs#+&r*	e""12&+&r*	s8==&Dq!f1-q!f1-dUajkkr   rf   pbatchc                 2   |d   }| j                   j                  r|dz  }|d   j                         }|dddgfxx   |d   d   | j                   j                  z  z  cc<   |dddgfxx   |d   d   | j                   j                  z  z  cc<   ||d	   |d
S )a  
        Prepare predictions by scaling bounding boxes to original image dimensions.

        Args:
            pred (Dict[str, torch.Tensor]): Raw predictions containing 'cls', 'bboxes', and 'conf'.
            pbatch (Dict[str, torch.Tensor]): Prepared batch information containing 'ori_shape' and other metadata.

        Returns:
            (Dict[str, torch.Tensor]): Predictions scaled to original image dimensions.
        rR   r   rP   .rm   rk   ro   rp   rQ   rO   )r   
single_clscloner/   )r   rf   ru   rR   rP   s        r   _prepare_predzRTDETRValidator._prepare_pred   s     5k991HCh%%'sQF{vk215		GGsQF{vk215		GG $v,sCCr   )valN)r7   r8   r9   r:   rD   r   rX   Tensorr   r   r   strrh   intr   rt   ry    r   r   r   r   h   s    4
2X5<<ell);U5<<=PPQX	d3$%	&XBl lT#s(^ lS#X l0D$sELL'8"9 D4S> DVZ[^`e`l`l[lVm Dr   r   )typingr   r   r   r   r   rX   ultralytics.datar   ultralytics.data.augmentr	   r
   r   ultralytics.models.yolo.detectr   ultralytics.utilsr   r   __all__r   r   r~   r   r   <module>r      sD    1 0  ( C C = +
VK Vr~D( ~Dr   