serving icon indicating copy to clipboard operation
serving copied to clipboard

Export nmt trained model to tensorflow serving

Open luozhouyang opened this issue 8 years ago • 49 comments

I have trained nmt models, but I can not understand how to export the models to tensorflow serving. I read the documentations of MNIST and Inception, but I think these models are different with nmt models. Can you add a demo to show how to export the nmt models? This would be a great help to beginners like me, thanks!

luozhouyang avatar Jan 03 '18 07:01 luozhouyang

export saved model--this is my answer in SO. Hope that helps. You also need to define your signature

aforwardz avatar Jan 05 '18 06:01 aforwardz

@aforwardz thanks for your reply. Here is my code:

export_path = os.path.join(
            tf.compat.as_bytes(self.export_base_path),
            tf.compat.as_bytes(str(self.version_number))
        )

        # with tf.device('/gpu:0'):
        sess = tf.Session()
        saver = tf.train.import_meta_graph(os.path.join(self.model_dir, "translate.ckpt-21000.meta"))
        latest_ckpt = tf.train.latest_checkpoint(self.model_dir)
        saver.restore(sess, latest_ckpt)

        builder = tf.saved_model.builder.SavedModelBuilder(export_path)

        # I am not sure this way to create PREDICT_INPUTS and PREDICT_OUTPUTS is right or not.
        feature_configs = {
            'x': tf.VarLenFeature(shape=[], dtype=tf.string),
            'y': tf.VarLenFeature(shape=[], dtype=tf.string)
        }
        serialized_example = tf.placeholder(tf.string, name="tf_example")
        tf_example = tf.parse_example(serialized_example, feature_configs)
        x = tf.identity(tf_example['x'], name='x')
        y = tf.identity(tf_example['y'], name='y')
        predict_input = tf.saved_model.utils.build_tensor_info(x)
        predict_output = tf.saved_model.utils.build_tensor_info(y)
        predict_signature_def_map = tf.saved_model.signature_def_utils.predict_signature_def(
            inputs={
                tf.saved_model.signature_constants.PREDICT_INPUTS: predict_input
            },
            outputs={
                tf.saved_model.signature_constants.PREDICT_OUTPUTS: predict_output
            }
        )

        legacy_init_op = tf.group(tf.tables_initializer(), name="legacy_init_op")
        builder.add_meta_graph_and_variables(
            sess=sess,
            tags=[tf.saved_model.tag_constants.SERVING],
            signature_def_map={
                "predict_signature_map": predict_signature_def_map
            },
            legacy_init_op=legacy_init_op,
            assets_collection=None
        )
        builder.save()

But errors occur:

