我正在尝试使用ImageDataGenerator.flow_from_dataframe创建数据生成器,但遇到了关键错误: 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'
有人能告诉我如何解决这个问题吗?任何帮助都是非常感谢的。
发布于 2019-10-16 14:23:06
你能试一下吗,基本上把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")
发布于 2020-06-13 12:13:39
不要透视表。只需将y_col作为标签字段传递,并将唯一值列表放入class参数中。将class_mode设置为分类。另外,它将分别是x_col和y_col。Keras自动执行one-hot编码,并完成其余工作。
发布于 2020-06-13 09:58:22
您使用的是xcol
和ycol
,而不是导致此错误的x_col
和y_col
https://stackoverflow.com/questions/58165203
复制相似问题