§
    l¬¸jU  ã                  ó~   — d dl mZ d dlmZ 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
e¦  «        ZdS )é    )Úannotations)Úcopy)Úget_split_fraction)ÚDetectionTrainer)ÚRTDETRDetectionModel)ÚRANKÚcolorstré   )ÚRTDETRDatasetÚRTDETRValidatorc                  ó,   — e Zd ZdZddd
„Zddd„Z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): Names of the loss components, derived from the loss dict returned by the criterion.
        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.

    Methods:
        get_model: Initialize and return an RT-DETR model for object detection tasks.
        build_dataset: Build and return an RT-DETR dataset for training or validation.
        get_validator: Return a DetectionValidator suitable for RT-DETR model validation.

    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()

    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.
    NTÚcfgúdict | NoneÚweightsú
str | NoneÚverboseÚboolc           	     óÂ   — |                       t          || j        d         | j        d         |o
t          dk    ¬¦  «        ¦  «        }|r|                     |¦  «         |S )aW  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   Úchr   )Úset_model_names_for_loadr   Údatar   Úload)Úselfr   r   r   Úmodels        úo/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/models/rtdetr/train.pyÚ	get_modelzRTDETRTrainer.get_model-   sh   € ð ×-Ò-Ý  ¨¬°4¬¸T¼YÀzÔ=RÐ\cÐ\rÕhlÐprÒhrÐsÑsÔsñ
ô 
ˆð ð 	 Ø�JŠJ�wÑÔÐØˆó    ÚvalÚimg_pathÚstrÚmodeÚbatchú
int | Nonec                óX  — t          || j        j        ||dk    | j        d| j        j        pd| j        j        pdt          |› d�¦  «        | j        j        | j        | j                             d¦  «        rdn+t          | j        j
        |dk    rdn| j        j        ¦  «        ¬¦  «        S )as  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: Úcompleteg      ð?)r#   ÚimgszÚ
batch_sizeÚaugmentÚhypÚrectÚcacheÚ
single_clsÚprefixÚclassesr   Úfraction)r   Úargsr+   r0   r1   r	   r3   r   Úgetr   r4   Úsplit)r   r#   r%   r&   s       r   Úbuild_datasetzRTDETRTrainer.build_dataset?   s®   € õ ØØ”)”/ØØ˜G’OØ”	ØØ”)”/Ð) TØ”yÔ+Ð4¨uÝ˜t˜K˜K˜KÑ(Ô(Ø”IÔ%Ø”àŒy�}Š}˜ZÑ(Ô(ði�S�Så# D¤IÔ$6À4È7Â?À?¸¸ÐX\ÔXaÔXgÑhÔhð
ñ 
ô 
ð 	
r!   c                ó^   — t          | j        | j        t          | j        ¦  «        ¬¦  «        S )z@Return an RTDETRValidator suitable for RT-DETR model validation.)Úsave_dirr5   )r   Útest_loaderr:   r   r5   )r   s    r   Úget_validatorzRTDETRTrainer.get_validator[   s&   € å˜tÔ/¸$¼-ÍdÐSWÔS\ÉoÌoÐ^Ñ^Ô^Ð^r!   )NNT)r   r   r   r   r   r   )r"   N)r#   r$   r%   r$   r&   r'   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r8   r<   © r!   r   r   r      sd   € € € € € ðð ð:ð ð ð ð ð$
ð 
ð 
ð 
ð 
ð8_ð _ð _ð _ð _r!   r   N)Ú
__future__r   r   Úultralytics.data.utilsr   Úultralytics.models.yolo.detectr   Úultralytics.nn.tasksr   Úultralytics.utilsr   r	   r"   r   r   r   rA   r!   r   ú<module>rG      sÚ   ðð #Ð "Ð "Ð "Ð "Ð "à Ð Ð Ð Ð Ð à 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø ;Ð ;Ð ;Ð ;Ð ;Ð ;Ø 5Ð 5Ð 5Ð 5Ð 5Ð 5Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,à /Ð /Ð /Ð /Ð /Ð /Ð /Ð /ðN_ð N_ð N_ð N_ð N_Ð$ñ N_ô N_ð N_ð N_ð N_r!   