4

In keras documentation, it is mentioned that "categorical": 2D numpy array of one-hot encoded labels. Supports multi-label output. but in my case, it does not support.

train_generator_1 = train_data_gen.flow_from_dataframe(annotation,
                                                      directory="data",
                                                      target_size=(img_shape,img_shape),
                                                      x_col="Left",
                                                      y_col=['N','D','G','C','A','H','M','O'],
                                                      class_mode='categorical',
                                                      shuffle=False,
                                                      batch_size=batch_size,
                                                      seed=7)

The picture of labels is attached. Error i am getting is TypeError: If class_mode="categorical", y_col="['N', 'D', 'G', 'C', 'A', 'H', 'M', 'O']" column values must be type string, list or tuple.

enter image description here

2

2 Answers 2

3

If you have multiple outputs, you'd have to choose class_mode as 'raw'. I found a great tutorial by Vijayabhaskar J here. https://medium.com/@vijayabhaskar96/multi-label-image-classification-tutorial-with-keras-imagedatagenerator-cd541f8eaf24

Sign up to request clarification or add additional context in comments.

Comments

0

In my case, I didn't want to switch to raw class_mode, so found a different solution that still uses categorical class_mode.

flow_from_dataframe gets weird when using multiple column names in y_col -- especially when using 'categorical' class_mode. It's expecting the value of the column specified in y_col to be an array -- not for there to be an array of columns. Change it to a single combined column that contains an array of each value, and you'll be golden:

y_cols = ['N','D','G','C','A','H','M','O']
dataframe['classes'] = dataframe[y_cols].apply(lambda x: x.tolist(), axis=1)

train_generator_1 = train_data_gen.flow_from_dataframe(annotation,
                                                      directory="data",
                                                      target_size=(img_shape,img_shape),
                                                      x_col="Left",
                                                      y_col="classes",
                                                      class_mode='categorical',
                                                      shuffle=False,
                                                      batch_size=batch_size,
                                                      seed=7)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.