a
    yf                  	   @   s   d dl mZmZmZ zBerJ ed du s.J d dlZd dlmZ eedsPJ daW n e	e
fyp   dZY n0 dddZdd
dZdd Zdd Zdd Zdd Zdd Zdd Zereeeeedni ZdS )    )LOGGERSETTINGSTESTS_RUNNINGneptuneTN)File__version__c                 C   s,   t r(|  D ]\}}t | j||d qdS )/Log scalars to the NeptuneAI experiment logger.)valuestepN)runitemsappend)Zscalarsr
   kv r   _/var/www/html/django/DPS/env/lib/python3.9/site-packages/ultralytics/utils/callbacks/neptune.py_log_scalars   s    r    c                 C   s6   t r2|  D ]$\}}t | d|  t| qdS )r   /N)r   r   uploadr   )Z	imgs_dictgroupr   r   r   r   r   _log_images   s    r   c                 C   sd   ddl m} ddlm} ||}| }|jg dddg g d}|| td|   	| dS )z
    Log plots to the NeptuneAI experiment logger.

    Args:
        title (str): Title of the plot.
        plot_path (PosixPath | str): Path to the saved image file.
    r   N)r   r      r   Fauto)ZframeonZaspectZxticksZytickszPlots/)
Zmatplotlib.imageimageZmatplotlib.pyplotZpyplotZimreadZfigureZadd_axesZimshowr   r   )title	plot_pathZmpimgZpltZimgZfigaxr   r   r   	_log_plot!   s    

r   c              
   C   sx   z>t j| jjpd| jjdgdadd t| j D td< W n4 tyr } zt	
d|  W Y d}~n
d}~0 0 dS )z<Callback function called before the training routine starts.ZYOLOv8)projectnametagsc                 S   s"   i | ]\}}||d u rdn|qS )Nr   r   ).0r   r   r   r   r   
<dictcomp>8       z-on_pretrain_routine_start.<locals>.<dictcomp>zConfiguration/HyperparametersuX   WARNING ⚠️ NeptuneAI installed but not initialized correctly, not logging this run. N)r   Zinit_runargsr   r    r   varsr   	Exceptionr   warning)trainerer   r   r   on_pretrain_routine_start3   s
     r+   c                 C   sX   t | j| jdd| jd  t | j| jd  | jdkrTtdd | jdD d dS )	z7Callback function called at end of each training epoch.train)prefixr   c                 S   s   i | ]}|j t|qS r   stemstrr"   fr   r   r   r#   B   r$   z&on_train_epoch_end.<locals>.<dictcomp>ztrain_batch*.jpgZMosaicN)r   Zlabel_loss_itemsZtlossepochlrr   save_dirglobr)   r   r   r   on_train_epoch_end=   s    
r8   c                 C   s<   t r&| jdkr&ddlm} || t d< t| j| jd  dS )z>Callback function called at end of each fit (train+val) epoch.r   )model_info_for_loggerszConfiguration/Modelr   N)r   r3   Zultralytics.utils.torch_utilsr9   r   Zmetrics)r)   r9   r   r   r   on_fit_epoch_endE   s    r:   c                 C   s$   t r tdd | jdD d dS )z3Callback function called at end of each validation.c                 S   s   i | ]}|j t|qS r   r.   r1   r   r   r   r#   R   r$   zon_val_end.<locals>.<dictcomp>zval*.jpgZ
ValidationN)r   r   r5   r6   )	validatorr   r   r   
on_val_endN   s    r<   c                    s~   t rzdddgdd dD } fdd|D }|D ]}t|j|d	 q2t d
 jjpX jj d jj  tt	 j dS )z,Callback function called at end of training.zresults.pngzconfusion_matrix.pngzconfusion_matrix_normalized.pngc                 s   s   | ]}| d V  qdS )z
_curve.pngNr   )r"   xr   r   r   	<genexpr>]   r$   zon_train_end.<locals>.<genexpr>)ZF1ZPRPRc                    s$   g | ]} j |  r j | qS r   )r5   existsr1   r7   r   r   
<listcomp>_   r$   z on_train_end.<locals>.<listcomp>)r   r   zweights/r   N)
r   r   r/   r%   r    taskbestr   r   r0   )r)   filesr2   r   r7   r   on_train_endU   s    rF   )r+   r8   r:   r<   rF   )r   )r   )Zultralytics.utilsr   r   r   r   Zneptune.typesr   hasattrr   ImportErrorAssertionErrorr   r   r   r+   r8   r:   r<   rF   	callbacksr   r   r   r   <module>   s6   



	