
    j                    \    d dl mZ 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dZdS )    )annotations)	TASK2DATATASK2METRICget_cfgget_save_dir)DEFAULT_CFGDEFAULT_CFG_DICTLOGGERNUM_THREADScheckscolorstrN
   spacedict | Nonegrace_periodintgpu_per_trial
int | Nonemax_samplesc                
   t          j        d           	 t          j        d           ddlddlm ddlm} ddlm	} n# t          $ r t          d          w xY w	 ddl}t          |d	          sJ n# t          t          f$ r d
}Y nw xY wt          j        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d                              dd                              dd-          d/}	| j        }
                    |                               d0d1          fd2}|s+                    d3          s|	}t          j        d4                               d5t,          |
                   }||d5<   d5vrt          j        d6| d7                               |t0          |pdd8          } |d9t2          |
         d:                    d;          pt4          d;         pd<|d=>          }t7          t9          t:          i d?                    d3d
          i                              d0d1          @          }|                    dAdAB           j         !                    |          rRt          j        tE          dC           dD| dE           j         #                    tI          |          |dAF          }nG                     ||%                    ||dG dH I           ||j&        |j'        J          K          }|(                                 |)                                }*                                 |S )La  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("yolo26n.pt")  # Load a YOLO26n model

        Start tuning hyperparameters for YOLO26n 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-tunez	ray[tune]r   N)tune)	RunConfig)ASHASchedulerzHRay Tune required but not found. To install run: pip install "ray[tune]"__version__Fz>=2.0.0raylr0gh㈵>g{Gz?lrfg      ?momentumgffffff?g\(\?weight_decayg        gMbP?warmup_epochsg      @warmup_momentumgffffff?boxg      4@clsg?g      @dflg?g      (@hsv_hhsv_sg?hsv_vdegreesg     F@	translatescaleshearg      $@perspective)flipudfliplrbgrmosaicmixupcutmix
copy_pasteclose_mosaicnamer   c                b                                  }d|_        |                                 |                                	 	                                }d|v r|                    d          d         n|} d| | d<   n# t          $ r | d<   Y nw xY w |j        di | }|j        S )zKTrain the YOLO model with the specified hyperparameters and return results.N_r5    )	gettrainerreset_callbacksupdateget_trial_idsplit	Exceptiontrainresults_dict)
configmodel_to_traintrial_idtrial_suffixresults	base_namemodel_in_storer   
train_argsr   s
        \/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/utils/tuner.py_tunezrun_ray_tune.<locals>._tuneZ   s    00!%&&(((j!!!	'((**H69Xoo8>>#..r228L )::L::F6NN 	' 	' 	'&F6NNN	' '.&0000##s   ?B BB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_factorexist_ok)r5   T)parentsr[   zTuner: z Resuming tuning run z...)	trainableresume_erroredc                $    | j          d| j         S Nr7   trainable_namerE   trials    rK   <lambda>zrun_ray_tune.<locals>.<lambda>   s    E4H1[1[5>1[1[     c                $    | j          d| j         S r`   ra   rc   s    rK   re   zrun_ray_tune.<locals>.<lambda>   s    u7K4^4^en4^4^ rf   )	schedulernum_samplestrial_name_creatortrial_dirname_creator)storage_pathr5   )param_spacetune_config
run_config)+r
   infor   check_requirementsr   r   ray.airr   ray.tune.schedulersr   ImportErrorModuleNotFoundErrorwandbhasattrAssertionErrorcheck_versionr   uniformtaskputr:   warningr   with_resourcesr   r   r	   r   r   r   popmkdirTunercan_restorer   restorestr
TuneConfigparentr5   fitget_resultsshutdown)modelr   r   r   r   rJ   r   r   rv   default_spacer{   rL   rN   trainable_with_resourcesasha_schedulertune_dirtunerrG   rH   rI   r   r   s        `            @@@@rK   run_ray_tuner   	   s   8 K`aaan!+...


%%%%%%5555555 n n n!"lmmmnum,,,,,,(    )U;;;t||D$'' 	t||D#&& 	DLLd++	
 	S%00 	c3// 	4<<T22 	t||C&& 	t||C%% 	t||C&& 	c3'' 	c3'' 	c3'' 	4<<T** 	T\\#s++  	c4((!" 	c4((#$ 	t||C//%& ,,sC((,,sC((||C%%,,sC((c3'',,sC((ll3,,S$//5  M< :DWWU^^Nvv..I$ $ $ $ $ $ $ $ $(  Q11 QOPPP >>&)D/22DE&MZIIIIJJJ  $225+VcVhgh:i:ijj #]4 nnX&&K*:8*DK!  N KzKj*..5*I*IJK	
 	
 ^^FF++  H NN4$N///zh'' 
x	**NNNNNOOO
""3x==<Tei"jj

$('#[#[&^&^	 (   !yhoHMRRR  

 

 
IIKKK !!G LLNNNNs   *A A $A; ;BB)Nr   Nr   )r   r   r   r   r   r   r   r   )
__future__r   ultralytics.cfgr   r   r   r   ultralytics.utilsr   r	   r
   r   r   r   r   r9   rf   rK   <module>r      s    # " " " " " I I I I I I I I I I I I b b b b b b b b b b b b b b b b
  $^ ^ ^ ^ ^ ^ ^rf   