1

I am trying to create a multilabel classification model with keras. As such I have all my images in one folder. Furthermore, I have a CSV file containing a path to each image followed by multiple possible labels

Example of my CSV:

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

I am trying to read in my images using flow_from_directory and provide the respective labels via the CSV. My so far looks like this:

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")

A solution to a similar problem is suggested here: How to manually specify class labels in keras flow_from_directory? providing this code:

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)

However, I cant figure out how to implement the multiclasses_getter() such that it works.

AaronDT
  • 3,940
  • 8
  • 31
  • 71
  • 1
    If you are willing to move away from `flow_from_directory`, I think it might be easier in your case to write your own custom generator (with the Keras `Sequence`) object. [Here](https://github.com/sdcubber/Keras-Sequence-boilerplate/blob/master/Keras-Sequence.ipynb) is some boilerplate code that handles your case: A generator that is instantiated with a .csv file with the image names and labels in the columns. – sdcbr Aug 02 '18 at 07:21
  • See also my [answer](https://stackoverflow.com/questions/51644888/transfer-learning-from-a-u-net-for-image-segmentation-keras) to this question for an example – sdcbr Aug 02 '18 at 09:04

1 Answers1

0

Try to use flow_from_dataframe instead flow_from_directory