o
    VhB                     @   s   d Z ddlZddlZddlmZ ddlZddlZddlm	Z	m
Z
 ddlmZmZ ddlmZ ddlmZmZmZmZmZ ddlmZ dd	lmZ dd
lmZmZmZ G dd dZdS )a  
Check a model's accuracy on a test or val split of a dataset.

Usage:
    $ yolo mode=val model=yolo11n.pt data=coco8.yaml imgsz=640

Usage - formats:
    $ yolo mode=val model=yolo11n.pt                 # PyTorch
                          yolo11n.torchscript        # TorchScript
                          yolo11n.onnx               # ONNX Runtime or OpenCV DNN with dnn=True
                          yolo11n_openvino_model     # OpenVINO
                          yolo11n.engine             # TensorRT
                          yolo11n.mlpackage          # CoreML (macOS-only)
                          yolo11n_saved_model        # TensorFlow SavedModel
                          yolo11n.pb                 # TensorFlow GraphDef
                          yolo11n.tflite             # TensorFlow Lite
                          yolo11n_edgetpu.tflite     # TensorFlow Edge TPU
                          yolo11n_paddle_model       # PaddlePaddle
                          yolo11n.mnn                # MNN
                          yolo11n_ncnn_model         # NCNN
                          yolo11n_imx_model          # Sony IMX
                          yolo11n_rknn_model         # Rockchip RKNN
    N)Path)get_cfgget_save_dir)check_cls_datasetcheck_det_dataset)AutoBackend)LOGGERTQDM	callbackscolorstremojis)check_imgsz)Profile)de_parallelselect_devicesmart_inference_modec                   @   s   e Zd ZdZd6ddZe d7ddZ	d8dejd	ejd
ejde	dejf
ddZ
defddZdefddZdd Zdd Zdd Zdd Zdd Zdd Zd d! Zd"d# Zd$d% Zd&d' Zd(d) Zed*d+ Zd9d,d-Zd.d/ Zd0d1 Zd2d3 Zd4d5 ZdS ):BaseValidatora  
    A base class for creating validators.

    This class provides the foundation for validation processes, including model evaluation, metric computation, and
    result visualization.

    Attributes:
        args (SimpleNamespace): Configuration for the validator.
        dataloader (DataLoader): Dataloader to use for validation.
        pbar (tqdm): Progress bar to update during validation.
        model (nn.Module): Model to validate.
        data (dict): Data dictionary containing dataset information.
        device (torch.device): Device to use for validation.
        batch_i (int): Current batch index.
        training (bool): Whether the model is in training mode.
        names (dict): Class names mapping.
        seen (int): Number of images seen so far during validation.
        stats (dict): Statistics collected during validation.
        confusion_matrix: Confusion matrix for classification evaluation.
        nc (int): Number of classes.
        iouv (torch.Tensor): IoU thresholds from 0.50 to 0.95 in spaces of 0.05.
        jdict (list): List to store JSON validation results.
        speed (dict): Dictionary with keys 'preprocess', 'inference', 'loss', 'postprocess' and their respective
            batch processing times in milliseconds.
        save_dir (Path): Directory to save results.
        plots (dict): Dictionary to store plots for visualization.
        callbacks (dict): Dictionary to store various callback functions.

    Methods:
        __call__: Execute validation process, running inference on dataloader and computing performance metrics.
        match_predictions: Match predictions to ground truth objects using IoU.
        add_callback: Append the given callback to the specified event.
        run_callbacks: Run all callbacks associated with a specified event.
        get_dataloader: Get data loader from dataset path and batch size.
        build_dataset: Build dataset from image path.
        preprocess: Preprocess an input batch.
        postprocess: Postprocess the predictions.
        init_metrics: Initialize performance metrics for the YOLO model.
        update_metrics: Update metrics based on predictions and batch.
        finalize_metrics: Finalize and return all metrics.
        get_stats: Return statistics about the model's performance.
        check_stats: Check statistics.
        print_results: Print the results of the model's predictions.
        get_desc: Get description of the YOLO model.
        on_plot: Register plots (e.g. to be consumed in callbacks).
        plot_val_samples: Plot validation samples during training.
        plot_predictions: Plot YOLO model predictions on batch images.
        pred_to_json: Convert predictions to JSON format.
        eval_json: Evaluate and return JSON format of prediction statistics.
    Nc                 C   s   t |d| _|| _|| _d| _d| _d| _d| _d| _d| _	d| _
d| _d| _d| _d| _d| _ddddd| _|p>t| j| _| jjrI| jd n| jjddd | jjdu r[d| j_t| jjd	d
| j_i | _|pmt | _dS )a  
        Initialize a BaseValidator instance.

        Args:
            dataloader (torch.utils.data.DataLoader, optional): Dataloader to be used for validation.
            save_dir (Path, optional): Directory to save results.
            pbar (tqdm.tqdm, optional): Progress bar for displaying progress.
            args (SimpleNamespace, optional): Configuration for the validator.
            _callbacks (dict, optional): Dictionary to store various callback functions.
        )	overridesNTg        )
