Valery Noname
Valery Noname

Reputation: 381

Loading images with image_dataset_from_directory() from keras

The folder of images has the following structure:

train/
...batch0/
......file00.jpg
......file01.jpg
...batch1/
......file10.jpg
......file11.jpg

Names of directories batch0 and batch1 are not labels, labels are in a separate file. The problem is to load these images into a dataset. Function image_dataset_from_directory('/batch0'), image_dataset_from_directory('/batch1') doesn't work.

Error:

ValueError: Expected the lengths of labels to match the number of files in the target directory. len(labels) is 2 while we found 0 files in ../train/batch0/. 

Upvotes: 1

Views: 6715

Answers (1)

user11530462
user11530462

Reputation:

Looks like this should be fixed in tf-nightly version. The error message has been updated to,

If you wish to get a dataset that only contains images (no labels), pass labels_mode=None.

Instead of labels=None, pass label_mode=None

train/
...batch0/
......file00.jpg
......file01.jpg
...batch1/
......file10.jpg
......file11.jpg

import pathlib
data_dir = pathlib.Path('/content/train/')

import tensorflow as tf
batch_size = 16
img_height = 180
img_width = 180
train_ds = tf.keras.preprocessing.image_dataset_from_directory(
  data_dir,
  label_mode=None,
  validation_split=0.2,
  subset="training",
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

Output

Found 4 files belonging to 2 classes.
Using 3 files for training.

Upvotes: 2

Related Questions