2018-01-05 17:20:31.485773: I C:\tf_jenkins\home\workspace\tf-nightly-windows\M\windows-gpu\PY\35\tensorflow\core\common_runtime\gpu\gpu_device.cc:1154] Creating TensorFlow device (/device:GPU:0) -> (device: 0, name: GeForce GTX 950, pci bus id: 0000:01:00.0, compute capability: 5.2)
Traceback (most recent call last):
  File "D:\ProgramFiles\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1323, in _do_call
    return fn(*args)
  File "D:\ProgramFiles\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1293, in _run_fn
    self._extend_graph()
  File "D:\ProgramFiles\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1354, in _extend_graph
    self._session, graph_def.SerializeToString(), status)
  File "D:\ProgramFiles\Python\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 473, in __exit__
    c_api.TF_GetCode(self.status.status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation 'gradients/dynamic_seq2seq/encoder/embedding_lookup_grad/ToInt32': Could not satisfy explicit device specification '' because the node was colocated with a group of nodes that required incompatible device '/device:GPU:0'
Colocation Debug Info:
Colocation group had the following types and devices: 
Assign: GPU CPU 
Const: GPU CPU 
StridedSlice: GPU CPU 
TensorArrayScatterV3: GPU CPU 
Cast: GPU CPU 
Identity: GPU CPU 
StackV2: GPU CPU 
Sub: GPU CPU 
Enter: GPU CPU 
VariableV2: GPU CPU 
RandomUniform: GPU CPU 
ScatterSub: GPU CPU 
Neg: GPU CPU 
Mul: GPU CPU 
Add: GPU CPU 
L2Loss: CPU 
Size: GPU CPU 
TensorArrayV3: GPU CPU 
ExpandDims: GPU CPU 
Reshape: GPU CPU 
ConcatV2: GPU CPU 
TensorArrayReadV3: GPU CPU 
Gather: GPU CPU 
StackPopV2: GPU CPU 
RealDiv: GPU CPU 
BroadcastGradientArgs: GPU CPU 
FloorMod: GPU CPU 
ShapeN: GPU CPU 
ConcatOffset: GPU CPU 
StackPushV2: GPU CPU 
TensorArrayGradV3: GPU CPU 
TensorArrayGatherV3: GPU CPU 
Shape: GPU CPU 
Floor: GPU CPU 
MatMul: GPU CPU 
Slice: GPU CPU 
Sum: GPU CPU 
TensorArrayWriteV3: GPU CPU 
	 [[Node: gradients/dynamic_seq2seq/encoder/embedding_lookup_grad/ToInt32 = Cast[DstT=DT_INT32, SrcT=DT_INT64, _class=["loc:@embeddings/encoder/embedding_encoder"]](gradients/dynamic_seq2seq/encoder/embedding_lookup_grad/Shape)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:/PyCharmProjects/GNMT/nmt-demo/export/exporter.py", line 106, in <module>
    exporter.export()
  File "E:/PyCharmProjects/GNMT/nmt-demo/export/exporter.py", line 26, in export
    saver.restore(sess, latest_ckpt)
  File "D:\ProgramFiles\Python\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 1683, in restore
    {self.saver_def.filename_tensor_name: save_path})
  File "D:\ProgramFiles\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 889, in run
    run_metadata_ptr)
  File "D:\ProgramFiles\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1120, in _run
    feed_dict_tensor, options, run_metadata)
  File "D:\ProgramFiles\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1317, in _do_run
    options, run_metadata)
  File "D:\ProgramFiles\Python\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1336, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation 'gradients/dynamic_seq2seq/encoder/embedding_lookup_grad/ToInt32': Could not satisfy explicit device specification '' because the node was colocated with a group of nodes that required incompatible device '/device:GPU:0'
Colocation Debug Info:
Colocation group had the following types and devices: 
Assign: GPU CPU 
Const: GPU CPU 
StridedSlice: GPU CPU 
TensorArrayScatterV3: GPU CPU 
Cast: GPU CPU 
Identity: GPU CPU 
StackV2: GPU CPU 
Sub: GPU CPU 
Enter: GPU CPU 
VariableV2: GPU CPU 
RandomUniform: GPU CPU 
ScatterSub: GPU CPU 
Neg: GPU CPU 
Mul: GPU CPU 
Add: GPU CPU 
L2Loss: CPU 
Size: GPU CPU 
TensorArrayV3: GPU CPU 
ExpandDims: GPU CPU 
Reshape: GPU CPU 
ConcatV2: GPU CPU 
TensorArrayReadV3: GPU CPU 
Gather: GPU CPU 
StackPopV2: GPU CPU 
RealDiv: GPU CPU 
BroadcastGradientArgs: GPU CPU 
FloorMod: GPU CPU 
ShapeN: GPU CPU 
ConcatOffset: GPU CPU 
StackPushV2: GPU CPU 
TensorArrayGradV3: GPU CPU 
TensorArrayGatherV3: GPU CPU 
Shape: GPU CPU 
Floor: GPU CPU 
MatMul: GPU CPU 
Slice: GPU CPU 
Sum: GPU CPU 
TensorArrayWriteV3: GPU CPU 
	 [[Node: gradients/dynamic_seq2seq/encoder/embedding_lookup_grad/ToInt32 = Cast[DstT=DT_INT32, SrcT=DT_INT64, _class=["loc:@embeddings/encoder/embedding_encoder"]](gradients/dynamic_seq2seq/encoder/embedding_lookup_grad/Shape)]]

