WongKinYiu / yolov7

Implementation of paper - YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors
GNU General Public License v3.0
13.43k stars 4.23k forks source link

Using Focal loss for imbalanced detection task #1677

Open programmeriop opened 1 year ago

programmeriop commented 1 year ago

Hi, I'm working on a multiple-class detection with imbalanced training data, in fact, the distribution of images in each class is different. I checked that the focal loss function is defined in yolov7, but I don't see anything in the documentation on how to use class balance loss, if you have a reference or method, could you please let me know?

yilmaz0734 commented 1 year ago

You can check the hyp file you are using for training. If you are training your model on a custom dataset, you should be using the "hyp.scratch.custom.yaml" . Go into it and change fl_gamma to 1.5 from zero (which is the default gamma value for focal loss). Then you can optimize it accordingly with you specific case.

programmeriop commented 1 year ago

@yilmaz0734 , Thanks for the reply. I learned that YOLOV7 is using 3 loss: localization loss (Lbox), confidence loss (Lobj), and classification loss (Lcls). In order to see the performance/improvement on the imbalanced data, do I need to replace those with focal loss? (for example, setting those parameters to zero) or just keep those intact and try to set the focal loss=1.5?

Sangh0 commented 1 year ago

One thing to note is that focal loss was not proposed to address data class imbalance, but rather to address foreground-background class imbalance that arises during training. To address the data class imbalance issue in your dataset, it may be better to use techniques such as over-sampling or under-sampling.

PHaiJun commented 1 year ago

You can check the hyp file you are using for training. If you are training your model on a custom dataset, you should be using the "hyp.scratch.custom.yaml" . Go into it and change fl_gamma to 1.5 from zero (which is the default gamma value for focal loss). Then you can optimize it accordingly with you specific case.

I try to hange fl_gamma to 1.5 from zero, but get worse result.