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]

Can anyone please help me and tell me from what this error and how can I resolve this problem. Thanks!

1

There are 1 answers

0
AudioBubble On

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). If data_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.