Is it possible to automatically derive class_weight from flow_from_directory in Keras?

I have an unbalanced dataset for several classes, and I want to use the argument class_weightfrom fit_generatorto give classes weight according to the number of images of each class. I use ImageDataGenerator.flow_from_directoryto load a dataset from a directory.

Is it possible to directly derive an argument class_weightfrom an object ImageDataGenerator?

+4
source share
1 answer

Just figured out a way to achieve this.

from collections import Counter
train_datagen = ImageDataGenerator()
train_generator = train_datagen.flow_from_directory(...)

counter = Counter(train_generator.classes)                          
max_val = float(max(counter.values()))       
class_weights = {class_id : max_val/num_images for class_id, num_images in counter.items()}                     

model.fit_generator(...,
                    class_weight=class_weights)

train_generator.classes- This is a list of classes for each image. Counter(train_generator.classes)Creates a counter of the number of images in each class.

, , .

: https://github.com/fchollet/keras/issues/1875#issuecomment-273752868

+8

Source: https://habr.com/ru/post/1671392/


All Articles