Keras:如何使用flow_from_directory提供自定义标签?

时间:2018-08-02 03:40:43

标签: python tensorflow keras

我正在尝试使用keras创建多标签分类模型。因此,我将所有图像都放在一个文件夹中。此外,我有一个CSV文件,其中包含每个图像的路径,后跟多个可能的标签

Example of my CSV:

path, x1, x2, x3    
img/img_00000001.jpg,1,0,1
img/img_00000002.jpg,0,0,1
...

我正在尝试使用flow_from_directory读取图像,并通过CSV提供相应的标签。我到目前为止看起来像这样:

image_path= "C:/user/Images"


data_generator = ImageDataGenerator(rescale=1./255, 
                                    validation_split=0.20)

train_generator = data_generator.flow_from_directory(image_path, target_size=(IMAGE_HEIGHT, IMAGE_SIZE), shuffle=True, seed=13,
                                                     class_mode='binary', batch_size=BATCH_SIZE, subset="training")

validation_generator = data_generator.flow_from_directory(image_path, target_size=(IMAGE_HEIGHT, IMAGE_SIZE), shuffle=False, seed=13,
                                                     class_mode='binary', batch_size=BATCH_SIZE, subset="validation")

在此建议解决类似问题的方法:How to manually specify class labels in keras flow_from_directory?提供以下代码:

def multiclass_flow_from_directory(flow_from_directory_gen, multiclasses_getter):
    for x, y in flow_from_directory_gen:
        yield x, multiclasses_getter(x, y)

但是,我无法弄清楚如何实现multiclasses_getter()使其能够工作。

1 个答案:

答案 0 :(得分:0)

尝试使用flow_from_dataframe代替flow_from_directory