2
$\begingroup$

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
$\endgroup$

1 Answer 1

0
$\begingroup$

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

$\endgroup$
2
  • $\begingroup$ Thanks. Here the problem is multi-label classification. As you see in the folder name I am generating two classes for the same image. $\endgroup$ Commented Jan 4, 2021 at 10:51
  • $\begingroup$ Refer to this blog pyimagesearch.com/2018/05/07/… $\endgroup$ Commented Jan 4, 2021 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.