a
    yf                  
   @   s   d dl mZmZmZmZ z`er"J ed du s2J d dlZejddddsNJ d dlZd dlZd dl	m
Z
 dai Zd	aW n eeefy   dZY n0 dddZdddZdd Zdd Zdd Zdd Zdd Zdd Zdd Zereeeeeedni ZdS )     )LOGGERSETTINGSTESTS_RUNNINGchecksZdvcTNdvclivez2.11.0)verbose)PathF c                 C   s`   t r\| j}td| }rF|d }tdd| j}t|| | j}t 	t
j|||  dS )zDLogs images at specified path with an optional prefix using DVCLive.z_batch(\d+)   Z_batchN)livenameresearchsubstemr   with_suffixsuffixZ	log_imageospathjoin)r   prefixr   mniZnew_stem r   [/var/www/html/django/DPS/env/lib/python3.9/site-packages/ultralytics/utils/callbacks/dvc.py_log_images   s    r   c                 C   s>   |   D ]0\}}|d }t||krt|| |t|< qdS )zRLogs plot images for training progress if they have not been previously processed.	timestampN)items_processed_plotsgetr   )plotsr   r   paramsr   r   r   r   
_log_plots*   s
    
r"   c           	      C   s   g }g }| j j}t| j }| j jdkr4|dg7 }t|jt	D ]B\}}t|D ]0\}}|
|| g|  |
|| g|  qTqDtjd||ddd dS )z@Logs the confusion matrix for the given validator using DVCLive.detect
backgroundconfusion_matrixzcf.jsonT)r   
normalizedN)r%   matrixlistnamesvaluestask	enumerateTZastypeintextendr   Zlog_sklearn_plot)		validatortargetspredsr'   r)   tipredpinumr   r   r   _log_confusion_matrix3   s    
r7   c              
   C   sV   zt jdddatd W n4 tyP } ztd|  W Y d}~n
d}~0 0 dS )zMInitializes DVCLive logger for training metadata during pre-training routine.T)Zsave_dvc_expZcache_imagesz[DVCLive is detected and auto logging is enabled (run 'yolo settings dvc=False' to disable).uV   WARNING ⚠️ DVCLive installed but not initialized correctly, not logging this run. N)r   Liver   r   info	Exceptionwarning)trainerer   r   r   on_pretrain_routine_startD   s
    r>   c                 C   s   t | jd dS )zQLogs plots related to the training process at the end of the pretraining routine.trainN)r"   r    r<   r   r   r   on_pretrain_routine_endN   s    rA   c                 C   s   t rt | j dS )z:Logs the training parameters if DVCLive logging is active.N)r   Z
log_paramsargsr@   r   r   r   on_train_startS   s    rC   c                 C   s   da dS )z[Sets the global variable _training_epoch value to True at the start of training each epoch.TN)_training_epochr@   r   r   r   on_train_epoch_startY   s    rE   c                 C   s   t rtri | j| jdd| j| j}| D ]\}}t || q0| jdkrddl	m
} ||  D ]\}}t j||dd qht| jd t| jjd t   dadS )	z]Logs training metrics and model info, and advances to next step on the end of each fit epoch.r?   r   r   )model_info_for_loggersFZplotvalN)r   rD   label_loss_itemstlossmetricslrr   
log_metricepochZultralytics.utils.torch_utilsrG   r"   r    r0   Z	next_step)r<   all_metricsmetricvaluerG   r   r   r   on_fit_epoch_end_   s     
rS   c                 C   s   t ri | j| jdd| j| j}| D ]\}}t j||dd q,t| jd t| j	jd t
| j	 | j rt j| jddd t   d	S )
z_Logs the best metrics, plots, and confusion matrix at the end of training if DVCLive is active.r?   rF   FrH   rI   Tmodel)copytypeN)r   rJ   rK   rL   rM   r   rN   r"   r    r0   r7   bestexistsZlog_artifactend)r<   rP   rQ   rR   r   r   r   on_train_endt   s     

rZ   )r>   rA   rC   rE   rS   rZ   )r	   )r	   )Zultralytics.utilsr   r   r   r   r   Zcheck_versionr   r   pathlibr   r   r   rD   ImportErrorAssertionError	TypeErrorr   r"   r7   r>   rA   rC   rE   rS   rZ   	callbacksr   r   r   r   <module>   sB   


	
	