Keras image_gen.flow_from_directory() 中的 class_mode 参数表示什么?

what does class_mode parameter in Keras image_gen.flow_from_directory() signify?

train_image_gen = image_gen.flow_from_directory('/Users/harshpanwar/Desktop/Folder/train',
                                               target_size=image_shape[:2],
                                               batch_size=batch_size,
                                               class_mode='binary')

在上面的代码片段中,class_mode='binary' 表示什么。我认为这是针对图像类别的数量。我正在使用此代码在 Keras 中训练图像识别分类器,以在狗和猫等 2 个不同类别之间进行分类。因此,如果 class_mode='binary' 用于表示两个类别,我们如何将其表示为三个或更多?

class_mode:“分类”、“二进制”、“稀疏”、“输入”或 None 之一。默认值:“分类”。确定返回的标签数组的类型:-“分类”将是二维单热编码标签,-“二进制”将是一维二进制标签,“稀疏”将是一维整数标签,-“输入”将是相同的图像输入图像(主要用于自动编码器)。 - 如果 None,则不返回任何标签(生成器将只生成批量图像数据,这对于与 model.predict_generator() 一起使用很有用)。请注意,在 class_mode None 的情况下,数据仍需要驻留在目录的子目录中才能正常工作。

假设您的数据集中有 N 类。如果你有 4 个标签,狗(索引 0)、猫(1)、驴(2)和人(3),N 将为 4。

Class 模式:

  • "categorical":2D输出(也就是长度为N的数字列表),[0, 0, 1, 0],这是一个one-hot编码(只有一个数字是1/”hot ") 代表驴。这是用于互斥标签。狗不成猫,人不成狗。
  • "binary":一维输出(又名。1 number),可以是 0、1、2、3 ... N。之所以这样称呼是因为它是二进制的 如果只有两个 类(恕我直言,这是一个错误的理由),source。我建议仅将“二进制”用于单标签分类,因为它在代码中记录了您的意图。
  • "sparse":深入研究代码后,这与“二进制”相同。逻辑是用elif self.class_mode in {'binary', 'sparse'}:完成的,之后就不用class_mode了。不过,我建议使用“稀疏”进行多标签分类,同样是因为它在代码中记录了您的意图。
  • "input": 标签又是字面上的图像。所以狗的图像的标签是相同的狗图片数组。如果我对 autoencoders 了解更多,我可能能够进一步解释。
  • None:没有标签,因此对训练没有用,但对推理/预测有用。

TensorFlow 文档是 here,但我认为它应该更深入 class_mode:

One of "categorical", "binary", "sparse", "input", or None. Default: "categorical". Determines the type of label arrays that are returned: - "categorical" will be 2D one-hot encoded labels, - "binary" will be 1D binary labels, "sparse" will be 1D integer labels, - "input" will be images identical to input images (mainly used to work with autoencoders). - If None, no labels are returned (the generator will only yield batches of image data, which is useful to use with model.predict()). Please note that in case of class_mode None, the data still needs to reside in a subdirectory of directory for it to work correctly.


稀疏和二进制一样吗?:

正如您在我的搜索结果中看到的那样,稀疏只检查了两次(搜索结果中的第 2 行和第 4 行)。我相信“稀疏”的目的是用于多标签分类,“二进制”是为单标签分类设计的(热狗与无热狗),但目前没有区别,因为行为是相同的: