keras_to_tensorflow icon indicating copy to clipboard operation
keras_to_tensorflow copied to clipboard

unknown initializer: GlorotUniform

Open ndvbd opened this issue 6 years ago • 7 comments

I saved a tensorflow keras sequential model, and then tried the keras_to_tensorflow and got: Unknown initializer: GlorotUniform

ndvbd avatar Mar 24 '19 16:03 ndvbd

@ndvbd how did you fix this issue?

koriavinash1 avatar Apr 24 '19 09:04 koriavinash1

It just means that the installed keras library doesn't know a particular layer. Add something like before loading the model:

 custom_objects={"GlorotUniform": tf.keras.initializers.glorot_uniform}

amir-abdi avatar Apr 24 '19 18:04 amir-abdi

Check this thread: https://github.com/tensorflow/tfjs/issues/798

amir-abdi avatar Apr 24 '19 18:04 amir-abdi

Hello, I have the same issue:

Using TensorFlow backend. WARNING: Logging before flag parsing goes to stderr. W0717 11:32:34.720095 139954044680064 module_wrapper.py:136] From /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/util/module_wrapper.py:163: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

E0717 11:32:34.724069 139954044680064 keras_to_tensorflow.py:96] Input file specified only holds the weights, and not the model definition. Save the model using model.save(filename.h5) which will contain the network architecture as well as its weights. If the model is saved using the model.save_weights(filename) function, either input_model_json or input_model_yaml flags should be set to to import the network architecture prior to loading the weights.

ValueError: Unknown initializer: GlorotUniform

Where I have to add custom_objects={"GlorotUniform": tf.keras.initializers.glorot_uniform} ?

Because, I don't load the model: I execute model.save and I use keras_to_tensorflow to convert the saved .h5 Do I miss a step? It seems keras_to_tensorflow tool doesn't properly read the .h5: " ..Save the model using model.save(filename.h5).."

Any other suggestions to solve the problem?

CosmaBelli avatar Jul 17 '19 12:07 CosmaBelli

I modified line 19 to 21 in keras_to_tensorflow.py file

# import keras # from keras import backend as K # from keras.models import model_from_json, model_from_yaml from tensorflow import keras from tensorflow.keras import backend as K from tensorflow.keras.models import model_from_json, model_from_yaml

It worked with tensorflow keras file

Zerox16 avatar Aug 05 '19 08:08 Zerox16

I modified line 19 to 21 in keras_to_tensorflow.py file

# import keras # from keras import backend as K # from keras.models import model_from_json, model_from_yaml from tensorflow import keras from tensorflow.keras import backend as K from tensorflow.keras.models import model_from_json, model_from_yaml

It worked with tensorflow keras file

Thank you for the suggestion. How do you see this change be helpful to users?

amir-abdi avatar Aug 12 '19 11:08 amir-abdi

`from keras.utils import CustomObjectScope from keras.initializers import glorot_uniform

with CustomObjectScope({'GlorotUniform': glorot_uniform()}): model = keras.models.load_model('saved_gen.h5')`

This may help you!

talibiqbal247 avatar Mar 02 '20 09:03 talibiqbal247