
    h"                         d dl mZ 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)    )Path)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/eduruby.in/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_image6   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_transformsI   s     <<'+||DII3CJ%)\\$))		CI'+||DII3CJ&tTZZdKJ !J" -- $ 2 2>> --
	
 r   )F)N)__name__
__module____qualname____doc__r   r    r7   __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
eej                  f   de
eef   dd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_pathr0   
batch_sizer+   r5   r,   cacheprefixr   )r   r   r0   rA   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   r0   torchzerosrM   	enumerater   	xywh2xyxymaxcatargsortrQ   )r   rF   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prednpbatchc                    t        |d         j                  }|j                         rt        |      n|}|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<   t        j                  |      }|d	d	d	dfxx   |d	d	dd	f   dz  z  cc<   t        |j                         |d
   j                         |d   j                               D ]^  \  }}}| j                  j                  || j                  t        |         |D 	cg c]  }	t        |	d       c}	t        |d      d       ` y	c c}	w )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_filerP   .r      	ori_shape      NrQ   rR   rO   )image_idcategory_idrd   re   )r   stem	isnumericintcloner   r0   r   	xyxy2xywhziptolistjdictr1   	class_mapround)
r   ri   rj   rs   rq   boxbscrg   s
             r   pred_to_jsonzRTDETRValidator.pred_to_json   sY    F9%&++ $ 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W 	GAq!JJ (#'>>#a&#9234QU1a[4"1a[		
 5s   E8)valN)r8   r9   r:   r;   rE   r   rX   Tensorr   r   r   strrh   r   r    r   r   r   r   i   s    4
2X5<<ell);U5<<=PPQX	d3$%	&XB$sELL'8"9 4S> VZ r   r   )pathlibr   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      sE     0 0  ( C C = +
VK Vrm( mr   