I trained my CNN (VGG) through google colab and generated .h5 file. Now problem is, I can predict my output successfully through google colab but when i download that .h5 trained model file and try to predict output on my laptop, I am getting error when loading the model.
Here is the code:
import tensorflow as tf
from tensorflow import keras
import h5py
# Initialization
loaded_model = keras.models.load_model('./train_personCount_model.h5')
And the error:
ValueError: Unknown initializer: GlorotUniform
Wow I, just spent 6 Hours of my life trying to figure this out.. Dmitri posted a solution to this here: I trained a keras model on google colab. Now not able to load it locally on my system.
I'm just basically reposting it here because it worked for me.
This looks like some kind of a serialization bug in keras. If you wrap your load_model with the below CustomObjectScope thingy... all should work..
if you are loading the architecture and weights separtly, while loading archtiecture of the model change :
to :
and the problem is solved
I had a same problem and was fixed this way. just don't save the optimizer with the model! just change the save line like this:
Second parameter tells Keras to overwrite the model if the file existed or not and the 3rd one tells it not to save the optimizer with the model.
Edit: I ran over the problem again on another system today and this did not helped me this time. so i saved the model conf as json and weights as h5 and used them to rebuild the model in another machine. you can do it like this. save like this:
rebuild the model like this:
I ran into the same issue. After changing:
from tensorflow import keras
to:
import keras
life is once again worth living.
this worked for me when importing tensorflow keras
I fixed the problem:
Before:
Works for me