Orion-AI-Lab / S4A-Models

Various experiments on the Sen4AgriNet dataset
MIT License
29 stars 9 forks source link

How was the class weights were defined #11

Open Guolin-Yin opened 2 months ago

Guolin-Yin commented 2 months ago

Dear authors,

Thanks for your amazing work, it helps me a lot.

However, I am wondering how the class weight was defined (quoted). I guess it could be related to the number of samples for each class in the dataset, but I am not sure. Please give me some hints on how it is computed.

https://github.com/Orion-AI-Lab/S4A-Models/blob/ae5a3761395cd259724516fa52a89879850558ab/utils/settings/config.py#L128

buffalotheory commented 1 month ago

Hello,

I've been looking into this as well, but am not officially involved with the project.

The weights are calculated with the following block of code in compute_class_weights.py

  all_counts = sum(list(class_pixel_counts.values()))
  n_classes = len(class_pixel_counts)
  class_weights = {k: all_counts / (n_classes * v) for k, v in class_pixel_counts.items()}

all_counts / n_classes yeilds the average number of counts per class.

Each class weight is then the average_count_per_class / count_per_class

This means that:

  1. The values will vary from zero to positive infinity
  2. The weight for each class is inversely-proportional to number of pixels for that class
  3. If a class contains exactly the average number of pixels, then it's weight will be 1