QBoard » Artificial Intelligence & ML » AI and ML - Tensorflow » Unknown initializer: GlorotUniform when loading Keras model

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​
    This post was edited by Advika Banerjee at September 8, 2020 1:15 PM IST
      September 8, 2020 1:14 PM IST
    0
    • Rishi Pandya
      Rishi Pandya Probably caused by a Keras version mismatch between google colab's and your local machine's.
      September 8, 2020
  • I ran into the same issue. After changing:

    from tensorflow import keras

     

    to:

    import keras

     

      September 8, 2020 1:31 PM IST
    1
    • Advika Banerjee
      Advika Banerjee @Rakesh Racharla,this works :) it was due to version mismatch of keras. thanks
      September 8, 2020
  • 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.

      September 8, 2020 1:39 PM IST
    1
  • 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')
      September 8, 2020 1:27 PM IST
    0
  • 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')
      September 8, 2020 1:28 PM IST
    0
  • 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

      September 8, 2020 1:40 PM IST
    0