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 can I train one or several new classes on top of an existing training set. #3258

Closed
SpongeBab opened this issue May 20, 2021 · 15 comments
Closed
Labels
question Further information is requested Stale Stale and schedule for closing soon

Comments

@SpongeBab
Copy link
Contributor

❔Question

How can I train one or several new classes on top of an existing training set.

Additional context

For example,If I have a new dataset, and I want to add the new class on the trained COCO weights.Um.....What I should do or is there any guide?

@SpongeBab SpongeBab added the question Further information is requested label May 20, 2021
@yannclaes
Copy link

I think this tutorial should do it. All you need to do is to adapt the data/.yaml file for your specific dataset !

@SpongeBab
Copy link
Contributor Author

SpongeBab commented May 21, 2021

@yannclaes No, you don't understand what I'm saying! What I mean is ,simply,I want to train a new class on the base of the model that has been trained.At the same time, keep the classes that have been trained.I need to get 80+x classes.
@glenn-jocher This is a meaningless reply.You shouldn't give him 👍.

@glenn-jocher
Copy link
Member

@SpongeBab by definition training a model modifies all of the weights and biases in it to minimize the loss on your new labels.

There is no such thing as training a model on new classes while retaining existing weights and bias values anymore than I can drink a glass of water without affecting the water in the glass.

@yannclaes
Copy link

yannclaes commented May 21, 2021

@SpongeBab I think my answer still applies, but you have to provide COCO images too. When you modify your data.yaml, the number of classes is modified (as you noted, 80 + x) thus the number of outputs is modified too (it becomes nb_anchors * (80 + x + 5)). When loading your model with pre-trained weights, you'll fall into this:

state_dict = intersect_dicts(state_dict, model.state_dict(), exclude=exclude) # intersect

Your Detect() module will be initialized with new weights corresponding to this new nc because shapes don't match anymore. So the bottom line is that to train a model on COCO + x classes, you need to provide data (images + labels) for all classes you want to train on, including COCO classes.

Edit: found this after a quick search, conclusions were already identical.

@SpongeBab
Copy link
Contributor Author

SpongeBab commented May 21, 2021

@glenn-jocher @yannclaes Thank you for your kind answers.
@glenn-jocher Yeah,I know. Thank you for your patient answer😆.I found some information about this:https://zhuanlan.zhihu.com/p/73162940. Emmm,What do you think about it?I'm trying to run the program. But there are some problems that I haven't solved yet.
I think this is a very very great and significant technology.And I'm really looking forward to your V5 paper.The Scaled-yolo, which have published a paper, its code is similar to your code. You've achieved a significant improvement👍.
Edit:Something great, the V5 can train at dozens of times the speed of the V4, or even more. And, of course, about three to five times faster than Scaled-YOLO.Even retraining is acceptable.

@SpongeBab
Copy link
Contributor Author

@yannclaes Hi.
If I understand you correctly. You mean to add the new images and categories to the COCO data set and retrain again, right? It's like training class 83 instead of 80+3.
What I want to do is keep 80 classes of COCO and then train a new dataset, assuming it contains only two classes, then I use the weights of COCO already trained and train these two classes on the new dataset and finally I am able to detect 83(80+3).
And I've seen that so far it doesn't seem to work......
Some information: https://zhuanlan.zhihu.com/p/73162940.

@yannclaes
Copy link

@yannclaes Hi.
If I understand you correctly. You mean to add the new images and categories to the COCO data set and retrain again, right? It's like training class 83 instead of 80+3.
What I want to do is keep 80 classes of COCO and then train a new dataset, assuming it contains only two classes, then I use the weights of COCO already trained and train these two classes on the new dataset and finally I am able to detect 83(80+3).
And I've seen that so far it doesn't seem to work......
Some information: https://zhuanlan.zhihu.com/p/73162940.

That's exactly it ! I guess your goal is to save training time... However I think you have no other choice than full re-training from COCO weights, as explained in the tutorial. Nevertheless, preparing your data to include your 3 new classes should not be too hard as you would simply need to move your splits to the appropriate COCO split folders and do the same for your annotation files.

I didn't know about GroupSoftmax but it seems rather aimed towards dealing with class imbalance in the learning set, so I don't see how it could help here but I might miss it ;)

@SpongeBab
Copy link
Contributor Author

@yannclaes ahah,yeah. Everyone is working to improve AP.Thank you again.

@glenn-jocher
Copy link
Member

@SpongeBab @yannclaes yes that is correct. You can train multiple datasets simultaneously by adding them to the train and val fields of your dataset.yaml as a list. All the datasets in the list must share common class naming convention though, so for example if you want to train classes in addition to coco you can add your custom dataset to the train/val lists, making sure it's class indices start at 80. GlobalWheat2020.yaml is a good example of grouping multiple datasets togethor:

# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
train: # 3422 images
- ../datasets/GlobalWheat2020/images/arvalis_1
- ../datasets/GlobalWheat2020/images/arvalis_2
- ../datasets/GlobalWheat2020/images/arvalis_3
- ../datasets/GlobalWheat2020/images/ethz_1
- ../datasets/GlobalWheat2020/images/rres_1
- ../datasets/GlobalWheat2020/images/inrae_1
- ../datasets/GlobalWheat2020/images/usask_1
val: # 748 images (WARNING: train set contains ethz_1)
- ../datasets/GlobalWheat2020/images/ethz_1
test: # 1276 images
- ../datasets/GlobalWheat2020/images/utokyo_1
- ../datasets/GlobalWheat2020/images/utokyo_2
- ../datasets/GlobalWheat2020/images/nau_1
- ../datasets/GlobalWheat2020/images/uq_1

