-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
Using the "RMSProp" optimizer when training the YoloV11 model #1013
Comments
👋 Hello @alon-12345, thank you for raising an issue about Ultralytics HUB 🚀! Please visit our HUB Docs to learn more:
It looks like you're encountering an issue while configuring the If this is a ❓ Question, additional details like library versions, platforms used, and full code snippets where applicable will help us provide a better response. Additionally, please note that some parameters may not be supported in the current implementation of the YoloV11 model and its training API. This is an automated response 🤖, but don't worry—an Ultralytics engineer will review your issue and provide further assistance as soon as possible. Thank you for your patience 🚀! |
@alon-12345 thanks for your question about customizing RMSProp parameters in YOLO11 training! 🚀 While the Ultralytics YOLO training interface provides many optimizer options, some advanced parameters like For those wanting full control over optimizer parameters, you can implement a custom trainer by subclassing the default trainer. Here's a quick example: from ultralytics import YOLO
from ultralytics.engine.trainer import BaseTrainer
import torch.optim as optim
class CustomTrainer(BaseTrainer):
def build_optimizer(self, model, name="RMSprop", lr=0.001, momentum=0.9, decay=1e-5, **kwargs):
g = self.model.split_params()
return optim.RMSprop(g[2], lr=lr, momentum=momentum,
weight_decay=decay,
eps=1e-8, # your epsilon value
alpha=0.9) # rho parameter is called alpha in PyTorch
# Then use your custom trainer:
model = YOLO("yolo11n.yaml")
model.train(trainer=CustomTrainer, data="coco8.yaml", epochs=100, optimizer="RMSprop") The current implementation uses PyTorch's RMSprop defaults (alpha=0.99, eps=1e-8), but you can modify these in the custom optimizer. For most use cases, we find the default parameters work well, but advanced users might want to tune these for specific datasets. If you need these parameters added to the main API, feel free to open a feature request on our GitHub repo! We always appreciate community input to make YOLO more flexible. 💡 For more details on optimizer configurations, check out our Training Configuration docs. |
In addition, the model has no attribute/method 'split_params' |
Ultralytics doesn't support setting those parameters. You would need to modify the source code. |
@alon-12345 @YustasDev Hey guys! you could modify the trainer a bit to use from ultralytics import YOLO
from ultralytics.models.yolo.detect import DetectionTrainer
import torch.optim as optim
class CustomTrainer(DetectionTrainer):
def build_optimizer(self, model, name="RMSprop", lr=0.001, momentum=0.9, decay=1e-5, **kwargs):
return optim.RMSprop(
model.parameters(),
lr=lr,
momentum=momentum,
weight_decay=decay,
eps=1e-8, # your epsilon value
alpha=0.9,
) # rho parameter is called alpha in PyTorch
# Then use your custom trainer:
model = YOLO("yolo11n.yaml")
model.train(trainer=CustomTrainer, data="coco8.yaml", epochs=100) |
@Laughing-q , Then the training also starts, and if you look at the arguments with which the training started, you can see that "optimizer=RMSProp" But if you look at the rest of the arguments, you will see that the model training started with the default settings, and not with those defined in the "CustomTrainer". |
The arguments shown don't have an effect here. They are just showing the defaults. The optimizer is being overridden in the custom code |
@alon-12345 Hi the method here is to directly use the new def build_optimizer(self, model, name="RMSprop", lr=0.001, momentum=0.9, decay=1e-5, **kwargs):
return optim.RMSprop(
model.parameters(),
lr=0.1,
momentum=0.9,
weight_decay=decay,
eps=1e-8, # your epsilon value
alpha=0.9,
) then directly launch the training with model.train(trainer=CustomTrainer, data="coco8.yaml", epochs=100) |
Colleagues, thank you so much for your advices! |
Great observation! � You're absolutely correct. The To address this while maintaining full control over optimizer parameters, here's a professional solution: from ultralytics import YOLO
from ultralytics.models.yolo.detect import DetectionTrainer
import torch.optim as optim
class CustomTrainer(DetectionTrainer):
def build_optimizer(self, model, name="RMSprop", lr=0.001, momentum=0.9, decay=1e-5, **kwargs):
# Your custom parameters here
return optim.RMSprop(model.parameters(),
lr=0.1, # Custom learning rate
momentum=0.9, # Your modified momentum
eps=1e-8,
alpha=0.9)
# Train with explicit logging of critical parameters
model = YOLO("yolov11n.yaml")
results = model.train(
trainer=CustomTrainer,
data="coco8.yaml",
epochs=100,
optimizer="RMSprop",
momentum=0.9, # Still pass here for args.yaml logging
lr0=0.1 # Matches custom optimizer lr
) While the
print(f"Actual momentum used: {model.trainer.optimizer.param_groups[0]['momentum']}")
print(f"Actual alpha (rho) used: {model.trainer.optimizer.param_groups[0]['alpha']}") For permanent logging, you could add this to your custom trainer: def build_optimizer(...):
# ... custom optimizer creation ...
self.args.momentum = 0.9 # Force-update args namespace
return optimizer This architecture pattern ensures compatibility with Ultralytics' configuration system while allowing deep customization. If you'd like to see first-class support for these parameters in future releases, feel free to open a Feature Request! Keep up the great experimentation – this level of parameter tuning is exactly how cutting-edge results are achieved. 🔬 |
@pderrenger |
@alon-12345 You're very welcome! I'm glad you found the previous responses helpful. It's excellent that you're diving deep into optimizer behavior. The issue you're encountering with RMSProp, where losses are Here's a breakdown of potential causes and how to debug them, along with a refined code example: Potential Causes and Debugging Steps
Refined Code Example (with integrated debugging checks): from ultralytics import YOLO
from ultralytics.models.yolo.detect import DetectionTrainer
import torch.optim as optim
import torch
class CustomTrainer(DetectionTrainer):
def build_optimizer(self, model, name="RMSprop", lr=0.001, momentum=0.9, decay=1e-5, **kwargs):
# VERY IMPORTANT: Try a much lower learning rate first.
return optim.RMSprop(model.parameters(),
lr=1e-5, # Start very small
momentum=momentum,
weight_decay=decay,
eps=1e-8,
alpha=0.9)
def on_train_epoch_end(self, *args, **kwargs):
"""Checks for inf / nan values in training, raises warning if found."""
if not torch.isfinite(self.loss).all():
raise FloatingPointError('Loss is inf or nan, please check model, data and all hyperparameters')
if not torch.isfinite(self.tloss).all():
raise FloatingPointError('Train loss is inf or nan, please check model, data and all hyperparameters')
# Train with the custom trainer and EXPLICITLY set parameters
model = YOLO("yolov11n.yaml") # or your model
results = model.train(
trainer=CustomTrainer,
data="coco8.yaml", # or your data.yaml
epochs=100,
imgsz=640,
batch=16,
device=0,
# optimizer="RMSprop", # No need to specify here, handled by CustomTrainer
lr0=1e-5, # MUST match the lr in CustomTrainer. For logging in args.yaml.
momentum=0.9, # For logging purposes
)
# After training, check the optimizer's actual learning rate
print(f"Actual learning rate used: {model.trainer.optimizer.param_groups[0]['lr']}") Key Changes and Why:
Troubleshooting Steps (in order):
If you still have issues after these steps, please provide the following, and I'll assist further:
By systematically working through these steps, we can pinpoint the root cause of the issue and get your RMSProp training working correctly. |
@pderrenger |
@alon-12345 You're very welcome! I'm thrilled to hear the detailed explanation was helpful. That's what we strive for – empowering users like you to understand and push the boundaries of what's possible with YOLO. 😊 We appreciate you taking the time to engage so thoroughly with the debugging process. It helps us improve the framework for everyone. Don't hesitate to ask if you have any more questions as you continue your work! |
Search before asking
Question
I would like to use the “RMSprop” optimizer, when training the YoloV11 model, but I did not understand how to set the parameters “rho” (Decay rate for RMSProp) and “epsilon” (constant to prevent division by zero) by myself.
When using the Ultralytics API for training and specifying parameters as:
model.train(data="data.yaml",
device=0,
epochs=400,
optimizer="RMSprop",
batch=16,
lr0=0.001,
lrf=0.01,
weight_decay=0.0001,
momentum=0.9,
rho=0.9,
epsilon=1e-8,
workers=8,
single_cls=True,
degrees=45,
perspective=0.001,
erasing=0.0,
mosaic=0,
multi_scale=True,
patience=30)
I am getting an error message that “rho” and “epsilon” are not valid parameters for training YoloV11 model.
Additional
No response
The text was updated successfully, but these errors were encountered: