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',
#validation_split=0.2,
#subset="validation",
#labels='inferred',
seed=123,
image_size=(img_height, img_width),
batch_size=64)
CodePudding user response:
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]