0
$\begingroup$

I am working on a multi-label classification problem I faced memory issues so I want to use Keras image_dataset_from_directory method to load all images as batch. how to apply 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 the sample code tutorial for multi-label but they didn't use image_dataset_from_directory technique. https://www.pyimagesearch.com/2018/05/07/multi-label-classification-with-keras/

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

and I got the below result but I don't know how to use image_dataset_from_directory method to apply the multi-label?

  1. BacterialSpot
  2. EarlyBlight
  3. Healthy
  4. LateBlight
  5. Tomato
$\endgroup$
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$ – Bala venkatesh Jan 4 at 10:51
  • $\begingroup$ Refer to this blog pyimagesearch.com/2018/05/07/… $\endgroup$ – Bala venkatesh Jan 4 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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