1
image_input = Input(shape=(224, 224, 3))

model = ResNet50(weights='imagenet', include_top=True)

model.summary()

last_layer = model.get_layer('avg_pool').output  
last_layer.shape  
x= Flatten(name='flatten')(last_layer)  
out = Dense(num_classes, activation='softmax', name='output_layer')(x)  
custom_resnet_model = Model(inputs=image_input,outputs= out)
custom_resnet_model.summary()

for layer in custom_resnet_model.layers[:-1]:  
    layer.trainable = False

custom_resnet_model.layers[-1].trainable

custom_resnet_model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

t=time.time()  
hist = custom_resnet_model.fit(X_train, y_train, batch_size=32, epochs=1, verbose=1, 
validation_data=(X_test, y_test))  
print('Training time: %s' % (t - time.time()))  
(loss, accuracy) = custom_resnet_model.evaluate(X_test, y_test, batch_size=10, verbose=1)

print("[INFO] loss={:.4f}, accuracy: {:.4f}%".format(loss,accuracy * 100))

我收到了这个错误

输入 0 与 layer flatten 不兼容:预期 min_ndim=3,在运行 x= Flatten(name='flatten')(last_layer) 时发现 ndim=2。

的形状last_layerTensorShape([Dimension(None), Dimension(2048)])? 谁能解释如何在 keras 中解决这个问题?

4

0 回答 0