Taro Kiritani
Taro Kiritani

Reputation: 724

How to feed serialized data to tf saved model?

I am trying to feed serialized data to my saved model. I suspect either the model is not exported correctly, or serialization of the data is not done correctly. Any hints or suggestions would be appreciated.

import tensorflow as tf
import numpy as np

train_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={'x': np.array([[1,2,3,4,3], [1,3,4,2,4], [10,2,4,1.3,4], [1,3,5.2,9, 0.3]]).astype(np.float32)},
        y=np.array([0,0,1,1]).astype(np.float32),
        batch_size=2,
        shuffle=True
        )

eval_input_fn = tf.estimator.inputs.numpy_input_fn(
        x={'x': np.array([[1,2,3,1,4], [1,23,4,1,90]]).astype(np.float32)},
        y=np.array([0,1]).astype(np.float32),
        batch_size=2,
        num_epochs=1,
        shuffle=False
        )

def my_model(features, labels, mode, params):
    net = features['x']
    net = tf.layers.dense(net, 32, activation=tf.nn.relu)
    logits = tf.layers.dense(net, 1)
    if mode == tf.estimator.ModeKeys.PREDICT:
        return tf.estimator.EstimatorSpec(
                mode=mode,
                predictions=logits,
                export_outputs=
                    {'logits':tf.estimator.export.PredictOutput(logits)}
                )
    loss = tf.reduce_sum(tf.square(labels-logits))
    if mode == tf.estimator.ModeKeys.TRAIN:
        optimizer = tf.train.AdamOptimizer(learning_rate=0.02)
        train_op = optimizer.minimize(loss=loss,
                global_step=tf.train.get_global_step())
        return tf.estimator.EstimatorSpec(mode=mode, loss=loss, train_op=train_op)
    if mode == tf.estimator.ModeKeys.EVAL:
        return tf.estimator.EstimatorSpec(mode=mode, loss=loss)

train_spec = tf.estimator.TrainSpec(input_fn=train_input_fn, max_steps=5)
eval_spec = tf.estimator.EvalSpec(input_fn=eval_input_fn)

my_estimator = tf.estimator.Estimator(model_fn=my_model)
tf.estimator.train_and_evaluate(my_estimator, train_spec, eval_spec)

saved_model = my_estimator.export_savedmodel('foo', tf.estimator.export.build_parsing_serving_input_receiver_fn({'x': tf.FixedLenFeature([5],tf.float32)}))
predict_fn = tf.contrib.predictor.from_saved_model(saved_model,) 
features=tf.train.Features(feature={'x':tf.train.Feature(float_list=tf.train.FloatList(value=[1,2,3,4,1]))})
ex = tf.train.Example(features=features)
prediction = predict_fn({'examples':ex.SerializeToString()})

Here is the error message:

ValueError: Cannot feed value of shape () for Tensor 'input_example_tensor:0', which has shape '(?,)'

Upvotes: 1

Views: 263

Answers (1)

Taro Kiritani
Taro Kiritani

Reputation: 724

Turned out that the list of tf.train.Examples.SerializeToString() had to be provided.

In the last line, prediction = predict_fn({'examples': ex.SerializeToString()}) is changed to prediction = predict_fn({'examples': [ex.SerializeToString()]}).

I figured this out thanks to this nice tutorial: http://shzhangji.com/blog/2018/05/14/serve-tensorflow-estimator-with-savedmodel/

Upvotes: 1

Related Questions