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 · 11 comments
Closed
Labels
question Further information is requested Stale

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 ⭐!

@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
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
Projects
None yet
Development

No branches or pull requests

4 participants