a
    yµÿf  ã                	   @   sÞ   d Z ddlmZmZmZmZmZ z`ddlZerDdej 	dd¡v sDJ ‚ed du sTJ ‚ddl
a
et
d	ƒsjJ ‚dd
lmZ edƒZW n eefyš   da
Y n0 dd„ Zdd„ Zdd„ Zdd„ Zdd„ Zt
rÖeeeedœni ZdS )az  
MLflow Logging for Ultralytics YOLO.

This module enables MLflow logging for Ultralytics YOLO. It logs metrics, parameters, and model artifacts.
For setting up, a tracking URI should be specified. The logging can be customized using environment variables.

Commands:
    1. To set a project name:
        `export MLFLOW_EXPERIMENT_NAME=<your_experiment_name>` or use the project=<project> argument

    2. To set a run name:
        `export MLFLOW_RUN=<your_run_name>` or use the name=<name> argument

    3. To start a local MLflow server:
        mlflow server --backend-store-uri runs/mlflow
       It will by default start a local server at http://127.0.0.1:5000.
       To specify a different URI, set the MLFLOW_TRACKING_URI environment variable.

    4. To kill all running MLflow server instances:
        ps aux | grep 'mlflow' | grep -v 'grep' | awk '{print $2}' | xargs kill -9
é    )ÚLOGGERÚRUNS_DIRÚSETTINGSÚTESTS_RUNNINGÚcolorstrNZtest_mlflowZPYTEST_CURRENT_TESTÚ ÚmlflowTÚ__version__)ÚPathzMLflow: c                 C   s   dd„ |   ¡ D ƒS )zQSanitize dictionary keys by removing parentheses and converting values to floats.c                 S   s*   i | ]"\}}|  d d¡  dd¡t|ƒ“qS )ú(r   ú))ÚreplaceÚfloat)Ú.0ÚkÚv© r   ú^/var/www/html/django/DPS/env/lib/python3.9/site-packages/ultralytics/utils/callbacks/mlflow.pyÚ
<dictcomp>,   ó    z!sanitize_dict.<locals>.<dictcomp>)Úitems)Úxr   r   r   Úsanitize_dict*   s    r   c                 C   s4  t j d¡pttd ƒ}t t› d|› ¡ t 	|¡ t j d¡pL| j
jpLd}t j d¡p`| j
j}t |¡ t ¡  zxt ¡ pˆtj|d}t t› d|jj› d	|› ¡ t|ƒ ¡ rÊt t› d
|› d¡ t t› d¡ t t| j
ƒ¡ W nB ty. } z(t t› d|› dt› d¡ W Y d}~n
d}~0 0 dS )aÞ  
    Log training parameters to MLflow at the end of the pretraining routine.

    This function sets up MLflow logging based on environment variables and trainer arguments. It sets the tracking URI,
    experiment name, and run name, then starts the MLflow run if not already active. It finally logs the parameters
    from the trainer.

    Args:
        trainer (ultralytics.engine.trainer.BaseTrainer): The training object with arguments and parameters to log.

    Global:
        mlflow: The imported mlflow module to use for logging.

    Environment Variables:
        MLFLOW_TRACKING_URI: The URI for MLflow tracking. If not set, defaults to 'runs/mlflow'.
        MLFLOW_EXPERIMENT_NAME: The name of the MLflow experiment. If not set, defaults to trainer.args.project.
        MLFLOW_RUN: The name of the MLflow run. If not set, defaults to trainer.args.name.
        MLFLOW_KEEP_RUN_ACTIVE: Boolean indicating whether to keep the MLflow run active after the end of training.
    ZMLFLOW_TRACKING_URIr   z tracking uri: ZMLFLOW_EXPERIMENT_NAMEz/Shared/YOLOv8Z
MLFLOW_RUN)Úrun_namezlogging run_id(z) to zFview at http://127.0.0.1:5000 with 'mlflow server --backend-store-uri ú'ú)disable with 'yolo settings mlflow=False'u%   WARNING âš ï¸ Failed to initialize: Ú
u$   WARNING âš ï¸ Not tracking this runN)ÚosÚenvironÚgetÚstrr   r   ÚdebugÚPREFIXr   Zset_tracking_uriÚargsÚprojectÚnameZset_experimentZautologÚ
active_runZ	start_runÚinfoZrun_idr
   Úis_dirZ
log_paramsÚdictÚ	ExceptionÚwarning)ÚtrainerÚuriZexperiment_namer   r&   Úer   r   r   Úon_pretrain_routine_end/   s     

r/   c                 C   s6   t r2t ji t| jƒ¥t| j| jddƒ¥| jd dS )z>Log training metrics at the end of each train epoch to MLflow.Útrain)Úprefix©ÚmetricsÚstepN)r   Úlog_metricsr   ÚlrZlabel_loss_itemsZtlossÚepoch©r,   r   r   r   Úon_train_epoch_endZ   s    ÿþûr9   c                 C   s   t rt jt| jƒ| jd dS )z<Log training metrics at the end of each fit epoch to MLflow.r2   N)r   r5   r   r3   r7   r8   r   r   r   Úon_fit_epoch_endf   s    r:   c                 C   s¬   t sdS t  t| jjƒ¡ | j d¡D ]}|jdv r&t  t|ƒ¡ q&tj	 
dd¡ ¡ dk}|rpt t› d¡ nt  ¡  t t› d¡ t t› d	t  ¡ › d
t› d¡ dS )z/Log model artifacts at the end of the training.NÚ*>   z.ptz.pngz.jpgz.yamlz.csvZMLFLOW_KEEP_RUN_ACTIVEÚFalseÚtruezCmlflow run still alive, remember to close it using mlflow.end_run()zmlflow run endedzresults logged to r   r   )r   Zlog_artifactr    ÚbestÚparentÚsave_dirÚglobÚsuffixr   r   r   Úlowerr   r'   r"   Zend_runr!   Zget_tracking_uri)r,   ÚfZkeep_run_activer   r   r   Úon_train_endl   s    
ÿrE   )r/   r9   r:   rE   )Ú__doc__Zultralytics.utilsr   r   r   r   r   r   r   r   r   ÚhasattrÚpathlibr
   r"   ÚImportErrorÚAssertionErrorr   r/   r9   r:   rE   Ú	callbacksr   r   r   r   Ú<module>   s2   
+úüø