
    v-jlH                        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mZ ddlmZ ddlmZ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&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
                          yolo26n_executorch_model   # ExecuTorch
                          yolo26n_axelera_model      # Axelera AI
                          yolo26n_deepx_model        # DeepX
                          yolo26n_qnn_model          # Qualcomm QNN
    )annotationsN)Path)get_cfgget_save_dir)check_cls_datasetcheck_det_dataset convert_ndjson_to_yolo_if_needed)AutoBackend)
LOCAL_RANKLOGGERRANKTQDM	callbackscolorstremojis)check_imgsz)Profile)attempt_compileselect_devicesmart_inference_modetorch_distributed_zero_first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+   r9   r*   r   r)   s         \/var/www/html/banglarbhumi/venv/lib/python3.11/site-packages/ultralytics/engine/validator.py__init__zBaseValidator.__init__j   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_        |                                 nt1           j        j	                                      d          r|t5          j        d           t9          j                    t          |d          rO j        j         j        j        |_        |j        r+|                     j        j          j        j!        	           tE          tF                    5  tI           j        j                   j        _        ddd           n# 1 swxY w Y   tK          |p j        j	        tL          d
k    rtO           j        j                  nt          j        dtL                     j        j(         j        j         j        j                  }|j         _        |j)         j        _        |j*        |j+        }}|dk    }tY           j        j-        |          }|dvrkt]          |dd          sZ|j/        0                    dd           j        _1        t5          j2        d j        j1         d j        j1         d| d| d	           t1           j        j                  3                    dd          d
         dv rti           j        j                   _        nr j        j5        dk    r+tm           j        j         j        j7                   _        n7tq          ts          d j        j         d j        j5         d                     j        j        dv rd  j        _:        |s#t]          |dd          r|d!k    sd j        _;        |j*         _*         j<        pA =                     j        0                     j        j7                   j        j1                   _<        |                                  j        j
        rt}          | j                  }|?                    |rdn j        j1         j        d"         ||f#            @                    d$           t           j                  t           j                  t           j                  t           j                  f}t           j<         C                                t           j<                  %          }	 E                    t          |                     g  _G        t          |	          D ]\  }
} @                    d&           |
 _I        |d          5   J                    |          }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   K                    |          }ddd           n# 1 swxY w Y    L                    ||            j        j        r<|
d*k     r6tL          d+v r- M                    ||
            N                    |||
            @                    d,           i } O                                 tL          d+v r P                                }t          t           jS        T                                 fd-|D                                  _S         U                                  V                                  @                    d.            j         r|                                  j        W                                X                                }|jY        dk    r&t          j[        |d t          j\        j]        /           tL          d k    rdS i ||^                    |_                                t           j<                  z  d01          }d2 |`                                D             S tL          d k    r|S t5          j2         d3j+        t           jS        b                                                       j        jc        r jG        rt          t1           je        d4z            d5d67          5 }t5          j2        d8|jf         d9           t          jh         jG        |           ddd           n# 1 swxY w Y    i                    |          } j        j        s j        jc        r*t5          j2        d:t          d; je                              |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,   >   rP   torchscriptdynamicFbatchzSetting batch=z input of shape (z, 3, z, ).>   ymlyamlclassify)splitz	Dataset 'z' for task=u    not found ❌>   rF   mpsr   imxchannels)r>   on_val_start)desctotalon_val_batch_startimg)augment      >   r   rK   on_val_batch_endc              3  `   K   | ](}|j         t          j        j                  z  d z  V  )dS )g     @@N)tlenr+   dataset).0xrA   s     rB   	<genexpr>z)BaseValidator.__call__.<locals>.<genexpr>   s>      5k5kcdacC@W<X<X6X[^6^5k5k5k5k5k5krD   
on_val_end)dstopval)prefixc                P    i | ]#\  }}|t          t          |          d           $S )   )roundfloat)rj   kvs      rB   
<dictcomp>z*BaseValidator.__call__.<locals>.<dictcomp>  s.    FFFdaAuU1XXq))FFFrD   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)kr0   r*   rb   r.   r-   typeamphalfemarM   compilehasattrrG   ru   torch
zeros_like
loss_itemsr!   r?   stopperpossible_stopepochepochsevalstrendswithr   warningr   add_integration_callbacksrH   set_head_attrrI   rJ   r   r   r	   r
   r   r   rN   rO   r,   formatr   r>   getattrmetadatagetrS   inforsplitr   r=   r   rY   FileNotFoundErrorr   workersrectr+   get_dataloaderr   warmuprun_callbacksr   r   get_descrh   init_metricsr   r7   	enumerater/   r   r"   update_metricsplot_val_samplesplot_predictionsgather_stats	get_statsdictzipr8   keysfinalize_metricsprint_resultsclonedetach
