Reputation: 402
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)
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!
Upvotes: 2
Views: 4050
Reputation: 698
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)
Upvotes: 2