Model with weights for the last layer - incompatible when include_last=False

Regarding the second assignment.

We are tasked with building a transfer learning network. The issue I’m having is that we are asked to exclude the last layer when setting our model. However, the weights that are used to initialize the model include the last layer. I get a expect 104 layers, loaded 105.

When I use 'imagenet' to load the weights the error goes away. This requires that I replace what was already in place (already set for me in the template). Everything is so well thought out in these assignments that I suspect I might be doing something wrong.

base_model = tf.keras.applications.MobileNetV2(input_shape=input_shape,
                                                   include_top=False, # <== Important!!!!
                                                   weights='imagenet') # WAS base_model_path

# where base_model_path="imagenet_base_model/with_top_mobilenet_v2_weights_tf_dim_ordering_tf_kernels_1.0_160.h5"

I still have not passed all the tests (I’m still working on the network configuration), but wanted to get clarification here. What weights should I load to initialize the model where I’m tasked with training the last few layers?

Thank you in advance!!

The current method for loading the weights (from the notebook update 3 months ago) is to use the variable ‘base_model_path’.

I suspect a newer version of the template had the wrong reference to the previously computed weights. The model works and passes the tests using 'imagenet' instead of the locally available file.

Note: There are older posts that ask different questions about the assignment. The older version uses 'imagenet'.

@TMosh – Thank you for your reply. I used what you proposed to start with. It’s the weights that you mention that raises the error.

The older version is obsolete. I suspect your notebook maybe has two offsetting mistakes.

In the end the submitted assignment passed (100%).

The template is indeed off. The last reporting cell does not initialize the accumulators correctly. After I add the following, the code works.

loss = []
val_loss = []
acc = []
val_acc = []

As you are the only person to report an issue on this notebook since it was updated, I suspect the issue is elsewhere in your code.

Please check your personal messages for instructions.

I have tested your notebook after making the following changes:

  • In cells [10] and [14], I used the correct base_model_path to load the weights. Note that the base model weights paths are different for cell [10] (with top layer) than for cell [14] (without top layer).

  • I removed the chained assignment “=loss=” from the definition of the loss function (in UNQ_C3).

  • In cell [26], I removed the initialization of the variables. The reason for not initializing the variables here is that for the history plots, we want to show the first 5 epochs (the base training) along with the second 5 epochs (for the fine tuning) to present them all in one plot of all 10 epochs.

1 Like