KeyError: 'class' while using ImageDataGenerator.flow_from_dataframe

我们两清 提交于 2020-06-17 14:05:21

问题


I am trying to create data generator using ImageDataGenerator.flow_from_dataframe but facing keyerror: class

Before using flow_from_dataframe, i created a pivot of training dataframe where class labels are converted to columns

train_df = train[['Label', 'filename', 'subtype']].drop_duplicates().pivot(index='filename', columns='subtype', values='Label').reset_index()

Below is the output of dataframe train_df.

subtype filename    any epidural    intraparenchymal    intraventricular    subarachnoid    subdural
0   ID_000039fa0.dcm    0   0   0   0   0   0
1   ID_00005679d.dcm    0   0   0   0   0   0
2   ID_00008ce3c.dcm    0   0   0   0   0   0
3   ID_0000950d7.dcm    0   0   0   0   0   0
4   ID_0000aee4b.dcm    0   0   0   0   0   0
train_gen = datagen.flow_from_dataframe(train_df,
                                       directory='/kaggle/input/rsna-intracranial-hemorrhage-detection/stage_1_train_images',
                                       xcol='filename',
                                       ycol=['any', 'epidural', 'intraparenchymal','intraventricular', 'subarachnoid', 'subdural'],
                                       class_mode='categorical',
                                       target_size=(300, 300),
                                       batch_size=64,
                                       subset='training')
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2896             try:
-> 2897                 return self._engine.get_loc(key)
   2898             except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'class'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-93-0b64db9da6bb> in <module>
      6                                        target_size=(300, 300),
      7                                        batch_size=64,
----> 8                                        subset='training')

/opt/conda/lib/python3.6/site-packages/keras_preprocessing/image/image_data_generator.py in flow_from_dataframe(self, dataframe, directory, x_col, y_col, weight_col, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, save_to_dir, save_prefix, save_format, subset, interpolation, validate_filenames, **kwargs)
    681             subset=subset,
    682             interpolation=interpolation,
--> 683             validate_filenames=validate_filenames
    684         )
    685 

/opt/conda/lib/python3.6/site-packages/keras_preprocessing/image/dataframe_iterator.py in __init__(self, dataframe, directory, image_data_generator, x_col, y_col, weight_col, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, subset, interpolation, dtype, validate_filenames)
    127         self.dtype = dtype
    128         # check that inputs match the required class_mode
--> 129         self._check_params(df, x_col, y_col, weight_col, classes)
    130         if validate_filenames:  # check which image files are valid and keep them
    131             df = self._filter_valid_filepaths(df, x_col)

/opt/conda/lib/python3.6/site-packages/keras_preprocessing/image/dataframe_iterator.py in _check_params(self, df, x_col, y_col, weight_col, classes)
    202         if self.class_mode == 'categorical':
    203             types = (str, list, tuple)
--> 204             if not all(df[y_col].apply(lambda x: isinstance(x, types))):
    205                 raise TypeError('If class_mode="{}", y_col="{}" column '
    206                                 'values must be type string, list or tuple.'

/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py in __getitem__(self, key)
   2978             if self.columns.nlevels > 1:
   2979                 return self._getitem_multilevel(key)
-> 2980             indexer = self.columns.get_loc(key)
   2981             if is_integer(indexer):
   2982                 indexer = [indexer]

/opt/conda/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
   2897                 return self._engine.get_loc(key)
   2898             except KeyError:
-> 2899                 return self._engine.get_loc(self._maybe_cast_indexer(key))
   2900         indexer = self.get_indexer([key], method=method, tolerance=tolerance)
   2901         if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: 'class'

Could someone let me know how can I fix this issue. Any help is appreciated.


回答1:


Can you try this, basically setting class_mode to other

columns=["any", "epidural", "intraparenchymal","intraventricular", "subarachnoid", "subdural"]
train_generator=datagen.flow_from_dataframe(
directory="/kaggle/input/rsna-intracranial-hemorrhage-detection/stage_1_train_images",
x_col="filename",
y_col=columns,
class_mode="other"
target_size=(300, 300)
batch_size=64,
subset="training")



回答2:


You have used xcol and ycol instead of x_col and y_col which is causing this error




回答3:


Do not pivot the table. Just pass the y_col as the Label field, and put the list of unique values in class parameter. Set the class_mode as categorical. Also, it'd be x_col and y_col respectively. Keras automatically performs the one-hot encoding and does the rest.



来源:https://stackoverflow.com/questions/58165203/keyerror-class-while-using-imagedatagenerator-flow-from-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!