
    |h(                         d dl m Z  d dlmZmZ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mZ d d
lmZmZmZ  G d de
      Zy)    )copy)AnyDictOptionalN)ClassificationDatasetbuild_dataloader)BaseTrainer)yolo)ClassificationModel)DEFAULT_CFGLOGGERRANK)plot_imagesplot_results)is_parallelstrip_optimizertorch_distributed_zero_firstc            	       N    e Zd ZdZeddfdeeeef      f fdZ	d Z
ddefdZ fdZdd	ed
efdZddededed
efdZdeeej$                  f   deeej$                  f   fdZdefdZd Zddeej$                     defdZd Zd Zdeeej$                  f   defdZ xZS ) ClassificationTraineraV  
    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 labelled training loss items.
        plot_metrics: Plot metrics from a CSV file.
        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="yolo11n-cls.pt", data="imagenet10", epochs=3)
        >>> trainer = ClassificationTrainer(overrides=args)
        >>> trainer.train()
    N	overridesc                 f    |i }d|d<   |j                  d      d|d<   t        | 	  |||       y)ay  
        Initialize a ClassificationTrainer object.

        This constructor sets up a trainer for image classification tasks, configuring the task type and default
        image size if not specified.

        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 (List[Any], optional): List of callback functions to be executed during training.

        Examples:
            Create a trainer with custom configuration
            >>> from ultralytics.models.yolo.classify import ClassificationTrainer
            >>> args = dict(model="yolo11n-cls.pt", data="imagenet10", epochs=3)
            >>> trainer = ClassificationTrainer(overrides=args)
            >>> trainer.train()
        Nclassifytaskimgsz   )getsuper__init__)selfcfgr   
_callbacks	__class__s       e/var/www/html/test/engine/venv/lib/python3.12/site-packages/ultralytics/models/yolo/classify/train.pyr   zClassificationTrainer.__init__4   sD    & I&	&==!)!$Igi4    c                 @    | j                   d   | j                  _        y)z9Set the YOLO model's class names from the loaded dataset.namesN)datamodelr&   r   s    r#   set_model_attributesz*ClassificationTrainer.set_model_attributesN   s    99W-

r$   verbosec                    t        || j                  d   | j                  d   |xr	 t        dk(        }|r|j                  |       |j	                         D ]  }| j
                  j                  st        |d      r|j                          t        |t        j                  j                        sZ| j
                  j                  sq| j
                  j                  |_         |j                         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_modelR   s     $CDIIdO		*@U_f_ukosukuvJJw 	(A99''GA7I,J""$!UXX--.4993D3Dii''		(
 !!# 	#A"AO	#r$   c                 p   ddl }t        | j                        |j                  j                  v rJ |j                  j                  | j                     | j
                  j                  rdnd      | _        d}nt        | !         }t        j                  | j                  | j                  d          |S )z
        Load, 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_modelk   s     	tzz?k00999@++44TZZ@+/99+?+?TDJ D7&(D++DJJ		$Hr$   img_path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.
        train)rootr4   augmentprefix)r   r4   )r   rK   rL   batchs       r#   build_datasetz#ClassificationTrainer.build_dataset~   s     %(DT[Odhiir$   dataset_path
batch_sizerankc                    t        |      5  | j                  ||      }ddd       t        || j                  j                  |      }|dk7  rkt        | j                        r1|j                  j                  | j                  j                  _
        |S |j                  j                  | j                  _
        |S # 1 sw Y   xY w)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.
        N)rV   rN   )r   rS   r   r4   workersr   r(   datasettorch_transformsmodule
transforms)r   rT   rU   rV   rL   rY   loaders          r#   get_dataloaderz$ClassificationTrainer.get_dataloader   s     *$/ 	=((t<G	= "':tyy7H7HtT7?4::&/5~~/N/N

