Reputation: 711
I load the saved model and for finetuning reason I add classification layers to the output of loaded model, So this what I write :
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]
How can I resolve this problem?
Upvotes: 3
Views: 4125
Reputation: 1
if you are using GoogleColab which I was try rerunning all the whole cells above from when you started building the model
Upvotes: 0
Reputation:
Could have answered better if you would have shared model.h5
architecture or the last layer of the model.h5
.
In your case the input dimension is 2
where as tf.keras.layers.GlobalAveragePooling2D()
expects input dimension of 4
.
As per tf.keras.layers.GlobalAveragePooling2D documentation, the tf.keras.layers.GlobalAveragePooling2D layer expects below input shape -
Input shape: If
data_format='channels_last'
: 4D tensor with shape(batch_size, rows, cols, channels)
. Ifdata_format='channels_first'
: 4D tensor with shape(batch_size, channels, rows, cols)
.
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.
Upvotes: 0