How to load all images using image_dataset_from_directory function?

I am working on a multi-label classification problem and faced some memory issues so I would to use the Keras image_dataset_from_directory method to load all the images as batch. How do you apply a multi-label technique on this method.

I have these folders:

['Tomato_BacterialSpot', 'Tomato_EarlyBlight', 'Tomato_Healthy', 'Tomato_LateBlight']

I am generating class names using the below code.

Here is the sample code tutorial for multi-label but they did not use the image_dataset_from_directory technique.

 label = imagePath.split(os.path.sep)[-2].split(_)

and I got the below result but I do not know how to use the image_dataset_from_directory method to apply the multi-label?

  1. BacterialSpot
  2. EarlyBlight
  3. Healthy
  4. LateBlight
  5. Tomato

Topic keras tensorflow deep-learning dataset

Category Data Science


You don't actually need to apply the class labels, these don't matter. Keras will detect these automatically for you. It does this by studying the directory your data is in. Make sure you point to the parent folder where all your data should be. Your data should be in the following format:

my_data/
...BacterialSpot/
...EarlyBlight/
...Healthy/
...LateBlight/
...Tomato/

where the data source you need to point to is my_data. Here is an implementation:

train = tf.keras.preprocessing.image_dataset_from_directory(
  'my_data',
  validation_split=0.2,
  subset="training",
  image_size=(128, 128),
  batch_size=128)

val = tf.keras.preprocessing.image_dataset_from_directory(
  'my_data',
  validation_split=0.2,
  subset="validation",
  image_size=(128, 128),
  batch_size=128)

Then when you run it:

Found 3647 files belonging to 1 classes.
Using 2918 files for training.
Found 3647 files belonging to 1 classes.
Using 729 files for validation.

Keras has detected the classes automatically for you. I have used only one class in my example so you should be able to see something relating to 5 classes for yours. See an example implementation here by Google: https://colab.research.google.com/github/tensorflow/docs/blob/master/site/en/tutorials/images/classification.ipynb#scrollTo=iscU3UoVJBXj

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.