Search code examples
pythontensorflowkerastensorflow-datasets

Using tf.keras.utils.image_dataset_from_directory with label list


I have list of labels corresponding numbers of files in directory example: [1,2,3]

train_ds = tf.keras.utils.image_dataset_from_directory(
  train_path,
  label_mode='int',
  labels = train_labels,
#   validation_split=0.2,
#   subset="training",
  shuffle=False,
  seed=123,
  image_size=(img_height, img_width),
  batch_size=batch_size)

I get error:

ValueError: Expected the lengths of `labels` to match the number of files in the target directory. len(labels) is 51033 while we found 0 files in ../input/jpeg-happywhale-128x128/train_images-128-128/train_images-128-128.

I tried define parent directory, but in that case I get 1 class.


Solution

  • Your data folder probably does not have the right structure. Try something like this:

    import numpy
    from PIL import Image
    import tensorflow as tf
    
    samples = 10
    for idx, c in enumerate(['/content/data/class1/', '/content/data/class2/']*samples):
      imarray = numpy.random.rand(100,100,3) * 255
      im = Image.fromarray(imarray.astype('uint8')).convert('RGB')
      im.save('{}result_image{}.png'.format(c, idx))
    
    train_labels = [0]*samples + [1]*samples
    train_ds = tf.keras.utils.image_dataset_from_directory(
      '/content/data',
      label_mode='int',
      labels = train_labels,
      shuffle=False,
      seed=123,
      image_size=(100, 100),
      batch_size=4)
    
    for x, y in train_ds.take(1):
      print(x.shape, y)
    
    Found 20 files belonging to 2 classes.
    (4, 100, 100, 3) tf.Tensor([0 0 0 0], shape=(4,), dtype=int32)
    

    Your folder structure should look like this:

    ├── data
    │   ├── class2
    │   │   ├── result_image5.png
    │   │   ├── result_image9.png
    │   │   ├── result_image15.png
    │   │   ├── result_image13.png
    │   │   ├── result_image1.png
    │   │   ├── result_image3.png
    │   │   ├── result_image11.png
    │   │   ├── result_image19.png
    │   │   ├── result_image7.png
    │   │   └── result_image17.png
    │   └── class1
    │       ├── result_image14.png
    │       ├── result_image8.png
    │       ├── result_image12.png
    │       ├── result_image18.png
    │       ├── result_image16.png
    │       ├── result_image6.png
    │       ├── result_image2.png
    │       ├── result_image10.png
    │       ├── result_image4.png
    │       └── result_image0.png