mrdbourke / tensorflow-deep-learning

All course materials for the Zero to Mastery Deep Learning with TensorFlow course.
https://dbourke.link/ZTMTFcourse
MIT License
5.05k stars 2.5k forks source link

Unable to load weights from checkpoint in 05_transfer_learning_in_tensorflow_part_2_fine_tuning.ipynb #519

Open piyushongithub opened 1 year ago

piyushongithub commented 1 year ago

I ran the notebook "05_transfer_learning_in_tensorflow_part_2_fine_tuning.ipynb" on google colab but I got an error in "Model 4: Fine-tuning an existing model all of data" section. It seems like tensorflow is unable to load weights from checkpoint for model_2. . I am getting shape incompatible error. I have attached screenshot of the error below.

image
JordanTheDodger commented 1 year ago

@piyushongithub

Review your model.fit() code. It is a shape error caused due to not includingModelCheckpoint callback while refitting model 3(unfroze,fine-tune last 10). I encountered same error and attempted to resolve it by adding Model Checkpoint. I have also provided solution on Udemy as well

piyushongithub commented 1 year ago

@JordanTheDodger But it would start fine-tuning from the stage model_3 was completed not from the stage of feature extractor. We want to fine tune from the stage of feature extractor (model_2 before fine tuning on 10% data).

gonbaez commented 1 year ago

@piyushongithub I had the same issue. Apparently is because the order of the weights changes when saving the model with modified trainable attributes link.

I fixed it by saving the checkpoint as as h5 and setiing the base_modle layer to not trainable before loading the weights. image

image

I hope this helps.

mrdbourke commented 1 year ago

Hi @piyushongithub,

This seems to be an issue with various versions of TensorFlow and the tf.keras.applications.efficientnet models.

Please see https://github.com/mrdbourke/tensorflow-deep-learning/issues/544 for some potential fixes (namely installing TensorFlow 2.9.0 which seems to be the most stable version at the time of writing).