
    FPhS              	       Z    d dl Z d dlmZmZmZ d dlmZmZmZm	Z	 	 	 	 	 d	de
dededefdZy)
    N)	TASK2DATATASK2METRICget_save_dir)DEFAULT_CFGDEFAULT_CFG_DICTLOGGERNUM_THREADSspacegrace_periodgpu_per_trialmax_samplesc           	      
   t        j                  d       i 	 t        j                  d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 	 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      d/}| j&                  }j)                  |       fd0}|s|}t        j*                  d1       j-                  d2t.        |         }||d2<   d2vrt        j*                  d3| d4       |j1                  |t2        |xs dd5      } |	d6t4        |   d7j-                  d8      xs t6        d8   xs d9|d:;      }|
r
 |d<=      gng }t9        t:        d>?      j=                         }|j?                  dd@       |jA                  |||jC                  ||A       |||B      C      }|jE                          |jG                         S # t        $ r t        d      w xY w# t        t"        f$ r d}
Y w xY w)Da  
    Runs hyperparameter tuning using Ray Tune.

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

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

    Example:
        ```python
        from ultralytics import YOLO

        # Load a YOLOv8n model
        model = YOLO('yolov8n.pt')

        # Start tuning hyperparameters for YOLOv8n 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pip install ray[tune]T)checkr   )tune)	RunConfig)WandbLoggerCallback)ASHASchedulerzOTuning hyperparameters requires Ray Tune. Install with: pip install "ray[tune]"__version__F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mosaicmixup
copy_pastec                     j                        }|j                          | j                          |j                  di | }|j                  S )z
        Trains the YOLO model with the specified hyperparameters and additional arguments.

        Args:
            config (dict): A dictionary of hyperparameters to use for training.

        Returns:
            None.
         )getreset_callbacksupdatetrainresults_dict)configmodel_to_trainresultsmodel_in_storeray
train_argss      bC:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\ultralytics/utils/tuner.py_tunezrun_ray_tune.<locals>._tune[   sL     0&&(j!&.&&00###    uE   WARNING ⚠️ search space not provided, using default search space.datau6   WARNING ⚠️ data not provided, using default "data=z".)cpugpuepochmaxepochsd      )	time_attrmetricmodemax_tr   reduction_factorzYOLOv8-tune)projectr   )name)parentsexist_ok)	schedulernum_samples)	callbacksstorage_path)param_spacetune_config
run_config)$r   info
subprocessrunsplitr5   r   ray.airr   ray.air.integrations.wandbr   ray.tune.schedulersr   ImportErrorModuleNotFoundErrorwandbhasattrAssertionErroruniformtaskputwarningr,   r   with_resourcesr	   r   r   r   r   resolvemkdirTuner
TuneConfigfitget_results)modelr
   r   r   r   r6   r   r   r   r   r[   default_spacer_   r8   r:   trainable_with_resourcesasha_schedulertuner_callbackstune_dirtunerr4   r5   s        `              @@r7   run_ray_tunerp   	   s   @ KK`a
	u.446dC%B5um,,,.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(,,sC(c3'll3,-.M2 ::DWWU^N$" ^_ >>&)D/2DE&MZOPTvUWXY  $225+VcVhgh:ij #W*5d*;(-)3)A)fEUV^E_)fcf0<457N GL*=ABQSO Kf5==?HNN4$N/JJ/#(#'??^Yd?#e"+oT\"]  _E 
IIK w  u!"sttu ( s   AM  M- M*-NN)N
   Nrq   )rS   ultralytics.cfgr   r   r   ultralytics.utilsr   r   r   r	   dictintrp   r+   r9   r7   <module>rv      sR     @ @ P P  $%'&*$&	GG"G !$G "	Gr9   