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

Optimize parameter #11932

Open
1 task done
caarmeecoorbii opened this issue May 10, 2024 · 14 comments
Open
1 task done

Optimize parameter #11932

caarmeecoorbii opened this issue May 10, 2024 · 14 comments
Labels
question Further information is requested

Comments

@caarmeecoorbii
Copy link

Search before asking

Question

Hello, I noticed that in the training parameters, the learning rate 'lr0' is set to 0.01. However, I would like to change it to a lower value, for example, 0.0001. But since I have optimizer = 'auto', the learning rate I defined in 'lr0' is not taken into account. Which optimizer mode has a lower 'lr0'? Thank you.

Additional

No response

@caarmeecoorbii caarmeecoorbii added the question Further information is requested label May 10, 2024
Copy link

👋 Hello @caarmeecoorbii, 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
Copy link
Member

@caarmeecoorbii hello! If you're looking to customize the learning rate when using optimizer='auto', which typically selects the optimizer based on the model architecture, then I recommend manually setting the optimizer to one that allows for customization, like 'SGD' or 'Adam'.

Here's how you can explicitly set the optimizer and learning rate in your training script:

yolo detect train data=coco128.yaml model=yolov8n.pt epochs=100 optimizer=sgd lr0=0.0001

This command sets the optimizer to 'SGD' and the learning rate (lr0) to 0.0001. Adjust accordingly if you prefer a different optimizer or configuration.

@caarmeecoorbii
Copy link
Author

Thank you very much. I have another question: I'm training the YOLOv8x model with my own detections on a total of 42,750 images, and then I plan to use the weights for tracking. What I don't understand is why, when I use my training weights, I get fewer detections, whereas when I use the yolov8x.pt weights, I get more detections, even though I'm training the detector with my own images. I've tried training for 1 epoch with a learning rate of 0, and I still get fewer detections than with yolov8x.pt. Do you know what could be happening?

@glenn-jocher
Copy link
Member

Hello! Thanks for reaching out with your question. 😊 It looks like you might be facing an issue with underfitting if your custom-trained model with your specific dataset yields fewer detections compared to the pre-trained yolov8x.pt model.

Training for only 1 epoch, especially with a learning rate set to 0, is likely not sufficient for the model to learn effectively from your data. The learning rate being set to 0 means that the weights of the model do not get updated during training, rendering the training process ineffective.

I would recommend training for more epochs and using a positive learning rate. Here’s an example command to adjust your learning rate:

yolo detect train data=your_dataset.yaml model=yolov8x.yaml epochs=100 lr0=0.01

This sets the learning rate to a starting value of 0.01 and trains for 100 epochs, which should provide more opportunity for your model to learn from your dataset. Adjust the number of epochs and learning rate based on your validation results for optimal performance.

@caarmeecoorbii
Copy link
Author

Hello again, I already trained my detections with the YOLOv8x detector for 100 epochs and took the best.pt weights, but the results do not improve compared to using the yolov8x.pt weights. What I mean is that if I use the YOLOv8x detector and train it with my data, and then perform tracking on my own dataset, I get better results with the yolov8x.pt weights, which have not been trained with my data, than with the best.pt weights, which have been trained with similar images. Thanks for the feedback!
image
image

@glenn-jocher
Copy link
Member

@caarmeecoorbii hello! Thanks for updating us on your progress. It's intriguing that the pre-trained yolov8x.pt weights are performing better than your custom-trained best.pt weights. This could be due to several factors:

  1. Data Diversity and Quality: Ensure your dataset is diverse and representative enough of the scenarios in which you want the model to perform. Also, check if the annotations are accurate.

  2. Overfitting/Underfitting: Although you've trained for 100 epochs, it's possible that the model might be either overfitting or underfitting your data. You might want to experiment with different hyperparameters. Using the Val mode regularly during training can help validate this by checking the model's performance on a validation set.

  3. Post-Processing Thresholds: Differences in detection might also be due to thresholds used for confidence and non-max suppression (NMS). Adjusting conf and iou thresholds might yield better results.

