本文介绍了keras 模型加载速度极慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组我训练和保存的 Keras 模型 (30):

I have a set of Keras models (30) that I trained and saved using:

 model.save('model{0}.h5'.format(n_model))

当我尝试加载它们时,使用 load_model,每个模型所需的时间都非常大且增量.加载完成为:

When I try to load them, using load_model, the time required for each model is quite large and incremental. The loading is done as:

models = {}
for i in range(30):
    start = time.time()
    models[i] = load_model('model{0}.h5'.format(ix))
    end = time.time()
    print "Model {0}: seconds {1}".format(ix, end - start)

输出是:

...
Model 9: seconds 7.38966012001
Model 10: seconds 9.99283003807
Model 11: seconds 9.7262301445
Model 12: seconds 9.17000102997
Model 13: seconds 10.1657290459
Model 14: seconds 12.5914049149
Model 15: seconds 11.652477026
Model 16: seconds 12.0126030445
Model 17: seconds 14.3402299881
Model 18: seconds 14.3761711121
...

每个模型都非常简单:2 个隐藏层,每个隐藏层有 10 个神经元(大小约 50Kb).为什么加载需要这么多时间,为什么时间会增加?我是否遗漏了什么(例如模型的关闭功能?)

Each model is really simple: 2 hidden layers with 10 neurons each (size ~50Kb). Why is the loading taking so much and why is the time increasing? Am I missing something (e.g. close function for the model?)

解决方案

我发现为了加快模型的加载速度,最好将网络结构和权重存储到两个不同的文件中:保存部分:

I found out that to speed up the loading of the model is better to store the structure of the networks and the weights into two distinct files:The saving part:

model.save_weights('model.h5')
model_json = model.to_json()
with open('model.json', "w") as json_file:
    json_file.write(model_json)
json_file.close()

加载部分:

from keras.models import model_from_json
json_file = open("model.json", 'r')
loaded_model_json = json_file.read()
json_file.close()
model = model_from_json(loaded_model_json)
model.load_weights("model.h5")

推荐答案

我通过在每次加载前清除 keras 会话来解决问题

I solved the problem by clearing the keras session before each load

from keras import backend as K
for i in range(...):
  K.clear_session()
  model = load_model(...)

这篇关于keras 模型加载速度极慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 02:43