world_sizedistreduceReduceOpAVGlabel_loss_itemsrF   itemstuplevalues	save_jsonopenr9   namejsondump	eval_jsonr   )rA   trainerrM   rb   r,   fmtrP   r>   dtbarr/   rS   predsr3   r!   resultsfs   `                rB   __call__zBaseValidator.__call__   s
     t+)#;T]):= 8	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-j99 R R!A$).!Q!Q	R R R R R R R R R R R R R R R.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Lsl    $H00H47H4<YY"	%Y"	1ZZ	Z	$2[""[&	)[&	5\\	\	78g;;g?g?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'   rK   )return_index)dtyper.   )npzerosshaper6   astyper   rF   numpyr   tolistscipyanyoptimizelinear_sum_assignmentnonzeroarrayTargsortuniqueintr   tensorr.   )rA   r   r   r   r   correctcorrect_classi	thresholdr   cost_matrix
labels_idxdetections_idxvalidmatchess                  rB   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RrD   eventr   c                F    | j         |                             |           dS )z1Append the given callback to the specified event.N)r   appendrA   r   callbacks      rB   add_callbackzBaseValidator.add_callbackG  s#    u$$X.....rD   c                X    | j                             |g           D ]} ||            dS )z4Run all callbacks associated with a specified event.N)r   r   r   s      rB   r   zBaseValidator.run_callbacksK  s<    **5"55 	 	HHTNNNN	 	rD   c                     t          d          )z1Get data loader from dataset path and batch size.z:get_dataloader function not implemented for this validatorNotImplementedError)rA   dataset_path
batch_sizes      rB   r   zBaseValidator.get_dataloaderP  s    !"^___rD   c                     t          d          )zBuild dataset from image path.z3build_dataset function not implemented in validatorr   )rA   img_paths     rB   build_datasetzBaseValidator.build_datasetT  s    !"WXXXrD   c                    |S )zPreprocess an input batch. )rA   rS   s     rB   r   zBaseValidator.preprocessX      rD   c                    |S )zPostprocess the predictions.r   )rA   r   s     rB   r"   zBaseValidator.postprocess\  r   rD   c                    dS )z2Initialize performance metrics for the YOLO model.Nr   )rA   rM   s     rB   r   zBaseValidator.init_metrics`      rD   c                    dS )z.Update metrics based on predictions and batch.Nr   rA   r   rS   s      rB   r   zBaseValidator.update_metricsd  r   rD   c                    dS )z Finalize and return all metrics.Nr   rA   s    rB   r   zBaseValidator.finalize_metricsh  r   rD   c                    i S )z0Return statistics about the model's performance.r   r   s    rB   r   zBaseValidator.get_statsl  s    	rD   c                    dS )zAGather statistics from all the GPUs during DDP training to GPU 0.Nr   r   s    rB   r   zBaseValidator.gather_statsp  r   rD   c                    dS )z-Print the results of the model's predictions.Nr   r   s    rB   r   zBaseValidator.print_resultst  r   rD   c                    dS )z"Get description of the YOLO model.Nr   r   s    rB   r   zBaseValidator.get_descx  r   rD   c                    g S )z8Return the metric keys used in YOLO training/validation.r   r   s    rB   metric_keyszBaseValidator.metric_keys|  s	     	rD   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.r|   Nc              3  t   K   | ]2}|                     d           pi                      d          k    V  3dS )r-   r|   N)r   )rj   rw   	plot_types     rB   rl   z(BaseValidator.on_plot.<locals>.<genexpr>  sF      iiPQaeeFmm1r66v>>)KiiiiiirD   )r-   	timestamp)r   r   r?   r   timer   )rA   r   r-   r  s      @rB   on_plotzBaseValidator.on_plot  s    (,6DHHV$$$$	 	iiiiUYU_UfUfUhUhiiiii 	F*.TY[[!I!I
4::rD   c                    dS )z(Plot validation samples during training.Nr   )rA   rS   nis      rB   r   zBaseValidator.plot_val_samples  r   rD   c                    dS )z,Plot YOLO model predictions on batch images.Nr   )rA   rS   r   r  s       rB   r   zBaseValidator.plot_predictions  r   rD   c                    dS )z#Convert predictions to JSON format.Nr   r   s      rB   pred_to_jsonzBaseValidator.pred_to_json  r   rD   c                    dS )z9Evaluate and return JSON format of prediction statistics.Nr   )rA   r3   s     rB   r   zBaseValidator.eval_json  r   rD   )NNNN)r   r   )NN)F)
r   r   r   r   r   r   r   r   r   r   )r   r   )N)__name__
__module____qualname____doc__rC   r   r   r   r   r   r   r   r   r"   r   r   r   r   r   r   r   propertyr  r  r   r   r  r   r   rD   rB   r   r   7   s       0 0d"I "I "I "I "IH M M M M` lq'S 'S 'S 'S 'SR/ / / /   
` ` `Y Y Y                     XJ J J J          rD   r   ))r  
__future__r   r   r
  pathlibr   r   r   r   torch.distributeddistributedr   ultralytics.cfgr   r   ultralytics.data.utilsr   r   r	   ultralytics.nn.autobackendr
   ultralytics.utilsr   r   r   r   r   r   r   ultralytics.utils.checksr   ultralytics.utils.opsr   ultralytics.utils.torch_utilsr   r   r   r   r   r   r   rD   rB   <module>r"     s   8 # " " " " "                          1 1 1 1 1 1 1 1 i i i i i i i i i i 2 2 2 2 2 2 Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y Y 0 0 0 0 0 0 ) ) ) ) ) )             _ _ _ _ _ _ _ _ _ _rD   