0

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?

  • You need to have a separate directory containing test images. Then do the same thing you did for train/val datasets but with `shuffle=False` and without `validation_split`. – Djinn Jul 19 '22 at 23:37

1 Answers1

0

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.