Ë
    Fêñiì  ã                   óÐ   — 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 ‚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                 óR  — ddl }ddlm} |j                  ||| dœ«      j	                  |j                  «       j                  d«      «      }|j                  g d¢«      j                  «       }	ddddœ}
|||d	œ}t        j                  d
t        j                  |	g d¢¬«      |
|¬«      S )aL  Create and log a custom metric visualization table.

    This function crafts a custom metric visualization that mimics the behavior of the default wandb 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.
        x_title (str, optional): Label for the x-axis.
        y_title (str, optional): Label for the y-axis.

    Returns:
        (wandb.Object): A wandb object suitable for logging, showcasing the crafted metric visualization.
    r   N)ÚclassÚyÚxé   r   r
   r	   )r   r
   r	   )Útitlezx-axis-titlezy-axis-titlezwandb/area-under-curve/v0©ÚdataÚcolumns)ÚfieldsÚstring_fields)ÚpolarsÚpolars.selectorsÚ	selectorsÚ	DataFrameÚwith_columnsÚnumericÚroundÚselectÚrowsÚwbÚ
plot_tableÚTable)r   r
   Úclassesr   Úx_titleÚy_titleÚplÚcsÚdfr   r   r   s               ú`/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/ultralytics/utils/callbacks/wb.pyÚ_custom_tabler&      s•   € ó$ Ý!à	�‰ ¨a°aÑ8Ó	9×	FÑ	FÀrÇzÁzÃ|×GYÑGYÐZ[ÓG\Ó	]€BØ�9‰9Ò(Ó)×.Ñ.Ó0€Dà˜S¨7Ñ3€FØ#°WÈgÑV€MÜ�=‰=Ø#Ü
�‰�dÒ$7Ô8ØØ#ô	ð ó    c	                 ó"  — ddl }	|€g }|	j                  | d   | d   |«      j                  d«      }
|
j                  «       }|	j	                  |
| |	j                  |d¬«      «      j                  d«      j                  «       }|rot        j                  t        t        ||«      «      ||g¬«      }t        j                  j                  |t        j                  j                  ||||¬«      i«       yd	gt        |«      z  }t        |«      D ]Y  \  }}|j!                  |
«       |j!                  |	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 (C, N), where C is the number of classes.
        names (list, optional): Names of the classes corresponding to the y-axis data; length C.
        id (str, optional): Unique identifier for the logged data in wandb.
        title (str, optional): Title for the visualization plot.
        x_title (str, optional): Label for the x-axis.
        y_title (str, optional): Label for the y-axis.
        num_x (int, optional): Number of interpolated data points for visualization.
        only_mean (bool, optional): Flag to indicate if only the mean curve should be plotted.

    Notes:
        The function leverages the '_custom_table' function to generate the actual visualization.
    r   Néÿÿÿÿé   )Úaxisr   r   )r   ÚmeanF)Úcommit)ÚnumpyÚlinspacer   ÚtolistÚinterpr,   r   r   ÚlistÚzipÚrunÚlogÚplotÚlineÚlenÚ	enumerateÚextendr&   )r   r
   ÚnamesÚidr   r    r!   Únum_xÚ	only_meanÚnpÚx_newÚx_logÚy_logÚtabler   ÚiÚyis                    r%   Ú_plot_curverF   4   sR  € ó< ð €}ØˆØ�K‰K˜˜!™˜a ™e UÓ+×1Ñ1°!Ó4€Eð �L‰L‹N€EØ�I‰I�e˜Q §¡¨° Ó 2Ó3×9Ñ9¸!Ó<×CÑCÓE€EáÜ—‘œd¤3 u¨eÓ#4Ó5ÀÈÐ?QÔRˆÜ
�‰�
‰
�Eœ2Ÿ7™7Ÿ<™<¨¨w¸Àu˜<ÓMÐNÕOà�(œS ›ZÑ'ˆÜ˜q“\ò 	4‰EˆAˆrØ�L‰L˜ÔØ�L‰L˜Ÿ™ 5¨!¨RÓ0Ô1Ø�N‰N˜E !™H˜:¬¨E«
Ñ2Õ3ð	4ô 	�‰�”M %¨°¸ÀÈÓQÐRÐ[`Öar'   c           	      ó0  — | j                  «       j                  «       D ]u  \  }}|d   }t        j                  |«      |k7  sŒ$t        j
                  j                  |j                  t	        j                  t        |«      «      i|¬«       |t        |<   Œw y)aç  Log plots to WandB at a specific step if they haven't been logged already.

    This function checks each plot in the input dictionary against previously processed plots and logs new or updated
    plots to WandB at the specified step.

    Args:
        plots (dict): Dictionary of plots to log, where keys are plot names and values are dictionaries containing plot
            metadata including timestamps.
        step (int): The step/epoch at which to log the plots in the WandB run.

    Notes:
        The function uses a shallow copy of the plots dictionary to prevent modification during iteration.
        Plots are identified by their stem name (filename without extension).
        Each plot is logged as a WandB Image object.
    Ú	timestamp©ÚstepN)