Caused by op 'gradients/dynamic_seq2seq/encoder/embedding_lookup_grad/ToInt32', defined at:
  File "E:/PyCharmProjects/GNMT/nmt-demo/export/exporter.py", line 106, in <module>
    exporter.export()
  File "E:/PyCharmProjects/GNMT/nmt-demo/export/exporter.py", line 24, in export
    saver = tf.train.import_meta_graph(os.path.join(self.model_dir, "translate.ckpt-21000.meta"))
  File "D:\ProgramFiles\Python\Python35\lib\site-packages\tensorflow\python\training\saver.py", line 1835, in import_meta_graph
    **kwargs)
  File "D:\ProgramFiles\Python\Python35\lib\site-packages\tensorflow\python\framework\meta_graph.py", line 660, in import_scoped_meta_graph
    producer_op_list=producer_op_list)
  File "D:\ProgramFiles\Python\Python35\lib\site-packages\tensorflow\python\util\deprecation.py", line 316, in new_func
    return func(*args, **kwargs)
  File "D:\ProgramFiles\Python\Python35\lib\site-packages\tensorflow\python\framework\importer.py", line 349, in import_graph_def
    op_def=op_def)
  File "D:\ProgramFiles\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 3076, in create_op
    op_def=op_def)
  File "D:\ProgramFiles\Python\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 1561, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): Cannot assign a device for operation 'gradients/dynamic_seq2seq/encoder/embedding_lookup_grad/ToInt32': Could not satisfy explicit device specification '' because the node was colocated with a group of nodes that required incompatible device '/device:GPU:0'
Colocation Debug Info:
Colocation group had the following types and devices: 
Assign: GPU CPU 
Const: GPU CPU 
StridedSlice: GPU CPU 
TensorArrayScatterV3: GPU CPU 
Cast: GPU CPU 
Identity: GPU CPU 
StackV2: GPU CPU 
Sub: GPU CPU 
Enter: GPU CPU 
VariableV2: GPU CPU 
RandomUniform: GPU CPU 
ScatterSub: GPU CPU 
Neg: GPU CPU 
Mul: GPU CPU 
Add: GPU CPU 
L2Loss: CPU 
Size: GPU CPU 
TensorArrayV3: GPU CPU 
ExpandDims: GPU CPU 
Reshape: GPU CPU 
ConcatV2: GPU CPU 
TensorArrayReadV3: GPU CPU 
Gather: GPU CPU 
StackPopV2: GPU CPU 
RealDiv: GPU CPU 
BroadcastGradientArgs: GPU CPU 
FloorMod: GPU CPU 
ShapeN: GPU CPU 
ConcatOffset: GPU CPU 
StackPushV2: GPU CPU 
TensorArrayGradV3: GPU CPU 
TensorArrayGatherV3: GPU CPU 
Shape: GPU CPU 
Floor: GPU CPU 
MatMul: GPU CPU 
Slice: GPU CPU 
Sum: GPU CPU 
TensorArrayWriteV3: GPU CPU 
	 [[Node: gradients/dynamic_seq2seq/encoder/embedding_lookup_grad/ToInt32 = Cast[DstT=DT_INT32, SrcT=DT_INT64, _class=["loc:@embeddings/encoder/embedding_encoder"]](gradients/dynamic_seq2seq/encoder/embedding_lookup_grad/Shape)]]


Process finished with exit code 1

My trained model is a NMT model that used to correct the address. For example: "土海市 浦东新区 张东路 1387 号" --- model --- “上海市 浦东新区 张东路 1387 号”

I think the way I create the signature_def_map is wrong but I have no idea how to correct it. Do you have any ideas?

luozhouyang avatar Jan 05 '18 09:01 luozhouyang

I exported the model finally!
Here is my code:

        if not self.model_dir:
            raise ValueError("Please specify a model dir.")
        export_path = os.path.join(
            tf.compat.as_bytes(self.export_base_path),
            tf.compat.as_bytes(str(self.version_number))
        )
        config = tf.ConfigProto(allow_soft_placement=True)
        sess = tf.Session(config=config)
        saver = tf.train.import_meta_graph(os.path.join(self.model_dir, "translate.ckpt-21000.meta"))
        latest_ckpt = tf.train.latest_checkpoint(self.model_dir)
        saver.restore(sess, latest_ckpt)
        builder = tf.saved_model.builder.SavedModelBuilder(export_path)
        feature_configs = {
            'x': tf.FixedLenFeature(shape=[], dtype=tf.string),
            'y': tf.FixedLenFeature(shape=[], dtype=tf.string)
        }
        serialized_example = tf.placeholder(tf.string, name="tf_example")
        tf_example = tf.parse_example(serialized_example, feature_configs)
        x = tf.identity(tf_example['x'], name='x')
        y = tf.identity(tf_example['y'], name='y')
        predict_input = x
        predict_output = y
        predict_signature_def_map = tf.saved_model.signature_def_utils.predict_signature_def(
            inputs={
                tf.saved_model.signature_constants.PREDICT_INPUTS: predict_input
            },
            outputs={
                tf.saved_model.signature_constants.PREDICT_OUTPUTS: predict_output
            }
        )

        legacy_init_op = tf.group(tf.tables_initializer(), name="legacy_init_op")
        builder.add_meta_graph_and_variables(
            sess=sess,
            tags=[tf.saved_model.tag_constants.SERVING],
            signature_def_map={
                "predict_signature_map": predict_signature_def_map
            },
            legacy_init_op=legacy_init_op,
            assets_collection=None
        )
        builder.save()

