a
    yµÿfj  ã                   @   sR   d dl Z d dlmZmZmZ d dlmZmZmZm	Z	m
Z
 deeeedœdd„ZdS )	é    N)Ú	TASK2DATAÚTASK2METRICÚget_save_dir)ÚDEFAULT_CFGÚDEFAULT_CFG_DICTÚLOGGERÚNUM_THREADSÚchecksé
   )ÚspaceÚgrace_periodÚgpu_per_trialÚmax_samplesc                    sÊ  t  d¡ ˆdu ri ‰zNtjd ¡ dd ddl‰ddlm} ddlm} dd	l	m
} dd
lm}	 W n ty~   tdƒ‚Y n0 zddl}
t|
dƒs˜J ‚W n ttfy´   d}
Y n0 t ˆj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d!d"„}|sÜ|}t  d#¡ ˆ d$t| ¡}||d$< d$ˆvrt  d%|› d&¡ | |t|p dd'œ¡}|	d(t| d)ˆ d*¡pLtd* pLd+|d,d-}|
rh|d.d/gng }tt d0d1 !¡ }|j"ddd2 |j#|||j$||d3|||d4d5}| %¡  | &¡ }ˆ '¡  |S )6aÛ  
    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)ÚASHASchedulerzHRay Tune required but not found. To install run: pip install "ray[tune]"Ú__version__Fz>=2.0.0Úraygñhãˆµøä>gš™™™™™¹?g{®Gáz„?g      ð?g333333ã?g\Âõ(\ï?g        gü©ñÒMbP?g      @gffffffî?g{®Gáz”?gš™™™™™É?g      @gÍÌÌÌÌÌì?g     €F@g      $@)Zlr0ZlrfZmomentumZweight_decayZwarmup_epochsZwarmup_momentumÚboxÚclsZhsv_hZhsv_sZhsv_vÚdegreesÚ	translateÚscaleZshearZperspectiveZflipudZfliplrZbgrZmosaicZmixupZ
copy_pastec                    s2   ˆ  ˆ ¡}| ¡  |  ˆ¡ |jf 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
        )ÚgetZreset_callbacksÚupdateÚtrainZresults_dict)ÚconfigZmodel_to_trainÚresults©Zmodel_in_storer   Ú
train_args© úS/var/www/html/django/DPS/env/lib/python3.9/site-packages/ultralytics/utils/tuner.pyÚ_tuneZ   s
    


zrun_ray_tune.<locals>._tuneuE   WARNING âš ï¸ search space not provided, using default search space.Údatau6   WARNING âš ï¸ data not provided, using default "data=z".)ÚcpuZgpuÚepochÚmaxZepochséd   é   )Z	time_attrZmetricÚmodeZmax_tr   Zreduction_factorzYOLOv8-tune)Úprojectr   )Úname)ÚparentsÚexist_ok)Z	schedulerZnum_samples)Ú	callbacksZstorage_path)Zparam_spaceZtune_configZ
run_config)(r   ÚinfoÚ
subprocessÚrunÚsplitr   r   Zray.airr   Zray.air.integrations.wandbr   Zray.tune.schedulersr   ÚImportErrorÚModuleNotFoundErrorÚwandbÚhasattrÚAssertionErrorr	   Zcheck_versionr   ÚuniformÚtaskÚputÚwarningr   r   Zwith_resourcesr   r   r   r   r   ÚresolveÚmkdirZTunerZ
TuneConfigÚfitZget_resultsÚshutdown)Úmodelr   r   r   r   r!   r   r   r   r   r7   Zdefault_spacer;   r$   r%   Ztrainable_with_resourcesZasha_schedulerZtuner_callbacksZtune_dirZtunerr   r"   r    r#   Úrun_ray_tune	   s’    























é


ú

ürC   )Nr
   Nr
   )r2   Zultralytics.cfgr   r   r   Zultralytics.utilsr   r   r   r   r	   ÚdictÚintrC   r"   r"   r"   r#   Ú<module>   s    ÿÿ