简体   繁体   English

来自 tf.keras.preprocessing.image_dataset_from_directory 的 x_test 和 y_test

[英]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 创建带有XY的列表

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.

相关问题 如何将 tf.data.Dataset 拆分为 x_train、y_train、x_test、y_test for keras - how to split up tf.data.Dataset into x_train, y_train, x_test, y_test for keras 如何从 tf.keras.preprocessing.image_dataset_from_directory() 探索和修改创建的数据集? - How can I explore and modify the created dataset from tf.keras.preprocessing.image_dataset_from_directory()? 如何使用 tf.keras.preprocessing.image_dataset_from_directory() 创建具有特定形状的数据集? - How do I use tf.keras.preprocessing.image_dataset_from_directory() to create a dataset with a certain shape? ValueError:使用 tf.keras.preprocessing.image_dataset_from_directory 时要解压的值太多(预期为 2) - ValueError: too many values to unpack (expected 2) when using tf.keras.preprocessing.image_dataset_from_directory 使用 tf.keras.preprocessing.image_dataset_from_directory 的 tf.data.Dataset 训练模型是非常慢的 keras - train model using tf.data.Dataset of tf.keras.preprocessing.image_dataset_from_directory is very slow keras tf.keras.preprocessing.image_dataset_from_directory 值错误:找不到图像 - tf.keras.preprocessing.image_dataset_from_directory Value Error: No images found 设置一次后更改 tf.keras.preprocessing.image_dataset_from_directory 的 label_mode - Changing label_mode of tf.keras.preprocessing.image_dataset_from_directory after setting it once 如何使用 tf.keras.preprocessing.image_dataset_from_directory 获取类的数量? - how to obtain the number of classes using tf.keras.preprocessing.image_dataset_from_directory? 无法将 tf.keras.preprocessing.image_dataset_from_directory 转换为 np.array - Cannot convert tf.keras.preprocessing.image_dataset_from_directory to np.array tf.keras.preprocessing.image_dataset_from_directory 如何将 output 显示到控制台 - How tf.keras.preprocessing.image_dataset_from_directory display output to console
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM