
    h                    j    S SK Jr  S SKJr  S SKJr  S SKJr  S SKJrJ	r	  SSK
JrJr   " S S	\5      rg
)    )annotations)copy)DetectionTrainer)RTDETRDetectionModel)RANKcolorstr   )RTDETRDatasetRTDETRValidatorc                  :    \ rS rSrSrSS	S jjrS
SS jjrS rSrg)RTDETRTrainer   a[  
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 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.

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.

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()
Nc                    [        XR                  S   U R                  S   U=(       a	    [        S:H  S9nU(       a  UR                  U5        U$ )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   models        Y/home/james-whalen/.local/lib/python3.13/site-packages/ultralytics/models/rtdetr/train.py	get_modelRTDETRTrainer.get_model-   sD     %SYYt_:AV`g`vlptvlvwJJw    c                p   [        UU R                  R                  UUS:H  U R                  SU R                  R                  =(       d    SU R                  R                  =(       d    S[        U S35      U R                  R                  U R                  US:X  a  U R                  R                  S9$ SS9$ )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batchs       r   build_datasetRTDETRTrainer.build_dataset>   s     ))//GO		))//)Tyy++4utfBK(II%%+/7?TYY''
 	
 AD
 	
r   c                t    SU l         [        U R                  U R                  [	        U R
                  5      S9$ )zBReturn a DetectionValidator suitable for RT-DETR model validation.)	giou_losscls_lossl1_loss)save_dirr,   )
loss_namesr   test_loaderr5   r   r,   )r   s    r   get_validatorRTDETRTrainer.get_validatorY   s-    <t//$--dSWS\S\o^^r   )r6   )NNT)r   zdict | Noner   z
str | Noner   bool)valN)r!   strr-   r<   r.   z
int | None)	__name__
__module____qualname____firstlineno____doc__r   r/   r8   __static_attributes__ r   r   r   r      s    <"
6_r   r   N)
__future__r   r   ultralytics.models.yolo.detectr   ultralytics.nn.tasksr   ultralytics.utilsr   r   r;   r
   r   r   rC   r   r   <module>rH      s*    #  ; 5 , /N_$ N_r   