And here is my export directory(without assets_collection):

|----1
    |----saved_model.pb
    |----variables
        |----variables.data-00000-of-00002
        |----variables.data-00001-of-00002
        |----varaibles.index

luozhouyang avatar Jan 08 '18 06:01 luozhouyang

Hi @luozhouyang, I tried your code for exporting and it works fine. Thanks for posting it. Do you also have some code for the client side? (Sorry not sure if I should put this in a new issue, however I think it would be very helpful for people reading this issue to see the example code for both the server and client side for NMT in TensorFlow Serving.)

I am trying to make a call to the gRPC server (adapting the TF Serving example code to TensorFlow NMT). This is my code:

x = ["this is the text to translate"]

request = predict_pb2.PredictRequest()
request.model_spec.name = 'myModelName'

tp = tf.contrib.util.make_tensor_proto(x)

request.inputs['inputs'].CopyFrom(tp)
# 30 secs timeout because it takes a long time to initialise
result = stub.Predict(request, 30.0)

I also tried

x = [["this is the text to translate"]]

and adding

request.inputs['tf_example'].CopyFrom(tp)

I get errors telling me the input is in the wrong format

AbortionError: AbortionError(code=StatusCode.INVALID_ARGUMENT, details="input size does not match signature")

or I get this error:

AbortionError: AbortionError(code=StatusCode.INVALID_ARGUMENT, details="You must feed a value for placeholder tensor 'tf_example' with dtype string
	 [[Node: tf_example = Placeholder[_output_shapes=[<unknown>], dtype=DT_STRING, shape=<unknown>, _device="/job:localhost/replica:0/task:0/device:CPU:0"]()]]")

Do you have any idea? thanks!

woodthom2 avatar Jan 15 '18 15:01 woodthom2

@woodthom2 I am facing the same problem as you. I exported the model but fails when call serving from the client. I think that the predict_input and predict_output tensor is not correct. From the mnist_saved_model.py we can know that the predict_input tensor should be the input of the neural network and the predict_output tensor should be the output of the network, so my code is obviously wrong. That's the problem. I haven't solved this problem. If you work out, please let me know.

luozhouyang avatar Jan 17 '18 08:01 luozhouyang

see https://github.com/tensorflow/tensor2tensor#walkthrough and https://github.com/tensorflow/tensor2tensor/tree/master/tensor2tensor/serving

samithaj avatar Jan 22 '18 11:01 samithaj

@samithaj Thanks for your reply. I read the source code and I think it makes things more complex. It involves new concepts like registry and problem. I believe things can be done much easier than that way. Do you have another ideas? Thanks anyway.

luozhouyang avatar Jan 23 '18 07:01 luozhouyang

@luozhouyang Can you provide the tensorflow version?

TypeError: Expected binary or unicode string, got <tensorflow.python.framework.sparse_tensor.SparseTensor object at 0x7f282d830b00>

and it looks like that is on the VarLenFeature for x variable:

TypeError: Failed to convert object of type <class 'tensorflow.python.framework.sparse_tensor.SparseTensor'> to Tensor. Contents: SparseTensor(indices=Tensor("ParseExample_4/ParseExample:0", shape=(?, 2), dtype=int64), values=Tensor("ParseExample_4/ParseExample:2", shape=(?,), dtype=string), dense_shape=Tensor("ParseExample_4/ParseExample:4", shape=(2,), dtype=int64)). Consider casting elements to a supported type.

Also, when you export, did you use GPU or CPU on the model file?

bugra avatar Feb 21 '18 14:02 bugra

In case anybody is having the same problem, I didn't get NMT to work together with Serving directly. The Serving examples all use simpler models such as Inception which have a clearly defined input and output placeholder, whereas NMT uses the newer Datasets API and it's not clear what the equivalents would be.

However I did use a workaround to get NMT to work on a server as a REST API. I took the example code for NMT, which is reading a text file and rewriting to another text file, and I refactored this to receive input from a REST API and return the response via REST. So there is no use of gRPC but you could adapt the same approach for gRPC.

@luozhouyang Until the problems in TensorFlow Serving this thread are fixed, I would suggest trying my approach and you will get NMT working on a server.

woodthom2 avatar Feb 21 '18 15:02 woodthom2

@woodthom2 your solution sounds good. Currently I start multi docker containers that provide inference service and use haproxy as load balancing. It works fine, but not efficient. I am interested in your solution. Can you share your code with me?

luozhouyang avatar Feb 26 '18 13:02 luozhouyang

@bugra The tensorflow version in my code is 1.4.1 with gpu. You can export the model using CPU if you set another argument to True in builder.add_meta_graph_adn_variables() method. I can not remember the arg name exactly, you can check the docs.

luozhouyang avatar Feb 26 '18 13:02 luozhouyang

@luozhouyang I wrote my exact problem here: https://github.com/tensorflow/serving/issues/777 Do you mind commenting there? I still have a hard time to understand how to interpret feed_dict to tensorflow_serving.

bugra avatar Feb 26 '18 19:02 bugra

@aforwardz @woodthom2 @samithaj @bugra @ewilderj GOOD NEWS! I exported the model and it works well with tf serving these days! I have made a pull request to tensorflow/nmt, you can have a look: pull request#344. Or, you can visit my fork of tensorflow/nmt at: tensorflow/nmt

luozhouyang avatar May 30 '18 14:05 luozhouyang

@luozhouyang Can you share your client?

mdasadul avatar May 30 '18 17:05 mdasadul

@mdasadul Here is my client:

class GNMTClient(Client):

    def __init__(self, model_name="address", host="localhost", port=9000, timeout=10):
        self.model_name = model_name
        self.host = host
        self.port = port
        self.timeout = timeout
        channel = implementations.insecure_channel(self.host, self.port)
        self.stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)

    def request(self, input_seq):
        future = self._translate(input_seq)
        result = self._parse_result(future)
        return (input_seq, result)

    def request_many(self, input_seqs):
        futures = []
        for s in input_seqs:
            future = self._translate(s)
            futures.append(future)
        pairs = []
        for seq, future in zip(input_seqs, futures):
            result = self._parse_result(future)
            pairs.append((seq, result))
        return pairs

    def _parse_result(self, future):
        result = self._parse_translation(future.result())
        words = ""
        for w in list(result):
            words += str(w, encoding="utf8") + " "
        return words

    def _translate(self, seq):
        request = predict_pb2.PredictRequest()
        # model_name should keep the same as tf serving start arg `--model_name`
        request.model_spec.name = self.model_name
        # signature_name should keep the same as your `signature_def_map` 's `key` in `exporter`
        request.model_spec.signature_name = "serving_default"
        # `seq_input` should be the same as the `inference_signature` in the `exporter`
        request.inputs["seq_input"].CopyFrom(tf.make_tensor_proto(seq, dtype=tf.string, shape=[1, ]))
        return self.stub.Predict.future(request, self.timeout)

    @staticmethod
    def _parse_translation(result):
        # `seq_output` should be the same as the `inference_signature` in the `exporter`
        inference_output = tf.make_ndarray(result.outputs["seq_output"])
        return inference_output


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    parser.add_argument("--model_name", required=True, help="model name")
    parser.add_argument("--host", default="localhost", help="model server host")
    parser.add_argument("--port", type=int, default=9000, help="model server port")
    parser.add_argument("--timeout", type=float, default=10.0, help="request timeout")
    args = parser.parse_args()

    test_seqs = [
        "上海 浦东新区 张东路",
        "浙江 杭州 下沙区",
        "北京市 海淀区 北京西路"
    ]
    client = GNMTClient(model_name=args.model_name, host=args.host, port=args.port, timeout=args.timeout)

    input_seq, output_seq = client.request(test_seqs[0])
    print("Input : %s" % input_seq)
    print("Output: %s" % output_seq)
   
    results = client.request_many(test_seqs)
    for r in results:
        print("Input : %s" % r[0])
        print("Output: %s" % r[1])