yolo detect predict model=best.pt conf=0.3 iou=0.5

It's often a process of tweaking and testing, so don't hesitate to experiment with these aspects. Let us know how it goes! 😊

@caarmeecoorbii
Copy link
Author

Thank you for all your comments. What I've done is train the detector with a much lower learning rate = 0.0001, and I manage to improve the tracking results slightly, but I still can't improve the weights results of YOLOv8x. I've tried adjusting the conf and iou parameters, but fewer detections are generated. Do you know if I could change any other parameter in training to further improve my results, such as the dropout parameter? I'm attaching the loss graphs for your reference.
image
image

@glenn-jocher
Copy link
Member

Hello and thanks for providing the updates and loss graphs! 🌟 It looks like you've made some progress, which is great to hear. If you’ve already worked with learning rates, conf, and iou adjustments without the desired improvement, considering adjustments in other hyperparameters could indeed be beneficial.

One parameter you might explore adjusting is the dropout rate if the model seems to be overfitting. Often, a slight increase in dropout can help regularize the model, though this needs to be finely balanced, as too much can harm your model's ability to learn.

Additionally, since you're working on tracking, double-check your anchor box scales if you haven’t already. They should be well-tuned to the sizes of objects in your dataset, as mismatched anchor sizes could lead to suboptimal detection performance.

Here’s a quick adjustment to add dropout:

yolo detect train data=your_dataset.yaml model=yolov8x.yaml epochs=100 lr0=0.0001 dropout=0.1

Adjust dropout=0.1 to the value that suits your model's needs. Keep iterating and good luck! 😊

@caarmeecoorbii
Copy link
Author

Thank you so much! Could you provide some guidance on how to adapt anchor box dimensions?

@glenn-jocher
Copy link
Member

Hi there! Absolutely, adapting your anchor box dimensions can be very effective for improving model performance, especially if your objects have particular shapes or sizes.

To adjust the anchor boxes for your dataset, you typically start by analyzing the common dimensions of your objects. This can be done by clustering the dimensions (width and height) of your ground truth boxes, a technique often done using k-means clustering.

Once you have your new anchor sizes, you'll modify them in your model's configuration file (usually a .yaml file) under the anchors field. Here's an example snippet from a configuration file:

anchors:
  - [10,13, 16,30, 33,23]  # Small anchors
  - [30,61, 62,45, 59,119]  # Medium anchors
  - [116,90, 156,198, 373,326]  # Large anchors

Replace these with your calculated anchor boxes. Ensure your model is retrained or fine-tuned with these new anchors for best results. 🚀

Hope this helps! Let me know if you need more detailed steps! 😊

@caarmeecoorbii
Copy link
Author

I have used the ByteTrack and BoT-SORT trackers for object tracking; I'm not using a custom tracker. I tried changing the dropout value to 0.1, but it didn't make a difference. Are there any other parameters I could try adjusting during training?

@glenn-jocher
Copy link
Member

Hello! Thanks for reaching out. Adjusting dropout might not always yield noticeable differences, especially if the model's overfitting isn't the primary issue. You could try tweaking the weight_decay to help regularize the model further, or adjust the momentum parameters if you're using SGD, which can influence the convergence behavior during training.

Here's a quick example of how you might adjust these parameters:

yolo detect train data=your_dataset.yaml model=yolov8x.yaml epochs=100 lr0=0.0001 weight_decay=0.0005 momentum=0.9

Adjusting these could provide different learning dynamics that might better suit your data. Good luck, and let us know how it goes! 😊

@caarmeecoorbii
Copy link
Author

Thanks! I have another question. When I do tracking, can I also adjust the values of conf and iou? What are their default values?

@glenn-jocher
Copy link
Member

glenn-jocher commented May 26, 2024

Absolutely! Yes, you can adjust the conf and iou values during tracking. The default values are conf=0.25 and iou=0.45. You can modify them like this:

yolo track model=yolov8n.pt source=path/to/video.mp4 conf=0.30 iou=0.50

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

2 participants