是否可以从 Keras 中的 flow_from_directory 自动推断 class_weight? [英] Is it possible to automatically infer the class_weight from flow_from_directory in Keras?

查看:26
本文介绍了是否可以从 Keras 中的 flow_from_directory 自动推断 class_weight?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个不平衡的多类数据集,我想使用 fit_generator 中的 class_weight 参数根据每个类的图像数量为类赋予权重.我正在使用 ImageDataGenerator.flow_from_directory 从目录加载数据集.

I have an imbalanced multi-class dataset and I want to use the class_weight argument from fit_generator to give weights to the classes according to the number of images of each class. I'm using ImageDataGenerator.flow_from_directory to load the dataset from a directory.

是否可以直接从 ImageDataGenerator 对象推断 class_weight 参数?

Is it possible to directly infer the class_weight argument from the ImageDataGenerator object?

推荐答案

刚刚想出了一种方法来实现这一点.

Just figured out a way of achieving 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 是每个图像的类列表.Counter(train_generator.classes) 创建每个类中图像数量的计数器.

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

请注意,这些权重可能不利于收敛,但您可以将其用作其他类型的基于出现的权重的基础.

Note that these weights may not be good for convergence, but you can use it as a base for other type of weighting based on occurrence.

这个答案的灵感来自:https://github.com/fchollet/keras/issues/1875#issuecomment-273752868

这篇关于是否可以从 Keras 中的 flow_from_directory 自动推断 class_weight?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