KeyError:使用flow_from_dataframe为'0'

时间:2020-03-17 05:46:07

标签: python pandas image-processing keras deep-learning

我正在执行多标签分类。我正在尝试使用Keras创建数据生成器。

我直接使用read_csv打开csv文件并将其用于我的数据框(如下)。

我的数据集看起来像第0列是文件名。

from tensorflow.keras.preprocessing.image import ImageDataGenerator
image_data_generator = ImageDataGenerator(
    rotation_range=10,
    width_shift_range=0.1,
    height_shift_range=0.1,
    zoom_range=.1,
    rescale = 1./255
)
train_generator = image_data_generator.flow_from_dataframe(dataframe = df, 
                                              directory = './dataset_faces/', 
                                              target_size = (128, 128), 
                                              class_mode = 'other',
                                              x_col = '0',
                                              y_col  = '1',
                                            batch_size = 32
                                             )

这是我的代码:

KeyError                                  Traceback (most recent call last) c:\users\sgho0007\appdata\local\continuum\anaconda3\envs\tf_gpu\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)    2645             try:
-> 2646                 return self._engine.get_loc(key)    2647             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\index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()

KeyError: '0'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last) <ipython-input-46-3a68158370f8> in <module>
     13                                               x_col = '0',
     14                                               y_col  = '1',
---> 15                                             batch_size = 32
     16                                              )

c:\users\sgho0007\appdata\local\continuum\anaconda3\envs\tf_gpu\lib\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 

c:\users\sgho0007\appdata\local\continuum\anaconda3\envs\tf_gpu\lib\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)

c:\users\sgho0007\appdata\local\continuum\anaconda3\envs\tf_gpu\lib\site-packages\keras_preprocessing\image\dataframe_iterator.py in _check_params(self, df, x_col, y_col, weight_col, classes)
    179             )
    180         # check that filenames/filepaths column values are all strings
--> 181         if not all(df[x_col].apply(lambda x: isinstance(x, str))):
    182             raise TypeError('All values in column x_col={} must be strings.'
    183                             .format(x_col))

c:\users\sgho0007\appdata\local\continuum\anaconda3\envs\tf_gpu\lib\site-packages\pandas\core\frame.py in __getitem__(self, key)    2798             if self.columns.nlevels
> 1:    2799                 return self._getitem_multilevel(key)
-> 2800             indexer = self.columns.get_loc(key)    2801             if is_integer(indexer):    2802                 indexer = [indexer]

c:\users\sgho0007\appdata\local\continuum\anaconda3\envs\tf_gpu\lib\site-packages\pandas\core\indexes\base.py in get_loc(self, key, method, tolerance)    2646                 return self._engine.get_loc(key)    2647             except KeyError:
-> 2648                 return self._engine.get_loc(self._maybe_cast_indexer(key))    2649         indexer = self.get_indexer([key], method=method, tolerance=tolerance)  2650         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\index_class_helper.pxi in pandas._libs.index.Int64Engine._check_type()

KeyError: '0'

我收到此错误:

null

有人可以帮助我解决此问题吗?感谢您的帮助

0 个答案:

没有答案