Keras ImageDataGenerator() how to get all labels from data

Well - when you know the batch_size you could obtain number of images from flow_from_directory object:

test_batches = ImageDataGenerator().flow_from_directory(.., batch_size=n)
number_of_examples = len(test_batches.filenames)
number_of_generator_calls = math.ceil(number_of_examples / (1.0 * n)) 
# 1.0 above is to skip integer division

test_labels = []

for i in range(0,int(number_of_generator_calls)):
    test_labels.extend(np.array(test_batches[i][1]))

You can simply get a dictionary from the DirectoryIterator containing both the labels and the index in the one-hot encoding. Accessing the keys will then give you all the labels.

test_batches.class_indices.keys()

I know that the flow method allows for passing both the images and the labels. But you need to have your images already loaded in memory.

I never tried that with flow_from_directory, but if you look at the documentation it seems that they ask you to have a main directory and a subdirectory for each label.


If you just wants the labels, you can directly use

test_batches.labels

But some times you want the value, then you can do like this: validation_x = []

for i in range( test_batches.__len__() ):
    validation_x.extend(
        test_batches.__getitem__( i )[0] 
        )

Tags:

Python

Keras