a
    yfe                     @   s`   d dl m Z  d dl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 )
    )copyN)DetectionTrainer)RTDETRDetectionModel)RANKcolorstr   )RTDETRDatasetRTDETRValidatorc                       s<   e Zd ZdZdddZdddZd	d
 Z fddZ  ZS )RTDETRTrainera(  
    Trainer class for the RT-DETR model developed by Baidu for real-time object detection. Extends the DetectionTrainer
    class for YOLO to adapt to the specific features and architecture of RT-DETR. This model leverages Vision
    Transformers and has capabilities like IoU-aware query selection and adaptable inference speed.

    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.

    Example:
        ```python
        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   s.   t || 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. Defaults to None.
            weights (str, optional): Path to pre-trained model weights. Defaults to None.
            verbose (bool): Verbose logging if True. Defaults to True.

        Returns:
            (RTDETRDetectionModel): Initialized model.
        nc)r   verbose)r   datar   load)selfcfgweightsr   model r   [/var/www/html/django/DPS/env/lib/python3.9/site-packages/ultralytics/models/rtdetr/train.py	get_model"   s    
zRTDETRTrainer.get_modelvalc                 C   s8   t || jj||dk| jd| jjp"dt| d| j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. Defaults to None.

        Returns:
            (RTDETRDataset): Dataset object for the specific mode.
        trainFNz: )	img_pathimgszZ
batch_sizeZaugmentZhyprectcacheprefixr   )r   argsr   r   r   r   )r   r   modebatchr   r   r   build_dataset3   s    
zRTDETRTrainer.build_datasetc                 C   s   d| _ t| j| jt| jdS )z
        Returns a DetectionValidator suitable for RT-DETR model validation.

        Returns:
            (RTDETRValidator): Validator object for model validation.
        )Z	giou_lossZcls_lossZl1_loss)save_dirr   )Z
loss_namesr	   Ztest_loaderr"   r   r   )r   r   r   r   get_validatorK   s    zRTDETRTrainer.get_validatorc                    s~   t  |}t|d }|d }g g  }}t|D ]F}||d ||k |j ||d ||k j|jtjd q2|S )z
        Preprocess a batch of images. Scales and converts the images to float format.

        Args:
            batch (dict): Dictionary containing a batch of images, bboxes, and labels.

        Returns:
            (dict): Preprocessed batch.
        Zimg	batch_idxZbboxescls)deviceZdtype)	superpreprocess_batchlenrangeappendtor&   torchlong)r   r    bsr$   Zgt_bboxZgt_classi	__class__r   r   r(   U   s    

&zRTDETRTrainer.preprocess_batch)NNT)r   N)	__name__
__module____qualname____doc__r   r!   r#   r(   __classcell__r   r   r1   r   r
      s
   


r
   )r   r-   Zultralytics.models.yolo.detectr   Zultralytics.nn.tasksr   Zultralytics.utilsr   r   r   r   r	   r
   r   r   r   r   <module>   s   