Dhruvin modi
Dhruvin modi

Reputation: 643

Unknown initializer: GlorotUniform when loading Keras model

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

Upvotes: 37

Views: 50831

Answers (11)

Jasminy
Jasminy

Reputation: 109

For the json file problem mentioned by @Derk in one of the comment, you can write the following:

model_from_json(model_path, custom_objects = {'GlorotUniform': glorot_uniform()})

and in your import line, remember to write:

from tensorflow.keras.initializers import glorot_uniform

instead of from keras.initializers import glorot_uniform.

It worked out for me when I try to read a model saved in tf2.2 in the environment with only tf1.9.

Upvotes: 0

Saish Reddy
Saish Reddy

Reputation: 61

In either kaggle or colabs

tf.keras.models.load_model("model_path")

works well

Upvotes: 3

I fixed the problem:

Before:

from keras.models import load_model
classifierLoad = load_model('model/modeltest.h5')

Works for me

import tensorflow as tf 
classifierLoad = tf.keras.models.load_model('model/modeltest.h5')

Upvotes: 43

Benjamin
Benjamin

Reputation: 51

I had the same problem with a model built with tensorflow 1.11.0 (using tensorflow.python.keras.models.save_model) and loaded with tensoflow 1.11.0 (using tensorflow.python.keras.models.load_model).

I solved it by upgrading everything to tensorflow 1.13.1, after building the model again with the new version, I could load it without this error.

Upvotes: 0

Josh Anish
Josh Anish

Reputation: 109

from tensorflow.keras.initializers import glorot_uniform

loaded_model = tf.keras.models.load_model("pruned.h5",custom_objects={'GlorotUniform': glorot_uniform()})

this worked for me when importing tensorflow keras

Upvotes: 2

Ilyes
Ilyes

Reputation: 611

if you are loading the architecture and weights separtly, while loading archtiecture of the model change :

models.model_from_json(json)

to :

tf.keras.models.model_from_json(json)

and the problem is solved

Upvotes: 1

0x01h
0x01h

Reputation: 925

Changing

from keras.models import load_model

to

from tensorflow.keras.models import load_model

solved my problem!

To eliminate errors, import all things directly from Keras or TensorFlow. Mixing both of them in same project may result in problems.

Upvotes: 15

M. Viaz
M. Viaz

Reputation: 83

Something that helped me which wasn't in any of the answers:

custom_objects={'GlorotUniform': glorot_uniform()}

Upvotes: 3

Alex Begun
Alex Begun

Reputation: 451

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..

import keras
from keras.models import load_model
from keras.utils import CustomObjectScope
from keras.initializers import glorot_uniform

with CustomObjectScope({'GlorotUniform': glorot_uniform()}):
        model = load_model('imdb_mlp_model.h5')

Upvotes: 30

lintex
lintex

Reputation: 456

I ran into the same issue. After changing:

from tensorflow import keras

to:

import keras

life is once again worth living.

Upvotes: 44

Babakslt
Babakslt

Reputation: 199

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:

the_model.save(file_path,True/False,False)

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:

json = model.to_json()
# Save the json on a file
model.save_weights(weights_filepath,save_format="h5")

rebuild the model like this:

# load the json file
# here i use json as loaded content of json file
model = keras.models.model_from_json(json)
model.load_weights(weights_file_path)

Upvotes: 3

Related Questions