python - Keras ImageDataGenerator() 如何从数据中获取所有标签

标签 python keras

我在 Keras 中使用 ImageDataGenerator(),我想获取整个测试数据的标签。

目前我正在使用下面的代码来完成这个任务:

test_batches = ImageDataGenerator().flow_from_directory(...)

test_labels = []

for i in range(0,3):
    test_labels.extend(np.array(test_batches[i][1]))

然而,这段代码之所以有效,是因为我知道我总共有 150 张图像,并且我的批处理大小定义为 50。

此外使用:

imgs, labels = next(test_batches)

正如关于此主题的类似帖子中所建议的那样,只返回一批标签,而不是整个数据集。因此,我想知道是否有比我上面使用的方法更有效的方法。

最佳答案

您可以简单地从包含单热编码中的标签和索引的 DirectoryIterator 中获取字典。然后访问 key 将为您提供所有标签。

test_batches.class_indices.keys()

关于python - Keras ImageDataGenerator() 如何从数据中获取所有标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48373685/

相关文章:

python - python中矩形的颜色

python - python是否支持导入包

tensorflow - 即使使用相同的数据集进行训练和验证,Keras 验证准确度也远低于训练准确度

python - 为什么我会收到此图表已断开连接的错误?

tensorflow - 如何从不同时期的 Mask R-CNN 开始训练?

python - ValueError : Shapes (16, ) 和 (1, 16) 在 tensorflow1.0 中不兼容

python - 使用 python Wand 替换颜色但坐标未知

python - 解释 3D 数组在内存中的间距、宽度、高度、深度

python - 值错误: name for name_scope must be a string

machine-learning - GAN 在短短几个 epoch 内就收敛了