The export_savedmodel function requires the serve_input_receiver_fn argument, that is, a function with no arguments that defines the input to the model and predictor. Therefore, you should create your own serve_input_receiver_fn , where the input type of the model matches the input of the model in the training script, and the input type of the predictor matches the input of the predictor in the testing script. On the other hand, if you are creating a custom model, you must define export_outputs, defined by the tf.estimator.export.PredictOutput function, which is a dictionary that defines a name that must match the predictor output name in a testing script.
For instance:
SCRIPT TRAINING
def serving_input_receiver_fn(): serialized_tf_example = tf.placeholder(dtype=tf.string, shape=[None], name='input_tensors') receiver_tensors = {"predictor_inputs": serialized_tf_example} feature_spec = {"words": tf.FixedLenFeature([25],tf.int64)} features = tf.parse_example(serialized_tf_example, feature_spec) return tf.estimator.export.ServingInputReceiver(features, receiver_tensors) def estimator_spec_for_softmax_classification(logits, labels, mode): predicted_classes = tf.argmax(logits, 1) if (mode == tf.estimator.ModeKeys.PREDICT): export_outputs = {'predict_output': tf.estimator.export.PredictOutput({"pred_output_classes": predicted_classes, 'probabilities': tf.nn.softmax(logits)})} return tf.estimator.EstimatorSpec(mode=mode, predictions={'class': predicted_classes, 'prob': tf.nn.softmax(logits)}, export_outputs=export_outputs)
SCRIPT TEST
def main(): # ... # preprocess-> features_test_set # ... with tf.Session() as sess: tf.saved_model.loader.load(sess, [tf.saved_model.tag_constants.SERVING], full_model_dir) predictor = tf.contrib.predictor.from_saved_model(full_model_dir) model_input = tf.train.Example(features=tf.train.Features( feature={"words": tf.train.Feature(int64_list=tf.train.Int64List(value=features_test_set)) })) model_input = model_input.SerializeToString() output_dict = predictor({"predictor_inputs":[model_input]}) y_predicted = output_dict["pred_output_classes"][0]
(Code tested in Python 3.6.3, Tensorflow 1.4.0)
source share