Search code examples
tensorflowmachine-learningkerasdeep-learningartificial-intelligence

modify the classname part of Keras ImageDataGenerator and flow_from_directory


I was wondering if it is possible to change the flow_from_directory function in order to give my desired class names.

For example, I have folders with names 101, 102 and with flow_from_directory I'll have y/class names as 101 and 102, but I need to have [1,0,1] and [0,1,0] in y for multi-label classification.

Something like this in flow_from_directory.

if folder_name == 101:
    y=[1,0,1]
if folder_name == 102:
    y=[0,1,0]

Thanks in advance.


Solution

  • If you try to assign a list to single class name in flow_from_directory you will get an error

    For multi-label classification , create a pandas Dataframe with file name and labels as columns like this example and use flow_from_dataframe instead

    enter image description here

    Tthen create your train generator with train dir as directory for images

    train_gen = ImageDataGenerator().flow_from_dataframe(
        df,
        directory=train_dir,
        x_col='filename',
        y_col='labels',
        class_mode='categorical'
    )