!!,  )/(G(G

%	= 	=s   B<<CrR   returnc                     |d   j                  | j                        |d<   |d   j                  | j                        |d<   |S )z)Preprocess a batch of images and classes.imgcls)todevice)r   rR   s     r#   preprocess_batchz&ClassificationTrainer.preprocess_batch   s;    U|t{{3eU|t{{3er$   c                 j    dddt        | j                        z   z  z   ddg| j                  ddz  S )z4Return a formatted string showing training progress.
z%11s   EpochGPU_mem	InstancesSize)len
loss_namesr)   s    r#   progress_stringz%ClassificationTrainer.progress_string   sT    vS%9!9::?
 __?
 	?

 ?
 
 	
r$   c                     dg| _         t        j                  j                  | j                  | j
                  t        | j                        | j                        S )z=Return an instance of ClassificationValidator for validation.loss)r4   r!   )	rn   r
   r   ClassificationValidatortest_loadersave_dirr   r4   	callbacksr)   s    r#   get_validatorz#ClassificationTrainer.get_validator   sF    !(}}44dmm$tyy/dnn 5 
 	
r$   
loss_itemsrQ   c                     | j                   D cg c]	  }| d|  }}||S t        t        |      d      g}t        t	        ||            S c c}w )a  
        Return a loss dict with labelled training loss items tensor.

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

        Returns:
            keys (List[str]): List of loss keys if loss_items is None.
            loss_dict (Dict[str, float]): Dictionary of loss items if loss_items is provided.
        /   )rn   roundfloatdictzip)r   rw   rQ   xkeyss        r#   label_loss_itemsz&ClassificationTrainer.label_loss_items   s[     *.9A6(!A399KE*-q12
Cj)**	 :s   Ac                 H    t        | j                  d| j                         y)zPlot metrics from a CSV file.T)filer   on_plotN)r   csvr   r)   s    r#   plot_metricsz"ClassificationTrainer.plot_metrics   s    $((T4<<Hr$   c                 
   | j                   | j                  fD ]  }|j                         st        |       || j                  u s.t	        j
                  d| d       | j                  j                  | j                  j                  _        | j                  j                  | j                  j                  _	        | j                  |      | _
        | j                  j                  dd       | j                  d        y)z3Evaluate trained model and save validation results.z
Validating z...)r(   fitnessNon_fit_epoch_end)lastbestexistsr   r   infor4   r'   	validatorplotsmetricspoprun_callbacks)r   fs     r#   
final_evalz ClassificationTrainer.final_eval   s    DII% 		;Axxz"		>KK-s# 67/3yy~~DNN'',04		DNN''-#'>>>#:DLLL$$Y5&&'9:		;r$   nic                     t        j                  t        |d               |d<   t        || j                  d| dz  | j
                         y)z
        Plot training samples with their annotations.

        Args:
            batch (Dict[str, torch.Tensor]): Batch containing images and class labels.
            ni (int): Number of iterations.
        ra   	batch_idxtrain_batchz.jpg)labelsfnamer   N)r8   arangerm   r   rt   r   )r   rR   r   s      r#   plot_training_samplesz+ClassificationTrainer.plot_training_samples   sG     #\\#eEl*;<k--Kt4"88LL	
r$   )NNT)rN   N)   r   rN   )NrN   )__name__
__module____qualname____doc__r   r   r   rE   r   r   r*   boolrA   rH   rS   intr^   r8   Tensorre   ro   rv   r   r   r   r   __classcell__)r"   s   @r#   r   r      s    D 'd_c 58DcN3K 54. 2&jc j j3 C C [^ 2d3+<&= $sELLGXBY 
 

+8ELL+A +RU +$I;
4U\\0A+B 
 
r$   r   )r   typingr   r   r   r8   ultralytics.datar   r   ultralytics.engine.trainerr	   ultralytics.modelsr
   ultralytics.nn.tasksr   ultralytics.utilsr   r   r   ultralytics.utils.plottingr   r   ultralytics.utils.torch_utilsr   r   r   r    r$   r#   <module>r      s<     & &  D 2 # 4 7 7 @ d d[
K [
r$   