繁体   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

如何从以下代码中获取 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)

基于文档image_dataset_from_directory-function - 它可能会返回元组(images, labels) ,因此您可以尝试使用for -loop 创建带有XY的列表

因为它在一些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

如果你使用label_mode='int'那么它给出Y = [ 1, 0, 2 ]
如果您使用label_mode='categorical'那么它给出Y = [ [0, 1, 0], [1, 0, 0], [0, 0, 1] ]

如果您需要具有 class 名称的字符串(并且您使用label_mode='int' ):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM