问题
I am reading in a csv file with pandas, and give the column names stored in colname
colnames=['file', 'label']
# Read data from file
data = pd.read_csv('./Hand_Annotations_2.csv',names=colnames, header=None)
# Preview the first 5 lines of the loaded data
data.head()
Then, I use ImageDataGenerator()
and flow_fromdataframe()
to get batches of data
train_generator=datagen.flow_from_dataframe(dataframe=data,
directory=None,
x_col=colnames[0],
y_col=colnames[1],
class_indices=IDmap,
class_mode="categorical", target_size=(224,224), batch_size=32)
But I get an error, as below:
TypeError: If class_mode="categorical", y_col="label" column values must be type string, list or tuple.
But my y_col
is a string. I get the same error if I just enter "label"
. It also seems to work with x_col
.
Can someone point me to my mistake?
Thanks
Solution
read csv with dtype e.g. str:
data = pd.read_csv('./Hand_Annotations_2.csv',dtype=str,names=colnames, header=None)
回答1:
You can check the code in github for what it is actually being checked, specially here. Keras preprocessing is checking that all the values have allowed types (str, list or tuple), so the check is failing, you have to check that all values of the y_col
column have the right type.
来源:https://stackoverflow.com/questions/56460581/using-flow-from-dataframe-what-is-the-correct-value-for-y-col