我正在尝试使用keras构建图像分类器,并且我的数据集的大小要求我使用ImageDataGenerator类及其flow_from_dataframe方法。这是我正在使用的代码。
training = pd.read_csv("dataset/train/training.csv")
training = (training[(training["view_type"].isin(["view_4","view_5"]))])
training = (training[["id", "class"]]).reset_index(drop = True)
datagen = ImageDataGenerator(rescale = 1. /255)
training.head()
id class
0 image_view4_0.jpg lace_up
1 image_view4_1.jpg lace_up
2 image_view4_2.jpg zipper
3 image_view4_3.jpg lace_up
4 image_view4_4.jpg hook&look
train_generaor = datagen.flow_from_dataframe(dataframe = training,
directory = "dataset/train/images/",
xcol = "id",
ycol = "class",
has_ext = True,
class_mode = "categorical",
target_size = (224, 224))
但是,当我在colab中运行火车生成器时,出现以下错误
KeyError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2524 try:
-> 2525 return self._engine.get_loc(key)
2526 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: 'filename'
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-39-e1787553cadc> in <module>()
7 target_size = (224, 224),
8 color_mode = "rgb",
----> 9 validate_filenames = True)
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/image_data_generator.py in flow_from_dataframe(self, dataframe, directory, x_col, y_col, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, save_to_dir, save_prefix, save_format, subset, interpolation, drop_duplicates, **kwargs)
664 subset=subset,
665 interpolation=interpolation,
--> 666 drop_duplicates=drop_duplicates
667 )
668
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/dataframe_iterator.py in __init__(self, dataframe, directory, image_data_generator, x_col, y_col, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, subset, interpolation, dtype, drop_duplicates)
118 self.dtype = dtype
119 # check that inputs match the required class_mode
--> 120 self._check_params(df, x_col, y_col, classes)
121 if drop_duplicates:
122 df.drop_duplicates(x_col, inplace=True)
/usr/local/lib/python3.6/dist-packages/keras_preprocessing/image/dataframe_iterator.py in _check_params(self, df, x_col, y_col, classes)
162 .format(self.class_mode, self.allowed_class_modes))
163 # check that filenames/filepaths column values are all strings
--> 164 if not all(df[x_col].apply(lambda x: isinstance(x, str))):
165 raise ValueError('All values in column x_col={} must be strings.'
166 .format(x_col))
/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in __getitem__(self, key)
2137 return self._getitem_multilevel(key)
2138 else:
-> 2139 return self._getitem_column(key)
2140
2141 def _getitem_column(self, key):
/usr/local/lib/python3.6/dist-packages/pandas/core/frame.py in _getitem_column(self, key)
2144 # get column
2145 if self.columns.is_unique:
-> 2146 return self._get_item_cache(key)
2147
2148 # duplicate columns & possible reduce dimensionality
/usr/local/lib/python3.6/dist-packages/pandas/core/generic.py in _get_item_cache(self, item)
1840 res = cache.get(item)
1841 if res is None:
-> 1842 values = self._data.get(item)
1843 res = self._box_item_values(item, values)
1844 cache[item] = res
/usr/local/lib/python3.6/dist-packages/pandas/core/internals.py in get(self, item, fastpath)
3841
3842 if not isna(item):
-> 3843 loc = self.items.get_loc(item)
3844 else:
3845 indexer = np.arange(len(self.items))[isna(self.items)]
/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2525 return self._engine.get_loc(key)
2526 except KeyError:
-> 2527 return self._engine.get_loc(self._maybe_cast_indexer(key))
2528
2529 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
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: 'filename'
出了什么问题?我已经尝试了多种方法来解决此问题,但无法弄清楚为什么会发生这种情况
答案 0 :(得分:0)
仅查看keras文档,“ filename”就是参数xcol
中所期望的。 xcol
是指数据框中的一列,该列应包含相对于参数directory
中提供的图像路径。
因此,您只是在提供错误的列作为参数。更改2列数据框的列名:
training.columns = ['path', 'class']
然后将您的参数更改为所需参数:
train_generaor = datagen.flow_from_dataframe(dataframe = training,
directory = "dataset/train/images/",
xcol = "path",
ycol = "class",
has_ext = True,
class_mode = "categorical",
target_size = (224, 224))