TensorFlow的多模型部署,关键在于每个模型拥有一个独立的graph与session,各模型间互不干扰即可。最终直接依据各模型的结果,综合起来做决定。
import tensorflow as tfimport numpy as npclass Model: def __init__(self,meta_path,ckpt_path,out_tensor_name,input_tensor_name): self.graph = tf.Graph() #恢复模型 with self.graph.as_default(): self.saver = tf.train.import_meta_graph(meta_path) self.session = tf.Session(graph=self.graph) with self.session.as_default(): with self.graph.as_default(): self.saver.restore(self.session,tf.train.latest_checkpoint(ckpt_path)) #获取输入输出tensor self.out = self.graph.get_tensor_by_name(name=out_tensor_name) self.input = self.graph.get_tensor_by_name(name=input_tensor_name) #做预测 def predict(self,image): result = self.session.run(self.out,feed_dict={self.input:image}) index = np.argmax(result,1) return index[0]Age_pre = Model(meta_path='',ckpt_path='',out_tensor_name='softmax:0',input_tensor_name='input:0')Gender_pre = Model(meta_path='',ckpt_path='',out_tensor_name='softmax:0',input_tensor_name='input:0')with tf.Session() as session: image = session.run(fetches='') age = Age_pre.predict(image) gender = Gender_pre.predict(image)