
    /juF                        d Z ddlm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 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mZ dd	lmZ dd
lmZ ddl m!Z!m"Z"m#Z#m$Z$  G 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=yolo26n.pt data=coco8.yaml imgsz=640

Usage - formats:
    $ yolo mode=val model=yolo26n.pt                 # PyTorch
                          yolo26n.torchscript        # TorchScript
                          yolo26n.onnx               # ONNX Runtime or OpenCV DNN with dnn=True
                          yolo26n_openvino_model     # OpenVINO
                          yolo26n.engine             # TensorRT
                          yolo26n.mlpackage          # CoreML (macOS-only)
                          yolo26n_saved_model        # TensorFlow SavedModel
                          yolo26n.pb                 # TensorFlow GraphDef
                          yolo26n.tflite             # TensorFlow Lite
                          yolo26n_edgetpu.tflite     # TensorFlow Edge TPU
                          yolo26n_paddle_model       # PaddlePaddle
                          yolo26n.mnn                # MNN
                          yolo26n_ncnn_model         # NCNN
                          yolo26n_imx_model          # Sony IMX
                          yolo26n_rknn_model         # Rockchip RKNN
    )annotationsN)Path)get_cfgget_save_dir)check_cls_datasetcheck_det_dataset)AutoBackend)LOGGERRANKTQDM	callbackscolorstremojis)check_imgsz)Profile)attempt_compileselect_devicesmart_inference_modeunwrap_modelc                      e Zd ZdZd%d&dZ e            d'd            Z	 d(d)dZd*dZd*dZ	d Z
d Zd Zd Zd Zd Zd Zd Zd Zd Zd Zed             Zd+d Zd! Zd" Zd# Zd$ 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.
        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 steps 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.
        stride (int): Model stride for padding calculations.
        loss (torch.Tensor): Accumulated loss during training validation.

    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.
        print_results: Print the results of the model's predictions.
        get_desc: Get description of the YOLO model.
        on_plot: Register plots for visualization.
        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.
    N
_callbacksdict | Nonec                   ddl }t          |          | _        || _        d| _        d| _        d| _        d| _        d| _        d| _	        d| _
        d| _        d| _        d| _        d| _        d| _        ddddd| _        |pt#          | j                  | _        | j        j        r
| j        dz  n| j                            dd           | j        j        | j        j        d	k    rd
nd| j        _        t/          | j        j        d          | j        _        i | _        |pt5          j                    | _        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.
            args (SimpleNamespace, optional): Configuration for the validator.
            _callbacks (dict, optional): Dictionary to store various callback functions.
        r   N)	overridesTg        )
preprocess	inferencelosspostprocesslabels)parentsexist_okobbg{Gz?gMbP?   )max_dim)torchvisionr   args
dataloaderstridedatadevicebatch_itrainingnamesseenstatsconfusion_matrixnciouvjdictspeedr   save_dirsave_txtmkdirconftaskr   imgszplotsr   get_default_callbacks)selfr(   r6   r'   r   r&   s         a/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/engine/validator.py__init__zBaseValidator.__init__`   s:    	d+++	$	
	
 $	
$'c3WZ[[
 ;L$;$;%)Y%7	J	!	!T]QQZ^imQnnn9>!%)Y^u%<%<TT%DIN%dioqAAA	
#Hy'F'H'H    c                     |du _          j        j        o j          } j         r|j         _        |j         _         j        j        dk    o|j         j        _        |j        j        p|j	        }|j        j
        rt          |d          r|j        } j        j        r|                                n|                                }t          j        |j        |j                   _         j        xj        |j        j        p|j        |j        dz
  k    z  c_        |                                 nt1           j        j	                                      d          r|t5          j        d           t9          j                    t          |d          rO j        j         j        j        |_        |j        r+|                     j        j          j        j!        	           tE          |p j        j	        tF          d
