
    /j%                        d dl mZ d dlmZ d dlmZ d dl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  G d de
          ZdS )    )annotations)copy)AnyN)ClassificationDatasetbuild_dataloader)BaseTrainer)yolo)ClassificationModel)DEFAULT_CFGLOGGERRANK)plot_images)is_paralleltorch_distributed_zero_firstc                       e Zd ZdZeddfd& fdZd Zd'd(dZ fdZd)d*dZ	d+d,dZ
d-dZd.dZd Zd/d0d#Zd1d%Z xZS )2ClassificationTrainera  A trainer class extending BaseTrainer for training image classification models.

    This trainer handles the training process for image classification tasks, supporting both YOLO classification models
    and torchvision models with comprehensive dataset handling and validation.

    Attributes:
        model (ClassificationModel): The classification model to be trained.
        data (dict[str, Any]): Dictionary containing dataset information including class names and number of classes.
        loss_names (list[str]): Names of the loss functions used during training.
        validator (ClassificationValidator): Validator instance for model evaluation.

    Methods:
        set_model_attributes: Set the model's class names from the loaded dataset.
        get_model: Return a modified PyTorch model configured for training.
        setup_model: Load, create or download model for classification.
        build_dataset: Create a ClassificationDataset instance.
        get_dataloader: Return PyTorch DataLoader with transforms for image preprocessing.
        preprocess_batch: Preprocess a batch of images and classes.
        progress_string: Return a formatted string showing training progress.
        get_validator: Return an instance of ClassificationValidator.
        label_loss_items: Return a loss dict with labeled training loss items.
        final_eval: Evaluate trained model and save validation results.
        plot_training_samples: Plot training samples with their annotations.

    Examples:
        Initialize and train a classification model
        >>> from ultralytics.models.yolo.classify import ClassificationTrainer
        >>> args = dict(model="yolo26n-cls.pt", data="imagenet10", epochs=3)
        >>> trainer = ClassificationTrainer(overrides=args)
        >>> trainer.train()
    N	overridesdict[str, Any] | None
_callbacksdict | Nonec                    |i }d|d<   |                     d          d|d<   t                                          |||           dS )a  Initialize a ClassificationTrainer object.

        Args:
            cfg (dict[str, Any], optional): Default configuration dictionary containing training parameters.
            overrides (dict[str, Any], optional): Dictionary of parameter overrides for the default configuration.
            _callbacks (dict, optional): Dictionary of callback functions to be executed during training.
        Nclassifytaskimgsz   )getsuper__init__)selfcfgr   r   	__class__s       k/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/models/yolo/classify/train.pyr   zClassificationTrainer.__init__4   sX     I&	&==!!)!$Igi44444    c                4    | j         d         | j        _        dS )z9Set the YOLO model's class names from the loaded dataset.namesN)datamodelr%   r   s    r"   set_model_attributesz*ClassificationTrainer.set_model_attributesC   s    9W-
r#   Tverboseboolc                   t          || j        d         | j        d         |o
t          dk              }|r|                    |           |                                D ]n}| j        j        s$t          |d          r|                                 t          |t          j        j                  r| j        j        r| j        j        |_        o|                                D ]	}d|_        
|S )a  Return a modified PyTorch model configured for training YOLO classification.

        Args:
            cfg (Any, optional): Model configuration.
            weights (Any, optional): Pre-trained model weights.
            verbose (bool, optional): Whether to display model information.

        Returns:
            (ClassificationModel): Configured PyTorch model for classification.
        ncchannels)r-   chr*   reset_parametersT)r
   r&   r   loadmodulesargs
pretrainedhasattrr1   
isinstancetorchnnDropoutdropoutp
parametersrequires_grad)r   r    weightsr*   r'   mr<   s          r"   	get_modelzClassificationTrainer.get_modelG   s     $CDIdO	*@U_f_ukosukuvvv 	 JJw 	( 	(A9' %GA7I,J,J %""$$$!UX-.. (493D (i'!!## 	# 	#A"AOOr#   c                H   ddl }t          | j                  |j        j        v r7 |j        j        | j                 | j        j        rdnd          | _        d}n t                                                      }t          j
        | j        | j        d                    |S )zLoad, create or download model for classification tasks.

        Returns:
            (Any): Model checkpoint if applicable, otherwise None.
        r   NIMAGENET1K_V1)r?   r-   )torchvisionstrr'   models__dict__r4   r5   r   setup_modelr
   reshape_outputsr&   )r   rD   ckptr!   s      r"   rH   z!ClassificationTrainer.setup_model_   s     	tz??k0999@+4TZ@+/9+?IT  DJ DD77&&((D+DJ	$HHHr#   trainimg_pathrE   modec                :    t          || j        |dk    |          S )a  Create a ClassificationDataset instance given an image path and mode.

        Args:
            img_path (str): Path to the dataset images.
            mode (str, optional): Dataset mode ('train', 'val', or 'test').
            batch (Any, optional): Batch information (unused in this implementation).

        Returns:
            (ClassificationDataset): Dataset for the specified mode.
        rK   )rootr4   augmentprefix)r   r4   )r   rL   rM   batchs       r"   build_datasetz#ClassificationTrainer.build_datasetq   s#     %(DT[Odhiiiir#      r   dataset_path