@github-actions
Copy link
Contributor

github-actions bot commented Jun 23, 2021

👋 Hello, this issue has been automatically marked as stale because it has not had recent activity. Please note it will be closed if no further activity occurs.

Access additional YOLOv5 🚀 resources:

Access additional Ultralytics ⚡ resources:

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 YOLOv5 🚀 and Vision AI ⭐!

@github-actions github-actions bot added the Stale Stale and schedule for closing soon label Jun 23, 2021
@rizi1122
Copy link

@SpongeBab hello !
hope so you are fine!
i am solve these type of problem but still not solve .kindly share with me your best idea and notebook ?
thanks in advance.

@MartinPedersenpp MartinPedersenpp mentioned this issue Nov 21, 2022
1 task
@mariswarycharan
Copy link

i have already model object detection modle in yolov8 with 25 classes and now i want to add new label 26 th i want to train that exciting model with new class only i want to train only new class images not that others 25 already train but i want to updated model with 26 classes

is there any way to this

@glenn-jocher
Copy link
Member

Hello @mariswarycharan,

To add a new class to your existing YOLOv8 model with 25 classes, you will need to follow a few steps to ensure the model is updated correctly. Here’s a concise guide to help you achieve this:

  1. Update the Dataset Configuration:

    • Modify your dataset.yaml file to include the new class. Ensure the names section lists all 26 classes, including the new one.
  2. Prepare the New Data:

    • Collect and label images for the new class. Ensure the labels are in the correct YOLO format.
  3. Modify the Model Configuration:

    • Update the model configuration to reflect the new number of classes. This involves changing the nc (number of classes) parameter in your model configuration file to 26.
  4. Transfer Learning:

    • Load the pre-trained weights of your existing model and fine-tune it on the new dataset. This way, the model retains the knowledge of the original 25 classes while learning the new class.

Here’s a code snippet to illustrate the training process:

import torch
from yolov5 import train

# Load the pre-trained model
model = torch.hub.load('ultralytics/yolov5', 'custom', path='path/to/your/model.pt')

# Update the number of classes
model.nc = 26  # Update this to the new number of classes
model.names = ['class1', 'class2', ..., 'class26']  # Update with your class names

# Train the model on the new dataset
train.run(imgsz=640, epochs=10, data='path/to/your/dataset.yaml', weights='path/to/your/model.pt')
  1. Fine-Tuning:

    • Fine-tune the model on the new dataset. This involves training the model with a lower learning rate to adjust the weights for the new class without significantly altering the weights for the existing classes.
  2. Validation:

    • After training, validate the model to ensure it performs well on both the original 25 classes and the new class.

For more detailed instructions, you can refer to the YOLOv5 documentation.

If you encounter any issues during this process, please ensure you are using the latest versions of the packages and feel free to reach out with specific questions. Good luck with your training! 🚀

@Gnanapriya2000
Copy link

Hello @mariswarycharan,

To add a new class to your existing YOLOv8 model with 25 classes, you will need to follow a few steps to ensure the model is updated correctly. Here’s a concise guide to help you achieve this:

  1. Update the Dataset Configuration:

    • Modify your dataset.yaml file to include the new class. Ensure the names section lists all 26 classes, including the new one.
  2. Prepare the New Data:

    • Collect and label images for the new class. Ensure the labels are in the correct YOLO format.
  3. Modify the Model Configuration:

    • Update the model configuration to reflect the new number of classes. This involves changing the nc (number of classes) parameter in your model configuration file to 26.
  4. Transfer Learning:

    • Load the pre-trained weights of your existing model and fine-tune it on the new dataset. This way, the model retains the knowledge of the original 25 classes while learning the new class.

Here’s a code snippet to illustrate the training process:

import torch
from yolov5 import train

# Load the pre-trained model
model = torch.hub.load('ultralytics/yolov5', 'custom', path='path/to/your/model.pt')

# Update the number of classes
model.nc = 26  # Update this to the new number of classes
model.names = ['class1', 'class2', ..., 'class26']  # Update with your class names

# Train the model on the new dataset
train.run(imgsz=640, epochs=10, data='path/to/your/dataset.yaml', weights='path/to/your/model.pt')
  1. Fine-Tuning:

    • Fine-tune the model on the new dataset. This involves training the model with a lower learning rate to adjust the weights for the new class without significantly altering the weights for the existing classes.
  2. Validation:

    • After training, validate the model to ensure it performs well on both the original 25 classes and the new class.

For more detailed instructions, you can refer to the YOLOv5 documentation.

If you encounter any issues during this process, please ensure you are using the latest versions of the packages and feel free to reach out with specific questions. Good luck with your training! 🚀

But while detection ,the model is not detecting the previous classes it only detects the new classes

@glenn-jocher
Copy link
Member

glenn-jocher commented Aug 13, 2024

Hello @Gnanapriya2000 yes this is normal. Any classes not present in the new training will naturally not be learned.

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

No branches or pull requests

6 participants