k    rtI           j        j                  nt          j        dtF                     j        j%         j        j         j        j                  }|j         _        |j&         j        _        |j'        |j(        }}|dk    }tS           j        j*        |          }|dvrktW          |dd          sZ|j,        -                    dd           j        _.        t5          j/        d j        j.         d j        j.         d| d| d	           t1           j        j                  0                    dd          d
         dv rtc           j        j                   _        nr j        j2        dk    r+tg           j        j         j        j4                   _        n7tk          tm          d j        j         d j        j2         d                     j        j        dv rd  j        _7        |s#tW          |dd          r|d!k    sd j        _8        |j'         _'         j9        pA :                     j        -                     j        j4                   j        j.                   _9        |                                  j        j
        rtw          | j                  }|<                    |rdn j        j.         j        d"         ||f#            =                    d$           t}           j                  t}           j                  t}           j                  t}           j                  f}t           j9         @                                t           j9                  %          }	 B                    t          |                     g  _D        t          |	          D ]\  }
} =                    d&           |
 _F        |d          5   G                    |          }ddd           n# 1 swxY w Y   |d         5   ||d'         |(          }ddd           n# 1 swxY w Y   |d)         5   j         r* xj        |                    ||          d         z  c_        ddd           n# 1 swxY w Y   |d*         5   H                    |          }ddd           n# 1 swxY w Y    I                    ||            j        j        r<|
d*k     r6tF          d+v r- J                    ||
            K                    |||
            =                    d,           i } L                                 tF          d+v r M                                }t          t           jP        Q                                 fd-|D                                  _P         R                                  S                                  =                    d.            j         r|                                  j        T                                U                                }|jV        dk    r&t          jX        |d t          jY        jZ        /           tF          d k    rdS i ||[                    |\                                t           j9                  z  d01          }d2 |]                                D             S tF          d k    r|S t5          j/         d3j(        t           jP        _                                                       j        j`        r jD        rt          t1           jb        d4z            d5d67          5 }t5          j/        d8|jc         d9           t          je         jD        |           ddd           n# 1 swxY w Y    f                    |          } j        j        s j        j`        r*t5          j/        d:t          d; jb                              |S )<ax  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:
            (dict): Dictionary containing validation statistics.
        Ncpu	_orig_mod)r+   r$   z.yamlz8validating an untrained model YAML will result in 0 mAP.end2end)max_detagnostic_nmscuda)modelr+   dnnr*   fp16pt)r)   >   rM   torchscriptdynamicFbatchzSetting batch=z input of shape (z, 3, z, ).>   ymlyamlclassify)splitz	Dataset 'z' for task=u    not found ❌>   rC   mpsr   imxchannels)r;   on_val_start)desctotalon_val_batch_startimg)augment      >   r   rH   on_val_batch_endc              3  `   K   | ](}|j         t          j        j                  z  d z  V  )dS )g     @@N)tlenr(   dataset).0xr>   s     r?   	<genexpr>z)BaseValidator.__call__.<locals>.<genexpr>   s>      5k5kcdacC@W<X<X6X[^6^5k5k5k5k5k5krA   
on_val_end)dstopval)prefixc                P    i | ]#\  }}|t          t          |          d           $S )   )roundfloat)rg   kvs      r?   
<dictcomp>z*BaseValidator.__call__.<locals>.<dictcomp>   s.    FFFdaAuU1XXq))FFFrA   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)hr-   r'   r_   r+   r*   typeamphalfemarJ   compilehasattrrD   rr   torch
zeros_like
loss_itemsr   r<   stopperpossible_stopepochepochsevalstrendswithr
   warningr   add_integration_callbacksrE   set_head_attrrF   rG   r	   r   r   rK   rL   r)   formatr   r;   getattrmetadatagetrP   inforsplitr   r:   r   rV   FileNotFoundErrorr   workersrectr(   get_dataloaderr   warmuprun_callbacksr   r   get_descre   init_metricsr   r4   	enumerater,   r   r   update_metricsplot_val_samplesplot_predictionsgather_stats	get_statsdictzipr5   keysfinalize_metricsprint_resultsclonedetach
world_sizedistreduceReduceOpAVGlabel_loss_itemsrC   itemstuplevalues	save_jsonopenr6   namejsondump	eval_jsonr   )r>   trainerrJ   r_   r)   fmtrM   r;   dtbarr,   rP   predsr0   r   resultsfs   `                r?   __call__zBaseValidator.__call__   s^
     t+)#;T]):= 6	d!.DKDI![-6F7;DINKO4w}E|# ({(C(C ($(INEEJJLLLE();GNSSSDIIOOw<eRYR`cdRdAdeOOJJLLLL49?##,,W55 [%-YZZZ/555ui(( h9$0$(I$5EM= h''	0APTPYPf'ggg.ty:>"**}TY%5666%,W]_cJdJdIMY^Y^  E  ,DK"ZDIN,CFB	???E///yRW8X8X/"'."4"4Wa"@"@	vTY_vvtyvvejvvnsvvvwww49>""))#q11"5HH-din==		:---dinDIOTTT		'/t49>/t/tVZV_Vd/t/t/t(u(uvvv{>11$%	! '75)U;; 'u!&	,DK"ou1D1DTY]]SWS\SbEcEceienet1u1uDOJJLLLy  C'dkBBBLLR <TY_di
