Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to train a model after carrying out hyperparameter tuning? #12805

Closed
1 task done
Habib0905 opened this issue May 18, 2024 · 7 comments
Closed
1 task done

How to train a model after carrying out hyperparameter tuning? #12805

Habib0905 opened this issue May 18, 2024 · 7 comments
Labels
question Further information is requested

Comments

@Habib0905
Copy link

Search before asking

Question

I have carried out hyperparameter tuning on a yolo pose estimation model. I have used this:
from ultralytics import YOLO

Initialize the YOLO model

model = YOLO("yolov8n.pt")

Tune hyperparameters on COCO8 for 30 epochs

model.tune(data="coco8.yaml", epochs=30, iterations=300, optimizer="AdamW", plots=False, save=False, val=False)

But now with these new hyperparameters I want to train a model. Could you please provide details on how to do it.

Additional

No response

@Habib0905 Habib0905 added the question Further information is requested label May 18, 2024
@Kayzwer
Copy link
Contributor

Kayzwer commented May 19, 2024

Search before asking

Question

I have carried out hyperparameter tuning on a yolo pose estimation model. I have used this:
from ultralytics import YOLO

Initialize the YOLO model

model = YOLO("yolov8n.pt")

Tune hyperparameters on COCO8 for 30 epochs

model.tune(data="coco8.yaml", epochs=30, iterations=300, optimizer="AdamW", plots=False, save=False, val=False)

But now with these new hyperparameters I want to train a model. Could you please provide details on how to do it.

Additional

No response

do you mean using previous hyperparameters on new training?

@glenn-jocher
Copy link
Member

@Kayzwer hello!

To train your model using the new hyperparameters you've tuned, you simply need to integrate those hyperparameters into your training session. Here’s how you can start training your model with the adjusted parameters:

from ultralytics import YOLO

# Load a model
model = YOLO("yolov8n.pt")  

# Train the model with the tuned hyperparameters
results = model.train(data="coco8.yaml", epochs=30, optimizer="AdamW")

Make sure to adjust the data, epochs, and any specific hyperparameters according to your tuned settings. Best of luck with your training, and I hope this helps! 😊

@Habib0905
Copy link
Author

Habib0905 commented May 19, 2024

Sorry, but I don't seem to understand how the code you provided is training the model with the tuned parameters. Do you mean that when loading the model, I should use the weights I got after hyperparameter tuning like this? :

model = YOLO("/content/runs/detect/tune/weights/last.pt")

And then train the model like this? :

model.train(data = 'config.yaml', epochs =100, imgsz = 640)

Or the training has to something with the best_hyperparameters.yaml file provided after hyperparameter tuning? Please guide me on this. Thanks.

@Kayzwer
Copy link
Contributor

Kayzwer commented May 19, 2024

Sorry, but I don't seem to understand how the code you provided is training the model with the tuned parameters. Do you mean that when loading the model, I should use the weights I got after hyperparameter tuning like this? :

model = YOLO("/content/runs/detect/tune/weights/last.pt")

And then train the model like this? :

model.train(data = 'config.yaml', epochs =100, imgsz = 640)

Or the training has to something with the best_hyperparameters.yaml file provided after hyperparameter tuning? Please guide me on this. Thanks.

@Habib0905 there is no way to load config like that, so sad you need to type it all out, or maybe you can make a pull request for this feature.

@glenn-jocher
Copy link
Member

@Habib0905 @Kayzwer yes you can load a custom config YAML like this instead of using default.yaml:

model.train(cfg = "path/to/custom/config.yaml")

This config.yaml should be a copy of default.yaml with any changes you want.

@Habib0905
Copy link
Author

Sorry, but I don't seem to understand how the code you provided is training the model with the tuned parameters. Do you mean that when loading the model, I should use the weights I got after hyperparameter tuning like this? :

model = YOLO("/content/runs/detect/tune/weights/last.pt")

And then train the model like this? :

model.train(data = 'config.yaml', epochs =100, imgsz = 640)

Or the training has to something with the best_hyperparameters.yaml file provided after hyperparameter tuning? Please guide me on this. Thanks.

Could you please clear my confusion about this?

@glenn-jocher
Copy link
Member

Hello! Absolutely, I can help clarify this for you. 🌟

After hyperparameter tuning, you should indeed use the best_hyperparameters.yaml file to set up your training configuration. This file contains the optimized hyperparameters from your tuning session. You can load these settings directly into your training setup like this:

from ultralytics import YOLO

# Load the model with the best weights from tuning
model = YOLO("/content/runs/detect/tune/weights/best.pt")

# Train the model using the optimized hyperparameters
model.train(data='config.yaml', cfg='path/to/best_hyperparameters.yaml', epochs=100, imgsz=640)

Here, cfg points to your best_hyperparameters.yaml which adjusts the training according to the tuned hyperparameters. This approach ensures that your model trains with the best-found settings. Happy training! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants