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

Is it possible to change class weight? #2703

Closed
1 task done
KazuhideMimura opened this issue May 19, 2023 · 15 comments
Closed
1 task done

Is it possible to change class weight? #2703

KazuhideMimura opened this issue May 19, 2023 · 15 comments
Labels
question Further information is requested Stale

Comments

@KazuhideMimura
Copy link

Search before asking

Question

Hi, I'd like to train on a dataset with imbalanced number of classes.
I trained a detection model with on a dataset with 2 classes, and class1:class2 is ~1:4.
The trained model seems to be more fitted to detect class2, and cannot detect class1 as I expected. Therefore, I'd like to increase the weight for class1 and decrease class2's weight, which is typically conducted in image classification tasks.

Is that possible?

Additional

No response

@KazuhideMimura KazuhideMimura added the question Further information is requested label May 19, 2023
@github-actions
Copy link

👋 Hello @KazuhideMimura, thank you for your interest in YOLOv8 🚀! We recommend a visit to the YOLOv8 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.

Install

Pip install the ultralytics package including all requirements in a Python>=3.7 environment with PyTorch>=1.7.

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

@KazuhideMimura hello!
Yes, it is possible to change class weights in YOLOv8. You can apply a loss weight multiplier to each class in the loss function. By adjusting the weight values, you can adjust the model's bias towards specific classes and achieve better detection results on imbalanced datasets.
To implement this, check the weight parameter in the nn.BCEWithLogitsLoss() as this will allow you to modify the per-class weights. We recommend using a higher value for the underrepresented class to balance out the distribution.
Best of luck with your project!

@KazuhideMimura
Copy link
Author

@glenn-jocher Hi, thank you for your reply!
Do you mean that I should edit L147 of v8/detect/train.py as follows?
pos_weight=torch.FloatTensor([2.5, 0.625]) # 4:1 (for example)
self.bce = nn.BCEWithLogitsLoss(reduction='none', pos_weight=pos_weight)

@glenn-jocher
Copy link
Member

@KazuhideMimura You're welcome! Yes, that's exactly right. By modifying the pos_weight parameter in nn.BCEWithLogitsLoss(), you can adjust the weight assigned to each class in the loss function. You should adjust the weight values to balance the model's bias towards specific classes and achieve better detection results on imbalanced datasets. In your case, where class1 is underrepresented compared to class2, you should choose a higher value for class1 to balance out the distribution. Best of luck with your project!

@KazuhideMimura
Copy link
Author

Thanks, I'll try!

@glenn-jocher
Copy link
Member

@KazuhideMimura of course, KazuhideMimura! You understood it correctly. By modifying the pos_weight parameter in nn.BCEWithLogitsLoss(), you can adjust the weight assigned to each class in the loss function. You should adjust the weight values to balance the model's bias towards specific classes and achieve better detection results on imbalanced datasets. In your case, where class1 is underrepresented compared to class2, you should choose a higher value for class1 to balance out the distribution. Best of luck with your project!

@github-actions
Copy link

👋 Hello there! We wanted to give you a friendly reminder that this issue has not had any recent activity and may be closed soon, but don't worry - you can always reopen it if needed. If you still have any questions or concerns, please feel free to let us know how we can help.

For additional resources and information, please see the links below:

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

@github-actions github-actions bot added the Stale label Jun 23, 2023
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 3, 2023
@NikhilM97
Copy link

NikhilM97 commented Jul 25, 2023

Hi,
@glenn-jocher and @KazuhideMimura
I am unable to find pos_Weight parameter in nn.BCEWithLogitsLoss().Also not able to find this train.py file L147 of v8/detect/train.py.
Can i please get some help.Thanks

@glenn-jocher
Copy link
Member

@NikhilM97 hello, I apologize for the confusion, but in the current YOLOv8 implementation, nn.BCEWithLogitsLoss() does not have a default pos_weight parameter. If it was mentioned, it was a conceptual suggestion about how one might adjust class weights.

As for the train.py file, it might have been moved or refactored. The current YOLOv8 implementation could be different from the previous one when that guidance was provided.

Please ensure you're working with the most recent version of YOLOv8 and consult the most recent documentation for the most up-to-date information.

I hope this helps. Let me know if you have more questions.

@NikhilM97
Copy link

NikhilM97 commented Jul 26, 2023

Thanks for the reply @glenn-jocher ,Is there any way i can change the class weights in yolov8?Because i am dealing with the imbalanced dataset.I did go through the documentation.But i couldn't find any parameters,where i can change change weights of classes.Can i change class weights here?https://github.com/ultralytics/ultralytics/blob/main/ultralytics/utils/loss.py#L115
By modifying pos_weight parameter?

@glenn-jocher
Copy link
Member

Hello @NikhilM97,

Apologies for the confusion earlier.

Under the current YOLOv8 architecture, we do not have a direct pos_weight parameter or similar that allows for explicit weighting of classes in the loss function. In the current implementation, all classes are treated equally during training.

While deep learning models like YOLOv8 can generally handle class imbalance to some extent, one common approach to deal with severe class imbalance in the object detection tasks is to oversample the minority class or undersample the majority class.

You can also try data augmentation techniques geared towards the minority class to artificially balance your dataset. However, I would advise caution and careful fine-tuning, as too much augmentation could make the model overfit to the minority class.

While adjusting class weights might seem the norm in problems like multi-class classification, it doesn't directly extend to object detection tasks due to the complexity of considerations including the object scales, ratios, and spatial locations.

Hope this clarifies your issue. Please let me know if you have further questions.

@NikhilM97
Copy link

Hi @glenn-jocher ,
Thanks for reply and suggestion.

@glenn-jocher
Copy link
Member

Hello @NikhilM97,

You're welcome! I'm glad I could help. If you have any more questions or run into any more issues, don't hesitate to ask. YOLOv8 is a constantly evolving project, and I'm here to make sure you're able to get the most out of it. Happy coding!

@hulkds
Copy link
Contributor

hulkds commented Feb 29, 2024

@glenn-jocher I have created a PR for this feature. Please check that!
#8557
Thanks

@glenn-jocher
Copy link
Member

@hulkds hi there!

Awesome, thanks for the heads up and for your contribution! 😊 I'll take a look at PR #8557 and get back to you shortly.

Happy coding!

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