preprocess	inferencelosspostprocesslabels)parentsexist_okgMbP?   )max_dim)r   args
dataloaderpbarstridedatadevicebatch_itrainingnamesseenstatsconfusion_matrixnciouvjdictspeedr   save_dirsave_txtmkdirconfr   imgszplotsr
   get_default_callbacks)selfr   r-   r   r   
_callbacks r6   P/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/engine/validator.py__init__^   s.   "zBaseValidator.__init__c                    s  |du _  jjo j  } j rX|j _|j _ jjdko |j j_|jjp)|j	} jjr2| n|
 }tj|j|jd _ j j|jjpO|j|jd kM  _|  nt jj	drj|du rjtd t  t|pu jj	t jj jj jj jj jjd}|j _|j j_|j |j!|j"|j#f\}}}}t$ jj%|d}|r|j& j_n)|s|st'|d	d
s|j()dd j_t*d jj d jj d| d| d	 t jj+dd dv rt, jj _n$ jj-dkrt. jj jj+d _nt/t0d jj d jj- d jjdv rd j_1|s+t'|d	d
s+d
 j_2|j  _  j3p@ 4 j) jj+ jj _3|  |j5|rMdn jj jd ||fd  6d t7 jdt7 jdt7 jdt7 jdf}	t8 j3 9 t: j3d}
 ;t<| g  _=t>|
D ]\}} 6d | _?|	d   @|}W d   n	1 sw   Y  |	d  ||d  |d!}W d   n	1 sw   Y  |	d"   j r  j|||d 7  _W d   n	1 sw   Y  |	d#   A|}W d   n	1 sw   Y   B||  jjr0|d#k r0 C||  D|||  6d$ q E } F| tGtH jIJ  fd%d&|	D  _I K   L   6d'  j r|
  i ||jM jN t: j3 d(d)}d*d+ |O D S t*d,jPtQ jIR    jjSr̈ j=rtTt jUd- d.d/d0}t*d1|jV d2 tWX j=| W d   n	1 sw   Y   Y|} jjsֈ jjSrt*d3tZd4 jU  |S )5a  
        Execute validation process, running inference on dataloader and computing performance metrics.

        Args:
            trainer (object, optional): Trainer object that contains the model to validate.
            model (nn.Module, optional): Model to validate if not using a trainer.

        Returns:
            stats (dict): Dictionary containing validation statistics.
        Ncpu)r"   r   z.yamlz8validating an untrained model YAML will result in 0 mAP.)weightsr"   dnnr!   fp16)r    dynamicFbatchzSetting batch=z input of shape (z, 3, z, ).>   ymlyamlclassify)splitz	Dataset 'z' for task=u    not found ❌>   r9   mpsr   channels)r1   on_val_start)desctotalon_val_batch_startimg)augment      on_val_batch_endc                 3   s&    | ]}|j t jj d  V  qdS )g     @@N)tlenr   dataset).0xr4   r6   r7   	<genexpr>   s   $ z)BaseValidator.__call__.<locals>.<genexpr>
on_val_endval)prefixc                 S   s    i | ]\}}|t t|d qS )   )roundfloat)rT   kvr6   r6   r7   
<dictcomp>   s     z*BaseValidator.__call__.<locals>.<dictcomp>z]Speed: {:.1f}ms preprocess, {:.1f}ms inference, {:.1f}ms loss, {:.1f}ms postprocess per imagezpredictions.jsonwzutf-8)encodingzSaving z...zResults saved to bold)[r$   r   rM   r"   r!   typeamphalfemamodelr]   torch
zeros_like
loss_itemsr   r2   stopperpossible_stopepochepochsevalstrendswithr   warningr
   add_integration_callbacksr   r   r>   r;   r<   r    ptjitenginer   r1   
batch_sizegetattrmetadatagetinforE   r   taskr   FileNotFoundErrorr   workersrectr   get_dataloaderwarmuprun_callbacksr   r	   get_descrR   init_metricsr   r+   	enumerater#   r   r   update_metricsplot_val_samplesplot_predictions	get_statscheck_statsdictzipr,   keysfinalize_metricsprint_resultslabel_loss_itemsr9   itemsformattuplevalues	save_jsonopenr-   namejsondump	eval_jsonr   )r4   trainerrh   rM   r    ru   rv   rw   r1   dtbarr#   r>   predsr'   resultsfr6   rV   r7   __call__   s   
$



,"&(










$
&
zBaseValidator.__call__Fpred_classestrue_classesiou	use_scipyreturnc                 C   s  t |jd | jjd ft}|dddf |k}|| }|  }t| j 	 D ]\}}|r_ddl
}	|||k }
|
 r^|	j|
