Python 未知的初始化程序:加载 Keras 模型时的 GlorotUniform

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/53183865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:15:35  来源:igfitidea点击:

Unknown initializer: GlorotUniform when loading Keras model

pythontensorflowmachine-learningkerasgoogle-colaboratory

提问by Dhruvin modi

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.

我通过 google colab 训练了我的 CNN (VGG) 并生成了 .h5 文件。现在的问题是,我可以通过 google colab 成功预测我的输出,但是当我下载 .h5 训练模型文件并尝试在我的笔记本电脑上预测输出时,我在加载模型时出错。

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

回答by Richar Fernandez Vilchez

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')

回答by lintex

I ran into the same issue. After changing:

我遇到了同样的问题。更改后:

from tensorflow import keras

from tensorflow import keras

to:

到:

import keras

import keras

life is once again worth living.

生命再次值得一过。

回答by Alex Begun

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.

哇,我花了我生命中的 6 个小时试图解决这个问题.. Dmitri 在这里发布了一个解决方案:我在 google colab 上训练了一个 keras 模型。现在无法在我的系统上本地加载它。

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

这看起来像是 keras 中的某种序列化错误。如果你用下面的 CustomObjectScope 东西包装你的 load_model ......一切都应该工作..

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')

回答by 0x01h

Changing

改变

from keras.models import load_model

to

from tensorflow.keras.models import load_model

solved my problem!

解决了我的问题!

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

要消除错误,请直接从 Keras 或 TensorFlow导入所有内容。在同一个项目中混合使用它们可能会导致问题。

回答by M. Viaz

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

一些对我帮助但不在任何答案中的东西:

custom_objects={'GlorotUniform': glorot_uniform()}

custom_objects={'GlorotUniform': glorot_uniform()}

回答by Babakslt

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.

如果文件存在与否,第二个参数告诉 Keras 覆盖模型,第三个参数告诉它不要将优化器与模型一起保存。



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:

编辑:我今天在另一个系统上再次遇到了这个问题,这次这对我没有帮助。所以我将模型 conf 保存为 json,将权重保存为 h5,并使用它们在另一台机器上重建模型。你可以这样做。像这样保存:

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)

回答by Azizi ilias

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

问题解决了

回答by Josh Anish

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

这在导入 tensorflow keras 时对我有用

回答by Saish Reddy

In either kaggle or colabs

在 kaggle 或 colab 中

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

works well

效果很好

回答by Benjamin

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

我在使用 tensorflow 1.11.0(使用 tensorflow.python.keras.models.save_model)构建并加载 tensoflow 1.11.0(使用 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.

我通过将所有内容升级到 tensorflow 1.13.1 解决了这个问题,在使用新版本再次构建模型后,我可以加载它而不会出现此错误。