
    FPhj                         d dl mZmZ d dlmZ 	 erJ ed   du sJ d dlZ eed      sJ d dlZ	d dl
Zi ZddZ	 	 	 	 	 	 	 ddZd	 Zd
 Zd Zd Zd ZereeeedZyi Zy# eef$ r dZY 6w xY w)    )SETTINGSTESTS_RUNNING)model_info_for_loggerswandbTN__version__c                     t        j                  ||| d      j                  d      }dddd}|||d}t        j                  dt        j
                  |	      ||
      S )a  
    Create and log a custom metric visualization to wandb.plot.pr_curve.

    This function crafts a custom metric visualization that mimics the behavior of wandb's default precision-recall curve
    while allowing for enhanced customization. The visual metric is useful for monitoring model performance across different classes.

    Args:
        x (List): Values for the x-axis; expected to have length N.
        y (List): Corresponding values for the y-axis; also expected to have length N.
        classes (List): Labels identifying the class of each point; length N.
        title (str, optional): Title for the plot; defaults to 'Precision Recall Curve'.
        x_title (str, optional): Label for the x-axis; defaults to 'Recall'.
        y_title (str, optional): Label for the y-axis; defaults to 'Precision'.

    Returns:
        (wandb.Object): A wandb object suitable for logging, showcasing the crafted metric visualization.
    )classyx   r   r
   r	   )r   r
   r	   )titlezx-axis-titlezy-axis-titlezwandb/area-under-curve/v0)	dataframe)fieldsstring_fields)pd	DataFrameroundwb
plot_tableTable)	r   r
   classesr   x_titley_titledfr   r   s	            iC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\ultralytics/utils/callbacks/wb.py_custom_tabler      sb    $ 
aa8	9	?	?	BBS73F#WgVM==4B/ &'46 6    c	                 :   |g }t        j                  | d   | d   |      j                  d      }	|	j                         }
t        j                  |	| t        j
                  |d            j                  d      j                         }|rot        j                  t        t        |
|            ||g      }t        j                  j                  |t        j                  j                  ||||      i       yd	gt        |
      z  }t        |      D ]]  \  }}|
j!                  |	       |j!                  t        j                  |	| |             |j!                  ||   gt        |	      z         _ t        j                  |t#        |
|||||      id
       y)a  
    Log a metric curve visualization.

    This function generates a metric curve based on input data and logs the visualization to wandb.
    The curve can represent aggregated data (mean) or individual class data, depending on the 'only_mean' flag.

    Args:
        x (np.ndarray): Data points for the x-axis with length N.
        y (np.ndarray): Corresponding data points for the y-axis with shape CxN, where C represents the number of classes.
        names (list, optional): Names of the classes corresponding to the y-axis data; length C. Defaults to an empty list.
        id (str, optional): Unique identifier for the logged data in wandb. Defaults to 'precision-recall'.
        title (str, optional): Title for the visualization plot. Defaults to 'Precision Recall Curve'.
        x_title (str, optional): Label for the x-axis. Defaults to 'Recall'.
        y_title (str, optional): Label for the y-axis. Defaults to 'Precision'.
        num_x (int, optional): Number of interpolated data points for visualization. Defaults to 100.
        only_mean (bool, optional): Flag to indicate if only the mean curve should be plotted. Defaults to True.

    Note:
        The function leverages the '_custom_table' function to generate the actual visualization.
    Nr      )axisr   )datacolumns)r   meanF)commit)nplinspacer   tolistinterpr$   r   r   listziprunlogplotlinelen	enumerateextendr   )r   r
   namesidr   r   r   num_x	only_meanx_newx_logy_logtabler   iyis                   r   _plot_curver=   1   sF   < }KK!aeU+11!4E LLNEIIeQ 2399!<CCEEd3ue#45?QR


E277<<wu<MNO(SZ'q\EArLLLL5!R01NNE!H:E
23 " 	M%QR[`ar   c           	         | j                         D ]u  \  }}|d   }t        j                  |      |k7  s$t        j                  j                  |j                  t        j                  t        |            i|       |t        |<   w y)z_Logs plots from the input dictionary if they haven't been logged already at the specified step.	timestampstepN)	items_processed_plotsgetr   r,   r-   stemImagestr)plotsrA   nameparamsr?   s        r   
_log_plotsrK   c   sh    f;'	%2FFJJ		288CI#67dJC%.T"	 &r   c                     t         j                  xsY t        j                  | j                  j                  xs d| j                  j
                  t        | j                               y y)z0Initiate and start project if module is present.YOLOv8)projectrI   configN)r   r,   initargsrN   rI   varstrainers    r   on_pretrain_routine_startrU   l   sD    FFrbgggll22>hW\\EVEV_cdkdpdp_qrrr   c                    t         j                  j                  | j                  | j                  dz          t        | j                  | j                  dz          t        | j                  j                  | j                  dz          | j                  dk(  r8t         j                  j                  t        |       | j                  dz          yy)zCLogs training metrics and model information at the end of an epoch.   r@   r   N)	r   r,   r-   metricsepochrK   rH   	validatorr   rS   s    r   on_fit_epoch_endr[   q   s    FFJJwW]]Q%6J7w}}7==1#45w  &&W]]Q->?}}


)'29J
K r   c                 n   t         j                  j                  | j                  | j                  d      | j
                  dz          t         j                  j                  | j                  | j
                  dz          | j
                  dk(  r%t        | j                  | j
                  dz          yy)z>Log metrics and save images at the end of each training epoch.train)prefixrW   r@   N)	r   r,   r-   label_loss_itemstlossrY   lrrK   rH   rS   s    r   on_train_epoch_endrb   z   s}    FFJJw''g'FW]]]^M^J_FFJJwzz 1J2}}7==w}}q'89 r   c           
      :   t        | j                  j                  | j                  dz          t        | j                  | j                  dz          t	        j
                  ddt        j                  j                   d      }| j                  j                         r=|j                  | j                         t        j                  j                  |dg       t        | j                  j                  j                  | j                  j                  j                        D ]V  \  }}|\  }}}}t!        ||t#        | j                  j                  j$                  j'                               d	| |||
       X t        j                  j)                          y)z6Save the best model as an artifact at end of training.rW   r@   modelrun__model)typerI   best)aliaseszcurves/)r3   r4   r   r   r   N)rK   rZ   rH   rY   r   Artifactr,   r4   rh   existsadd_filelog_artifactr+   rX   curvescurves_resultsr=   r*   r3   valuesfinish)rT   art
curve_namecurve_valuesr   r
   r   r   s           r   on_train_endru      s.   w  &&W]]Q->?w}}7==1#45
++74		{&)A
BC||W\\"
C&2$'(9(9(A(A(H(H'J[J[JcJcJrJr$s 
L!-1gww((0066==?@%	
 %t FFMMOr   )rU   rb   r[   ru   )Precision Recall CurveRecall	Precision)Nzprecision-recallrv   rw   rx   d   F)ultralytics.utilsr   r   ultralytics.utils.torch_utilsr   r   r   hasattrnumpyr&   pandasr   rC   ImportErrorAssertionErrorr   r=   rK   rU   r[   rb   ru   	callbacks r   r   <module>r      s    6 @G$$$2}%%%6: %. #/bd/s
L:4 &( ";,( 	"	 .0	 
M 	^$ 	Bs   &A! !	A-,A-