o
    VhC                     @   sX   d dl m Z  d dlmZ d dlmZ d dlmZmZ ddlm	Z	m
Z
 G dd deZd	S )
    )copy)DetectionTrainer)RTDETRDetectionModel)RANKcolorstr   )RTDETRDatasetRTDETRValidatorc                   @   s,   e Zd ZdZdddZdddZd	d
 ZdS )RTDETRTrainera  
    Trainer class for the RT-DETR model developed by Baidu for real-time object detection.

    This class extends the DetectionTrainer class for YOLO to adapt to the specific features and architecture of RT-DETR.
    The model leverages Vision Transformers and has capabilities like IoU-aware query selection and adaptable inference
    speed.

    Attributes:
        loss_names (Tuple[str]): Names of the loss components used for training.
        data (dict): Dataset configuration containing class count and other parameters.
        args (dict): Training arguments and hyperparameters.
        save_dir (Path): Directory to save training results.
        test_loader (DataLoader): DataLoader for validation/testing data.

    Notes:
        - F.grid_sample used in RT-DETR does not support the `deterministic=True` argument.
        - AMP training can lead to NaN outputs and may produce errors during bipartite graph matching.

    Examples:
        >>> from ultralytics.models.rtdetr.train import RTDETRTrainer
        >>> args = dict(model="rtdetr-l.yaml", data="coco8.yaml", imgsz=640, epochs=3)
        >>> trainer = RTDETRTrainer(overrides=args)
        >>> trainer.train()
    NTc                 C   s6   t || jd | jd |otdkd}|r|| |S )a`  
        Initialize and return an RT-DETR model for object detection tasks.

        Args:
            cfg (dict, optional): Model configuration.
            weights (str, optional): Path to pre-trained model weights.
            verbose (bool): Verbose logging if True.

        Returns:
            (RTDETRDetectionModel): Initialized model.
        ncchannels)r   chverbose)r   datar   load)selfcfgweightsr   model r   S/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/models/rtdetr/train.py	get_model&   s   $
zRTDETRTrainer.get_modelvalc                 C   s^   t || jj||dk| jd| jjpd| jjpdt| d| jj| j|dkr+| jjdS ddS )a|  
        Build and return an RT-DETR dataset for training or validation.

        Args:
            img_path (str): Path to the folder containing images.
            mode (str): Dataset mode, either 'train' or 'val'.
            batch (int, optional): Batch size for rectangle training.

        Returns:
            (RTDETRDataset): Dataset object for the specific mode.
        trainFNz: g      ?)img_pathimgsz
batch_sizeaugmenthyprectcache
single_clsprefixclassesr   fraction)	r   argsr   r!   r"   r   r$   r   r%   )r   r   modebatchr   r   r   build_dataset7   s    

zRTDETRTrainer.build_datasetc                 C   s   d| _ t| j| jt| jdS )zCReturns a DetectionValidator suitable for RT-DETR model validation.)	giou_losscls_lossl1_loss)save_dirr&   )
loss_namesr	   test_loaderr-   r   r&   )r   r   r   r   get_validatorR   s   zRTDETRTrainer.get_validator)NNT)r   N)__name__
__module____qualname____doc__r   r)   r0   r   r   r   r   r
      s
    

r
   N)r   ultralytics.models.yolo.detectr   ultralytics.nn.tasksr   ultralytics.utilsr   r   r   r   r	   r
   r   r   r   r   <module>   s   