[FIXED] Keras Flow_From_Dataframe for Regression Task

Issue

I am trying to build a CNN model to process images and solve a regression task. Since I have an excel that includes image paths and output values, I used flow_from_dataframe in my code. But it looks like it has no class_mode argument value for regression. Here is my code below. Any help is highly appreciated.

kf = KFold(n_splits = n_split)

for train_ix, test_ix in kf.split(np.zeros(len(train_data)),train_data['time']):
  training_data = train_data.iloc[train_ix]
  validation_data = train_data.iloc[test_ix]
  train_data_generator = idg.flow_from_dataframe(training_data, directory = train_image_dir,
                    x_col = "id_code", y_col = "time",
                    class_mode = "raw", shuffle = True, target_size=(224, 224), batch_size = batch_size)
    
  valid_data_generator  = valid_datagen.flow_from_dataframe(validation_data, directory = valid_image_dir,
                            x_col = "id_code", y_col = "time",
                            class_mode = "raw", shuffle = False, target_size=(224, 224), batch_size = batch_size)
    
  model = compile_model()
  history = model.fit_generator(train_data_generator, validation_data=valid_data_generator, epochs=epochs, callbacks=callbacks
                                               , workers = 8, steps_per_epoch = len(train_data/batch_size))

Solution

For regression tasks, you may set the class_mode=raw to treat the data in the column or list of columns of the dataframe as raw target values.

Answered By – Kaveh

Answer Checked By – Marie Seifert (Easybugfix Admin)

Leave a Reply

(*) Required, Your email will not be published