x_test and y_test from tf.keras.preprocessing.image_dataset_from_directory


How to obtain x_test and y_test from the following code

  test_generator = tf.keras.preprocessing.image_dataset_from_directory(
   data_dir2, labels ='inferred', label_mode='categorical',
    image_size=(img_height, img_width),


Based on documentation image_dataset_from_directory-function – it may return tuples (images, labels) so you may try to use for-loop to create lists with X and Y

Because it returns values in some EagerTensor so I use append() to move to normal list.

X = []
Y = []

for images, labels in test_generator:
    for image in images:
        X.append(image)                    # append tensor
        #X.append(image.numpy())           # append numpy.array
        #X.append(image.numpy().tolist())  # append list
    for label in labels:
        Y.append(image)                    # append tensor
        #Y.append(image.numpy())           # append numpy.array
        #Y.append(image.numpy().tolist())  # append list

If you use label_mode='int' then it gives Y = [ 1, 0, 2 ]
If you use label_mode='categorical' then it gives Y = [ [0, 1, 0], [1, 0, 0], [0, 0, 1] ]

If you need strings with class names (and you use label_mode='int'):

Y = [test_generator.class_names[x] for x in Y]

Answered By – furas

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published