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

TensorFlow的多模型部署,關鍵在於每一個模型擁有一個獨立的graph與session,各模型間互不干擾便可。最終直接依據各模型的結果,綜合起來作決定。python

 

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)
相關文章
相關標籤/搜索