Hello,
I had some questions about loading the pretrained model with/without top.
In C4 W2 Exercise 2, we use a pretrained model specifying the option without top layers when loading it:
include_top=False
Then, what is the point in having in the models folders 2 models saved, one “with” tp and one “without” top, if you can manage this with the option “include_top”?
Moreover,
Why does loading the pre-trained model “without” top give a dimension error if you choose the option include_top=True? If the model is already "without” a top, what difference does it make?
ValueError: You are trying to load a weight file containing 104 layers into a model with 105 layers.
And why, if you load the model “with” top and include_top=False, the parameter weights=base_model_path fails and asks you to choose another option like “imagenet” for these weights.
If I load the model “with” top and choose weights=“imagenet”, the test is also passed successfully.
top refers to the output layer of a model. Since the base model was trained on different number of classes than in the problem, it’s important to set include_top to exclude the final layer.
Weights are floating point arrays. So, their dimensions and layer counts should match the model architecture i.e., loading correct weights file based on include_top matters. The change you’re asking for requires changes to keras / tensorflow codebase. Please talk to the respective teams for partially loading weights.
When weights is set to a path, tensorflow will load model weights from the specified location. Ifweights="imagenet", weights will be downloaded via internet if they aren’t cached locally (typically under $HOME/.keras/models).
1 Like
Right! The key point is that there are two separate objects involved here: the model (which is the instantiated function code for all the layers) and the weights (the actual tensors containing the parameters that are referenced by the model code) that get loaded separately. The TF logic gives you the ability to modify the model code when you load it by changing the include_top value, but apparently it does not handle that for the weights. The weight file you reference has to match the way the model is set up according to your include_top choice.
Hello,
Thank you both very much for your answers!
Much clearer now.
tf.keras.applications.MobileNetV2 is a function that builds the model architecture
the .h5 files we refer to for the weights parameter contain only the weights tensors.
Thanks!
1 Like
You got that right. Do see .keras file extension which saves all details about the model. One can load the entire model architecture and weights from the file directly.