
    hh                         d dl mZmZm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de	ddfd	Z
de	d
e	ddfdZddZddZddZddZddZer	eeeeedZyi Zy# eef$ r dZY @w xY w)    )LOGGERSETTINGSTESTS_RUNNINGclearmlTN)Task__version__titlereturnc                    ddl }t        j                         x}r| D ]  }|j                         s|j	                  d|j
                        }|rt        |j                         d         nd}|j                         j                  ||j
                  j                  |j                         d      t        |      |        yy)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   s          a/var/www/html/dev/engine/venv/lib/python3.12/site-packages/ultralytics/utils/callbacks/clearml.py_log_debug_samplesr!      s       ""t" 	AxxzYY~qvv635C		A/1	!..rxxz2(FSVWXSYen / 		 #    	plot_pathc                    ddl m} ddlm} |j	                  |      }|j                         }|j                  g dddg g       }|j                  |       t        j                         j                         j                  | d|d       y)	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axs          r    	_log_plotr:   %   su     %#
,,y
!C
**,C	lE&TV	WBIIcN""$==Bsu > r"   c           
         	 t        j                         x}r/ddlm} ddlm} |j                  d       |j                  d       n`t        j                  | j                  j                  xs d| j                  j                  dgddddd	      }t        j                  d
       |j                  t        | j                        d       y# t        $ r"}t        j                  d|        Y d}~yd}~ww xY w)zHInitialize and connect ClearML task at the start of pretraining routine.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_bindr<   clearml.binding.matplotlib_bindr=   update_current_taskinitargsprojectr   r   warningconnectvars	Exception)trainerr   r<   r=   es        r    on_pretrain_routine_startrT   :   s    f$$&&4& TI33D911$799$\\11B]!,,++#_#(49(OD NN_ 	T',,'i8 f`ab`cdeefs   CC 	C9C44C9c                 
   t        j                         x}r| j                  dk(  r.t        t	        | j
                  j                  d            d       | j                  | j                  d      j                         D ]2  \  }}|j                         j                  d||| j                         4 | j                  j                         D ]2  \  }}|j                         j                  d||| j                         4 yy)	zKLog debug samples for the first epoch and report 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_scalarrZ   )rR   r   kvs       r    on_train_epoch_endre   W   s      ""t"==Avg&6&6&;&;<N&OPRZ[,,W]]7,KQQS 	TDAqOO++GQW]]+S	TJJ$$& 	QDAqOO++D!Q'--+P	Q #r"   c                    t        j                         x}r|j                         j                  dd| j                  | j
                         | j                  j                         D ]2  \  }}|j                         j                  d||| j
                         4 | j
                  dk(  rEddlm	}  ||       j                         D ]%  \  }}|j                         j                  ||       ' yyy)zFReport model information and metrics to logger at the end of an epoch.z
Epoch Time)r	   r   valuer   valrY   r   )model_info_for_loggersN)r   r   r   rb   
epoch_timer[   metricsra   ultralytics.utils.torch_utilsri   report_single_value)rR   r   rc   rd   ri   s        r    on_fit_epoch_endrn   d   s      ""t"''|7;M;MY`YfYf 	( 	
 OO))+ 	RDAqOO++E1a7==+Q	R==AL.w7==? <1!55a;<  #r"   c                     t        j                         r/t        t        | j                  j                  d            d       yy)z8Log validation results including labels and predictions.zval*.jpg
ValidationN)r   r   r!   r\   r]   r^   )	validators    r    
on_val_endrr   t   s3    6)"4"4"9"9*"EFU r"   c                 *   t        j                         x}rdddgd dD        }|D cg c]/  }| j                  |z  j                         s!| j                  |z  1 }}|D ]  }t	        |j
                  |        | j                  j                  j                  j                         D ]%  \  }}|j                         j                  ||       ' |j                  t        | j                        | j                  j                   d       y	y	c c}w )
z<Log final model and training results on training completion.zresults.pngzconfusion_matrix.pngzconfusion_matrix_normalized.pngc              3   &   K   | ]	  }| d   yw)z
_curve.pngN ).0xs     r    	<genexpr>zon_train_end.<locals>.<genexpr>   s     ?1:?s   )F1PRPR)r	   r#   F)
model_path
model_nameauto_delete_fileN)r   r   r]   r   r:   stemrq   rk   results_dictra   r   rm   update_output_modelr   bestrL   r   )rR   r   r   r   rc   rd   s         r    on_train_endr   {   s	     ""t" "-
 @(>?	
 27ZA7;K;Ka;O:W:W:Y'""Q&ZZ 	1AAFFa0	1 %%--::@@B 	8DAqOO11!Q7	8 	  C,=',,J[J[ns t # [s   "DD)rT   re   rn   rr   r   )zDebug Samples)r
   N)ultralytics.utilsr   r   r   r   r   hasattrImportErrorAssertionErrorr   r!   r:   rT   re   rn   rr   r   	callbacksru   r"   r    <module>r      s    > =	I$&&&7M***S t (S S T *f:
Q< Vu8  &?0, $ 
 
 
E 	^$ Gs   "A$ $	A0/A0