ÚcopyÚitemsÚ_processed_plotsÚgetr   r4   r5   ÚstemÚImageÚstr)ÚplotsrJ   ÚnameÚparamsrH   s        r%   Ú
_log_plotsrU   i   sx   € ð  Ÿ
™
›×*Ñ*Ó,ò /‰ˆˆfØ˜;Ñ'ˆ	Ü×Ñ Ó%¨Ó2Ü�F‰F�J‰J˜Ÿ	™	¤2§8¡8¬C°«IÓ#6Ð7¸dˆJÔCØ%.Ô˜TÒ"ñ	/r'   c           
      óô  — t         j                  �sgddlm} ddlm} t        | j                  j                  «      j                  dd«      j                  dd«      } || j                  «      dz  d	z  }| j                  j                  xr |j                  «       }t        j                  | j                  j                  r/t        | j                  j                  «      j                  dd«      nd
|t        | j                  «      |r-|j                  «       j                  j!                  dd«      d   n#|› d|j#                  «       j%                  d«      › �|rdndt        | j                  «      ¬«       yy)z8Initialize and start wandb project if module is present.r   )Údatetime)ÚPathú/ú-ú Ú_r   z
latest-runÚUltralyticsé   z%Y%m%d_%H%M%SÚallowN)ÚprojectrS   Úconfigr<   ÚresumeÚdir)r   r4   rW   ÚpathlibrX   rQ   ÚargsrS   ÚreplaceÚsave_dirrb   ÚexistsÚinitr`   ÚvarsÚresolveÚsplitÚnowÚstrftime)ÚtrainerrW   rX   rS   Ú
latest_runÚresumings         r%   Úon_pretrain_routine_startrr   €   s   € ä�6‹6Ý%Ý ä�7—<‘<×$Ñ$Ó%×-Ñ-¨c°3Ó7×?Ñ?ÀÀSÓIˆÙ˜'×*Ñ*Ó+¨gÑ5¸ÑDˆ
Ø—<‘<×&Ñ&Ò>¨:×+<Ñ+<Ó+>ˆÜ
�‰ØCJÇ<Á<×CWÒCW”C˜Ÿ™×,Ñ,Ó-×5Ñ5°c¸3Ô?Ð]jØÜ˜Ÿ™Ó%áð ×!Ñ!Ó#×(Ñ(×.Ñ.¨s°AÓ6°qÒ9à�6˜˜8Ÿ<™<›>×2Ñ2°?ÓCÐDÐEÙ&‘7¨DÜ�G×$Ñ$Ó%ö		
ð r'   c                 ó¦  — t        | j                  | j                  dz   ¬«       t        | j                  j                  | j                  dz   ¬«       | j                  dk(  r7t        j
                  j                  t        | «      | j                  dz   ¬«       t        j
                  j                  | j                  | j                  dz   d¬«       y)zBLog training metrics and model information at the end of an epoch.é   rI   r   T)rJ   r-   N)	rU   rR   ÚepochÚ	validatorr   r4   r5   r   Úmetrics©ro   s    r%   Úon_fit_epoch_endry   •   sŠ   € äˆw�}‰} 7§=¡=°1Ñ#4Õ5Üˆw× Ñ ×&Ñ&¨W¯]©]¸QÑ->Õ?Ø‡}�}˜ÒÜ
�‰�
‰
Ô)¨'Ó2¸¿¹ÈÑ9Jˆ
ÔKÜ‡F�F‡J�Jˆw�‰ W§]¡]°QÑ%6¸t€JÕDr'   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)Úprefixrt   rI   N)	r   r4   r5   Úlabel_loss_itemsÚtlossru   ÚlrrU   rR   rx   s    r%   Úon_train_epoch_endr€   ž   s}   € ä‡F�F‡J�Jˆw×'Ñ'¨¯©¸gÐ'ÓFÈWÏ]É]Ð]^ÑM^€JÔ_Ü‡F�F‡J�Jˆw�z‰z §¡°Ñ 1€JÔ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¬«       | j                  j                  rÁt        | j                  j                  d	«      r¡t        | j                  j                  j                   | j                  j                  j"                  «      D ]V  \  }}|\  }}}}t%        ||t'        | j                  j                  j(                  j+                  «       «      d
|› �|||¬«       ŒX t        j                  j-                  «        y)zNSave the best model as an artifact and log final plots at the end of training.rt   rI   ÚmodelÚrun_Ú_model)ÚtyperS   Úbest)ÚaliasesÚcurves_resultszcurves/)r;   r<   r   r    r!   N)rU   rv   rR   ru   r   ÚArtifactr4   r<   r†   rh   Úadd_fileÚlog_artifactre   Úhasattrrw   r3   Úcurvesrˆ   rF   r2   r;   ÚvaluesÚfinish)ro   ÚartÚ
curve_nameÚcurve_valuesr   r
   r    r!   s           r%   Úon_train_endr“   ¦   sU  € äˆw× Ñ ×&Ñ&¨W¯]©]¸QÑ->Õ?Üˆw�}‰} 7§=¡=°1Ñ#4Õ5Ü
�+‰+˜7¨4´·±·	±	¨{¸&Ð)AÔ
B€CØ‡|�|×ÑÔØ�‰�W—\‘\Ô"Ü
�‰×Ñ˜C¨&¨ÐÔ2à‡|�|×Òœg g×&7Ñ&7×&?Ñ&?ÐAQÔRÜ(+¨G×,=Ñ,=×,EÑ,E×,LÑ,LÈg×N_ÑN_×NgÑNg×NvÑNvÓ(wò 
	Ñ$ˆJ˜Ø%1Ñ"ˆAˆq�'˜7ÜØØÜ˜7×,Ñ,×4Ñ4×:Ñ:×AÑAÓCÓDØ˜Z˜LÐ)Ø ØØöð
	ô ‡F�F‡M�M…Or'   )rr   r€   ry   r“   )úPrecision Recall CurveÚRecallÚ	Precision)Nzprecision-recallr”   r•   r–   éd   F)Úultralytics.utilsr   r   Úultralytics.utils.torch_utilsr   r   r   rŒ   rM   ÚImportErrorÚAssertionErrorr&   rF   rU   rr   ry   r€   r“   Ú	callbacks© r'   r%   ú<module>rž      sÈ   ð÷ 6Ý @ð	ÙÐÐØ�GÑ Ñ$Ð$Ð$Ûá�2�}Ô%Ð%Ð%ØÐóðJ ØØ
"ØØØ
Øó2bòj/ò.
ò*Eò:òñ> 
ð &?Ø0Ø,Ø$ñ	ñ 
ð 
ñ 
øða 	�^Ð$ò Ø	‚Bðús   �A Á	A%Á$A%