a
    yf#                  	   @   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sPJ W n eefyl   dZY n0 dddd	d
Z	dd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clearmlTN)Task__version__Debug Samples)returnc                 C   sx   ddl }t  }rt| D ]Z}| r|d|j}|rFt| d nd}| j	||j
| dt||d qdS )z
    Log files (images) as debug samples in the ClearML task.

    Args:
        files (list): A list of file paths in PosixPath format.
        title (str): A title that groups together images with the same values.
    r   Nz_batch(\d+) )titleseriesZ
local_path	iteration)rer   current_taskexistssearchnameintgroups
get_loggerZreport_imagereplacegroupstr)filesr   r   taskfitr    r   _/var/www/html/django/DPS/env/lib/python3.9/site-packages/ultralytics/utils/callbacks/clearml.py_log_debug_samples   s    r   c                 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)ZframeonZaspectZxticksZyticksr
   )r   r   figureZreport_interactive)Zmatplotlib.imageimageZmatplotlib.pyplotZpyplotZimreadr"   Zadd_axesZimshowr   r   r   Zreport_matplotlib_figure)r   	plot_pathZmpimgZpltZimgZfigaxr   r   r   	_log_plot%   s    

r&   c              
   C   s   zt   }r<ddlm} ddlm} |d |d n4t j| jj	pJd| jj
dgdddddd	}td
 |jt| jdd W n4 ty } ztd|  W Y d}~n
d}~0 0 dS )zURuns at start of pretraining routine; initializes and connects/ logs task to ClearML.r   )PatchPyTorchModelIO)PatchedMatplotlibNZYOLOv8TF)ZpytorchZ
matplotlib)project_nameZ	task_nametagsZ
output_uriZreuse_last_task_idZauto_connect_frameworkszClearML Initialized a new task. If you want to run remotely, please add clearml-init and connect your arguments before initializing YOLO.ZGeneral)r   uV   WARNING ⚠️ ClearML installed but not initialized correctly, not logging this run. )r   r   Z'clearml.binding.frameworks.pytorch_bindr'   Zclearml.binding.matplotlib_bindr(   Zupdate_current_taskinitargsprojectr   r   warningconnectvars	Exception)trainerr   r'   r(   er   r   r   on_pretrain_routine_start:   s(    

r4   c                 C   s   t   }r| 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 qldS )	z]Logs debug samples for the first epoch of YOLO training and report current training progress.r    ztrain_batch*.jpgZMosaictrain)prefixr   lrN)r   r   epochr   sortedsave_dirglobZlabel_loss_itemsZtlossitemsr   report_scalarr8   )r2   r   kvr   r   r   on_train_epoch_endW   s    
rA   c                 C   s   t   }r| jdd| j| jd | j D ] \}}| jd||| jd q0| jdkrddlm	} ||  D ]\}}| 
|| qtdS )z;Reports model information to logger at the end of an epoch.z
Epoch Time)r   r   valuer   valr7   r   )model_info_for_loggersN)r   r   r   r>   Z
epoch_timer9   metricsr=   Zultralytics.utils.torch_utilsrD   report_single_value)r2   r   r?   r@   rD   r   r   r   on_fit_epoch_endd   s    
rG   c                 C   s"   t  rtt| jdd dS )z9Logs validation results including labels and predictions.zval*.jpgZ
ValidationN)r   r   r   r:   r;   r<   )	validatorr   r   r   
on_val_endt   s    rI   c                    s   t   }rd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 )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>       zon_train_end.<locals>.<genexpr>)ZF1ZPRPRc                    s$   g | ]} j |  r j | qS r   )r;   r   )rJ   r   r2   r   r   
<listcomp>   rM   z on_train_end.<locals>.<listcomp>)r   r$   F)Z
model_pathZ
model_nameZauto_delete_fileN)r   r   r&   stemrH   rE   Zresults_dictr=   r   rF   Zupdate_output_modelr   bestr,   r   )r2   r   r   r   r?   r@   r   rP   r   on_train_end{   s    rT   )r4   rA   rG   rI   rT   )r   )Zultralytics.utilsr   r   r   r   r   hasattrImportErrorAssertionErrorr   r&   r4   rA   rG   rI   rT   	callbacksr   r   r   r   <module>   s2   
