Search code examples
pythontensorflowkerasimage-classification

Why is my code showing no .jpeg images found in the folder though the folder has .jpeg images?


Keras: Image Classification: (code showing error)

code is showing that there are no images in the folder, though there are images in .jpeg format which is an accepted format. Why is my code showing that there are no images, even if there are images in the allotted folder, ( Refer image of folder attached for proof)

in foldername (.jpeg files)

image_size = (180,180)
batch_size =1

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    'foldername',
    validation_split=0.9,
    subset= 'training',
    seed = 20,
    image_size = image_size,
    batch_size= batch_size,
)
val_ds = tf.keras.preprocessing.image_dataset_from_directory(
    'foldername',
    validation_split=0.9,
    subset = 'validation',
    seed = 20,
    image_size = image_size,
    batch_size = batch_size,
)

result-

 Found 0 files belonging to 0 classes.
 Using 0 files for training

error-

ValueError                                Traceback (most recent call last)
Input In [4], in <cell line: 4>()
      1 image_size = (180,180)
      2 batch_size =1
----> 4 train_ds = tf.keras.preprocessing.image_dataset_from_directory(
      5     'both_new',
      6     validation_split=0.9,
      7     subset= 'training',
      8     seed = 20,
      9     image_size = image_size,
     10     batch_size= batch_size,
     11 )
     12 val_ds = tf.keras.preprocessing.image_dataset_from_directory(
     13     'both_new',
     14     validation_split=0.9,
   (...)
     18     batch_size = batch_size,
     19 )

File ~\anaconda3\lib\site-packages\keras\utils\image_dataset.py:209, in image_dataset_from_directory(directory, labels, label_mode, class_names, color_mode, batch_size, image_size, shuffle, seed, validation_split, subset, interpolation, follow_links, crop_to_aspect_ratio, **kwargs)
    206 image_paths, labels = dataset_utils.get_training_or_validation_split(
    207     image_paths, labels, validation_split, subset)
    208 if not image_paths:
--> 209   raise ValueError(f'No images found in directory {directory}. '
    210                    f'Allowed formats: {ALLOWLIST_FORMATS}')
    212 dataset = paths_and_labels_to_dataset(
    213     image_paths=image_paths,
    214     image_size=image_size,
   (...)
    219     interpolation=interpolation,
    220     crop_to_aspect_ratio=crop_to_aspect_ratio)
    221 dataset = dataset.prefetch(tf.data.AUTOTUNE)

ValueError: No images found in directory both_new. Allowed formats: ('.bmp', '.gif', '.jpeg', '.jpg', '.png')

.


Solution

  • You must have a directory structure of the form show below for image_dataset_from_directory to function properly

    dir (dir references in image_dataset_from_directory)
    ````class dir A
        `````````````image0 for class A
        -------------image1 for class A
        ------------- etc
        --------------imageN for class A
    ````class dir B
        `````````````image0 for class B
        -------------image1 for class B
        ------------- etc
        --------------imageN for class B
    

    So in your directory foldername make a subdirectory classA. Move all your images into the classA directory.