ultralytics / ultralytics

Ultralytics YOLO11 🚀
https://docs.ultralytics.com
GNU Affero General Public License v3.0
32.15k stars 6.17k forks source link

Missing Return Statement in tune Method Causing Failure to Retrieve Best Hyperparameters #10316

Closed mpperez3 closed 5 months ago

mpperez3 commented 6 months ago

Search before asking

YOLOv8 Component

No response

Bug

Issue Description: The tune method in our codebase is intended to return the best hyperparameters after conducting a search. However, it appears that the return statement might be missing or incorrectly implemented, leading to the method not returning the expected results. This was observed while revising the source code.

Method Definition: Tune method at Model.py is defined as follows:

def tune(
    self,
    use_ray=False,
    iterations=10,
    *args,
    **kwargs,
):
    """
    Conducts hyperparameter tuning for the model, with an option to use Ray Tune.

    This method supports two modes of hyperparameter tuning: using Ray Tune or a custom tuning method.
    When Ray Tune is enabled, it leverages the 'run_ray_tune' function from the ultralytics.utils.tuner module.
    Otherwise, it uses the internal 'Tuner' class for tuning. The method combines default, overridden, and
    custom arguments to configure the tuning process.

    Args:
        use_ray (bool): If True, uses Ray Tune for hyperparameter tuning. Defaults to False.
        iterations (int): The number of tuning iterations to perform. Defaults to 10.
        *args (list): Variable length argument list for additional arguments.
        **kwargs (any): Arbitrary keyword arguments. These are combined with the model's overrides and defaults.

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

    Raises:
        AssertionError: If the model is not a PyTorch model.
    """
    self._check_is_pytorch_model()
    if use_ray:
        from ultralytics.utils.tuner import run_ray_tune

        return run_ray_tune(self, max_samples=iterations, *args, **kwargs)
    else:
        from .tuner import Tuner

        custom = {}  # method defaults
        args = {**self.overrides, **custom, **kwargs, "mode": "train"}  # highest priority args on the right
         =======>return Tuner(args=args, _callbacks=self.callbacks)(model=self, iterations=iterations)

Problem Source: Upon reviewing the related Tuner class at ultralytics/engine/tuner.py, it appears that this class's call method does not return any value, which might be causing the observed issue.

Environment

ultralytics main branch

Minimal Reproducible Example

result = model.tune(data=train_dataset_path / "data.yaml", project=project_path/"tune",
           seed=1024, batch=120,
           space=default_space, device=[0], epochs=1, iterations=2,
           plots=True, save=True, val=False)

Additional

No response

Are you willing to submit a PR?

github-actions[bot] commented 6 months ago

👋 Hello @mpperez3, thank you for your interest in Ultralytics YOLOv8 🚀! We recommend a visit to the Docs for new users where you can find many Python and CLI usage examples and where many of the most common questions may already be answered.

If this is a 🐛 Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Join the vibrant Ultralytics Discord 🎧 community for real-time conversations and collaborations. This platform offers a perfect space to inquire, showcase your work, and connect with fellow Ultralytics users.

Install

Pip install the ultralytics package including all requirements in a Python>=3.8 environment with PyTorch>=1.8.

pip install ultralytics

Environments

YOLOv8 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

Ultralytics CI

If this badge is green, all Ultralytics CI tests are currently passing. CI tests verify correct operation of all YOLOv8 Modes and Tasks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

glenn-jocher commented 6 months ago

Thank you for bringing this to our attention! It seems like you've pinpointed a critical point where the expected behavior and the actual implementation diverge due to the missing return value from the Tuner class's call method.

To address this, you're absolutely right: ensuring the Tuner class appropriately returns the best hyperparameters is essential. This requires modifying the Tuner class, ensuring that its call method concludes with something akin to:

# Inside the Tuner class call method
return best_hyperparameters

And of course, ensuring that best_hyperparameters accurately captures the outcome of the tuning process. We'll need to review and possibly adjust the implementation within the Tuner class to ensure it can gather and return the necessary information.

Your detailed issue description makes it clear where the problem lies. 🎯 We value contributions from our community, and if you're inclined to dive deeper, please feel free to work on this and submit a PR. Otherwise, we'll take this up and ensure a fix is issued soon. Keep an eye on subsequent commits for updates.

github-actions[bot] commented 5 months ago

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

Feel free to inform us of any other issues you discover or feature requests that come to mind in the future. Pull Requests (PRs) are also always welcomed!

Thank you for your contributions to YOLO 🚀 and Vision AI ⭐