
    hD                         S r SSKrSSKrSSKJr  SSKrSSKrSSKJ	r
  SSKJrJr  SSKJrJr  SSKJr  SSKJrJrJrJrJrJr  SSKJr  SS	KJr  SS
KJrJ r J!r!J"r"   " S S5      r#g)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RANKTQDM	callbackscolorstremojis)check_imgsz)Profile)attempt_compileselect_devicesmart_inference_modeunwrap_modelc                   D   \ rS rSrSrS!S jr\" 5       S"S j5       r S#S\R                  S\R                  S\R                  S	\
S
\R                  4
S jjrS\4S jrS\4S jrS rS rS rS rS rS rS rS rS rS rS r\S 5       rS$S jrS rS rS rS r S r!g)%BaseValidator+   a`
  
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 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.
    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.
Nc                    SSK n[        US9U l        Xl        SU l        SU l        SU l        SU l        SU l        SU l	        SU l
        SU l        SU l        SU l        SU l        SU l        SSSSS.U l        U=(       d    [#        U R                  5      U l        U R                  R&                  (       a  U R$                  S-  OU R$                  R)                  SSS9  U R                  R*                  c-  U R                  R,                  S	:X  a  S
OSU R                  l        [/        U R                  R0                  SS9U R                  l        0 U l        U=(       d    [4        R6                  " 5       U l        g)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&   r4   r%   
_callbacksr$   s         V/home/james-whalen/.local/lib/python3.13/site-packages/ultralytics/engine/validator.py__init__BaseValidator.__init___   s#    	d+	$	
	
 $	
$'c3WZ[
 ;L$;%)YY%7%7	!T]]QQZ^imQn99>>!%)YY^^u%<T%DIIN%diiooqA		
#Hy'F'F'H    c                   ^  USLT l         T R                  R                  =(       a    T R                   (       + nT R                   (       Ga  UR                  T l        UR                  T l        T R                  R
                  S:g  =(       a    UR                  T R                  l        UR                  R                  =(       d    UR                  nUR                  R                  (       a  [        US5      (       a  UR                  nT R                  R                  (       a  UR                  5       OUR                  5       n[        R                  " UR                   UR                  S9T l        T R                  =R$                  UR&                  R(                  =(       d    UR*                  UR,                  S-
  :H  -  sl        UR/                  5         GOZ[1        T R                  R                  5      R3                  S5      (       a  Uc  [4        R6                  " S5        [8        R:                  " T 5        [=        U=(       d    T R                  R                  [>        S:X  a  [A        T R                  R                  5      O[        R                  " S	[>        5      T R                  RB                  T R                  R                  T R                  R                  S
9nUR                  T l        URD                  T R                  l        URF                  URH                  URJ                  pen[M        T R                  RN                  US9nU(       d  U(       d  [Q        USS5      (       dv  URR                  RU                  SS5      T R                  l+        [4        RX                  " ST R                  RV                   ST R                  RV                   SU SU S3	5        [1        T R                  R                  5      R[                  SS5      S   S;   a%  []        T R                  R                  5      T l        OT R                  R^                  S:X  a8  [a        T R                  R                  T R                  Rb                  S9T l        OC[e        [g        ST R                  R                   ST R                  R^                   S35      5      eT R                  R
                  S;   a  ST R                  l4        U(       d4  [Q        USS5      (       a  URj                  (       a  ST R                  l6        URF                  T l#        T Rn                  =(       dS    T Rq                  T R                  RU                  T R                  Rb                  5      T R                  RV                  5      T l7        UR/                  5         T R                  R                  (       a  [s        UT R                  S9nURu                  U(       a  SOT R                  RV                  T R                  S   Xw4S9  T Rw                  S5        [y        T R                  S9[y        T R                  S9[y        T R                  S9[y        T R                  S94n[{        T Rn                  T R}                  5       [        T Rn                  5      S 9n	T R                  [        U5      5        / T lB        [        U	5       GH,  u  pT Rw                  S!5        U
T lD        US      T R                  U5      nSSS5        US      U" US"   US#9nSSS5        US$      T R                   (       a(  T =R"                  UR#                  UW5      S   -  sl        SSS5        US%      T R                  W5      nSSS5        T R                  WU5        T R                  R$                  (       a3  U
S%:  a-  [>        S&;   a#  T R                  X5        T R                  XU
5        T Rw                  S'5        GM/     0 nT R                  5         [>        S&;   a}  T R                  5       n[        [        T R                  R                  5       U 4S( jU 5       5      5      T lN        T R                  5         T R                  5         T Rw                  S)5        T R                   (       a  UR                  5         T R"                  R                  5       R                  5       nUR                  S:  a.  [        R                  " US[        R                  R                  S*9  [>        S:  a  g0 UEUR                  UR                  5       [        T Rn                  5      -  S+S,9EnUR                  5        VVs0 s H  u  nnU[        [        U5      S-5      _M     snn$ [>        S:  a  U$ [4        RX                  " S.R                  " [        T R                  R                  5       5      6 5        T R                  R                  (       a  T R                  (       a  [        [1        T R                  S/-  5      S0S1S29 n[4        RX                  " S3UR                   S435        [        R                  " T R                  U5        SSS5        T R                  U5      nT R                  R$                  (       d  T R                  R                  (       a-  [4        RX                  " S5[        S6T R                  5       35        U$ ! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN= f! , (       d  f       GN= fs  snnf ! , (       d  f       N= f)7aI  
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.cuda)modelr)   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 ❌>   rC   mpsr   channels)r9   on_val_start)desctotalon_val_batch_startimg)augment      >   r   rE   on_val_batch_endc              3      >#    U  H3  oR                   [        TR                  R                  5      -  S -  v   M5     g7f)g     @@N)tlenr&   dataset).0xr<   s     r>   	<genexpr>)BaseValidator.__call__.<locals>.<genexpr>   s0     5khjcdccC@W@W<X6X[^6^hjs   ;>
on_val_end)dstopval)prefix   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%   rY   r)   r(   typeamphalfemarG   compilehasattrrD   floattorch
zeros_like
loss_itemsr   r:   stopperpossible_stopepochepochsevalstrendswithr	   warningr   add_integration_callbacksr   r
   r   rH   rI   r'   ptjitr   r9   getattrmetadatagetrK   inforsplitr   r8   r   rQ   FileNotFoundErrorr   workersimxrectr&   get_dataloaderr   warmuprun_callbacksr   r   get_descr_   init_metricsr   r2   	enumerater*   r   r   update_metricsplot_val_samplesplot_predictionsgather_stats	get_statsdictzipr3   keysfinalize_metricsprint_resultsclonedetach
world_sizedistreduceReduceOpAVGlabel_loss_itemsrC   itemsroundformattuplevalues	save_jsonopenr4   namejsondump	eval_jsonr   )r<   trainerrG   rY   r'   r   r   r9   dtbarr*   rK   predsr.   r   resultskvfs   `                  r>   __call__BaseValidator.__call__   s     t+))##;T]]):===!..DKDI![[--6F7;;DIINKKOO4w}}E||##{(C(C$(IINNEJJLE((););GNNSDIIIOOw<<eRYR`R`cdRdAdeOJJL499??#,,W55%-YZ//5.tyy:>"*}TYY%5%56%,,W]_cJdIIMMYY^^YY^^E  ,,DK"ZZDIIN#llEHHeiiF		?E#	5!A!A"'.."4"4Wa"@		nTYY__,==NtyyN__dejdkkmnsmttuvw499>>"))#q1"5H-diinn=	:--diinnDIIOOT	'499>>:J+VZV_V_VdVdUees/t(uvv{{>1$%		!75)U;;EII!&		,,DK"oou1D1DTYY]]SWS\S\SbSbEceienenetet1uDOJJLyy  'dkkBLLRTYY__dii
>SUZbLc>*4;;'4;;'4;;'4;;'	
 4??DOO@TU,u-.
