o
    Wh                  	   @   s`   d dl mZmZmZmZ d dlmZmZmZm	Z	m
Z
mZ 				ddedededefd	d
ZdS )    )	TASK2DATATASK2METRICget_cfgget_save_dir)DEFAULT_CFGDEFAULT_CFG_DICTLOGGERNUM_THREADScheckscolorstrN
   spacegrace_periodgpu_per_trialmax_samplesc              	      s  t d du ri z#td ddlddlm} ddlm} ddlm	} ddl
m}	 W n ty9   td	w zddl}
t|
d
sFJ W n ttfyT   d}
Y nw tjdd i d|ddd|ddd|ddd|ddd|ddd|ddd|dd d!|d d"d#|ddd$|dd%d&|dd%d'|dd(d)|dd%d*|dd%d+|dd,d-|ddd.|dd|dd|dd|dd|dd|ddd/}| j}|   fd0d1}|sd2s|}t d3 d4t| }||d4< d4vr,t d5| d6 ||t|p4dd7}|	d8t| d9d:pJtd: pJd;|d<d=}|
rX|d>d?gng }ttti d@ d2di dAdBdC! }|j"dDdDdE |j#$|rt t%dF dG| dH |j#j&t'||dDdI}n|j#|||j(||dJdK dLdK dM|||j)|j*dNdO}|+  |, }-  |S )Pab  
    Run hyperparameter tuning using Ray Tune.

    Args:
        model (YOLO): Model to run the tuner on.
        space (dict, optional): The hyperparameter search space.
        grace_period (int, optional): The grace period in epochs of the ASHA scheduler.
        gpu_per_trial (int, optional): The number of GPUs to allocate per trial.
        max_samples (int, optional): The maximum number of trials to run.
        **train_args (Any): Additional arguments to pass to the `train()` method.

    Returns:
        (dict): A dictionary containing the results of the hyperparameter search.

    Examples:
        >>> from ultralytics import YOLO
        >>> model = YOLO("yolo11n.pt")  # Load a YOLO11n model

        Start tuning hyperparameters for YOLO11n training on the COCO8 dataset
        >>> result_grid = model.tune(data="coco8.yaml", use_ray=True)
    uN   💡 Learn about RayTune at https://docs.ultralytics.com/integrations/ray-tuneNz	ray[tune]r   )tune)	RunConfig)WandbLoggerCallback)ASHASchedulerzHRay Tune required but not found. To install run: pip install "ray[tune]"__version__Fz>=2.0.0raylr0gh㈵>g?lrfg{Gz?g      ?momentumg333333?g\(\?weight_decayg        gMbP?warmup_epochsg      @warmup_momentumgffffff?boxg{Gz?g?clsg      @hsv_hhsv_sg?hsv_vdegreesg     F@	translatescaleshearg      $@perspectiveflipud)fliplrbgrmosaicmixup
copy_pastec                    s2     }|  |  |jdi | }|jS )z8Train the YOLO model with the specified hyperparameters.N )getreset_callbacksupdatetrainresults_dict)configmodel_to_trainresultsmodel_in_storer   
train_argsr-   K/var/www/vscode/kcb/lib/python3.10/site-packages/ultralytics/utils/tuner.py_tuneY   s
   

zrun_ray_tune.<locals>._tuneresumez6search space not provided, using default search space.dataz'data not provided, using default "data=z".)cpugpuepochmaxepochsd      )	time_attrmetricmodemax_tr   reduction_factorzYOLOv8-tune)projectexist_oknamer   )rK   T)parentsrJ   zTuner: z Resuming tuning run z...)	trainableresume_erroredc                 S      | j  d| j S N_trainable_nametrial_idtrialr-   r-   r9   <lambda>       zrun_ray_tune.<locals>.<lambda>c                 S   rO   rP   rR   rU   r-   r-   r9   rW      rX   )	schedulernum_samplestrial_name_creatortrial_dirname_creator)	callbacksstorage_pathrK   )param_spacetune_config
run_config).r   infor
   check_requirementsr   r   ray.airr   ray.air.integrations.wandbr   ray.tune.schedulersr   ImportErrorModuleNotFoundErrorwandbhasattrAssertionErrorcheck_versionr   uniformtaskputr.   warningr   with_resourcesr	   r   r   r   r   r   popresolvemkdirTunercan_restorer   restorestr
TuneConfigparentrK   fitget_resultsshutdown)modelr   r   r   r   r8   r   r   r   r   ri   default_spacern   r:   r<   trainable_with_resourcesasha_schedulertuner_callbackstune_dirtunerr5   r-   r6   r9   run_ray_tune   s   

	






	



r   )Nr   Nr   )ultralytics.cfgr   r   r   r   ultralytics.utilsr   r   r   r	   r
   r   dictintr   r-   r-   r-   r9   <module>   s    