我正在尝试获取通过 Vgg16 网络传递的图像(训练和验证)的输出include_top = false,然后添加最后几层,如下面的代码所示。
我想x存储完整的模型,以便我可以从中创建一个 tflite 文件(包括 vgg 和我添加的图层)
from tensorflow.keras.models import Model
import os
x= vgg16.output
print(x.shape)
x = GlobalAveragePooling2D()(x)
x = Flatten()(x)
x = Dense(100)(x)
x = tf.keras.layers.LeakyReLU(alpha=0.2)(x)
x = (Dropout(0.5)) (x)
x = (Dense(50)) (x)
x = tf.keras.layers.LeakyReLU(alpha=0.3)(x)
x = Dropout(0.3)(x)
x = Dense(num_classes, activation='softmax')(x)
# this is the model we will train
model = Model(inputs=vgg16.input, outputs=x)
# first: train only the top layers (which were randomly initialized)
# i.e. freeze all convolutional InceptionV3 layers
for layer in vgg16.layers:
layer.trainable = False
model.compile(loss='categorical_crossentropy',
optimizer=optimizers.RMSprop(lr=1e-4),
metrics=['acc'])
# train the model on the new data for a few epochs
history = model.fit(train_data, train_labels,
epochs=15,
batch_size=batch_size,
validation_data=(validation_data, validation_labels))
model.save(top_model_weights_path)
(eval_loss, eval_accuracy) = model.evaluate(
validation_data, validation_labels, batch_size=batch_size, verbose=1)
的输出x.shape是 (?, ?, ?, 512)
train_data.shape (1660, 2, 2, 512)
train_labels.shape (1660, 4)
validation_data.shape (137, 4)
validation_labels.shape (137, 2, 2, 512)
错误:
ValueError:检查输入时出错:预期 input_3 具有形状 (None, None, 3) 但得到形状为 (2, 2, 512) 的数组
此错误发生在以下行:
52 验证数据=(验证数据,验证标签))
如下所示的先前代码片段工作得非常好,并提供准确的输出。train_data存储一个 numpy 数组vgg16.predict_generator()
子衿沉夜
相关分类