>SUZ\abLccc>***4;'''4;'''4;'''4;'''	
 4?DO@T@TUUU,u--...
'nn 	3 	3NGU3444"DLA / /../ / / / / / / / / / / / / / / A = =eElG<<<= = = = = = = = = = = = = = = A = == =IIE5!9!9!!<<II= = = = = = = = = = = = = = =
 A 0 0((//0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 u---y =7Q;;47??%%eW555%%eUG<<<122227??NN$$Ec$*//"3"35k5k5k5khj5k5k5kllmmDJ!!###   |,,,= 	KKMMM9??$$++--D!A%%DaDM,=>>>>axxll'":":488::DOH\H\;\ej":"k"klGFFgmmooFFFFaxxKvov4:,,..//  
 y" .tz .#dm.@@AA3QXYYY -]^K 5!& 5 5 5666Idj!,,,- - - - - - - - - - - - - - - u--y S$)"5 SQ0O0OQQRRRLsZ   -XX	X	"YY		Y	2ZZ	Z	&[[	[	(8f,,f03f0Fpred_classestorch.Tensortrue_classesiou	use_scipyboolreturnc                   t          j        |j        d         | j        j        d         f                              t
                    }|dddf         |k    }||z  }|                                                                }t          | j                                        	                                          D ]\  }}|rpddl
}	|||k    z  }
|
                                rN|	j                            |
d          \  }}|
||f         dk    }|                                rd|||         |f<   xt          j        ||k              }t          j        |          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                  }|t          j        |dddf         d          d                  }d||dddf                             t&                    |f<   t)          j        |t(          j        |j                  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, optional): Whether to use scipy for matching (more precise).

        Returns:
            (torch.Tensor): Correct tensor of shape (N, 10) for 10 IoU thresholds.
        r   NT)maximizer$   rH   )return_index)dtyper+   )npzerosshaper3   astyper   rC   numpyr   tolistscipyanyoptimizelinear_sum_assignmentnonzeroarrayTargsortuniqueintr   tensorr+   )r>   r   r   r   r   correctcorrect_classi	thresholdr   cost_matrix
