How to use a dataset object as input in the model.fit() training loop, for a model with multiple inputs?
Trying to pass the dataset itself gives me the following error:
Failed to find data adapter that can handle input: (<class 'list'> containing values of types {"<class 'tensorflow.python.data.ops.dataset_ops.MapDataset'>"}), <class 'NoneType'>
My case here:
I have a multiple input model built with keras
The inputs are named 'First', 'Second' and 'Third'
I have an image dataset in keras-style:
main_directory/
...class_a/
......a_image_1.jpg
......a_image_2.jpg
...class_b/
......b_image_1.jpg
......b_image_2.jpg
I create the dataset object using tf.keras.utils.image_dataset_from_directory:
train_dataset = image_dataset_from_directory(train_dir,
shuffle=False,
label_mode='categorical',
batch_size=hyperparameters["BATCH_SIZE"],
image_size=IMG_SIZE)
Now, each image is divided in 3 parts, each part serving as input to each of the inputs of the model. I take care of that using some map functions. This is not relevant tot he problem and I will not include it. I cannot use the cropping layers included in TF because of unrelated reasons.
I then try to start the training loop:
history = model.fit([train_dataset1,
train_dataset2,
train_dataset3,
],
epochs=epochs,
callbacks=callbacks,
validation_data=validation_dataset
validation_steps=steps
)
And here is where I get the error. I have tried some other approaches, like using a dict instead of a list. The problem seems to be that when training a model with multiple inputs, the fit() loop expects data to come as a list for x-values and a list for y-values, but I haven't been able to split the dataset object into the required formats
I have read many topics on this, but all use datasets that are created using the tf.data.Dataset.from_tensor_slices() method, which is not applicable in my case
Additionally, there is no indication of how the validation dataset has to be structured (at least according to the model.fit() documentation)
I have found some guidance saying that the validation dataset must have the same number of input/outputs as the training datasets (makes sense), but again, no indication on how to build or feed the validation dataset for a multiple input model