
    FPhz                         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 d dlmZ d dlm	Z	  e
ed      sJ 	 ddd	Zdd
Zd Zd Zd Zd Zd Zer	eeeeedZyi Zy# eef$ r dZY 1w xY w)    )LOGGERSETTINGSTESTS_RUNNINGclearmlTN)Task)PatchPyTorchModelIO)PatchedMatplotlib__version__c                    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): 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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          nC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\ultralytics/utils/callbacks/clearml.py_log_debug_samplesr"      s       ""t"AxxzYY~qvv635C		A/1	!..U67ffnnRXXZQS6T:=a&9B / D	  #    c                    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   	plot_pathmpimgpltimgfigaxs          r!   	_log_plotr;   (   sx     %#
,,y
!C
**,C	lE&TV	WBIIcN""$==EEGEHQV > Xr#   c           
         	 t        j                         x}r+t        j                  d       t	        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)zURuns at start of pretraining routine; initializes and connects/ logs task to ClearML.NYOLOv8TF)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   uV   WARNING ⚠️ ClearML installed but not initialized correctly, not logging this run. )r   r   r   update_current_taskr	   initargsprojectr   r   warningconnectvars	Exception)trainerr   es      r!   on_pretrain_routine_startrQ   >   s    u$$&&4&  33D911$799',,*>*>*J('.||'8'8#+*(,05,1/4667D NN j kT',,'i8 uopqorsttus   CC
 
	C5C00C5c                 r   t        j                         x}r| j                  dk(  r.t        t	        | j
                  j                  d            d       | j                  j                  j                  j                         D ]2  \  }}|j                         j                  d||| j                         4 yy)z]Logs debug samples for the first epoch of YOLO training and report current training progress.r%   ztrain_batch*.jpgMosaictrain)r   N)r   r   epochr"   sortedsave_dirglob	validatormetricsresults_dictitemsr   report_scalar)rO   r   kvs       r!   on_train_epoch_endr`   V   s      ""t"==Avg&6&6&;&;<N&OPRZ[%%--::@@BDAqOO++GQW]]+S C #r#   c                 H   t        j                         x}r|j                         j                  dd| j                  | j
                         | j
                  dk(  rEddlm}  ||       j                         D ]%  \  }}|j                         j                  ||       ' yyy)z;Reports model information to logger at the end of an epoch.z
Epoch Time)r   r   valuer   r   )model_info_for_loggersN)
r   r   r   r]   
epoch_timerU   ultralytics.utils.torch_utilsrc   r\   report_single_value)rO   r   rc   r^   r_   s        r!   on_fit_epoch_endrg   a   s      ""t"''l/;.5.@.@29-- 	( 	A ==AL.w7==?1!55a; @  #r#   c                     t        j                         r/t        t        | j                  j                  d            d       yy)z9Logs validation results including labels and predictions.zval*.jpg
ValidationN)r   r   r"   rV   rW   rX   )rY   s    r!   
on_val_endrj   o   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 )
z5Logs final model and its name 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   r5   F)
model_path
model_nameauto_delete_fileN)r   r   rW   r   r;   stemrY   rZ   r[   r\   r   rf   update_output_modelr   bestrI   r   )rO   r   r   r   r^   r_   s         r!   on_train_endr{   v   s      ""t" 13TA?(>?A 27ZA7;K;Ka;O:W:W:Y'""Q&ZAAFFa0  %%--::@@BDAqOO11!Q7 C 	  C,=',,J[J[ns t #
 [s   "DD)rQ   r`   rg   rj   r{   )zDebug Samples)returnN)ultralytics.utilsr   r   r   r   r   'clearml.binding.frameworks.pytorch_bindr   clearml.binding.matplotlib_bindr	   hasattrImportErrorAssertionErrorr"   r;   rQ   r`   rg   rj   r{   	callbacksrm   r#   r!   <module>r      s    > =I$&&&KA7M***D*X,u0T<Vu, &-	 ";,( "	
 35 
q 	^$ Gs   .A! !	A-,A-