Keras:加载多个模型并在不同线程中进行预测 [英] Keras: Load multiple models and predict in different threads

查看:81
本文介绍了Keras:加载多个模型并在不同线程中进行预测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Tensorflow核心的Keras.我想在构造函数中加载2个不同的模型,然后在不同的线程中(根据请求)进行预测.我试图在张量流图上下文中加载这些模型,但是没有用.我的代码:

I'm using Keras with tensorflow core. I want to load 2 different models in constructor, and then make predictions (on request) in different threads. I tried to load these models within tensorflow graph contexts, but it didn't work. My code:

from keras.models import load_model
from keras import Sequential

def __init__(self):
    self.graph_A = tf.Graph()
    with self.graph_A.as_default():
        self.model_A: Sequential = load_model('model_A_filename')

    self.graph_B = tf.Graph()
    with self.graph_B.as_default():
        self.model_B: Sequential = load_model('model_B_filename')


def predict_with_model_A(X):
    with self.graph_A.as_default():
        return self.model_A.predict(X)


def predict_with_model_B(X):
    with self.graph_B.as_default():
        return self.model_B.predict(X)

当我运行程序时,模型A已成功加载.但是,我在加载模型B时收到错误消息:

When I run the program, model A is loaded successfully. However I receive an error on loading model B:

TypeError: Cannot interpret feed_dict key as Tensor: Tensor     
Tensor("Placeholder:0", shape=(7626, 210), dtype=float32) is not an element 
of this graph.

我们将很高兴听到您如何正确执行操作.谢谢!

Will be happy to hear, how to do it right. Thanks!

推荐答案

尝试在处理后每次重置图形.对于keras,请使用K.clear_session().为每个图形使用单独的会话.

Try to reset the graph everytime after processing. For keras use K.clear_session(). Use seperate sessions for every graph.

class Model:
   @staticmethod
   def loadmodel(path):
        return loadmodel(path)

def ___init__(self, path):
   self.model = self.loadmodel(path)
   self.graph = tf.get_default_graph()

def predict(self, X):
    with self.graph.as_default():
        return self.model.predict(X)

model1 = Model('model1.h5')
model1.predict(test_data)

model2 = Model('model2.h5')
model2.predict(test_data)

这篇关于Keras:加载多个模型并在不同线程中进行预测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