how to obtain the number of files in tf.keras.preprocessing.image_dataset_from_directory

Viewed 1002

how to obtain the number of files in tf.keras.preprocessing.image_dataset_from_directory?

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    data_dir,
    validation_split=0.2,
    subset="training",
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size)

Found 3670 files belonging to 5 classes. Using 2936 files for training.

2 Answers

When you run

import numpy as np

len(np.concatenate([i for x, i in train_ds], axis=0))

this gives you the number of files, in this case 2936.

You can use index_directory helper function:

from tensorflow.python.keras.preprocessing import dataset_utils
file_paths, _, _ = dataset_utils.index_directory(data_dir, 'inferred', ('.bmp', '.gif', '.jpeg', '.jpg', '.png'))
print('Total files:', len(file_paths))
Related