How to use a saved tensorflow model by `saved_model.simple_save`?

Viewed 1329

The model was saved using this function:

import tensorflow as tf
from tensorflow.python.estimator.export import export as export_helpers
import keras.backend as K

def save_for_serving(self):
        K.clear_session()  # to reset input tensor names
        model = self.build_model(serving=True)
        model.load_weights(self._train_config['model_checkpoint_path'])

        K.set_learning_phase(0)
        with K.get_session() as sess:
            tf.saved_model.simple_save(
                sess,
                export_helpers.get_timestamped_export_dir(self._output_model_dir),
                inputs=dict(zip([t.name.split(':')[0] for t in model.model.input], model.model.input)),
                outputs=dict(zip([t.name.split('/')[0] for t in model.model.outputs], model.model.outputs))
            )

And, I have the following directory structure:

│   ├── 1571292052
│   │   ├── saved_model.pb
│   │   └── variables
│   │       ├── variables.data-00000-of-00001
│   │       └── variables.index

How can I use this to predict the output for new cases? I have a pandas dataframe with all the input variables and the pre-processing function but I am not aware of how to inference the model?

I started like below but don't know how to pass the pre-processed pandas rows to the loaded model to get output:

import tensorflow as tf
import keras.backend as K

with K.get_session() as sess:
    i = tf.saved_model.load(sess,tags={'serve'}, export_dir='./exp_model/1571292052/')

>>> print(type(i))

<class 'tensorflow.core.protobuf.meta_graph_pb2.MetaGraphDef'>

>>> print(dir(i))

['ByteSize', 'Clear', 'ClearExtension', 'ClearField', 'CollectionDefEntry', 'CopyFrom', 'DESCRIPTOR', 'DiscardUnknownFields', 'Extensions', 'FindInitializationErrors', 'FromString', 'HasExtension', 'HasField', 'IsInitialized', 'ListFields', 'MergeFrom', 'MergeFromString', 'MetaInfoDef', 'ParseFromString', 'RegisterExtension', 'SerializePartialToString', 'SerializeToString', 'SetInParent', 'SignatureDefEntry', 'UnknownFields', 'WhichOneof', '_CheckCalledFromGeneratedFile', '_SetListener', '__class__', '__deepcopy__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getstate__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__unicode__', '_extensions_by_name', '_extensions_by_number', '_tf_api_names', '_tf_api_names_v1', 'asset_file_def', 'collection_def', 'graph_def', 'meta_info_def', 'object_graph_def', 'saver_def', 'signature_def']

And, the output of :

>>> saved_model_cli show --dir ./exp_model/1571292052 --all
MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['name_counts'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: name_counts:0
    .
    .
    .
    .
  The given SavedModel SignatureDef contains the following output(s):
    outputs['o1'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: o1/Sigmoid:0
    outputs['o2'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 1)
        name: o2/BiasAdd:0
  Method name is: tensorflow/serving/predict
1 Answers

Let me present a solution that works for me. In my case the the model has one input and one output:

MetaGraphDef with tag-set: 'serve' contains the following SignatureDefs:

signature_def['serving_default']:
  The given SavedModel SignatureDef contains the following input(s):
    inputs['text'] tensor_info:
        dtype: DT_STRING
        shape: (-1, 1)
        name: input_1:0
  The given SavedModel SignatureDef contains the following output(s):
    outputs['dense_2/Softmax:0'] tensor_info:
        dtype: DT_FLOAT
        shape: (-1, 2)
        name: dense_2/Softmax:0
  Method name is: tensorflow/serving/predict

Here is the code, tested with TF 1.14.0 on Ubuntu 18.04:

import tensorflow as tf
import numpy as np

def extract_tensors(signature_def, graph):
    output = dict()

    for key in signature_def:
        value = signature_def[key]

        if isinstance(value, tf.TensorInfo):
            output[key] = graph.get_tensor_by_name(value.name)

    return output

def extract_input_name(signature_def, graph):
    input_tensors = extract_tensors(signature_def['serving_default'].inputs, graph)
    #Assuming one input in model.
    key = list(input_tensors.keys())[0]
    return input_tensors.get(key).name

def extract_output_name(signature_def, graph):
    output_tensors = extract_tensors(signature_def['serving_default'].outputs, graph)
    #Assuming one output in model.
    key = list(output_tensors.keys())[0]
    return output_tensors.get(key).name

messages = [ "Some input text", "Another text" ]
new_text = np.array(messages, dtype=object)[:, np.newaxis]

model_dir = "./models/use/1"

with tf.Session(graph=tf.Graph()) as session:
    serve = tf.saved_model.load(session ,tags={'serve'}, export_dir=model_dir)

    input_tensor_name = extract_input_name(serve.signature_def, session.graph)
    output_tensor_name = extract_output_name(serve.signature_def, session.graph)

    result = session.run([output_tensor_name], feed_dict={input_tensor_name: new_text})
    print(result)

The output:

INFO:tensorflow:Restoring parameters from ./models/use/1/variables/variables
[array([[0.76517266, 0.2348273 ],
       [0.1170708 , 0.8829292 ]], dtype=float32)]
Related