使用ImageDataGenerator时Keras给出错误

时间:2019-04-15 07:24:07

标签: python image tensorflow machine-learning keras

我有一个很大的数据集,已添加到Keras ImageDataGenerator中。

一切似乎都可以正常工作,直到抛出此错误:

    ValueError: Output of generator should be a tuple `(x, y, sample_weight)` or `(x, y)`. Found: [[[[ 91.  91.  93.]
   [ 96.  96.  98.]
   [ 98.  98. 100.]
   ...
   [115. 116. 118.]
   [108. 109. 111.]
   [107. 108. 110.]]

  [[ 93.  93.  95.]
   [ 97.  97.  99.]
   [ 98.  98. 100.]
   ...

我已经这样创建了生成器:

train_datagen = ImageDataGenerator()
train_generator = train_datagen.flow_from_dataframe(
    dataframe=img_celebs[img_celebs['SmallTrain'] == 1].reset_index(), 
    directory='data',
    target_size=(img_width, img_height),
    x_col='file', 
    class_mode=None)

似乎可行。例如

 train_generator.image_shape gives
(218, 178, 3)

我的模特是:

model = Sequential()
model.add(Conv2D(32, (3, 3), input_shape=(218, 178, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(32, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Conv2D(64, (3, 3)))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))

model.add(Flatten())  # this converts our 3D feature maps to 1D feature vectors
model.add(Dense(64))
model.add(Activation('relu'))
model.add(Dropout(0.5))
model.add(Dense(1))
model.add(Activation('sigmoid'))

model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])

然后运行:

model.fit_generator(
        train_generator,
        steps_per_epoch=2000 // batch_size,
        epochs=50,
        validation_data=validation_generator,
        validation_steps=800 // batch_size)

1 个答案:

答案 0 :(得分:0)

我将这部分的class_mode更改为“输入”

train_generator = train_datagen.flow_from_dataframe(
    dataframe=img_celebs[img_celebs['SmallTrain'] == 1].reset_index(), 
    directory='data',
    target_size=(img_width, img_height),
    x_col='file', 
    class_mode='input')

它似乎可以正常工作:-)