Skip to main content
 首页 » 编程设计

python之Keras ImageDataGenerator flow_from_dataframe 返回 KeyError

2024年12月31日8Terrylee

我正在尝试使用 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') 

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

请您参考如下方法:

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

x_col: string, column in the dataframe that contains the filenames of the target images.

y_col: string or list of strings,columns in the dataframe that will be the target data.