Keras- compatibility between Model architecture and saved weights file

I saved weights of a fine tuned model with last layer having only two outputs. I could not save the structure of fine tuned model because of lambda function in pre-processing.

When I created model by calling

model = VGG_16()

I did not get any error message. When I checked model.summary(), it had 1000 outputs in the last year. However, saved model has only two outputs.

How does Keras will check for model structure and its weights compatibility if they are loaded separately. gist file of this working is available here.

Please let me know.

Keras can’t load weights for layers that don’t exist yet.

You need to reconstruct your model exactly as it was (i.e. removing top VGG layer and adding your dense layers) before loading weights.

It will also help if you assign names to your layers with trained weights (otherwise Keras assigns them automatically, which may make it more difficult to get names to line up).

You can also try creating a full custom layer for your preprocessing function, which would let you save the full model (you will need to add some parameters to the layer and will need to import its code before loading the model).

Thank you for replying.
Why did not Keras throw an error message, when weights and original model are different.

I also ran into issues to save the full model. I posted another question on the forum here.

Pls let me know, if you have a reference to follow.

Thank you,

Here is the answer from fchollet on github:

I need to understand this statement – “Weights loading skips layers that have no weights, so many different models can be compatible with the same set of weights.”

It means that avg_vgg16 and vgg16 can use the same weight file (pooling steps can be different since no weights).