
    /j*                        d dl mZ d dlZd dlZd dlmZ d dlmZ d dlZd dl	Z	d dl
mZ d dlmZmZ d dlmZ d dlm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 dlmZm Z   G d de          Z!dS )    )annotationsN)copy)Any)build_dataloaderbuild_yolo_dataset)BaseTrainer)yolo)DetectionModel)DEFAULT_CFGLOGGERRANK)override_configs)plot_imagesplot_labels)torch_distributed_zero_firstunwrap_modelc                       e Zd ZdZeddfd- fdZd.d/dZd0d1dZd2dZd Z	d3d4d Z
d! Zd5d6d%Zd& Zd7d*Zd+ Z fd,Z xZS )8DetectionTrainera  A class extending the BaseTrainer class for training based on a detection model.

    This trainer specializes in object detection tasks, handling the specific requirements for training YOLO models for
    object detection including dataset building, data loading, preprocessing, and model configuration.

    Attributes:
        model (DetectionModel): The YOLO detection model being trained.
        data (dict): Dictionary containing dataset information including class names and number of classes.
        loss_names (tuple): Names of the loss components used in training (box_loss, cls_loss, dfl_loss).

    Methods:
        build_dataset: Build YOLO dataset for training or validation.
        get_dataloader: Construct and return dataloader for the specified mode.
        preprocess_batch: Preprocess a batch of images by scaling and converting to float.
        set_model_attributes: Set model attributes based on dataset information.
        get_model: Return a YOLO detection model.
        get_validator: Return a validator for model evaluation.
        label_loss_items: Return a loss dictionary with labeled training loss items.
        progress_string: Return a formatted string of training progress.
        plot_training_samples: Plot training samples with their annotations.
        plot_training_labels: Create a labeled training plot of the YOLO model.
        auto_batch: Calculate optimal batch size based on model memory requirements.

    Examples:
        >>> from ultralytics.models.yolo.detect import DetectionTrainer
        >>> args = dict(model="yolo26n.pt", data="coco8.yaml", epochs=3)
        >>> trainer = DetectionTrainer(overrides=args)
        >>> trainer.train()
    N	overridesdict[str, Any] | None
_callbacksdict | Nonec                N    t                                          |||           dS )a  Initialize a DetectionTrainer object for training YOLO object detection models.

        Args:
            cfg (dict, optional): Default configuration dictionary containing training parameters.
            overrides (dict, optional): Dictionary of parameter overrides for the default configuration.
            _callbacks (dict, optional): Dictionary of callback functions to be executed during training.
        N)super__init__)selfcfgr   r   	__class__s       i/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/models/yolo/detect/train.pyr   zDetectionTrainer.__init__7   s'     	i44444    trainimg_pathstrmodebatch
int | Nonec           	         t          t          t          | j                  j                                                   d          }t          | j        ||| j        ||dk    |          S )a  Build YOLO Dataset for training or validation.

        Args:
            img_path (str): Path to the folder containing images.
            mode (str): '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' mode.

        Returns:
            (Dataset): YOLO dataset object configured for the specified mode.
            val)r$   rectstride)maxintr   modelr+   r   argsdata)r   r"   r$   r%   gss        r   build_datasetzDetectionTrainer.build_datasetA   s]     \$*--488::;;R@@!$)XudidY]afYfoqrrrrr       r   dataset_path
batch_sizer-   rankc           	        |dv sJ d| d            t          |          5  |                     |||          }ddd           n# 1 swxY w Y   |dk    }t          |dd          r@|r>t          j        |j        |j        d         k              st          j        d	           d}t          |||dk    r| j	        j
        n| j	        j
        d
z  ||| j	        j        o|dk              S )a  Construct and return dataloader for the specified mode.

        Args:
            dataset_path (str): Path to the dataset.
            batch_size (int): Number of images per batch.
            rank (int): Process rank for distributed training.
            mode (str): 'train' for training dataloader, 'val' for validation dataloader.

        Returns:
            (DataLoader): PyTorch dataloader object.
        >   r)   r!   z#Mode must be 'train' or 'val', not .Nr!   r*   Fr   zJ'rect=True' is incompatible with DataLoader shuffle, setting shuffle=False   )r%   workersshuffler6   	drop_last)r   r2   getattrnpallbatch_shapesr   warningr   r/   r:   compile)r   r4   r5   r6   r$   datasetr;   s          r   get_dataloaderzDetectionTrainer.get_dataloaderO   sf    '''')Vt)V)V)V''')$// 	I 	I((tZHHG	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I 	I'/7FE** 	w 	rvgFZ^e^rst^uFu?v?v 	NghhhG)-DI%%di>ORS>Si';DGO
 
 
 	