'nNG34"DLA.  AeElG<  A==IIE5!9!!<<I 
 A((/  u-yy7Q;47?%%e5%%eG<123 -6 7?NN$Ec$**//"35khj5klmDJ!!# |,==KKM99??$++-D!!A%DaDMM,=,=>axll'":":488:DOOH\;\ej":"klG6=mmoFodaAuU1Xq))oFFaxKKovv4::,,./
 yy""tzz#dmm.@@A3QXY]^KK'!&& 56IIdjj!, Z u-yy$))"5"5/0O/PQRLs   
 8 G ZYsC   l 3l2:mm#m(Am. 
l/	2
m	
m	
m%	.
m<pred_classestrue_classesiou	use_scipyreturnc                    [         R                  " UR                  S   U R                  R                  S   45      R	                  [
        5      nUSS2S4   U:H  nX6-  nUR                  5       R                  5       n[        U R                  R                  5       R                  5       5       GHg  u  pxU(       aj  SSK
n	X3U:  -  n
U
R                  5       (       aH  U	R                  R                  U
5      u  pXU4   S:  nUR                  5       (       a  SX\U   U4'   Ms  Mu  Mw  [         R                  " X8:  5      n[         R                  " U5      R                   nUR                  S   (       d  M  UR                  S   S:  ao  XUSS2S4   USS2S4   4   R#                  5       SSS2      nU[         R$                  " USS2S4   SS9S      nU[         R$                  " USS2S4   SS9S      nSX^SS2S4   R	                  [&        5      U4'   GMj     [(        R*                  " U[(        R
                  UR,                  S9$ )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   NTr"   rE   )return_index)dtyper)   )npzerosshaper1   astypeboolrC   numpyr   tolistscipyanyoptimizelinear_sum_assignmentnonzeroarrayTargsortuniqueintru   tensorr)   )r<   r   r   r   r   correctcorrect_classi	thresholdr   cost_matrix
labels_idxdetections_idxvalidmatchess                  r>   match_predictionsBaseValidator.match_predictions  s     ((L..q1499??13EFGNNtT$QW-=!ggioo%diimmo&<&<&>?LA!I%56??$$161U1UVa1b.J'N(BCaGEyy{{<@u 5q 89 # % **S%56((7+--==##}}Q'!+")gadmWQT].J*K*S*S*UVZXZVZ*["\")"))GAqDMPT*UVW*X"Y")"))GAqDMPT*UVW*X"Y<@GAqDM005q89' @( ||G5::l>Q>QRRrA   eventc                 @    U R                   U   R                  U5        g)z1Append the given callback to the specified event.N)r   appendr<   r   callbacks      r>   add_callbackBaseValidator.add_callback8  s    u$$X.rA   c                 Z    U R                   R                  U/ 5       H  nU" U 5        M     g)z4Run all callbacks associated with a specified event.N)r   r   r   s      r>   r   BaseValidator.run_callbacks<  s$    **5"5HTN 6rA   c                     [        S5      e)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   BaseValidator.get_dataloaderA  s    !"^__rA   c                     [        S5      e)zBuild dataset from image path.z3build_dataset function not implemented in validatorr   )r<   img_paths     r>   build_datasetBaseValidator.build_datasetE  s    !"WXXrA   c                     U$ )zPreprocess an input batch. )r<   rK   s     r>   r   BaseValidator.preprocessI      rA   c                     U$ )zPostprocess the predictions.r   )r<   r   s     r>   r   BaseValidator.postprocessM  r   rA   c                     g)z2Initialize performance metrics for the YOLO model.Nr   )r<   rG   s     r>   r   BaseValidator.init_metricsQ      rA   c                     g)z.Update metrics based on predictions and batch.Nr   r<   r   rK   s      r>   r   BaseValidator.update_metricsU  r   rA   c                     g)z Finalize and return all metrics.Nr   r<   s    r>   r   BaseValidator.finalize_metricsY  r   rA   c                     0 $ )z0Return statistics about the model's performance.r   r   s    r>   r   BaseValidator.get_stats]  s    	rA   c                     g)zAGather statistics from all the GPUs during DDP training to GPU 0.Nr   r   s    r>   r   BaseValidator.gather_statsa  r   rA   c                     g)z-Print the results of the model's predictions.Nr   r   s    r>   r   BaseValidator.print_resultse  r   rA   c                     g)z"Get description of the YOLO model.Nr   r   s    r>   r   BaseValidator.get_desci  r   rA   c                     / $ )z8Return the metric keys used in YOLO training/validation.r   r   s    r>   metric_keysBaseValidator.metric_keysm  s	     	rA   c                 `    U[         R                   " 5       S.U R                  [        U5      '   g)z!Register plots for visualization.)r(   	timestampN)timer:   r   )r<   r   r(   s      r>   on_plotBaseValidator.on_plotr  s    *.TYY[!I

4:rA   c                     g)z(Plot validation samples during training.Nr   )r<   rK   nis      r>   r   BaseValidator.plot_val_samplesv  r   rA   c                     g)z,Plot YOLO model predictions on batch images.Nr   )r<   rK   r   r  s       r>   r   BaseValidator.plot_predictionsz  r   rA   c                     g)z#Convert predictions to JSON format.Nr   r   s      r>   pred_to_jsonBaseValidator.pred_to_json~  r   rA   c                     g)z9Evaluate and return JSON format of prediction statistics.Nr   )r<   r.   s     r>   r   BaseValidator.eval_json  r   rA   )r%   r*   r   r/   r(   r&   r)   r1   r2   r   r,   r0   r:   r4   r-   r3   r.   r'   r+   )NNNN)NN)F)N)"__name__
__module____qualname____firstlineno____doc__r?   r   r   ru   Tensorr   r   r}   r   r   r   r   r   r   r   r   r   r   r   r   r   propertyr	  r  r   r   r  r   __static_attributes__r   rA   r>   r   r   +   s    1f#IJ F FR lq)S!LL)S8=)SKP<<)Sdh)S	)SV/# /3 
`Y  JrA   r   )$r  r   r  pathlibr   r   r   ru   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,     sE   0        1 G 2 M M 0 ) l lY YrA   