Skip to content
Advertisement

extracting images and their label one by one from ImageDataGenerator().flow_from_directory

so I imported my dataset(38 classes) for validation using ImageDataGenerator().flow_from_directory

valid = ImageDataGenerator().flow_from_directory(directory="dataset/valid", target_size=(224,224))

and i wanted to pick each image and its label one by one. For example i want to pick the first image and it’s label

i tried this

for img, lbl in valid:
    print(lbl)
    break

i get the image but for the label i just get an array of shape (32,38) with 0 and 1s

[[0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 ...
 [0. 0. 0. ... 1. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]
 [0. 0. 0. ... 0. 0. 0.]]

Is there a way to get the label of this picture?

Advertisement

Answer

The documentation might help you with this question. More specifically, the default arguments from tf.keras.ImageDataGenerator.flow_from_directory:

flow_from_directory(
    directory, target_size=(256, 256), color_mode='rgb', classes=None,
    class_mode='categorical', batch_size=32, shuffle=True, seed=None,
    save_to_dir=None, save_prefix='', save_format='png',
    follow_links=False, subset=None, interpolation='nearest'
)

The class mode is categorical, so your labels will come as one-hot encoded matrices. The default batch size is 32, so when you iterate, 32 one-hot encoded rows will be fetched every time. This is what you’re getting.

To access the labels, you can index the list of all labels of your dataset:

valid.labels[0]
Advertisement