-
Hi, I am new to Pytroch and I have a difficulty in understanding the concept of setting class weights for imbalanced dataset. I know I can set class weights in Tensorflow and Keras using
I would like to know if this
or this function is doing exactly the same thing? Line 495 in 7ebb5e5 Thanks in advance. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 8 replies
-
@keerti-ds class weights are computed automatically for all datasets in train.py here: Line 255 in 7ebb5e5 They're not used during training by default, but you can do whatever you want with them. |
Beta Was this translation helpful? Give feedback.
-
@glenn-jocher Thanks for the reply. What do I need to change to use them for training? Because I don't see any parameter where I can pass them. |
Beta Was this translation helpful? Give feedback.
-
I see BCEWithLogitsLoss also has a weight argument. Can I not use this? Is there any particular reason you suggested me BCELoss or CELoss? And if I would want to pass the class weights, can I just add them in |
Beta Was this translation helpful? Give feedback.
-
Thanks! But I see weight argument in docs.
And pw is here And thanks, I will try with other losses. |
Beta Was this translation helpful? Give feedback.
-
Correct me if I was wrong Authors provide sampling method for imbalance dataset through Lines 288 to 291 in 63ddb6f where they use class weights and instances' classes in a image to decide how dataloader sample data (since this satistics needs dataloader to be static, so they use Dataloader not InfiniteDataloader). In the other word, sampling more images with rare classes than the others. The other method is directly change the class weight which is this thread mainly discussed. We can get the class weights directly from authors' code Line 266 in 63ddb6f with the shape of (nc). One can save/copy it, then put it to hyp.scratch.yaml 's option cls_pw .To be clearify, https://pytorch.org/docs/stable/generated/torch.nn.BCEWithLogitsLoss.html says that
|
Beta Was this translation helpful? Give feedback.
-
Hi! |
Beta Was this translation helpful? Give feedback.
@glenn-jocher Thanks for the reply. What do I need to change to use them for training? Because I don't see any parameter where I can pass them.