o
    Wh9                  	   @   s
  d dl mZmZmZ z!erJ ed du sJ d dlZd dlmZ eeds(J daW n e	e
fy8   dZY nw d deded	dfd
dZd!deded	dfddZdeded	dfddZd"ddZd"ddZd"ddZd"ddZd"ddZereeeeedZdS i ZdS )#    )LOGGERSETTINGSTESTS_RUNNINGneptuneTN)File__version__scalarsstepreturnc                 C   s0   t r|  D ]\}}t | j||d qdS dS )a@  
    Log scalars to the NeptuneAI experiment logger.

    Args:
        scalars (dict): Dictionary of scalar values to log to NeptuneAI.
        step (int): The current step or iteration number for logging.

    Examples:
        >>> metrics = {"mAP": 0.85, "loss": 0.32}
        >>> _log_scalars(metrics, step=100)
    )valuer	   N)runitemsappend)r   r	   kv r   W/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/utils/callbacks/neptune.py_log_scalars   s
   r    	imgs_dictgroupc                 C   s:   t r|  D ]\}}t | d|  t| qdS dS )a  
    Log images to the NeptuneAI experiment logger.

    This function logs image data to Neptune.ai when a valid Neptune run is active. Images are organized
    under the specified group name.

    Args:
        imgs_dict (dict): Dictionary of images to log, with keys as image names and values as image data.
        group (str, optional): Group name to organize images under in the Neptune UI.

    Examples:
        >>> # Log validation images
        >>> _log_images({"val_batch": img_tensor}, group="validation")
    /N)r   r   uploadr   )r   r   r   r   r   r   r   _log_images&   s
   r   title	plot_pathc                 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 (str): Path to the saved image file.
    r   N)r   r      r   Fauto)frameonaspectxticksytickszPlots/)
matplotlib.imageimagematplotlib.pyplotpyplotimreadfigureadd_axesimshowr   r   )r   r   mpimgpltimg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 dS  ty; } zt	
d|  W Y d}~dS d}~ww )z<Callback function called before the training routine starts.Ultralytics)projectnametagsc                 S   s"   i | ]\}}||d u rdn|qS )Nr   r   ).0r   r   r   r   r   
<dictcomp>U   s   " z-on_pretrain_routine_start.<locals>.<dictcomp>zConfiguration/HyperparameterszINeptuneAI installed but not initialized correctly, not logging this run. N)r   init_runargsr1   r2   r   varsr   	Exceptionr   warning)trainerer   r   r   on_pretrain_routine_startL   s   
"r=   c                 C   s\   t | j| jdd| jd  t | j| jd  | jdkr,tdd | jdD d dS dS )	z7Callback function called at end of each training epoch.train)prefixr   c                 S      i | ]}|j t|qS r   stemstrr4   fr   r   r   r5   _       z&on_train_epoch_end.<locals>.<dictcomp>ztrain_batch*.jpgMosaicN)r   label_loss_itemstlossepochlrr   save_dirglobr;   r   r   r   on_train_epoch_endZ   s
   
 rO   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   rJ   ultralytics.utils.torch_utilsrP   r   metrics)r;   rP   r   r   r   on_fit_epoch_endb   s   rS   c                 C   s(   t rtdd | jdD d dS dS )z3Callback function called at end of each validation.c                 S   r@   r   rA   rD   r   r   r   r5   o   rF   zon_val_end.<locals>.<dictcomp>zval*.jpg
ValidationN)r   r   rL   rM   )	validatorr   r   r   
on_val_endk   s    rV   c                    s   t r?dddgdd dD } fdd|D }|D ]	}t|j|d	 qt d
 jjp, jj d jj  tt	 j dS 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   )r4   xr   r   r   	<genexpr>z   s    zon_train_end.<locals>.<genexpr>)F1PRPRc                    s$   g | ]} j |  r j | qS r   )rL   existsrD   rN   r   r   
<listcomp>|   s   $ z on_train_end.<locals>.<listcomp>)r   r   zweights/r   N)
r   r/   rB   r7   r2   taskbestr   r   rC   )r;   filesrE   r   rN   r   on_train_endr   s   8rb   )r=   rO   rS   rV   rb   )r   )r   )r
   N)ultralytics.utilsr   r   r   r   neptune.typesr   hasattrr   ImportErrorAssertionErrordictintr   rC   r   r/   r=   rO   rS   rV   rb   	callbacksr   r   r   r   <module>   s<   



	
	