Tf.keras.preprocessing.image_dataset_from_directory() Question

Hello there! I am really really new to programming so I would appreciate any help on this question! I am using the tf.keras.preprocessing.image_dataset_from_directory() function to split my dataset into training, testing, and validation dataset but I am having some issues. To summarize, inside my “Dataset” folder, I have my FOUR classes of images that I want to split but when I include the directory path, it gives me files belonging to FIVE classes. It is including my “Dataset” folder as a class when really there are only FOUR classes inside my “Dataset” folder.

Here is the code for reference:

splitfolders.ratio(‘C:\Working\SCX\ALZ RESEARCH\Dataset\’, output=“output”, seed=1345, ratio=(.8, 0.1,0.1))

IMG_HEIGHT = 128
IMG_WIDTH = 128
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
“./output/train”, seed=123, label_mode = “int”, image_size=(IMG_HEIGHT, IMG_WIDTH), batch_size=64)
test_ds = tf.keras.preprocessing.image_dataset_from_directory(
“./output/test”, seed=123, label_mode = “int”, image_size=(IMG_HEIGHT, IMG_WIDTH),batch_size=64)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
“./output/val”, seed=123, label_mode = “int”, image_size=(IMG_HEIGHT, IMG_WIDTH), batch_size=64)

And here is the output:
Found 5119 files belonging to 5 classes.
Found 642 files belonging to 5 classes.
Found 639 files belonging to 5 classes.

Code:
class_names = train_ds.class_names
print(class_names)
train_ds

Output:
[‘Dataset’, ‘Mild_Demented’, ‘Moderate_Demented’, ‘Non_Demented’, ‘Very_Mild_Demented’]

My apologies if my question is not formatted correctly, it’s my first time and I’m a new programmer. Any help is appreciated!

-SCX

Check the directories present in your folder using this code, this displays the names of all directories in that folder. This code displays all directories even though it is hidden.

          import os
          dir = os.listdir('path_to_your_main_directory')
          print(dir)

Now, after you know the name of the unwanted subdirectory, delete it by using the below code

         file_path = ('path_to_your_main_directory/subdirectory_name')
         os.rmdir(file_path)

Thank you

1 Like

Hi, @SCX

Please make sure you have set the current working directory C:\Working\SCX\ALZ RESEARCH\"

Do not incluse the Dataset directory.

You can do it like

import os

os.chdir(“C:\Working\SCX\ALZ RESEARCH\”),

then, as suggested by @chunduriv check the content of your directory.

print(os.listdir(“C:\Working\SCX\ALZ RESEARCH\”) or use Window Explorer for a visual anlyses.

Check the output directory that was created by the splitfolders.ratio command. You may want to delete the output directory and run eveything again, making sure you working directory is properly set.