你能试试下面的代码吗:
def serving_input_receiver_fn():
"""
This is used to define inputs to serve the model.
:return: ServingInputReciever
"""
reciever_tensors = {
# The size of input image is flexible.
'image': tf.placeholder(tf.float32, [None, None, None, 1]),
}
# Convert give inputs to adjust to the model.
features = {
# Resize given images.
'image': tf.reshape(reciever_tensors[INPUT_FEATURE], [-1, INPUT_SHAPE])
}
return tf.estimator.export.ServingInputReceiver(receiver_tensors=reciever_tensors,
features=features)
tf.estimator.BestExporter
如下图所示:
best_exporter = tf.estimator.BestExporter(
serving_input_receiver_fn=serving_input_receiver_fn,
exports_to_keep=1)
exporters = [best_exporter]
eval_input_fn = tf.estimator.inputs.numpy_input_fn(
x={input_name: eval_data},
y=eval_labels,
num_epochs=1,
shuffle=False)
eval_spec = tf.estimator.EvalSpec(
input_fn=eval_input_fn,
throttle_secs=10,
start_delay_secs=10,
steps=None,
exporters=exporters)
# Train and evaluate the model.
tf.estimator.train_and_evaluate(classifier, train_spec=train_spec, eval_spec=eval_spec)
有关详细信息,请参阅链接:
https://github.com/yu-iskw/tensorflow-serving-example/blob/master/python/train/mnist_keras_estimator.py