与客户端连接时的Tensorflow服务错误“输入大小与签名不匹配”

时间:2017-10-17 23:49:47

标签: tensorflow tensorflow-serving textsum

我已经使用下面显示的export_textsum.py文件导出textum模型了,当我使用下面的textsumclient.py文件连接时,我收到错误:

  

回溯(最近一次调用最后一次):文件“textsum_client.py”,行   90,在       tf.app.run()文件“/usr/local/lib/python2.7/site-packages/tensorflow/python/platform/app.py”,   第48行,在运行中       _sys.exit(main(_sys.argv [:1] + flags_passthrough))文件“textsum_client.py”,第83行,主要       FLAGS.concurrency,FLAGS.num_tests)do_singleDecode中的文件“textsum_client.py”,第72行       result = stub.Predict(request,5.0)#5秒File“/usr/local/lib/python2.7/site-packages/grpc/beta/_client_adaptations.py”,   第324行,通话       self._request_serializer,self._response_deserializer)文件“/usr/local/lib/python2.7/site-packages/grpc/beta/_client_adaptations.py”,   第210行,在_blocking_unary_unary中       raise _abortion_error(rpc_error_call)grpc.framework.interfaces.face.face.AbortionError:   AbortionError(代码= StatusCode.INVALID_ARGUMENT,details =“输入大小   不符合签名“)

我相信它可能与我在export_textsum文件中构建tf_example有关,但老实说,我还没有好好解决这个问题。有经验的人知道我在这里做错了什么吗?如果有任何想法可以帮助我缩小目前的情况,我愿意接受任何建议。感谢。

textsumclient.py

from __future__ import print_function

import sys
import threading

# This is a placeholder for a Google-internal import.

from grpc.beta import implementations
import numpy
import tensorflow as tf
from datetime import datetime 

from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2
#from tensorflow_serving.example import mnist_input_data


tf.app.flags.DEFINE_integer('concurrency', 1,
                            'maximum number of concurrent inference requests')
tf.app.flags.DEFINE_integer('num_tests', 10, 'Number of test images')
tf.app.flags.DEFINE_string('server', '172.17.0.2:9000', 'PredictionService host:port')
tf.app.flags.DEFINE_string('work_dir', '/tmp', 'Working directory. ')
FLAGS = tf.app.flags.FLAGS


def do_singleDecode(hostport, work_dir, concurrency, num_tests):
  #Connect to server
  host, port = hostport.split(':')
  channel = implementations.insecure_channel(host, int(port))
  stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

  #Prepare our request object
  request = predict_pb2.PredictRequest()
  request.model_spec.name = 'textsum_model'
  request.model_spec.signature_name = 'predict'  

  #Make some test data
  test_data_set = ['This is a test','This is a sample']

  #Lets test her out
  now = datetime.now()
  article, abstract = test_data_set

  #***** POPULATE REQUEST INPUTS *****

  request.inputs['article'].CopyFrom(
      tf.contrib.util.make_tensor_proto(test_data_set[0], shape=[len(test_data_set[0])]))
  request.inputs['abstract'].CopyFrom(
      tf.contrib.util.make_tensor_proto(test_data_set[1], shape=[len(test_data_set[1])]))


  result = stub.Predict(request, 5.0)  # 5 seconds
  waiting = datetime.now() - now
  return result, waiting.microseconds


def main(_):
  if not FLAGS.server:
      print('please specify server host:port')
      return

  result, waiting = do_singleDecode(FLAGS.server, FLAGS.work_dir,
                            FLAGS.concurrency, FLAGS.num_tests)
  print('\nTextsum result: %s%%' % result)
  print('Waiting time is: ', waiting, 'microseconds.')



if __name__ == '__main__':
    tf.app.run()

export_textsum.py

            decode_mdl_hps = hps
            # Only need to restore the 1st step and reuse it since
            # we keep and feed in state for each step's output.
            decode_mdl_hps = hps._replace(dec_timesteps=1)
            model = seq2seq_attention_model.Seq2SeqAttentionModel(
                decode_mdl_hps, vocab, num_gpus=FLAGS.num_gpus)
            decoder = seq2seq_attention_decode.BSDecoder(model, batcher, hps, vocab)
            serialized_output = tf.placeholder(tf.string, name='tf_output')


            serialized_tf_example = tf.placeholder(tf.string, name='tf_example')
            feature_configs = {
                'article': tf.FixedLenFeature(shape=[1], dtype=tf.string),
                'abstract': tf.FixedLenFeature(shape=[1], dtype=tf.string),
            }
            tf_example = tf.parse_example(serialized_tf_example, feature_configs)

            saver = tf.train.Saver()
            config = tf.ConfigProto(allow_soft_placement = True)

            with tf.Session(config = config) as sess:

                # Restore variables from training checkpoints.
                ckpt = tf.train.get_checkpoint_state(FLAGS.checkpoint_dir)
                if ckpt and ckpt.model_checkpoint_path:
                    saver.restore(sess, ckpt.model_checkpoint_path)
                    global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
                    print('Successfully loaded model from %s at step=%s.' %
                        (ckpt.model_checkpoint_path, global_step))
                else:
                    print('No checkpoint file found at %s' % FLAGS.checkpoint_dir)
                    return

                # ************** EXPORT MODEL ***************
                export_path = os.path.join(FLAGS.export_dir,str(FLAGS.export_version))
                print('Exporting trained model to %s' % export_path)


                #-------------------------------------------

                tensor_info_inputs = tf.saved_model.utils.build_tensor_info(serialized_tf_example)
                tensor_info_outputs = tf.saved_model.utils.build_tensor_info(serialized_output)


                prediction_signature = (
                    tf.saved_model.signature_def_utils.build_signature_def(
                        inputs={ tf.saved_model.signature_constants.PREDICT_INPUTS: tensor_info_inputs},
                        outputs={tf.saved_model.signature_constants.PREDICT_OUTPUTS:tensor_info_outputs},
                        method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME
                        ))

                #----------------------------------

                legacy_init_op = tf.group(tf.tables_initializer(), name='legacy_init_op')
                builder = saved_model_builder.SavedModelBuilder(export_path)

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

                print('Successfully exported model to %s' % export_path)
    except:
        traceback.print_exc()
        pass


def main(_):
    Export()

if __name__ == "__main__":
    tf.app.run()

2 个答案:

答案 0 :(得分:1)

看起来您应该在客户端和图表定义中指定[1]的形状。 的 export_textsum.py

feature_configs = {
                'article': tf.FixedLenFeature(shape=[1], dtype=tf.string),
                'abstract': tf.FixedLenFeature(shape=[1], dtype=tf.string),
            }

<强> textsumclient.py

  request.inputs['article'].CopyFrom(
      tf.contrib.util.make_tensor_proto([test_data_set[0]], shape=[1]))
  request.inputs['abstract'].CopyFrom(
      tf.contrib.util.make_tensor_proto([test_data_set[1]], shape=[1]))

或许使用shape=[len(test_data_set[0])]会更合适

答案 1 :(得分:0)

QuantumLicht我再次感谢您在这里的帮助,因为这是我的问题的一部分。它似乎与功能配置中使用的密钥有关。我仍在使用TF 1.2,我记得有时候回读说有一些修复方法可以在新版本中使用正确的密钥名称。也就是说,当我调试时,我注意到它期待一个名为&#34; input&#34;的输入。所以我删除了#34; abstract&#34;并将文章设置为输入。然后我不得不修改解码的输出,最后的问题与我只是加载模型但从不运行函数对模型以获取我需要然后发送到tensor_info_outputs的输出这一事实有关。