【问题标题】:How does the one hot encoding work in Keras image data generator?Keras 图像数据生成器中的 one hot encoding 是如何工作的?
【发布时间】:2022-01-15 03:41:37
【问题描述】:

所以我有 3 个图像类,猫、牛和狗。

test_batches_1 = ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input) \
    .flow_from_directory(directory=test_path_1, target_size=(224,224), classes=['cat', 'dog','cow'], batch_size=10, shuffle=False)

当我这样做时

test_batches_1.class_indices

我明白了

{'cat': 0, 'dog': 1, 'cow': 2}

当我这样做时:-

test_imgs1, test_labels1 = next(test_batches_1)
print(test_labels1)

我明白了:-

[[1. 0. 0.]
 [1. 0. 0.]
 [0. 1. 0.]
 [0. 1. 0.]
 [0. 0. 1.]
 [0. 0. 1.]
 [0. 0. 1.]
 [0. 0. 1.]]

我更正了,因为我有 2 只猫、2 条狗和 4 只牛的照片。 但是,我不明白为什么 cat 的 0 显示为 1,0,0?或 1 表示狗显示为 0,1,0 和 2 表示牛显示为 0,0,1? 谁能帮忙解释一下逻辑?

【问题讨论】:

    标签: python keras image-classification


    【解决方案1】:

    原因:

    • 数组[1,0,0]中“1”的索引为0
    • 数组[0,1,0]中“1”的索引为1
    • 数组[0,0,1]中“1”的索引为2

    test_batches_1.class_indices 显示类的索引,所以第一个类是第 0 个索引...等

    【讨论】:

    • 第 0 位、第 1 位和第 2 位...谢谢
    猜你喜欢
    • 2021-11-06
    • 2019-12-11
    • 2021-11-02
    • 1970-01-01
    • 2017-05-11
    • 2018-07-10
    • 2021-08-13
    • 2020-04-13
    • 2019-04-21
    相关资源
    最近更新 更多