How can I create a TensorFlow image dataset labelled by filenames from multiple directories?


I found a similar question but it doesn’t quite fit this scenario.

I have some images of multiple classes in the same directory that I want to label according to their filename. I also have other directories with more images that I want to include in the same dataset.

Here’s an example of the folder structure:


Is there any efficient way to create a dataset from this structure? Since I usually use image_dataset_from_directory() but it doesn’t work in this case without moving images around.


There is no method that lets you do this automatically like image_dataset_from_directory (that I know of at least). However creating the dataset on your own is pretty easy.

You first have to iterate through your directory searching for the images. You save the path of each image you encounter in a list paths. For each image you check the class, either dog or cat in this case, and you create a dictionary where you map each class name to a different int, the label of that class. For each image you save aside the label of it in a list labels.

import glob

paths = []
labels = []
labels_dict = {}
index = 0

for filepath in glob.iglob('train/*/*.jpg'):
  class_name = filepath.split('_')[1].split("/")[-1]
  if class_name not in labels_dict:
    labels_dict[class_name] = index
    index += 1



# labels:
[0, 1, 1, 1, 1, 0]

# paths
['train/dir_1/dog_1.jpg', 'train/dir_1/cat_2.jpg', 'train/dir_1/cat_1.jpg', 'train/dir_2/cat_1.jpg', 'train/dir_2/cat_2.jpg', 'train/dir_2/dog_1.jpg']

# labels_dict
{'dog': 0, 'cat': 1}

Now that you have prepared your paths and labels, you can proceed as from this answer:

import tensorflow as tf
paths = tf.constant(paths)
labels = tf.constant(labels)

# create a dataset returning slices of `paths`
dataset =, labels))

# parse every image in the dataset using `map`
def _parse_function(filename, label):
    image_string =
    image_decoded = tf.image.decode_jpeg(image_string, channels=3)
    image = tf.cast(image_decoded, tf.float32)
    return image, label

dataset =

Answered By – ClaudiaR

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published