How to get x_train and y_train from ImageDataGenerator?

Viewed 1341

I am working on some image classification problem and I made Y Network for it. Y Network is a type of Neural Network which has two inputs and one output. If we want to fit our Tensorflow model we have to feed x_train and y_train in model.fit(). Like this -

model.fit([x_train, x_train], y_train, epochs=100, batch_size=64)

but how do I get x_train and y_train if I got my data from ImageDataGenerator ? Like this -

train_generator = train_datagen.flow_from_dataframe(... , batch_size=64, ...)

I tried getting x_train and y_train by this method:

x_train, y_train = train_generator.next()

but resulted x_train and y_train consist of only 64 images, I want all my 8644 images. I cannot increase batch_size to 8644 because it will need more memory and Google Colab will crash. What should I do ?

1 Answers

you can get the list of all images and labels from

class_dict=train_generator.class_indices
labels= train_generator.labels
file_names= train_generator.filenames 

the class dictionary is useful to correlate the class index to the class name, it is of the form {class name, index} I find it useful to reverse the order to get a dictionary of the form {index, class name} using the code below

for key,value in class_dict.items():
        new_dict[value]=key 

So when you do predictions and get the index of the prediction using index= np.argmax(p) you can get the corresponding class name from

class_name=new_dict[index]
Related