Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
763 views
in Technique[技术] by (71.8m points)

python - I trained a keras model on google colab. Now not able to load it locally on my system.

with open('2model.json','r') as f:
json = f.read()
model = model_from_json(json)
model.load_weights("color_tensorflow_real_mode.h5")

I trained a keras model on google colab. Now not able to load it locally on my system. Getting this error: ValueError: Unknown initializer: GlorotUniform

How to solve this?? Every time I make a model on colab and try loading it locally I am unable to do so. Getting this error message:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-c3ed162a8277> in <module>()
----> 1 model = model_from_json(json)
      2 model.load_weights("color_tensorflow_real_mode.h5")

~Anaconda3libsite-packagesensorflowpythonkerasenginesaving.py in model_from_json(json_string, custom_objects)
    349   config = json.loads(json_string)
    350   from tensorflow.python.keras.layers import deserialize  # pylint: disable=g-import-not-at-top
--> 351   return deserialize(config, custom_objects=custom_objects)
    352 
    353 

~Anaconda3libsite-packagesensorflowpythonkeraslayersserialization.py in deserialize(config, custom_objects)
     62       module_objects=globs,
     63       custom_objects=custom_objects,
---> 64       printable_module_name='layer')

~Anaconda3libsite-packagesensorflowpythonkerasutilsgeneric_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    171             custom_objects=dict(
    172                 list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 173                 list(custom_objects.items())))
    174       with CustomObjectScope(custom_objects):
    175         return cls.from_config(config['config'])

~Anaconda3libsite-packagesensorflowpythonkerasengine
etwork.py in from_config(cls, config, custom_objects)
   1290     # First, we create all layers and enqueue nodes to be processed
   1291     for layer_data in config['layers']:
-> 1292       process_layer(layer_data)
   1293     # Then we process nodes in order of layer depth.
   1294     # Nodes that cannot yet be processed (if the inbound node

~Anaconda3libsite-packagesensorflowpythonkerasengine
etwork.py in process_layer(layer_data)
   1276       from tensorflow.python.keras.layers import deserialize as deserialize_layer  # pylint: disable=g-import-not-at-top
   1277 
-> 1278       layer = deserialize_layer(layer_data, custom_objects=custom_objects)
   1279       created_layers[layer_name] = layer
   1280 

~Anaconda3libsite-packagesensorflowpythonkeraslayersserialization.py in deserialize(config, custom_objects)
     62       module_objects=globs,
     63       custom_objects=custom_objects,
---> 64       printable_module_name='layer')

~Anaconda3libsite-packagesensorflowpythonkerasutilsgeneric_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    173                 list(custom_objects.items())))
    174       with CustomObjectScope(custom_objects):
--> 175         return cls.from_config(config['config'])
    176     else:
    177       # Then `cls` may be a function returning a class.

~Anaconda3libsite-packagesensorflowpythonkerasenginease_layer.py in from_config(cls, config)
   1615         A layer instance.
   1616     """
-> 1617     return cls(**config)
   1618 
   1619 

~Anaconda3libsite-packagesensorflowpythonkeraslayersconvolutional.py in __init__(self, filters, kernel_size, strides, padding, data_format, dilation_rate, activation, use_bias, kernel_initializer, bias_initializer, kernel_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, bias_constraint, **kwargs)
    464         activation=activations.get(activation),
    465         use_bias=use_bias,
--> 466         kernel_initializer=initializers.get(kernel_initializer),
    467         bias_initializer=initializers.get(bias_initializer),
    468         kernel_regularizer=regularizers.get(kernel_regularizer),

~Anaconda3libsite-packagesensorflowpythonkerasinitializers.py in get(identifier)
    153     return None
    154   if isinstance(identifier, dict):
--> 155     return deserialize(identifier)
    156   elif isinstance(identifier, six.string_types):
    157     config = {'class_name': str(identifier), 'config': {}}

~Anaconda3libsite-packagesensorflowpythonkerasinitializers.py in deserialize(config, custom_objects)
    145       module_objects=globals(),
    146       custom_objects=custom_objects,
--> 147       printable_module_name='initializer')
    148 
    149 

~Anaconda3libsite-packagesensorflowpythonkerasutilsgeneric_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
    161       cls = module_objects.get(class_name)
    162       if cls is None:
--> 163         raise ValueError('Unknown ' + printable_module_name + ': ' + class_name)
    164     if hasattr(cls, 'from_config'):
    165       arg_spec = tf_inspect.getfullargspec(cls.from_config)

ValueError: Unknown initializer: GlorotUniform

Stackoverflow is asking me to add details while I have none to add. Or I am not sure what to add. Please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  1. Make sure you have the newest version of Keras and tensorflow (which are 2.4.4 and 1.11.0) by running either pip install keras tensorflow or conda install keras tensorflow.

  2. In case it is Google Colab that uses deprecated objects, you may need to use custom objects:

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

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

Not sure if this is your case though.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...