Using Keras flow_from_dataframe for multi output regression problem

Viewed 693

I'm using a simple Keras model with 2 Dense layers as outpus:

orientation = Dense(3, activation="linear", name='orientation')(base_output) position = Dense(3, activation="linear", name='position')(base_output)
model = Model(inputs=model.inputs, outputs=[position, orientation])

My Pandas dataframe contains images paths, position and orientation data (3 element arrays):

    paths                      position                        orientation  

0 /pth/to/img [0.001509, 0.707734, 0.020575] [-0.535463, 0.196570, -1.011444]

I'm using train generator and a generator wrapper:

train_datagen = ImageDataGenerator(rescale = 1./255, horizontal_flip = False,
                               fill_mode = "nearest", zoom_range = 0,
                               width_shift_range = 0, height_shift_range=0,
                               rotation_range=0) 

train_generator = train_datagen.flow_from_dataframe(train_dataframe, directory=None, x_col='paths', y_col=['position', 'orientation'], weight_col=None, target_size=(224, 224), color_mode='rgb', classes=None, class_mode='raw', batch_size=32, shuffle=True, seed=None, save_to_dir=None, save_prefix='', save_format='png', subset=None, interpolation='nearest', validate_filenames=True)

def generator_wrapper(generator):
    for batch_x,batch_y in generator:
        yield (batch_x,[batch_y[:,i] for i in range(2)])

But getting this error when trying to fit model with fit_generator: ValueError: Error when checking target: expected position to have shape (3,) but got array with shape (1,)

Is there a way to feed my Dense layers with arrays using this flow? Thanks in advance.

0 Answers
Related