Hyperparameter Tuning with Ray Tune and YOLOv8

I have tried to train my custom datasets with YOLOv8 in windows powershell and I am trying and having a hard time on how to do the Hyperparameter Tuning with Ray Tune (Efficient Hyperparameter Tuning with Ray Tune and YOLOv8 - Ultralytics YOLOv8 Docs) i tried to use this code right now in jupyter notebook and it always show error.

code:

Install and update Ultralytics and Ray Tune pacakges

pip install -U ultralytics ‘ray[tune]’

Optionally install W&B for logging

pip install wandb

from ultralytics import YOLO
from ray import tune

Define a YOLO model

model = YOLO(“yolov8n.pt”)

Run Ray Tune on the model

result_grid = model.tune(data=“data.yaml”,
space={“lr0”: tune.uniform(1e-5, 1e-1)},
epochs=50)
and this is the Error:


ValueError Traceback (most recent call last)
Cell In[10], line 8
5 model = YOLO(“yolov8n.pt”)
7 # Run Ray Tune on the model
----> 8 result_grid = model.tune(data=“data.yaml”,
9 space={“lr0”: tune.uniform(1e-5, 1e-1)},
10 epochs=50)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\ultralytics\yolo\engine\model.py:410, in YOLO.tune(self, *args, **kwargs)
408 self._check_is_pytorch_model()
409 from ultralytics.yolo.utils.tuner import run_ray_tune
→ 410 return run_ray_tune(self, *args, **kwargs)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\ultralytics\yolo\utils\tuner.py:117, in run_ray_tune(model, space, grace_period, gpu_per_trial, max_samples, **train_args)
111 tuner = tune.Tuner(trainable_with_resources,
112 param_space=space,
113 tune_config=tune.TuneConfig(scheduler=asha_scheduler, num_samples=max_samples),
114 run_config=RunConfig(callbacks=tuner_callbacks, storage_path=‘./runs/tune’))
116 # Run the hyperparameter search
→ 117 tuner.fit()
119 # Return the results of the hyperparameter search
120 return tuner.get_results()

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\ray\tune\tuner.py:347, in Tuner.fit(self)
345 if not self._is_ray_client:
346 try:
→ 347 return self._local_tuner.fit()
348 except TuneError as e:
349 raise TuneError(
350 _TUNER_FAILED_MSG.format(
351 path=self._local_tuner.get_experiment_checkpoint_dir()
352 )
353 ) from e

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\ray\tune\impl\tuner_internal.py:588, in TunerInternal.fit(self)
586 param_space = copy.deepcopy(self.param_space)
587 if not self._is_restored:
→ 588 analysis = self._fit_internal(trainable, param_space)
589 else:
590 analysis = self._fit_resume(trainable, param_space)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\ray\tune\impl\tuner_internal.py:712, in TunerInternal._fit_internal(self, trainable, param_space)
698 “”“Fitting for a fresh Tuner.”“”
699 args = {
700 **self._get_tune_run_arguments(trainable),
701 **dict(
(…)
710 **self._tuner_kwargs,
711 }
→ 712 analysis = run(
713 **args,
714 )
715 self.clear_remote_string_queue()
716 return analysis

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\ray\tune\tune.py:1018, in run(run_or_experiment, name, metric, mode, stop, time_budget_s, config, resources_per_trial, num_samples, storage_path, search_alg, scheduler, keep_checkpoints_num, checkpoint_score_attr, checkpoint_freq, checkpoint_at_end, checkpoint_keep_all_ranks, checkpoint_upload_from_workers, verbose, progress_reporter, log_to_file, trial_name_creator, trial_dirname_creator, chdir_to_trial_dir, sync_config, export_formats, max_failures, fail_fast, restore, server_port, resume, reuse_actors, raise_on_failed_trial, callbacks, max_concurrent_trials, trial_executor, local_dir, _experiment_checkpoint_dir, _remote, _remote_string_queue, _entrypoint)
1015 runner.update_pending_trial_resources(resources_per_trial)
1017 # Calls setup on callbacks
→ 1018 runner.setup_experiments(
1019 experiments=experiments, total_num_samples=search_alg.total_samples
1020 )
1022 tune_start = time.time()
1024 air_progress_reporter = None

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\ray\tune\execution\trial_runner.py:305, in _TuneControllerBase.setup_experiments(self, experiments, total_num_samples)
303 spec = experiment.public_spec if experiment else {}
304 spec[“total_num_samples”] = total_num_samples
→ 305 self._callbacks.setup(**spec)

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\ray\tune\callback.py:357, in CallbackList.setup(self, **info)
355 for callback in self._callbacks:
356 try:
→ 357 callback.setup(**info)
358 except TypeError as e:
359 if “argument” in str(e):

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\ray\air\integrations\wandb.py:588, in WandbLoggerCallback.setup(self, *args, **kwargs)
584 def setup(self, *args, **kwargs):
585 self.api_key_file = (
586 os.path.expanduser(self.api_key_path) if self.api_key_path else None
587 )
→ 588 _set_api_key(self.api_key_file, self.api_key)
590 self.project = _get_wandb_project(self.project)
591 if not self.project:

File ~\AppData\Local\Programs\Python\Python310\lib\site-packages\ray\air\integrations\wandb.py:336, in _set_api_key(api_key_file, api_key)
334 os.environ[WANDB_ENV_VAR] = api_key
335 elif not os.environ.get(WANDB_ENV_VAR):
→ 336 raise ValueError(
337 "No WandB API key found. Either set the {} environment "
338 “variable, pass api_key or api_key_file to the”
339 "WandbLoggerCallback class as arguments, "
340 “or run wandb login from the command line”.format(WANDB_ENV_VAR)
341 )

ValueError: No WandB API key found. Either set the WANDB_API_KEY environment variable, pass api_key or api_key_file to theWandbLoggerCallback class as arguments, or run wandb login from the command line

I use these commands to train my custom datasets in windows powershell without hyperparameters:
mkdir yolov8project

cd yolov8project
python -m venv env

env/Scripts/activate

pip install ultralytics

#image detection
yolo task=detect mode=predict model=yolov8n.pt source=“path/to/image.png”

yolo task=detect mode=train model=yolov8n.pt data=path/to/config.yaml epochs=10 imgsz=640

yolo task=detect mode=predict model=“runs/train/weights/best.pt” source=“image.png”

yolo val model=yolov8n.pt data=path/to/config.yaml batch=1 imgsz=640

yolo detect predict model=path/to/best.pt source=‘path/to/image.jpg’