
    |h              	       ^    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Zy)
    )	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                 	   t        j                  d       i 	 t        j                  d       ddlddlm} ddlm} ddlm	} ddl
m}	 	 ddl}
t        |
d
      sJ 	 t        j"                  j$                  dd       i d|j'                  dd      d|j'                  dd      d|j'                  dd      d|j'                  dd      d|j'                  dd      d|j'                  dd      d|j'                  dd       d!|j'                  d d"      d#|j'                  dd      d$|j'                  dd%      d&|j'                  dd%      d'|j'                  dd(      d)|j'                  dd%      d*|j'                  dd%      d+|j'                  dd,      d-|j'                  dd      d.|j'                  dd      |j'                  dd      |j'                  dd      |j'                  dd      |j'                  dd      |j'                  dd      |j'                  dd      d/}| j(                  }j+                  |       fd0}|s(j-                  d1      s|}t        j.                  d2       j-                  d3t0        |         }||d3<   d3vrt        j.                  d4| d5       |j3                  |t4        |xs dd6      } |	d7t6        |   d8j-                  d9      xs t8        d9   xs d:|d;<      }|
r
 |d=>      gng }t;        t=        t>        i d?jA                  d1d      i      jA                  d@dA      B      jC                         }|jE                  dCdCD       |jF                  jI                  |      rLt        j                  tK        dE       dF| dG       |jF                  jM                  tO        |      |dCH      }nG|jG                  |||jQ                  ||dI dJ K       |||jR                  |jT                  L      M      }|jW                          |jY                         }j[                          |S # t        $ r t        d	      w xY w# t        t         f$ r d}
Y $w xY w)Na  
    Run hyperparameter tuning using Ray Tune.

    Args:
        model (YOLO): Model to run the tuner on.
        space (dict, optional): The hyperparameter search space. If not provided, uses default 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:
        (ray.tune.ResultGrid): A ResultGrid 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cutmix
copy_pastec                     j                        }|j                          | j                          |j                  di | }|j                  S )zKTrain the YOLO model with the specified hyperparameters and return results. )getreset_callbacksupdatetrainresults_dict)configmodel_to_trainresultsmodel_in_storer   
train_argss      V/var/www/html/test/engine/venv/lib/python3.12/site-packages/ultralytics/utils/tuner.py_tunezrun_ray_tune.<locals>._tuneZ   sJ    0&&(j!&.&&00###    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   )rN   T)parentsrM   zTuner: z Resuming tuning run z...)	trainableresume_erroredc                 8    | j                    d| j                   S N_trainable_nametrial_idtrials    r;   <lambda>zrun_ray_tune.<locals>.<lambda>   s    E4H4H3I5>>JZ1[ r=   c                 8    | j                    d| j                   S rS   rU   rX   s    r;   rZ   zrun_ray_tune.<locals>.<lambda>   s    u7K7K6LAennM]4^ r=   )	schedulernum_samplestrial_name_creatortrial_dirname_creator)	callbacksstorage_pathrN   )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putr1   warningr   with_resourcesr
   r   r   r   r   r   popresolvemkdirTunercan_restorer   restorestr
TuneConfigparentrN   fitget_resultsshutdown)modelr   r   r   r   r:   r   r   r   r   rl   default_spacerq   r<   r?   trainable_with_resourcesasha_schedulertuner_callbackstune_dirtunerr8   r9   r   s        `               @@r;   run_ray_tuner      s   : KK`a
	n!!+.%B5um,,, )U;t||D$' 	t||D#& 	DLLd+	
 	S%0 	c3/ 	4<<T2 	t||D#& 	t||C% 	c3' 	c3' 	c3' 	4<<T* 	T\\#s+ 	c3'  	c4(!" 	t||C/#$ 	$,,sC(%& ,,sC(||C%,,sC(c3',,sC(ll3,1M8 ::DWWU^N$ 1OP >>&)D/2DE&MZ@bIJ  $225+VcVhgh:ij #4 nnX&K*:8*DK!N GL*=ABQSO KzKj*..5*IJK	
 ^^FF+ gi  NN4$N/zzh'x	*++@
#NO

""3x=<Tei"j

$('#[&^	 (  !?_g_l_lm  

 
IIK !G LLNN_  n!"lmmn ( s   1Q Q3 Q03RR)N
   Nr   )ultralytics.cfgr   r   r   r   ultralytics.utilsr   r   r	   r
   r   r   dictintr   r0   r=   r;   <module>r      sR    J I b b
 XX X 	X
 Xr=   