What should yielded dict mapping from generator look like for model.fit(), when I want the model to have multiple outputs?

Viewed 79

I have a keras model created with the functional API, that is pretty much MobileNet V2 until it is split into two heads, for classification and bounding box regression:

## Class prediction
x_class = keras.layers.Conv2D(NUM_CLASSES, (1, 1), padding='same')(x)
x_class = keras.layers.Activation('softmax')(x_class)
x_class = keras.layers.Reshape((NUM_CLASSES, ),  name='softmax')(x_class)

## Bounding box prediction
x_flatten = keras.layers.Flatten()(x)
x_bbox = keras.layers.Dense(128, activation='relu')(x_flatten)
x_bbox = keras.layers.Dense(64, activation='relu')(x_bbox)
x_bbox = keras.layers.Dense(32, activation='relu')(x_bbox)
x_bbox = keras.layers.Dense(4, kernel_regularizer='l2')(x_bbox)
x_bbox = keras.layers.Activation('linear', name='linear')(x_bbox)

model = keras.models.Model(inputs, [x_class, x_bbox])

losses = {'softmax': 'sparse_categorical_crossentropy',
          'linear': 'mse'}
model.compile(loss=losses, optimizer='adam', metrics=['accuracy'])

Since I have [x_class, x_bbox] as outputs, I made sure the train data generator yields the following dict mapping:

yield {'input_1': np.expand_dims(image, 0), 'softmax': np.array(label), 'linear': np.array(bbox)}

However, I get the following warning and error:

UserWarning: Input dict contained keys ['softmax', 'linear'] which did not match any model input. They will be ignored by the model.
TypeError: Target data is missing. Your model has loss: {'softmax': 'sparse_categorical_crossentropy', 'linear': 'mse'}, and therefore expects target data to be passed in fit().

So 'softmax' and 'linear' are clearly not the correct keys for the dict mapping. I also tried some keys like 'output', 'output_1' and 'outputs', but it doesn't accept those keys either.

What's the name of the key I'm looking for, or how can I set it up?

Edit Clarification: I want images to be the input (i.e. "X") and labels, bboxes to be the "y". When I only have a single output, I'm able to pass both the input and output as one generator, which is also what I want to do here.

0 Answers
Related