I want to create a model to categorize objects using the Functional Keras API that. The model will have two inputs and one output, the inputs being images and integers (the real world measure of the object in the picture). So the model will be obtained as the concatenation of two models, the first processing the images and obtained by Transfer Learning from InceptionV3, the second simply joining the input of the size. The final layer will be used to categorize the images + size into 12 classes. The model will be using one instances of ImageDataGenerator.flow_from_directory() method to get images from a set of subdirectories and a dictionary that associates every filename to a measure (an integer). Following an answer from a similar question: How to train a Keras model using Functional API which has two inputs and two outputs and uses two ImageDataGenerator methods (flow_from_directory) I created this joined generator:
class train_with_measures1(tf.keras.utils.Sequence):
def __init__(self, input_gen1, measures_gen):
self.gen = input_gen1
self.measures = measures_gen
def __len__(self):
return len(self.gen)
def __getitem__(self, i):
x1, y1 = self.gen[i]
x2 = dict_measures[os.path.basename(self.gen[i])]
return [x1, y1], [x2, y1]
def on_epoch_end(self):
self.gen.on_epoch_end()
That will accept in input the image generator and a dictionary of measures and will output two values: the original output from the image generator and an association of the measure and the class (directory). So I use this custom joined generator
training_generator = train_with_measures1(train_generator, dict_measures)
validation1_generator = train_with_measures1(validation_generator, dict_measures)
But when I try to invoke next on it
next(training_generator)
I get
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-33-087a0d7681d7> in <module>()
----> 1 next(training_generator)
2 print(training_generator[0])
TypeError: 'train_with_measures1' object is not an iterator
I create the model as follows
from keras.layers import *
from keras.utils.vis_utils import plot_model
model2 = Sequential()
model2.add(Dense(1, input_shape=(1,), activation="relu"))
x = layers.Conv2D(128,kernel_size=(3,3),activation='relu',padding='same')(last_output)
x = layers.GlobalAveragePooling2D()(x)
print('GlobalAveragePooling2D output shape: ', x.shape)
mergedOut = Concatenate()([x,model2.output])
x = layers.Dense(12, activation="softmax", name="classification")(mergedOut)
model = Model([pre_trained_model.input,model2.input], x)
# compile the model
model.compile(optimizer='adam',
loss='categorical_crossentropy',
metrics=['acc'])
plot_model(model, to_file='model_plot.png', show_shapes=True, show_layer_names=True)
And when I try to fit the model
history = model.fit(
training_generator,
validation_data=validation1_generator,
epochs=150,
verbose=1)
I get
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-28-28382d8d8a93> in <module>()
3 validation_data=validation1_generator,
4 epochs=150,
----> 5 verbose=1)
2 frames
/usr/lib/python3.7/posixpath.py in basename(p)
144 def basename(p):
145 """Returns the final component of a pathname"""
--> 146 p = os.fspath(p)
147 sep = _get_sep(p)
148 i = p.rfind(sep) + 1
TypeError: expected str, bytes or os.PathLike object, not tuple
How shoud I approach this problem? Is the joined generator a bad idea in this context?