gpt4 book ai didi

python - 尝试在Keras中加载顺序模型时出现“KeyError:0”

转载 作者:行者123 更新时间:2023-12-03 18:44:44 25 4
gpt4 key购买 nike

我已经使用Google Colab在Keras中训练了序列模型来处理Google Doodle数据集。我在这里做一些简单的图像分类。

以下函数定义了我的模型的体系结构:

def create_model(input_shape):
model = keras.Sequential()
model.add(layers.Conv2D(16, (3, 3), padding = 'same', input_shape = input_shape, activation = 'relu'))
model.add(layers.BatchNormalization(axis = 3))
model.add(layers.MaxPooling2D(pool_size = (2, 2)))

model.add(layers.Conv2D(32, (3, 3), padding = 'same', activation = 'relu'))
model.add(layers.BatchNormalization(axis = 3))
model.add(layers.MaxPooling2D(pool_size = (2, 2)))

model.add(layers.Conv2D(64, (3, 3), padding = 'same', activation = 'relu'))
model.add(layers.BatchNormalization(axis = 3))
model.add(layers.MaxPooling2D(pool_size = (2,2)))

model.add(layers.Flatten())
model.add(layers.Dense(128, activation = 'relu'))
model.add(layers.Dense(28, activation = 'softmax'))

return model


下面的代码创建,编译和适合它:

doodle_model = create_model((image_size, image_size, 1)) #image_size = 28
doodle_model.compile (optimizer = "Adam", loss = "categorical_crossentropy", metrics = ["accuracy"])
doodle_model.fit (x = X_train, y = Y_train, epochs = 4, batch_size = 256)


使用该模型取得了不错的效果,因此我决定将其保存下来,以便稍后在Web应用程序中进行部署。我用过了:

doodle_model.save("my_model.h5")


保存模型

但是,当我尝试使用以下命令再次加载模型时:

from keras.models import load_model
model = load_model ("my_model.h5")


确保它是否正确保存,出现以下错误...

KeyError                                  Traceback (most recent call last)
<ipython-input-61-6cde554a8add> in <module>()
1 from keras.models import load_model
2
----> 3 model = load_model ("my_model.h5")

/usr/local/lib/python3.6/dist-packages/keras/models.py in load_model(filepath, custom_objects, compile)
268 raise ValueError('No model found in config file.')
269 model_config = json.loads(model_config.decode('utf-8'))
--> 270 model = model_from_config(model_config, custom_objects=custom_objects)
271
272 # set weights

/usr/local/lib/python3.6/dist-packages/keras/models.py in model_from_config(config, custom_objects)
345 'Maybe you meant to use '
346 '`Sequential.from_config(config)`?')
--> 347 return layer_module.deserialize(config, custom_objects=custom_objects)
348
349

/usr/local/lib/python3.6/dist-packages/keras/layers/__init__.py in deserialize(config, custom_objects)
53 module_objects=globs,
54 custom_objects=custom_objects,
---> 55 printable_module_name='layer')

/usr/local/lib/python3.6/dist-packages/keras/utils/generic_utils.py in deserialize_keras_object(identifier, module_objects, custom_objects, printable_module_name)
142 return cls.from_config(config['config'],
143 custom_objects=dict(list(_GLOBAL_CUSTOM_OBJECTS.items()) +
--> 144 list(custom_objects.items())))
145 with CustomObjectScope(custom_objects):
146 return cls.from_config(config['config'])

/usr/local/lib/python3.6/dist-packages/keras/models.py in from_config(cls, config, custom_objects)
1404 @classmethod
1405 def from_config(cls, config, custom_objects=None):
-> 1406 if 'class_name' not in config[0] or config[0]['class_name'] == 'Merge':
1407 return cls.legacy_from_config(config)
1408

KeyError: 0


据我所知,KeyErrors与python词典有关,但是我不确定为什么在这里遇到KeyError。对于我为什么会收到此错误以及如何解决该错误的任何帮助,将不胜感激。

每行代码,在行之后

----> 3 model = load_model ("my_model.h5")


错误消息的一部分是Keras的内部代码,不是我写的。

最佳答案

听起来您可能正在尝试使用与用于保存模型的版本不兼容的Keras版本加载已保存的模型。

在每种情况下,您使用什么版本?您可以使用以下方法进行检查:

import keras
print(keras.__version__)


可能的解决方案是升级Keras版本。

关于python - 尝试在Keras中加载顺序模型时出现“KeyError:0”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52908944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com