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()
model.load_weights('my_model_weights.h5')
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.
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).
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.”