【发布时间】:2022-08-05 11:35:22
【问题描述】:
我正在尝试将数据增强作为一个层添加到模型中,但出现以下错误。
TypeError: The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.preprocessing.image.ImageDataGenerator object at 0x7f8c2dea0710>
data_augmentation = tf.keras.preprocessing.image.ImageDataGenerator(
rotation_range=30, horizontal_flip=True)
model = Sequential()
model.add(data_augmentation)
model.add(Dense(1028,input_shape=(final_features.shape[1],)))
model.add(Dropout(0.7,input_shape=(final_features.shape[1],)))
model.add(Dense(n_classes, activation= \'softmax\', kernel_regularizer=\'l2\'))
model.compile(optimizer=adam,
loss=\'categorical_crossentropy\',
metrics=[\'accuracy\'])
history = model.fit(final_features, y,
batch_size=batch_size,
epochs=epochs,
validation_split=0.2,
callbacks=[lrr,EarlyStop])
我也尝试过这种方式:
data_augmentation = Sequential(
[
preprocessing.RandomFlip(\"horizontal\"),
preprocessing.RandomRotation(0.1),
preprocessing.RandomZoom(0.1),
]
)
model = Sequential()
model.add(data_augmentation)
model.add(Dense(1028,input_shape=(final_features.shape[1],)))
model.add(Dropout(0.7,input_shape=(final_features.shape[1],)))
model.add(Dense(n_classes, activation= \'softmax\', kernel_regularizer=\'l2\'))
model.compile(optimizer=adam,
loss=\'categorical_crossentropy\',
metrics=[\'accuracy\'])
history = model.fit(final_features, y,
batch_size=batch_size,
epochs=epochs,
validation_split=0.2,
callbacks=[lrr,EarlyStop])
它给出了一个错误:
ValueError: Input 0 of layer sequential_7 is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [128, 14272]
您能否建议我如何在 Keras 中使用增强功能?