For run the client, you need to install the dependencies:

  • grpcio
  • google-auth
  • tensorflow-serving-api-python3(for python3) or tensorflow-serving-api (for python2)

luozhouyang avatar May 31 '18 01:05 luozhouyang

@luozhouyang here is my client, and it runs, but i get the same result every time no matter what the inputs are. can you help me? also i tried your code but i got an error in the first line: class GNMTClient(Client) because Client is not found

# -*- coding : utf-8 -*- 

from __future__ import print_function

import argparse
from nltk import word_tokenize
import time
import json
import os
import tensorflow as tf

from grpc.beta import implementations
from tensorflow_serving.apis import predict_pb2
from tensorflow_serving.apis import prediction_service_pb2

PYTHONIOENCODING="UTF-8"

def parse_translation_result(args,result):

  hypotheses = tf.make_ndarray(result.outputs["seq_output"])
  str1 = ' '.join(str(e) for e in hypotheses if e != "</s>")  
  return str1

def translate(stub, model_name, tokens, timeout=5.0):
  
  request = predict_pb2.PredictRequest()
  request.model_spec.name = model_name
  request.model_spec.signature_name = tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY
  request.inputs["seq_input"].CopyFrom(tf.contrib.util.make_tensor_proto(tokens))
  xy = stub.Predict.future(request, timeout)
  return xy

