我在 google colab 上训练了一个 keras 模型。现在无法在我的系统上本地加载它。
Posted
技术标签:
【中文标题】我在 google colab 上训练了一个 keras 模型。现在无法在我的系统上本地加载它。【英文标题】:I trained a keras model on google colab. Now not able to load it locally on my system. 【发布时间】:2019-04-02 17:25:05 【问题描述】:with open('2model.json','r') as f:
json = f.read()
model = model_from_json(json)
model.load_weights("color_tensorflow_real_mode.h5")
收到此错误:ValueError: Unknown initializer: GlorotUniform
如何解决这个问题? 每次我在 colab 上制作模型并尝试在本地加载它时,我都无法这样做。 收到此错误消息:
---------------------------------------------------------------------------
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")
~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\saving.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
~\Anaconda3\lib\site-packages\tensorflow\python\keras\layers\serialization.py in deserialize(config, custom_objects)
62 module_objects=globs,
63 custom_objects=custom_objects,
---> 64 printable_module_name='layer')
~\Anaconda3\lib\site-packages\tensorflow\python\keras\utils\generic_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'])
~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\network.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
~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\network.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
~\Anaconda3\lib\site-packages\tensorflow\python\keras\layers\serialization.py in deserialize(config, custom_objects)
62 module_objects=globs,
63 custom_objects=custom_objects,
---> 64 printable_module_name='layer')
~\Anaconda3\lib\site-packages\tensorflow\python\keras\utils\generic_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.
~\Anaconda3\lib\site-packages\tensorflow\python\keras\engine\base_layer.py in from_config(cls, config)
1615 A layer instance.
1616 """
-> 1617 return cls(**config)
1618
1619
~\Anaconda3\lib\site-packages\tensorflow\python\keras\layers\convolutional.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),
~\Anaconda3\lib\site-packages\tensorflow\python\keras\initializers.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':
~\Anaconda3\lib\site-packages\tensorflow\python\keras\initializers.py in deserialize(config, custom_objects)
145 module_objects=globals(),
146 custom_objects=custom_objects,
--> 147 printable_module_name='initializer')
148
149
~\Anaconda3\lib\site-packages\tensorflow\python\keras\utils\generic_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
*** 要求我添加详细信息,而我没有要添加的内容。或者我不确定要添加什么。请帮忙。
【问题讨论】:
您在 Colab 和本地机器中是否有相同的 TensorFlow 版本? 【参考方案1】:使用加载模型
from tensorflow.keras.models import load_model
而不是
from keras.models import load_model
我尝试了很多方法,但这是最终奏效的方法!
【讨论】:
非常感谢您的帖子!我使用: from keras.models import load_model 它适用于使用 Google AI 平台“runtimeVersion”:“2.2”和“pythonVersion”:“3.7”生成的模型 我想提供更多关于它是如何工作的细节:模型是在运行时 2.2 - tensorflow 2.2 上生成的。要加载它,我使用以下命令: pip install tensorflow==1.11.0 pip install keras==2.4.0 from keras.models import load_model new_model = load_model(local_model)【参考方案2】:当我尝试本地加载在 Colab 上训练的模型时,我遇到了类似的错误(未知层:名称)。我试图更改 keras 版本、tensorflow 版本、conda 版本等,但没有任何帮助。我通过在 Colab 上保存模型的权重、在本地创建相同的模型并将权重加载到该模型来解决了这个问题。
【讨论】:
【参考方案3】:通过运行pip install keras tensorflow
或conda install keras tensorflow
,确保您拥有最新版本的Keras
和tensorflow
(即2.4.4
和1.11.0
)。
如果是 Google Colab 使用已弃用的对象,您可能需要使用自定义对象:
from keras.utils import CustomObjectScope
from keras.initializers import glorot_uniform
with CustomObjectScope('GlorotUniform': glorot_uniform()):
model = load_model('my_model.h5')
不确定这是否是您的情况。
【讨论】:
以上是关于我在 google colab 上训练了一个 keras 模型。现在无法在我的系统上本地加载它。的主要内容,如果未能解决你的问题,请参考以下文章
如何在不使用本地PC转换的情况下将我在google colab中训练的tensorflow的protobuf训练模型转换为tensorflowjs?