batch_sizeintrankc                   t          |          5  |                     ||          }ddd           n# 1 swxY w Y   | j                            dd          t	          |j        j                  }r|k    ry|j        j        d         }t	          |j                  }fd|j        D             |_        |t	          |j                  z
  }	t          j	        | d| d d|	 d| 	           t          ||| j        j        || j        j        	          }
|d
k    rFt          | j                  r|
j        j        | j        j        _        n|
j        j        | j        _        |
S )a  Return PyTorch DataLoader with transforms to preprocess images.

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

        Returns:
            (torch.utils.data.DataLoader): DataLoader for the specified dataset and mode.
        Nr-   r   c                ,    g | ]}|d          k     |S )    ).0sr-   s     r"   
<listcomp>z8ClassificationTrainer.get_dataloader.<locals>.<listcomp>   s"    GGGQQqTBYYqYYYr#   z split has z classes but model expects z. Skipping z samples from extra classes: )rX   	drop_lastrK   )r   rS   r&   r   lenbaseclassessamplesr   warningr   r4   workerscompiler   r'   datasettorch_transformsmodule
transforms)r   rU   rV   rX   rM   rh   
dataset_ncextra_classesoriginal_countskippedloaderr-   s              @r"   get_dataloaderz$ClassificationTrainer.get_dataloader~   s    *$// 	= 	=((t<<G	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= Y]]4##-..
 	*r//#L05M 11NGGGG'/GGGGO$s7?';';;GN R RJ R R2 R R#R RBOR R  
 "':ty7Ht_c_h_pqqq7??4:&& H/5~/N
!,,(.(G
%s   488rR   dict[str, torch.Tensor]returnc                    |d                              | j        | j        j        dk              |d<   |d                              | j        | j        j        dk              |d<   |S )z)Preprocess a batch of images and classes.imgcuda)non_blockingcls)todevicetype)r   rR   s     r"   preprocess_batchz&ClassificationTrainer.preprocess_batch   s[    U|t{AQU[A[\\eU|t{AQU[A[\\er#   c                ^    dddt          | j                  z   z  z   ddg| j        ddR z  S )z4Return a formatted string showing training progress.
z%11s   EpochGPU_mem	InstancesSize)ra   
loss_namesr(   s    r"   progress_stringz%ClassificationTrainer.progress_string   sX    vS%9%9!9::?
 _?
 	?

 ?
 ?
 
 	
r#   c                    dg| _         t          j                            | j        | j        t          | j                  | j                  S )z=Return an instance of ClassificationValidator for validation.loss)r4   r   )	r   r	   r   ClassificationValidatortest_loadersave_dirr   r4   	callbacksr(   s    r"   get_validatorz#ClassificationTrainer.get_validator   sB    !(}44dm$ty//dn 5 
 
 	
r#   
loss_itemstorch.Tensor | NonerQ   c                    fd| j         D             }||S t          t          |          d          g}t          t	          ||                    S )a]  Return a loss dict with labeled training loss items tensor.

        Args:
            loss_items (torch.Tensor, optional): Loss tensor items.
            prefix (str, optional): Prefix to prepend to loss names.

        Returns:
            (dict | list): Dictionary of labeled loss items if loss_items is provided, otherwise list of keys.
        c                    g | ]	} d | 
S )/r\   )r]   xrQ   s     r"   r_   z:ClassificationTrainer.label_loss_items.<locals>.<listcomp>   s#    999A6A999r#   N   )r   roundfloatdictzip)r   r   rQ   keyss     ` r"   label_loss_itemsz&ClassificationTrainer.label_loss_items   s]     :999999KE*--q112
Cj))***r#   nic                    t          j        |d         j        d                   |d<   t          || j        d| dz  | j                   dS )zPlot training samples with their annotations.

        Args:
            batch (dict[str, torch.Tensor]): Batch containing images and class labels.
            ni (int): Batch index used for naming the output file.
        ru   r   	batch_idxtrain_batchz.jpg)labelsfnameon_plotN)r8   arangeshaper   r   r   )r   rR   r   s      r"   plot_training_samplesz+ClassificationTrainer.plot_training_samples   sc     #\%,*<Q*?@@k-"8"8"8"88L	
 	
 	
 	
 	
 	
r#   )r   r   r   r   )NNT)r*   r+   )rK   N)rL   rE   rM   rE   )rT   r   rK   )rU   rE   rV   rW   rX   rW   rM   rE   )rR   rr   rs   rr   )rs   rE   )NrK   )r   r   rQ   rE   )rR   rr   r   rW   )__name__
__module____qualname____doc__r   r   r)   rA   rH   rS   rq   r|   r   r   r   r   __classcell__)r!   s   @r"   r   r      s4        @ '4ko 5 5 5 5 5 5 5. . .    0    $j j j j j# # # # #J   
 
 
 

 
 
+ + + + + 
 
 
 
 
 
 
 
r#   r   )
__future__r   r   typingr   r8   ultralytics.datar   r   ultralytics.engine.trainerr   ultralytics.modelsr	   ultralytics.nn.tasksr
   ultralytics.utilsr   r   r   ultralytics.utils.plottingr   ultralytics.utils.torch_utilsr   r   r   r\   r#   r"   <module>r      s   # " " " " "              D D D D D D D D 2 2 2 2 2 2 # # # # # # 4 4 4 4 4 4 7 7 7 7 7 7 7 7 7 7 2 2 2 2 2 2 S S S S S S S SC
 C
 C
 C
 C
K C
 C
 C
 C
 C
r#   