keras-team/keras-preprocessing

If class_mode="categorical", y_col column values must be type string, list or tuple

talhaanwarch opened this issue · 3 comments

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.
Here is code

train_generator_1 = train_data_gen.flow_from_dataframe(annotation,
                                                      directory="data",
                                                      target_size=(img_shape,img_shape),
                                                      x_col="Image",
                                                      y_col=['C1'	,'C2'	,'C3',	'C4',	'C5'	,'C6'	,'C7',	'C8'],
                                                      class_mode='categorical',
                                                      shuffle=False,
                                                      batch_size=batch_size,
                                                      seed=7)

Error i am getting is TypeError: If class_mode="categorical", y_col="['C1' ,'C2' ,'C3', 'C4', 'C5' ,'C6' ,'C7', 'C8']" column values must be type string, list or tuple.

Hi @talhaanwarch, did you solve this issue?

I have the same problem.

Hi @talhaanwarch, did you solve this issue?

I have the same problem.

you should choose class_mode='raw'

I know this is a very old question on a defunct message board, but given that this still shows up in search results (and I was having the same issue), the solution I found was to first turn my multiple columns in a new column in my dataframe that is a list or tuple.

dataframe['combined_classes'] = dataframe[['C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7', 'C8']].apply(lambda x: x.tolist(), axis=1)
train_generator_1 = train_data_gen.flow_from_dataframe(dataframe,
                                                      directory="data",
                                                      target_size=(img_shape,img_shape),
                                                      x_col="Image",
                                                      y_col='combined_classes',
                                                      class_mode='categorical',
                                                      shuffle=False,
                                                      batch_size=batch_size,
                                                      seed=7)

I'm sure you're not still working on this, but wanted to share my solution anyways in case anyone else was looking for the answer like I was.