1
$\begingroup$

I'm new to tensorflow, so I've been trying to find the best way to do class balancing over a dataset where I used image_dataset_from_directory to load. But I haven't find the way to do it. I saw from different sources that SMOTE, but I couldn't find the way to make it work with image_dataset_from_directory.

The dataset contains 4 different classes where one of them is highly unbalanced. The below is how I'm extracting the dataset. The dataset I'm using is from Kaggle (https://www.kaggle.com/datasets/tourist55/alzheimers-dataset-4-class-of-images/data).

from keras.utils import image_dataset_from_directory as IDFD
IMAGE_SIZE = (176, 208)
BATCH_SIZE = 32
BASE_DIR = '/content/drive/' #this is the path to the drive folder where images are saved

train_ds = IDFD(DATASET_DIR+"/train",
                seed=7891,
                validation_split=0.13,
                subset="training",
                labels="inferred",
                label_mode="categorical",
                interpolation="bilinear",
                image_size=IMAGE_SIZE,
                batch_size=BATCH_SIZE,
                color_mode='rgb')

I've been reading the documentations regarding image_dataset_from_directory and looking for examples, but I haven't found anything that works yet. Even Gemini and ChatGPT are not getting me closer to any answer.

Really hope someone can help me with this. I'd really appreciate your help.

$\endgroup$

1 Answer 1

0
$\begingroup$

You can not do that directly with image_dataset_from_directory since this function is only designed to produce a dataset from your files. However, you can transform your dataset train_ds to get another dataset with some sort of balance if you want. Have a look at tf.data.Dataset, in particular, the method rejection_resample might be what you need. It lets you sample from the given dataset in such a way that a specific target distribution of classes is achieved, for instance $[0.25, 0.25, 0.25, 0.25]$ in the case of four classes.

$\endgroup$

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.