Home > other >  Load weight of a model trained as one functional restnet into restnet model but built using multiple
Load weight of a model trained as one functional restnet into restnet model but built using multiple

Time:02-05

I have used a code that trained a resnet model as none functional layer;

 base_model = tf.keras.applications.ResNet50(include_top=False, weights=None, input_shape=(224, 224, 3))
    base_model.trainable = True
    
    inputs = Input((224, 224, 3))
    h = base_model(inputs, training=True)
    model = Model(inputs, projection_3)

when you call summary:

Layer (type)                Output Shape              Param #   
=================================================================
 input_image (InputLayer)    [(None, 256, 256, 3)]     0         
                                                                 
 resnet50 (Functional)       (None, 8, 8, 2048)        23587712  
                                                                 
=================================================================

Now, I need to load the weight into resnet built of many layer

Resmodel  = tf.keras.applications.ResNet50(input_tensor=inputs, weights=None, include_top=False)

However, when loading the weight, I got:

 model.load_weights(filename)

ValueError: Layer count mismatch when loading weights from file. Model expected 106 layers, found 4 saved layers.

Its the same model, only one functional (whole model as one layer) and the other split into many layers. How do I transfer the weights between them.

CodePudding user response:

try saving the model again

model_n = model.layers[1] model_n.save("new_model.h5")

  •  Tags:  
  • Related