Search code examples
tensorflowdeep-learningtensorflow-datasets

Concatenate 2 Tensorflow dataset for model training


I have 2 tensorflow datasets of (384,384) images that I want to use as input data and labels data for my model.train()

data = tf.keras.preprocessing.image_dataset_from_directory('path1', labels=None, image_size=(384,384), batch_size=1)
labels = tf.keras.preprocessing.image_dataset_from_directory('path2', labels=None, image_size=(384,384), batch_size=1)

But it doesn't let me pass both x and y as Datasets.

model.train(data, labels, epochs=5)

ValueError: y argument is not supported when using dataset as input.

So what can I do in this case?


Solution

  • This error indicates that your first argument (data) includes both data and labels (a tuple), and model.fit() does not expect to get another argument as y, as you have specified labels.

    Based on this doc: If you don't specify label_mode argument, it's default is int. Then, what this object returns is:

    • If label_mode is None, it yields float32 tensors of shape (batch_size, image_size[0], image_size[1], num_channels).
    • Otherwise, it yields a tuple (images, labels), where images has shape (batch_size, image_size[0], image_size[1], num_channels), and labels if label_mode is int, the labels are an int32 tensor of shape (batch_size,).

    Inference:

    Just pass another argument as label_mode and set it to None like this:

    data = tf.keras.preprocessing.image_dataset_from_directory('path1', labels=None, label_mode=None, image_size=(384,384), batch_size=1)
    labels = tf.keras.preprocessing.image_dataset_from_directory('path2', labels=None, label_mode=None, image_size=(384,384), batch_size=1)