labels_idxdetections_idxvalidmatchess                  r?   match_predictionszBaseValidator.match_predictions  s^    (L.q149?13EFGGNNtTT$QQQW-=M!ggiioo%dimmoo&<&<&>&>?? 	A 	ALAy A!SI%56??$$ A161U1UValp1U1q1q.J'
N(BCaGEyy{{ A<@u 5q 89*SI%566(7++-=# A}Q'!++")#gaaadmWQQQT].J*K*S*S*U*UVZVZXZVZ*["\")")GAAAqDMPT*U*U*UVW*X"Y")")GAAAqDMPT*U*U*UVW*X"Y<@GGAAAqDM0055q89|G5:l>QRRRRrA   eventr   c                F    | j         |                             |           dS )z1Append the given callback to the specified event.N)r   appendr>   r   callbacks      r?   add_callbackzBaseValidator.add_callback;  s#    u$$X.....rA   c                X    | j                             |g           D ]} ||            dS )z4Run all callbacks associated with a specified event.N)r   r   r   s      r?   r   zBaseValidator.run_callbacks?  s<    **5"55 	 	HHTNNNN	 	rA   c                     t          d          )z1Get data loader from dataset path and batch size.z:get_dataloader function not implemented for this validatorNotImplementedError)r>   dataset_path
batch_sizes      r?   r   zBaseValidator.get_dataloaderD  s    !"^___rA   c                     t          d          )zBuild dataset from image path.z3build_dataset function not implemented in validatorr   )r>   img_paths     r?   build_datasetzBaseValidator.build_datasetH  s    !"WXXXrA   c                    |S )zPreprocess an input batch. )r>   rP   s     r?   r   zBaseValidator.preprocessL      rA   c                    |S )zPostprocess the predictions.r   )r>   r   s     r?   r   zBaseValidator.postprocessP  r   rA   c                    dS )z2Initialize performance metrics for the YOLO model.Nr   )r>   rJ   s     r?   r   zBaseValidator.init_metricsT      rA   c                    dS )z.Update metrics based on predictions and batch.Nr   r>   r   rP   s      r?   r   zBaseValidator.update_metricsX  r   rA   c                    dS )z Finalize and return all metrics.Nr   r>   s    r?   r   zBaseValidator.finalize_metrics\  r   rA   c                    i S )z0Return statistics about the model's performance.r   r   s    r?   r   zBaseValidator.get_stats`  s    	rA   c                    dS )zAGather statistics from all the GPUs during DDP training to GPU 0.Nr   r   s    r?   r   zBaseValidator.gather_statsd  r   rA   c                    dS )z-Print the results of the model's predictions.Nr   r   s    r?   r   zBaseValidator.print_resultsh  r   rA   c                    dS )z"Get description of the YOLO model.Nr   r   s    r?   r   zBaseValidator.get_descl  r   rA   c                    g S )z8Return the metric keys used in YOLO training/validation.r   r   s    r?   metric_keyszBaseValidator.metric_keysp  s	     	rA   c                    |r|                     d          ndr4t          fd| j                                        D                       rdS |t	          j                    d| j        t          |          <   dS )z8Register plots for visualization, deduplicating by type.ry   Nc              3  t   K   | ]2}|                     d           pi                      d          k    V  3dS )r*   ry   N)r   )rg   rt   	plot_types     r?   ri   z(BaseValidator.on_plot.<locals>.<genexpr>x  sF      iiPQaeeFmm1r66v>>)KiiiiiirA   )r*   	timestamp)r   r   r<   r   timer   )r>   r   r*   r  s      @r?   on_plotzBaseValidator.on_plotu  s    (,6DHHV$$$$	 	iiiiUYU_UfUfUhUhiiiii 	F*.TY[[!I!I
4::rA   c                    dS )z(Plot validation samples during training.Nr   )r>   rP   nis      r?   r   zBaseValidator.plot_val_samples|  r   rA   c                    dS )z,Plot YOLO model predictions on batch images.Nr   )r>   rP   r   r
  s       r?   r   zBaseValidator.plot_predictions  r   rA   c                    dS )z#Convert predictions to JSON format.Nr   r   s      r?   pred_to_jsonzBaseValidator.pred_to_json  r   rA   c                    dS )z9Evaluate and return JSON format of prediction statistics.Nr   )r>   r0   s     r?   r   zBaseValidator.eval_json  r   rA   )NNNN)r   r   )NN)F)
r   r   r   r   r   r   r   r   r   r   )r   r   )N)__name__
__module____qualname____doc__r@   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   propertyr  r  r   r   r  r   r   rA   r?   r   r   -   s       0 0d"I "I "I "I "IH K K K K\ lq'S 'S 'S 'S 'SR/ / / /   
` ` `Y Y Y                     XJ J J J          rA   r   )&r  
__future__r   r   r  pathlibr   r   r   r   torch.distributeddistributedr   ultralytics.cfgr   r   ultralytics.data.utilsr   r   ultralytics.nn.autobackendr	   ultralytics.utilsr
   r   r   r   r   r   ultralytics.utils.checksr   ultralytics.utils.opsr   ultralytics.utils.torch_utilsr   r   r   r   r   r   rA   r?   <module>r     sd   0 # " " " " "                          1 1 1 1 1 1 1 1 G G G G G G G G 2 2 2 2 2 2 M M M M M M M M M M M M M M M M 0 0 0 0 0 0 ) ) ) ) ) ) l l l l l l l l l l l l] ] ] ] ] ] ] ] ] ]rA   