0
$\begingroup$

I am trying to make some semantic segmentation. I have 7 imbalanced classes in my case. I found several methods for handling Class Imbalance in a dataset is to perform Undersampling for the Majority Classes or Oversampling for the minority classes. but the most used one is introducing weights in the Loss Function. And I found several formula to calculate weights such us: wj=n_samples / (n_classes * n_samplesj) or wj=1/n_samplesj

which is the best one?

$\endgroup$
1
$\begingroup$

I really don't suggest Under/Oversampling as it would change the distribution of dataset. we should consider distribution as a useful feature of dataset. so I think the weighted loss would have better performance in most cases. if you're using TF/Keras, this link would be useful. you can use a variety of loss functions, like the below one, to apply the weight.

tf.nn.weighted_cross_entropy_with_logits(
    labels, logits, pos_weight, name=None
)

A value pos_weight > 1 decreases the false negative count, hence increasing the recall. Conversely setting pos_weight < 1 decreases the false positive count and increases the precision.

$\endgroup$
5
  • $\begingroup$ Thank you for your answer, Actually, I have to introduce manually the class weights as a Tensor, and I don't no which formula I have to use? $\endgroup$
    – safa
    May 2 '21 at 18:25
  • $\begingroup$ the one you mentioned in your question is the one which is also introduced in the link I put from TF and works okay based on my experience w_j=n_samples / (n_classes * n_samples_j). but you can think of it as a hyperparameter so using the resulted weights, you might still need some tests and trials to find the best values for weights in order to get the highest accuracy. $\endgroup$
    – SoheilStar
    May 2 '21 at 18:42
  • $\begingroup$ In fact on three classes it works really well. but when I go to the seven classes, I have the impression that it no longer works, for example I have a tensor of weight = [1.0, 2.51, 8.52, 2.83, 168.53, 469.19, 1.35] for the seven classes , do we need to standardize them? $\endgroup$
    – safa
    May 2 '21 at 21:33
  • $\begingroup$ hmm, I guess it is better to reduce 168 and 469 to something less than 100 or 80. it needs a kind of test and trial. I just use the weight function to get an idea about weights. $\endgroup$
    – SoheilStar
    May 2 '21 at 22:11
  • $\begingroup$ Thank you very much for your help ! $\endgroup$
    – safa
    May 3 '21 at 6:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.