使用Keras图像生成器平衡不平衡的数据集

39

keras

ImageDataGenerator

可以用于"生成带有实时数据增强的张量图像数据批次"

这个教程在这里演示了如何使用ImageDataGenerator增强一个小但平衡的数据集。是否有一种简单的方法可以使用此生成器来增强一个严重不平衡的数据集,从而生成的数据集是平衡的?

2个回答

53
这不是处理不平衡数据的标准方法。我认为这样做并没有真正的理由 - 你会显著改变类别的分布,其中较小的类别现在变得更加不可变。较大的类别将具有丰富的变化,而较小的类别将是许多相似的图像,具有小的仿射变换。它们将生活在比大多数类别更小的图像空间中。
更标准的方法包括:
- 在model.fit中使用class_weights参数,可以使模型从少数类别中学习更多。 - 减少多数类别的大小。 - 接受不平衡性。深度学习可以应对这种情况,只需要更多的数据(实际上是解决所有问题的解决方案)。
前两个选项实际上是一种hack,可能会损害您应对真实世界(不平衡)数据的能力。它们都没有真正解决低变异性的问题,这是数据过少固有的问题。如果在模型训练后将应用于真实世界数据集不是问题,并且您只想在手头的数据上获得良好的结果,则这些选项很好(比为单个类别创建生成器要容易得多)。
The third option is the right way to go if you have enough data (as an example, the recent paper from Google about detecting diabetic retinopathy achieved high accuracy in a dataset where positive cases were between 10% and 30%).
If you truly want to generate a variety of augmented images for one class over another, it would probably be easiest to do it in pre-processing. Take the images of the minority class and generate some augmented versions, and just call it all part of your data. Like I say, this is all pretty hacky.

非常感谢您分享您的见解。我会研究那篇Google论文。 - Oblomov
嗨 @user1934212,你解决了吗? - Anshuman Kumar

3
你可以使用这种策略基于不平衡来计算权重:
from sklearn.utils import class_weight 
import numpy as np

class_weights = class_weight.compute_class_weight(
           'balanced',
            np.unique(train_generator.classes), 
            train_generator.classes)

train_class_weights = dict(enumerate(class_weights))
model.fit_generator(..., class_weight=train_class_weights)

这个答案受到了Is it possible to automatically infer the class_weight from flow_from_directory in Keras?的启发。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接