Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain filenames during prediction while using tf.keras.preprocessing.image_dataset_from_directory()?

Keras introduced tf.keras.preprocessing.image_dataset_from_directory function recently, which is more efficient than previously ImageDataGenerator.flow_from_directory method in tensorflow 2.x.

I am practising on the catsvsdogs problems and using this function to build a data pipeline for my model. After training the model, I use preds = model.predict(test_ds) to get the predictions for my test dataset. How should I match the preds with the name of pictures? (There is generator.filenames before, but doesn't exist in the new method any more.) Thanks!

like image 673
J.Kim Avatar asked Jun 03 '20 06:06

J.Kim


People also ask

What is Image_dataset_from_directory?

image_dataset_from_directory allows to load your data in the tf. data. Dataset format. Hence, it enables also the use, and this is the best, of the Keras preprocessing layers, including data augmentation.

How is data loaded with TensorFlow?

In memory data For any small CSV dataset the simplest way to train a TensorFlow model on it is to load it into memory as a pandas Dataframe or a NumPy array. A relatively simple example is the abalone dataset. The dataset is small. All the input features are all limited-range floating point values.

How do I load a custom dataset in keras?

The easiest way to load your dataset for training or testing is by using Keras ImageDataGenerator class (that also allows you some data augmentation methods). You have 3 options : If your dataset is structured like this : data/ train/ dogs/ dog001.


Video Answer


1 Answers

Expanding on @Daniel Woolcott's and @Almog David's answers, the file paths are returned by the image_dataset_from_directory() function in Tensorflow v2.4. already. No need to change the source code of the function.

To be more exact – you can easily retrieve the paths with the file_paths attribute.

Try this:

img_folder = "your_image_folder/"

img_generator = keras.preprocessing.image_dataset_from_directory(
    img_folder, 
    batch_size=32, 
    image_size=(224,224)
)

file_paths = img_generator.file_paths
print(file_paths)

Prints out:

your_file_001.jpg
your_file_002.jpg
…
like image 141
petezurich Avatar answered Sep 24 '22 10:09

petezurich