[英]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如何从以下代码中获取 x_test 和 y_test
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)
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
基于文档image_dataset_from_directory-function - 它可能会返回元组
(images, labels)
,因此您可以尝试使用for
-loop 创建带有X
和Y
的列表
Because it returns values in some EagerTensor
so I use append()
to move to normal list.因为它在一些
EagerTensor
中返回值,所以我使用append()
移动到普通列表。
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 ]
如果你使用
label_mode='int'
那么它给出Y = [ 1, 0, 2 ]
If you use label_mode='categorical'
then it gives Y = [ [0, 1, 0], [1, 0, 0], [0, 0, 1] ]
如果您使用
label_mode='categorical'
那么它给出Y = [ [0, 1, 0], [1, 0, 0], [0, 0, 1] ]
If you need strings with class names (and you use label_mode='int'
):如果您需要具有 class 名称的字符串(并且您使用
label_mode='int'
):
Y = [test_generator.class_names[x] for x in Y]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.