s   AA
A
dictreturnc                    |                                 D ]L\  }}t          |t          j                  r-|                     j         j        j        dk              ||<   M|d                                         dz  |d<    j        j	        dk    r|d         }t          j        t           j        j        d j        j	        z
  z            t           j        j        d j        j	        z   z   j        z                        j        z   j        z  }|t          |j        dd                   z  d	k    r? fd
|j        dd         D             }t"          j                            ||dd          }||d<   |S )zPreprocess a batch of images by scaling and converting to float.

        Args:
            batch (dict): Dictionary containing batch data with 'img' tensor.

        Returns:
            (dict): Preprocessed batch with normalized images.
        cuda)non_blockingimg   g        g      ?r9   N   c                ^    g | ])}t          j        |z  j        z            j        z  *S  )mathceilr+   ).0xr   sfs     r   
<listcomp>z5DetectionTrainer.preprocess_batch.<locals>.<listcomp>   sB       FGDIa"ft{233dkA  r    bilinearF)sizer$   align_corners)items
isinstancetorchTensortodevicetypefloatr/   multi_scalerandom	randranger-   imgszr+   r,   shapenn
functionalinterpolate)r   r%   kvimgssznsrS   s   `      @r   preprocess_batchz!DetectionTrainer.preprocess_batchk   s    KKMM 	V 	VDAq!U\** V44$+:Jf:T4UUaU|))++c1e9 3&&<D 	31F+FGHH	31F+FG$+UVV  ;	
 +  c$*QRR.)))BQww    KO:VWVXVX>   }00BZ_d0eeE%Lr    c                    | j         d         | j        _        | j         d         | j        _        | j        | j        _        t          | j        d          r'| j                            | j        j                   dS dS )z2Set model attributes based on dataset information.ncnamesend2end)max_detN)r0   r.   ro   rp   r/   r=   set_head_attrrr   r   s    r   set_model_attributesz%DetectionTrainer.set_model_attributes   st     	$
9W-
)
4:y)) 	@J$$TY->$?????	@ 	@r    Tr   
str | Noneweightsverboseboolc                    t          || j        d         | j        d         |o
t          dk              }|r|                    |           |S )a=  Return a YOLO detection model.

        Args:
            cfg (str, optional): Path to model configuration file.
            weights (str, optional): Path to model weights.
            verbose (bool): Whether to display model information.

        Returns:
            (DetectionModel): YOLO detection model.
        ro   channels)ro   chrx   )r
   r0   r   load)r   r   rw   rx   r.   s        r   	get_modelzDetectionTrainer.get_model   sS     sty49Z;PZaZpfjnpfpqqq 	 JJwr    c                    d| _         t          j                            | j        | j        t          | j                  | j                  S )z6Return a DetectionValidator for YOLO model validation.)box_losscls_lossdfl_loss)save_dirr/   r   )	
loss_namesr	   detectDetectionValidatortest_loaderr   r   r/   	callbacksrt   s    r   get_validatorzDetectionTrainer.get_validator   sB    <{--t}4	??W[We . 
 
 	
r    
loss_itemslist[float] | Noneprefixc                    fd| j         D             }|)d |D             }t          t          ||                    S |S )a_  Return a loss dict with labeled training loss items tensor.

        Args:
            loss_items (list[float], optional): List of loss values.
            prefix (str): Prefix for keys in the returned dictionary.

        Returns:
            (dict | list): Dictionary of labeled loss items if loss_items is provided, otherwise list of keys.
        c                    g | ]	} d | 
S )/rN   )rQ   rR   r   s     r   rT   z5DetectionTrainer.label_loss_items.<locals>.<listcomp>   s#    999A6A999r    Nc                H    g | ]}t          t          |          d            S )   )roundr_   )rQ   rR   s     r   rT   z5DetectionTrainer.label_loss_items.<locals>.<listcomp>   s(    AAA%a!,,AAAr    )r   rE   zip)r   r   r   keyss     ` r   label_loss_itemsz!DetectionTrainer.label_loss_items   sV     :999999!AAjAAAJD*--...Kr    c                ^    dddt          | j                  z   z  z   ddg| j        ddR z  S )z`Return a formatted string of training progress with epoch, GPU memory, loss, instances and size.
z%11s   EpochGPU_mem	InstancesSize)lenr   rt   s    r   progress_stringz DetectionTrainer.progress_string   sX    vS%9%9!9::?
 _?
 	?

 ?
 ?
 
 	
r    dict[str, Any]niNonec                Z    t          ||d         | j        d| dz  | j                   dS )zPlot training samples with their annotations.

        Args:
            batch (dict[str, Any]): Dictionary containing batch data.
            ni (int): Batch index used for naming the output file.
        im_filetrain_batchz.jpg)labelspathsfnameon_plotN)r   r   r   )r   r%   r   s      r   plot_training_samplesz&DetectionTrainer.plot_training_samples   sK     		"-"8"8"8"88L		
 	
 	
 	
 	
 	
r    c                4   t          j        d | j        j        j        D             d          }t          j        d | j        j        j        D             d          }t          ||                                | j        d         | j        | j	                   dS )z1Create a labeled training plot of the YOLO model.c                    g | ]
}|d          S )bboxesrN   rQ   lbs     r   rT   z9DetectionTrainer.plot_training_labels.<locals>.<listcomp>   s    XXX8XXXr    r   c                    g | ]
}|d          S )clsrN   r   s     r   rT   z9DetectionTrainer.plot_training_labels.<locals>.<listcomp>   s    SSSBbiSSSr    rp   )rp   r   r   N)
r>   concatenatetrain_loaderrC   r   r   squeezer0   r   r   )r   boxesr   s      r   plot_training_labelsz%DetectionTrainer.plot_training_labels   s    XXt7H7P7WXXXZ[\\nSS$2C2K2RSSSUVWWE3;;==	'0BT]dhdpqqqqqqr    c                :   t          | j        ddi          5 | _        |                     | j        d         dd          }ddd           n# 1 swxY w Y   t	          d |j        D                       d	z  }~t                                          |          S )
zGet optimal batch size by calculating memory occupation of model.

        Returns:
            (int): Optimal batch size.
        cacheF)r   r!   r3   )r$   r%   Nc              3  @   K   | ]}t          |d                    V  dS )r   N)r   )rQ   labels     r   	<genexpr>z.DetectionTrainer.auto_batch.<locals>.<genexpr>   s.      NN#eEl++NNNNNNr    r   )r   r/   r2   r0   r,   r   r   
auto_batch)r   train_datasetmax_num_objr   s      r   r   zDetectionTrainer.auto_batch   s     diGU3CDDD 	[	 ..ty/AWY.ZZM	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[ 	[NN9MNNNNNQRRww!!+...s   )AAA)r   r   r   r   )r!   N)r"   r#   r$   r#   r%   r&   )r3   r   r!   )r4   r#   r5   r-   r6   r-   r$   r#   )r%   rE   rF   rE   )NNT)r   rv   rw   rv   rx   ry   )Nr!   )r   r   r   r#   )r%   r   r   r-   rF   r   )__name__
__module____qualname____doc__r   r   r2   rD   rm   ru   r   r   r   r   r   r   r   __classcell__)r   s   @r   r   r      sC        < '4ko 5 5 5 5 5 5 5s s s s s
 
 
 
 
8   @
@ 
@ 
@     
 
 
    "
 
 

 
 
 
r r r
/ 
/ 
/ 
/ 
/ 
/ 
/ 
/ 
/r    r   )"
__future__r   rO   ra   r   typingr   numpyr>   rZ   torch.nnre   ultralytics.datar   r   ultralytics.engine.trainerr   ultralytics.modelsr	   ultralytics.nn.tasksr
   ultralytics.utilsr   r   r   ultralytics.utils.patchesr   ultralytics.utils.plottingr   r   ultralytics.utils.torch_utilsr   r   r   rN   r    r   <module>r      sb   # " " " " "                          A A A A A A A A 2 2 2 2 2 2 # # # # # # / / / / / / 7 7 7 7 7 7 7 7 7 7 6 6 6 6 6 6 ? ? ? ? ? ? ? ? T T T T T T T TP/ P/ P/ P/ P/{ P/ P/ P/ P/ P/r    