Tensorflow签名输出占位符

时间:2019-01-23 22:30:23

标签: python tensorflow tensorflow-serving

我正在尝试导出Tensorflow模型,以便可以在Tensorflow服务中使用它。这是我使用的脚本:

import os
import tensorflow as tf

trained_checkpoint_prefix = '/home/ubuntu/checkpoint'
export_dir = os.path.join('m', '0')

loaded_graph = tf.Graph()
config=tf.ConfigProto(allow_soft_placement=True)
with tf.Session(graph=loaded_graph, config=config) as sess:
    # Restore from checkpoint
    loader = tf.train.import_meta_graph(trained_checkpoint_prefix + 'file.meta')
    loader.restore(sess, tf.train.latest_checkpoint(trained_checkpoint_prefix))

    # Create SavedModelBuilder class
    # defines where the model will be exported
    export_path_base = "/home/ubuntu/m"
    export_path = os.path.join(
        tf.compat.as_bytes(export_path_base),
        tf.compat.as_bytes(str(0)))
    print('Exporting trained model to', export_path)
    builder = tf.saved_model.builder.SavedModelBuilder(export_path)

    batch_shape = (20, 256, 256, 3)
    input_tensor = tf.placeholder(tf.float32, shape=batch_shape, name="X_content")
    predictions_tf = tf.placeholder(tf.float32, shape=batch_shape, name='Y_output')

    tensor_info_input = tf.saved_model.utils.build_tensor_info(input_tensor)
    tensor_info_output = tf.saved_model.utils.build_tensor_info(predictions_tf)

    prediction_signature = (
        tf.saved_model.signature_def_utils.build_signature_def(
            inputs={'image': tensor_info_input},
            outputs={'output': tensor_info_output},
            method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME))

    builder.add_meta_graph_and_variables(
        sess, [tf.saved_model.tag_constants.SERVING],
        signature_def_map={
            'style_image':
                prediction_signature,
        })

    builder.save(as_text=True)

主要问题是输出签名(predictions_tf)。在这种情况下,将其设置为 placeholder 时,我收到一条错误消息,当从gRPC调用模型时必须设置其值。我应该怎么用呢?

我尝试过

predictions_tf = tf.Variable(0, dtype=tf.float32, name="Y_output")

predictions_tf = tf.TensorInfo(dtype=tf.float32)
predictions_tf.name = "Y_output"
predictions_tf.dtype = tf.float32

1 个答案:

答案 0 :(得分:1)

我可能会误解您想做的事情,但是基本上您在这里创建了一个新的SELECT * FROM ( SELECT con.Id AS [Id] , opp.Id AS [OpportunityId] , acc.Id AS [AccountID] , opp.Name AS [OpportunityName] , opp.CreatedDate AS [CreatedDate] , opp.StageName AS [OpportunityStage] , con.FirstName AS [FirstName] , con.LastName AS [LastName] , con.MobilePhone AS [Mobile] , con.Useractive__c AS [Useractive] , con.Email AS [Email] , con.HasOptedOutOfEmail AS [EmailOptOut] , acc.Name AS [AccountName] , acc.Total_Opportunities_in_Progress__c AS [AccOppotunityInProgress] , acc.Total_Loan_Paid__c AS [AccTotalLoanPaid] , acc.Total_Closed_Lost__c AS [AccTotalClosedLost] , opp.Total_Opportunities_Loan_Funded__c AS [TotalOppsLoanFunded] , CASE WHEN opp.StageName = 'Loan Funded' THEN 'X' ELSE 'FU' END AS ToUse , ROW_NUMBER() OVER (PARTITION BY opp_con_role.ContactId ORDER BY CASE WHEN opp.StageName = 'Loan Funded' THEN 1 ELSE 2 END, opp.CreatedDate DESC) AS RowNum FROM [Opportunitycontactrole] Opp_Con_Role INNER JOIN [Opportunity] opp ON Opp_Con_Role.Opportunityid = Opp.Id INNER JOIN [Contact] con ON Opp_Con_Role.Contactid = Con.Id INNER JOIN [account] acc ON acc.Id = opp.AccountId WHERE con.Email IS NOT NULL OR con.MobilePhone IS NOT NULL ) sr WHERE RowNum = 1 ORDER BY sr.OpportunityName 用于输入和一个新的placeholder用于输出。

我认为应该做的是,一旦加载了模型,就必须在变量placeholder和{{1}中获取模型的输入和输出张量。 }例如

input tensor