Keras ImageDataGenerator flow_from_dataframe返回KeyError

时间:2018-10-16 01:56:55

标签: python image-processing machine-learning keras

我正在尝试使用keras构建图像分类器,并且我的数据集的大小要求我使用ImageDataGenerator类及其flow_from_dataframe方法。这是我正在使用的代码。

train_datagen = keras.preprocessing.image.ImageDataGenerator(
        rescale=1./255,
        shear_range=0.2,
        zoom_range=0.2,
        horizontal_flip=True)
train_generator = train_datagen.flow_from_dataframe(
        directory='stage_1_train_images/',
        dataframe=box.drop(labels=['patientId'], axis=1).replace(to_replace=float('nan'),value=0),
        target_size=(1024, 1024))
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),activation='linear',input_shape=(28,28,1),padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D((2, 2),padding='same'))
model.add(Conv2D(64, (3, 3), activation='linear',padding='same'))
model.add(LeakyReLU(alpha=0.1))
model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
model.add(Conv2D(128, (3, 3), activation='linear',padding='same'))
model.add(LeakyReLU(alpha=0.1))                  
model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
model.add(Flatten())
model.add(Dense(128, activation='linear'))
model.add(LeakyReLU(alpha=0.1))                  
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adam(lr=1000,decay=.99),
              metrics=['accuracy'])
model.fit_generator(trainGen, steps_per_epoch=1024/16, epochs=317)

但是,当我运行此代码时,出现以下错误

KeyError                                  Traceback (most recent call last)
<ipython-input-7-5a88afda8de5> in <module>
      7         directory='stage_1_train_images/',
      8         dataframe=box.drop(labels=['patientId'], axis=1).replace(to_replace=float('nan'),value=0),
----> 9         target_size=(1024, 1024))
     10 model = Sequential()
     11 model.add(Conv2D(32, kernel_size=(3, 3),activation='linear',input_shape=(28,28,1),padding='same'))

/opt/conda/lib/python3.6/site-packages/keras_preprocessing/image.py in flow_from_dataframe(self, dataframe, directory, x_col, y_col, has_ext, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, save_to_dir, save_prefix, save_format, subset, interpolation)
   1105                                  save_format=save_format,
   1106                                  subset=subset,
-> 1107                                  interpolation=interpolation)
   1108 
   1109     def standardize(self, x):

/opt/conda/lib/python3.6/site-packages/keras_preprocessing/image.py in __init__(self, dataframe, directory, image_data_generator, x_col, y_col, has_ext, target_size, color_mode, classes, class_mode, batch_size, shuffle, seed, data_format, save_to_dir, save_prefix, save_format, follow_links, subset, interpolation, dtype)
   2056             raise ValueError("has_ext must be either True if filenames in"
   2057                              " x_col has extensions,else False.")
-> 2058         self.df = dataframe.drop_duplicates(x_col)
   2059         self.df[x_col] = self.df[x_col].astype(str)
   2060         self.directory = directory

/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py in drop_duplicates(self, subset, keep, inplace)
   4329         """
   4330         inplace = validate_bool_kwarg(inplace, 'inplace')
-> 4331         duplicated = self.duplicated(subset, keep=keep)
   4332 
   4333         if inplace:

/opt/conda/lib/python3.6/site-packages/pandas/core/frame.py in duplicated(self, subset, keep)
   4379         diff = Index(subset).difference(self.columns)
   4380         if not diff.empty:
-> 4381             raise KeyError(diff)
   4382 
   4383         vals = (col.values for name, col in self.iteritems()

KeyError: Index(['filename'], dtype='object')

出了什么问题?我已经尝试了多种方法来解决此问题,但无法弄清楚为什么会发生这种情况。

1 个答案:

答案 0 :(得分:1)

根据文档here,您需要在x_col方法中指定y_colflow_from_dataframe作为参数。 x_coly_col的默认值分别是'filename'和'class'。从错误中,我猜测您的DataFrame中没有名为"filename"的列,这就是导致KeyError的原因。要解决此问题,请在flow_from_dataframe方法中指定以下两个参数。

  

x_col :字符串,数据框中包含以下内容的列          目标图像的文件名。

     

y_col :字符串或字符串列表,列在          将成为目标数据的数据框。