KeyError: 'class' при использовании ImageDataGenerator.flow_from_dataframe - PullRequest
0 голосов
/ 30 сентября 2019

Я пытаюсь создать генератор данных, используя 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()

Ниже приведен вывод 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'

Может кто-нибудь сообщить мне, как я могу решить эту проблему. Любая помощь приветствуется.

1 Ответ

0 голосов
/ 16 октября 2019

Можете ли вы попробовать это, в основном установив 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")
...