Home > database >  How to view images in directory iterator object tensorflow
How to view images in directory iterator object tensorflow

Time:06-02

i created a test dataset using 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"])

but i want to view the images in imgs (directory iterator object) because they're shuffled

i tried to view them using matplotlib but i got TypeError

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

any help would be greatly appreciated

CodePudding user response:

Your image folders probably have the wrong structure or you are forgetting that you have a tuple of data (images, labels). Anyway, this works with a default batch size of 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
  • Related