KeyError:使用ImageDataGenerator.flow_from_dataframe时为“类”

时间:2019-09-30 09:37:51

标签: python-3.x pandas tensorflow keras conv-neural-network

我正在尝试使用ImageDataGenerator.flow_from_dataframe创建数据生成器,但是遇到keyerror:class

在使用flow_from_dataframe之前,我创建了训练数据框的枢轴,其中将类标签转换为列

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

下面是数据帧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'

有人可以让我知道如何解决此问题。 任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:0)

您能尝试一下吗,基本上将class_mode设置为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")

答案 1 :(得分:0)

您使用了xcolycol而不是x_coly_col,这会导致此错误

答案 2 :(得分:0)

请勿旋转桌子。只需将y_col作为Label字段传递,然后将唯一值列表放入类参数中即可。 将class_mode设置为分类。 另外,它分别是x_col和y_col。 Keras会自动执行一键编码,其余的会执行。