您的位置:首页 > 其它

【TensorFlow系列】【六】多模型部署

2018-03-28 00:00 323 查看
TensorFlow的多模型部署,关键在于每个模型拥有一个独立的graph与session,各模型间互不干扰即可。最终直接依据各模型的结果,综合起来做决定。

import tensorflow as tf
import numpy as np
class 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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  TensorFlow