ValueError:检查输入时出错:预期density_1_input具有形状(1,224,224),但数组的形状为(224,224,1)

时间:2019-07-11 17:34:36

标签: python keras deep-learning

我正在使用model.fit_generator,它给我一个错误,即输入大小与预期大小不匹配。但是我使用image_datagen.flow_from_directory使用target_size=(224, 224)重塑了它,无法将其设置为(1, 224, 224),否则给了我另一个错误。

使用train_generator = image_datagen.flow_from_directory(target_size =(224,224))

时,我不确定如何检查输入的大小
train_generator = image_datagen.flow_from_directory(
'C:/output/train/',
    class_mode="categorical",
    seed=seed,
    batch_size=batch_size,
    target_size=(input_size, input_size),
    color_mode='grayscale',
    shuffle=True)

valid_generator = image_datagen.flow_from_directory(
    'C:/output/valid/',
    class_mode="categorical",
    seed=seed,
    batch_size=batch_size,
    target_size=(input_size, input_size),
    color_mode='grayscale',
    shuffle=True)


# https://github.com/keras-team/keras/blob/master/keras/callbacks.py
class MyCheckPoint(keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs=None):
        loss = logs["loss"]
        val_loss = logs["val_loss"]
        fileName = "model.%02d_%0.5f_%0.5f.h5" % (epoch, loss, val_loss)
        self.model.save(fileName)

#weight_saver = MyCheckPoint()

model = models.getVGGModel(num_classes)
#model = models. getStandardModel(input_size)
model.compile(optimizer=Adam(lr=1e-5, decay=1e-8), loss=keras.losses.categorical_crossentropy)
#model.load_weights("weights.26-1.48.h5")

weight_saver = ModelCheckpoint('weights.{epoch:02d}-{val_loss:.2f}.h5',save_best_only=True, save_weights_only=True)
hist = model.fit_generator(train_generator, validation_data=valid_generator, validation_steps=80, steps_per_epoch=400, epochs=200, callbacks=[weight_saver])

def getVGGModel(num_classes):
    model = Sequential()
    model.add(Dense(32, input_shape=(1, 224, 224)))
    # Reshape((784,), input_shape=(1, 224, 224))
    model.add(Conv2D(64, (3, 3), activation='relu', strides=(1,1), padding='same',input_shape=(1, 224, 224), data_format="channels_first"))
    model.add(Conv2D(64, (3, 3), activation='relu', strides=(1,1), padding='same',data_format = 'channels_first'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2),data_format = 'channels_first'))

    model.add(Conv2D(128, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))
    model.add(Conv2D(128, (3, 3), activation='relu', padding='same', data_format = 'channels_first'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2), padding='same',data_format = 'channels_first'))

    model.add(Conv2D(256, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))
    model.add(Conv2D(256, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2), padding='same',data_format = 'channels_first'))

    model.add(Conv2D(512, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))
    model.add(Conv2D(512, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2), padding='same',data_format = 'channels_first'))

    model.add(Conv2D(512, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))
    model.add(Conv2D(512, (3, 3), activation='relu', padding='same',data_format = 'channels_first'))
    model.add(MaxPooling2D(pool_size=(2, 2), strides=(2,2), padding='same',data_format = 'channels_first'))

    model.add(Flatten())
    model.add(Dense(4096, activation="relu"))
    model.add(Dropout(0.5))
    model.add(Dense(4096, activation="relu"))
    model.add(Dropout(0.5))
    model.add(Dense(num_classes, activation="softmax"))

    return model

1 个答案:

答案 0 :(得分:0)

问题来自模型的输入:

model.add(Dense(32, input_shape=(1, 224, 224)))

默认情况下,target_size=(224, 224)将为您提供一个形状为(224,224,1)的张量,其通道为last。
但是,您要先为模型指定一个带有通道的输入。

只需将输入更改为:

model.add(Dense(32, input_shape=(224, 224, 1)))

当然,您不再需要为所有图层指定data_format了,Keras默认使用channel_last。