How to use tf.keras.utils.image_dataset_from_directory to load test dataset?

Issue

I am using tf.keras.utils.image_dataset_from_directory in my binary classification Mobilenet V2 model to split the dataset by defining training and validation subsets as following:

train_dataset = tf.keras.utils.image_dataset_from_directory(directory,
                                             shuffle=True,
                                             batch_size=BATCH_SIZE,
                                             image_size=IMG_SIZE,
                                             validation_split=0.2,
                                             subset='training',
                                             seed=42)
validation_dataset = tf.keras.utils.image_dataset_from_directory(directory,
                                             shuffle=True,
                                             batch_size=BATCH_SIZE,
                                             image_size=IMG_SIZE,
                                             validation_split=0.2,
                                             subset='validation',
                                             seed=42)

Now, I want to use model.predict() on a set of images to look at the predictions. How can I use image_dataset_from_directory considering that there won’t be two different folders containing the respective classes but only one folder for which I want the predictions? In addition, what should be the parameters of the image_dataset_from_directory function now?

Solution

As mentioned by @Djinn, You can do it in the same way and no need to define validation_split while accessing the folder.

For example,
Suppose I have a binary_data folder inside the dogs_cats/binary_data folder where I have stored multiple class images (5-5 images for each cats and dogs), then you can give the path till dogs_cats.

This will automatically fetch all images inside the binary_data folder by stating class 1 where you can have multiple class images(binary – as per model) stored.

After training the model, you can pass this dataset in model.predict() and can check the predictions for each image.

Please check the below code:

test_dataset = tf.keras.utils.image_dataset_from_directory(
          "/content/GoogleDrive/My Drive/MY WORK/dataset/dogs_cats/",
           shuffle=True,        #or False
           batch_size=BATCH_SIZE,
           image_size=IMG_SIZE)

Output:

Found 10 files belonging to 1 classes.

and the predictions part:

predictions = model.predict(test_dataset)
predictions = tf.where(predictions < 0.5,0, 1)
print('Predictions:\n', predictions.numpy())

Output:

Predictions:
 [[0]
 [1]
 [0]
 [0]
 [1]
 [1]
 [1]
 [1]
 [1]
 [1]]

Note: Prediction’s accuracy may depend on the model performance.

Answered By – TFer2

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published