o
    Whf                  	   @   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 W n eefy6   dZY nw d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rqeeeeedZdS i ZdS )    )LOGGERSETTINGSTESTS_RUNNINGclearmlTN)Task__version__Debug Samplestitlereturnc                 C   s|   ddl }t  }r:| D ]/}| r9|d|j}|r#t| d nd}| j	||j
| dt||d qdS dS )z
    Log files (images) as debug samples in the ClearML task.

    Args:
        files (List[Path]): A list of file paths in PosixPath format.
        title (str): A title that groups together images with the same values.
    r   Nz_batch(\d+) )r	   series
local_path	iteration)rer   current_taskexistssearchnameintgroups
get_loggerreport_imagereplacegroupstr)filesr	   r   taskfitr    r   W/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/utils/callbacks/clearml.py_log_debug_samples   s   r!   	plot_pathc                 C   sj   ddl m} ddlm} ||}| }|jg dddg g d}|| t	 
 j| d|dd dS )	z
    Log an image as a plot in the plot section of ClearML.

    Args:
        title (str): The title of the plot.
        plot_path (str): The path to the saved image file.
    r   N)r   r      r#   Fauto)frameonaspectxticksyticksr   )r	   r   figurereport_interactive)matplotlib.imageimagematplotlib.pyplotpyplotimreadr)   add_axesimshowr   r   r   report_matplotlib_figure)r	   r"   mpimgpltimgfigaxr   r   r    	_log_plot%   s   


r8   c              
   C   s   zDt   }rddlm} ddlm} |d |d nt j| jj	p%d| jj
dgdddddd	}td
 |jt| jdd W dS  ty_ } ztd|  W Y d}~dS d}~ww )zTRuns at start of pretraining routine; initializes and connects/logs task to ClearML.r   )PatchPyTorchModelIO)PatchedMatplotlibNUltralyticsTF)pytorch
matplotlib)project_name	task_nametags
output_urireuse_last_task_idauto_connect_frameworkszClearML Initialized a new task. If you want to run remotely, please add clearml-init and connect your arguments before initializing YOLO.General)r   zGClearML installed but not initialized correctly, not logging this run. )r   r   'clearml.binding.frameworks.pytorch_bindr9   clearml.binding.matplotlib_bindr:   update_current_taskinitargsprojectr   r   warningconnectvars	Exception)trainerr   r9   r:   er   r   r    on_pretrain_routine_start:   s,   

rQ   c                 C   s   t   }rG| jdkrtt| jdd | j| jdd	 D ]\}}|
 jd||| jd q | j	 D ]\}}|
 jd||| jd q6dS dS )	z^Logs debug samples for the first epoch of YOLO training and reports current training progress.r#   ztrain_batch*.jpgMosaictrain)prefixr   lrN)r   r   epochr!   sortedsave_dirgloblabel_loss_itemstlossitemsr   report_scalarrV   )rO   r   kvr   r   r    on_train_epoch_endW   s   
ra   c                 C   s   t   }rG| jdd| j| jd | j D ]\}}| jd||| jd q| jdkrIddlm	} ||  D ]\}}| 
|| q:dS dS dS )z;Reports model information to logger at the end of an epoch.z
Epoch Time)r	   r   valuer   valrU   r   )model_info_for_loggersN)r   r   r   r^   
epoch_timerW   metricsr]   ultralytics.utils.torch_utilsrd   report_single_value)rO   r   r_   r`   rd   r   r   r    on_fit_epoch_endd   s   
ri   c                 C   s&   t  rtt| jdd dS dS )z9Logs validation results including labels and predictions.zval*.jpg
ValidationN)r   r   r!   rX   rY   rZ   )	validatorr   r   r    
on_val_endt   s   rl   c                    s   t   }rJdddgdd dD } fdd|D }|D ]	}t|j|d	 q jjj D ]\}}| 	|| q.|j
t j jjd
d dS dS )z5Logs final model and its name on training completion.zresults.pngzconfusion_matrix.pngzconfusion_matrix_normalized.pngc                 s   s    | ]}| d V  qdS )z
_curve.pngNr   ).0xr   r   r    	<genexpr>   s    zon_train_end.<locals>.<genexpr>)F1PRPRc                    s$   g | ]} j |  r j | qS r   )rY   r   )rm   r   rO   r   r    
<listcomp>   s   $ z on_train_end.<locals>.<listcomp>)r	   r"   F)
model_path
model_nameauto_delete_fileN)r   r   r8   stemrk   rf   results_dictr]   r   rh   update_output_modelr   bestrI   r   )rO   r   r   r   r_   r`   r   rt   r    on_train_end{   s   r}   )rQ   ra   ri   rl   r}   )r   )r
   N)ultralytics.utilsr   r   r   r   r   hasattrImportErrorAssertionErrorr   r!   r8   rQ   ra   ri   rl   r}   	callbacksr   r   r   r    <module>   s8   




	