def main():
  json_data = {}
  start = time.time()
  json_data['start'] = str(time.ctime(int(start)))
  
  parser = argparse.ArgumentParser(description="Translation client")
  parser.add_argument("--model_name", required=True,
                      help="model name (name of the file?)")
  parser.add_argument("--host", default="localhost",
                      help="model server host")
  parser.add_argument("--port", type=int, default=9000,
                      help="model server port")
  parser.add_argument("--timeout", type=float, default=10.0,
                      help="request timeout")
  parser.add_argument("--text", default="",
                      help="Untokenized input text")

  args = parser.parse_args()
  channel = implementations.insecure_channel(args.host, args.port)
  stub = prediction_service_pb2.beta_create_PredictionService_stub(channel)
  
  tokens_list = []
  if args.text != "":
      json_data['input'] = args.text
      text = args.text
      tokens = text.split()
      tokens_list.append(tokens)
  
  token_count = 0
  for tokens in tokens_list:
    trans = translate(stub, args.model_name, tokens, timeout=args.timeout)
    result = trans.result()
    best_result = parse_translation_result(args,result)
    json_data['result_'+str(token_count)] = best_result
  
  end = time.time()
  json_data['duration'] = str(round(end-start,3))+" sec"
  json_data['end'] = str(time.ctime(int(end)))
  json_result = json.dumps(json_data, sort_keys=True)
  print (json_result)

if __name__ == "__main__":
  main()

ptamas88 avatar Jun 07 '18 08:06 ptamas88

@ptamas88 I think that same inference output in different inputs is not related to the exporting but to your pre-trained model. The Client is just an abstract class:

class Client:
    def request(self, input_seq):
        raise NotImplementedError()

    def request_many(self, input_seqs)
        raise NotImplementedError()

luozhouyang avatar Jun 07 '18 10:06 luozhouyang

@luozhouyang i have tried the model with the nmt inference command and it works well. Strange thing with serving is that I always get the first lines translation in the output. It seems that the input is not loaded into the predicition protobuf, and this way the translation is always the first line.

With the Client abstract i successfully ran your client also, but i get the same result: this is an english-hungarian model, and the output sentence is always the first line in the hungarian corpus. The strange appearance is because i use pretokenized corpus but it doesnt affect the results. (I expect not perfect but different results.)

Input : Hello world
Output: ▁ # ▁Még ▁soha ▁nem ▁álmodtam . </s> . </s> . </s> . </s> , ▁kérlek .
Input : Hello world
Output: ▁ # ▁Még ▁soha ▁nem ▁álmodtam . </s> . </s> . </s> . </s> , ▁kérlek .
Input : What's up?
Output: ▁ # ▁Még ▁soha ▁nem ▁álmodtam . </s> . </s> . </s> . </s> , ▁kérlek .
Input : My name is Bond
Output: ▁ # ▁Még ▁soha ▁nem ▁álmodtam . </s> . </s> . </s> . </s> , ▁kérlek .

When you run your client does it give back different (and acceptable) result?

FYI: I use Tensorflow 1.6 for training and tensorflow-serving-api 1.5 for serving on a different machine.

ptamas88 avatar Jun 07 '18 12:06 ptamas88

@ptamas88 I do get same results sometimes, but also get different results in my test. But the model used in my test is just a very simple model that trained only a few steps. I'll do more test and check the code again. Thanks for pointing out the problem!

luozhouyang avatar Jun 07 '18 12:06 luozhouyang

@luozhouyang I think something is missing around the input placeholder during the exporting. But i dont have deep knowledge in this area :( Please let me know if you reach some development :) Thank you very much

ptamas88 avatar Jun 07 '18 12:06 ptamas88