\}}|
||f dk}| r^d||| |f< q.t ||k}t |j}|jd r|jd dkr|||dddf |dddf f  ddd  }|t j|dddf ddd  }|t j|dddf ddd  }d||dddf t|f< q.tj|tj|jdS )a  
        Match predictions to ground truth objects using IoU.

        Args:
            pred_classes (torch.Tensor): Predicted class indices of shape (N,).
            true_classes (torch.Tensor): Target class indices of shape (M,).
            iou (torch.Tensor): An NxM tensor containing the pairwise IoU values for predictions and ground truth.
            use_scipy (bool): Whether to use scipy for matching (more precise).

        Returns:
            (torch.Tensor): Correct tensor of shape (N, 10) for 10 IoU thresholds.
        r   NTr   rA   )return_index)dtyper"   )npzerosshaper*   astypeboolr9   numpyr   tolistscipyanyoptimizelinear_sum_assignmentnonzeroarrayTargsortuniqueintri   tensorr"   )r4   r   r   r   r   correctcorrect_classi	thresholdr   cost_matrix
labels_idxdetections_idxvalidmatchesr6   r6   r7   match_predictions   s0   "
6""zBaseValidator.match_predictionseventc                 C   s   | j | | dS )z1Append the given callback to the specified event.N)r
   appendr4   r   callbackr6   r6   r7   add_callback,  s   zBaseValidator.add_callbackc                 C   s    | j |g D ]}||  qdS )z4Run all callbacks associated with a specified event.N)r
   r{   r   r6   r6   r7   r   0  s   
zBaseValidator.run_callbacksc                 C      t d)z1Get data loader from dataset path and batch size.z:get_dataloader function not implemented for this validatorNotImplementedError)r4   dataset_pathrx   r6   r6   r7   r   5     zBaseValidator.get_dataloaderc                 C   r   )zBuild dataset from image path.z3build_dataset function not implemented in validatorr   )r4   img_pathr6   r6   r7   build_dataset9  r   zBaseValidator.build_datasetc                 C      |S )zPreprocess an input batch.r6   )r4   r>   r6   r6   r7   r   =     zBaseValidator.preprocessc                 C   r   )zPostprocess the predictions.r6   )r4   r   r6   r6   r7   r   A  r   zBaseValidator.postprocessc                 C      dS )z2Initialize performance metrics for the YOLO model.Nr6   )r4   rh   r6   r6   r7   r   E  r   zBaseValidator.init_metricsc                 C   r   )z.Update metrics based on predictions and batch.Nr6   r4   r   r>   r6   r6   r7   r   I  r   zBaseValidator.update_metricsc                 O   r   )z Finalize and return all metrics.Nr6   )r4   r   kwargsr6   r6   r7   r   M  r   zBaseValidator.finalize_metricsc                 C   s   i S )z0Return statistics about the model's performance.r6   rV   r6   r6   r7   r   Q  r   zBaseValidator.get_statsc                 C   r   )zCheck statistics.Nr6   r4   r'   r6   r6   r7   r   U  r   zBaseValidator.check_statsc                 C   r   )z-Print the results of the model's predictions.Nr6   rV   r6   r6   r7   r   Y  r   zBaseValidator.print_resultsc                 C   r   )z"Get description of the YOLO model.Nr6   rV   r6   r6   r7   r   ]  r   zBaseValidator.get_descc                 C   s   g S )z8Return the metric keys used in YOLO training/validation.r6   rV   r6   r6   r7   metric_keysa  s   zBaseValidator.metric_keysc                 C   s   |t   d| jt|< dS )z2Register plots (e.g. to be consumed in callbacks).)r!   	timestampN)timer2   r   )r4   r   r!   r6   r6   r7   on_plotf  s   zBaseValidator.on_plotc                 C   r   )z(Plot validation samples during training.Nr6   )r4   r>   nir6   r6   r7   r   k  r   zBaseValidator.plot_val_samplesc                 C   r   )z,Plot YOLO model predictions on batch images.Nr6   )r4   r>   r   r   r6   r6   r7   r   o  r   zBaseValidator.plot_predictionsc                 C   r   )z#Convert predictions to JSON format.Nr6   r   r6   r6   r7   pred_to_jsons  r   zBaseValidator.pred_to_jsonc                 C   r   )z9Evaluate and return JSON format of prediction statistics.Nr6   r   r6   r6   r7   r   w  r   zBaseValidator.eval_json)NNNNN)NN)F)N) __name__
__module____qualname____doc__r8   r   r   ri   Tensorr   r   rq   r   r   r   r   r   r   r   r   r   r   r   r   r   propertyr   r   r   r   r   r   r6   r6   r6   r7   r   *   sJ    
3%}
,

r   )r   r   r   pathlibr   r   r   ri   ultralytics.cfgr   r   ultralytics.data.utilsr   r   ultralytics.nn.autobackendr   ultralytics.utilsr   r	   r
   r   r   ultralytics.utils.checksr   ultralytics.utils.opsr   ultralytics.utils.torch_utilsr   r   r   r   r6   r6   r6   r7   <module>   s   