[英]ValueError: Input 0 of layer global_average_pooling2d is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 128]
I load the saved model and for f.netuning reason I add classification layers to the output of loaded model, So this what I write:我加载保存的 model,出于 f.netuning 的原因,我将分类层添加到加载的 model 的 output,所以这是我写的:
def create_keras_model():
model = tf.keras.models.load_model('model.h5', compile=False)
resnet_output = model.output
layer1 = tf.keras.layers.GlobalAveragePooling2D()(resnet_output)
layer2 = tf.keras.layers.Dense(units=256, use_bias=False, name='nonlinear')(layer1)
model_output = tf.keras.layers.Dense(units=2, use_bias=False, name='output', activation='relu')(layer2)
model = tf.keras.Model(model.input, model_output)
return model
but I find this error:但我发现这个错误:
ValueError: Input 0 of layer global_average_pooling2d is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 128]
Can anyone please help me and tell me from what this error and how can I resolve this problem.任何人都可以帮助我并告诉我这个错误是什么以及如何解决这个问题。 Thanks!
谢谢!
Could have answered better if you would have shared model.h5
architecture or the last layer of the model.h5
.如果您共享
model.h5
架构或model.h5
的最后一层,本可以回答得更好。
In your case the input dimension is 2
where as tf.keras.layers.GlobalAveragePooling2D()
expects input dimension of 4
.在您的情况下,输入维度为
2
,其中tf.keras.layers.GlobalAveragePooling2D()
期望输入维度为4
。
As per tf.keras.layers.GlobalAveragePooling2D documentation, the tf.keras.layers.GlobalAveragePooling2D layer expects below input shape -根据tf.keras.layers.GlobalAveragePooling2D文档,tf.keras.layers.GlobalAveragePooling2D 层期望低于输入形状 -
Input shape: If
data_format='channels_last'
: 4D tensor with shape(batch_size, rows, cols, channels)
.输入形状:如果
data_format='channels_last'
:形状为(batch_size, rows, cols, channels)
的 4D 张量。 Ifdata_format='channels_first'
: 4D tensor with shape(batch_size, channels, rows, cols)
.如果
data_format='channels_first'
:形状为(batch_size, channels, rows, cols)
的 4D 张量。
In this tensorflow tutorial , you will learn how to classify images of cats and dogs by using transfer learning from a pre-trained.network along with fine-tuning.在此tensorflow 教程中,您将学习如何使用来自预训练网络的迁移学习以及微调对猫和狗的图像进行分类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.