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

The question with repeated training #13211

Open
1 task done
arkerman opened this issue Jul 23, 2024 · 5 comments
Open
1 task done

The question with repeated training #13211

arkerman opened this issue Jul 23, 2024 · 5 comments
Labels
question Further information is requested

Comments

@arkerman
Copy link

Search before asking

Question

I now have 100 defect images. I first used the pre-trained model of yolov5s to train and get best.pt. After a week, I have another 100 images. I don’t want to add up the 100 images twice and re-train them. Instead, I want to keep the weights of best.pt and only train the next 100 images. What do I need to do?

Additional

No response

@arkerman arkerman added the question Further information is requested label Jul 23, 2024
@glenn-jocher
Copy link
Member

@arkerman hi there!

Thank you for your question. You can indeed continue training your model with the new set of images without starting from scratch. Here's how you can do it:

  1. Prepare Your Dataset: Ensure your new 100 images are labeled and organized in the same format as your initial dataset.

  2. Update Your Dataset YAML File: Add the paths to your new images and annotations to your dataset YAML file. This file should now include paths to both your old and new images.

  3. Resume Training: Use the --weights flag to specify your best.pt file and the --resume flag to continue training. Here is an example command:

    python train.py --img 640 --batch 16 --epochs 50 --data your_dataset.yaml --weights path/to/best.pt --resume

This command will load your existing weights and continue training with the combined dataset of old and new images.

If you encounter any issues, please ensure you are using the latest version of YOLOv5 by pulling the latest changes from the repository and updating your dependencies.

Feel free to reach out if you have any more questions or run into any issues. Happy training! 😊

@arkerman
Copy link
Author

@glenn-jocher Hi, really appreciate your reply and valuable advice!
But I still have doubts about this. Can this method ensure that the previously detected features can still be detected after adding new image training?

@glenn-jocher
Copy link
Member

@arkerman, thank you for your follow-up question! I'm glad to assist further.

Continuing training with additional images while retaining previously learned features is a common practice in transfer learning. When you resume training using the best.pt weights, the model retains the features it has already learned and further refines them with the new data. This approach helps the model to generalize better by incorporating new examples without forgetting the old ones.

Here are a few key points to ensure optimal results:

  1. Balanced Dataset: Make sure your dataset remains balanced. If the new images introduce a significant class imbalance, consider augmenting your dataset to maintain balance.

  2. Fine-Tuning: When resuming training, you might want to start with a lower learning rate to fine-tune the model gently. This helps in preserving the previously learned features while adapting to the new data.

    python train.py --img 640 --batch 16 --epochs 50 --data your_dataset.yaml --weights path/to/best.pt --hyp hyp.finetune.yaml

    In hyp.finetune.yaml, you can set a lower learning rate, for example:

    lr0: 0.001  # initial learning rate
  3. Monitor Performance: Keep an eye on your training metrics (loss, mAP, precision, recall) to ensure that the model is improving and not overfitting. You can use TensorBoard or the plots generated in the runs/train/exp directory to monitor these metrics.

  4. Validation: Validate your model on a separate validation set that includes examples from both the old and new datasets. This will give you a clear picture of how well your model is performing across the entire dataset.

By following these steps, you can effectively continue training your model with new images while retaining the ability to detect previously learned features.

If you have any more questions or need further assistance, feel free to ask. Happy training! 😊

@arkerman
Copy link
Author

Thanks a lot dude! @glenn-jocher
I will try your valuable suggestions and do some experiment。
Thanks again!

@glenn-jocher
Copy link
Member

You're very welcome! 😊

I'm glad to hear that you found the suggestions helpful. Experimenting with different strategies is a great way to fine-tune your model and achieve the best results. If you encounter any issues or have further questions during your experiments, feel free to reach out here. The YOLO community and the Ultralytics team are always here to help.

Happy training, and best of luck with your project! 🚀

P.S. If you haven't already, you might find additional useful tips in our Tips for Best Training Results guide. It covers various aspects of dataset preparation, model selection, and training settings to help you get the most out of YOLOv5.

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