Image DataGenerator getting X and Y for each picture

With function bellow using ImageDataGenerator and after that flow_from_directory. It’s gonna solve shuffle problem which exist when we predict with model using test_generator as data, giving us Y which is not matching to his pair X

import math
import numpy as np
number_of_examples = len(test_generator.filenames)
number_of_generator_calls = math.ceil(number_of_examples / (1.0 * batch_size))
x_final = np.empty((0, *input_shape), float)
y_final = np.empty((0, number_of_classes), float)
for i in range(0, int(number_of_generator_calls)):
    # Getting image array from generator for each picture
    x_final = np.concatenate((x_final, test_generator[i][0]), axis=0)
    # Getting Y label for each picture
    y_final = np.concatenate((y_final, test_generator[i][1]), axis=0)
# Predicting label for each image in x_final
y_pred_prob = model.predict(x_final)
y_pred = np.argmax(y_pred_prob, axis=1)
# y_final is holding label for his own pair in x_final
y_test_binarized = y_final
y_test = np.argmax(y_final, axis=1)

Leave a comment

Your email address will not be published. Required fields are marked *