KeyError:使用ImageDataGenerator.flow_from_dataframe时为“类” [英] KeyError: 'class' while using ImageDataGenerator.flow_from_dataframe

查看:101
本文介绍了KeyError:使用ImageDataGenerator.flow_from_dataframe时为“类”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ImageDataGenerator.flow_from_dataframe创建数据生成器,但遇到键错误:类

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

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

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()

下面是数据帧train_df的输出。

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.

推荐答案

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

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.

这篇关于KeyError:使用ImageDataGenerator.flow_from_dataframe时为“类”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