如何在目录迭代器对象tensorflow中查看图像

How to view images in directory iterator object tensorflow

我使用 ImageDataGenerator class 创建了一个测试数据集:

imgs=tf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function=tf.keras.applications.vgg16.preprocess_input).flow_from_directory(r"C:\Users\Abhimanyu\Pictures\Camera Roll",target_size=(224,224),classes=["Class 1","Class 2"])

但我想查看 imgs(目录迭代器对象)中的图像,因为它们被打乱了

我尝试使用 matplotlib 查看它们,但我得到了 TypeError

temp=next(imgs)
plt.imshow(temp[0])
>>> TypeError: Invalid shape (12, 224, 224, 3) for image data

任何帮助将不胜感激

您的图像文件夹的结构可能有误,或者您忘记了数据元组(图像、标签)。无论如何,这适用于默认批量大小 32:

import tensorflow as tf
import matplotlib.pyplot as plt

flowers = tf.keras.utils.get_file(
    'flower_photos',
    'https://storage.googleapis.com/download.tensorflow.org/example_images/flower_photos.tgz',
    untar=True)
imgs = tf.keras.preprocessing.image.ImageDataGenerator(rescale=1./255).flow_from_directory(flowers, shuffle=True)

images, _ = next(imgs)
plt.imshow(images[0]) # display first image from batch