tf.keras.preprocessing.image_dataset_from_directory Value Error: No images found

Question:

belos is my code to ensure that the folder has images, but tf.keras.preprocessing.image_dataset_from_directory returns no images found. What did I do wrong? Thanks.

DATASET_PATH = pathlib.Path('C:\Users\xxx\Documents\images')
image_count = len(list(DATASET_PATH.glob('.\*.jpg')))
print(image_count)

output = 2715

batch_size = 4
img_height = 32
img_width = 32

train_ds = tf.keras.preprocessing.image_dataset_from_directory(
    DATASET_PATH.name,
    validation_split=0.8,
    subset="training",
    seed=123,
    image_size=(img_height, img_width),
    batch_size=batch_size)

output:

Found 0 files belonging to 0 classes.
Using 0 files for training.
Traceback (most recent call last):
  File ".tensorDataPreProcessed.py", line 23, in <module>
    batch_size=batch_size)
  File "C:UsersxxxAnaconda3envsxxxlibsite-packagestensorflowpythonkeraspreprocessingimage_dataset.py", line 200, in image_dataset_from_directory
    raise ValueError('No images found.')
ValueError: No images found.
Asked By: LLTeng

||

Answers:

There are two issues here, firstly image_dataset_from_directory requires subfolders for each of the classes within the directory. This way it can automatically identify and assign class labels to images.

So the standard folder structure for TF is:

data
|
|___train
|      |___class_1
|      |___class_2
|
|___validation
|      |___class_1
|      |___class_2
|
|___test(optional)
       |___class_1
       |___class_2

The other issue is that you are attempting to create a model using only one class which is not a way to go. The model needs to be able to differentiate between the class you are trying to generate using GAN but to do this it needs a sample of images that do not belong to this class.

Answered By: pavel

If you’re doing unsupervised learning and you genuinely only have one class then there is an argument for tf.keras.utils.image_dataset_from_directory called labels:

NOTE: If you’re unsure whether this applies to your problem you should become sure first. Just copying this solution without knowing if it’s what your problem needs is a bad idea.

From the docs:

labels: Either "inferred" (labels are generated from the directory structure), None (no labels), or a list/tuple of integer labels of the same size as the number of image files found in the directory. Labels should be sorted according to the alphanumeric order of the image file paths (obtained via os.walk(directory) in Python).

So you can set labels to None and it’ll import all images into 1 class.

Answered By: stephan