Created
June 12, 2018 08:40
-
-
Save ydp/d4edd1b34d93856737c435eb5f5620f6 to your computer and use it in GitHub Desktop.
tensorflow custom DNN estimator support serving default signature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Copyright 2016 The TensorFlow Authors. All Rights Reserved. | |
# | |
# Licensed under the Apache License, Version 2.0 (the "License"); | |
# you may not use this file except in compliance with the License. | |
# You may obtain a copy of the License at | |
# | |
# http://www.apache.org/licenses/LICENSE-2.0 | |
# | |
# Unless required by applicable law or agreed to in writing, software | |
# distributed under the License is distributed on an "AS IS" BASIS, | |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
# See the License for the specific language governing permissions and | |
# limitations under the License. | |
"""An Example of a custom Estimator for the Iris dataset.""" | |
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
import argparse | |
import tensorflow as tf | |
import shutil | |
import iris_data | |
from tensorflow.python.estimator import model_fn | |
from tensorflow.python.estimator.canned import prediction_keys | |
from tensorflow.python.estimator.export import export_output | |
from tensorflow.python.ops import string_ops | |
from tensorflow.python.ops import math_ops | |
from tensorflow.python.ops import array_ops | |
from tensorflow.python.saved_model import signature_constants | |
parser = argparse.ArgumentParser() | |
parser.add_argument('--batch_size', default=100, type=int, help='batch size') | |
parser.add_argument('--train_steps', default=1000, type=int, | |
help='number of training steps') | |
_DEFAULT_SERVING_KEY = signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY | |
# The above default is defined by TF Serving, but these next three are just | |
# a local convention without any special meaning. | |
_CLASSIFY_SERVING_KEY = 'classification' | |
_REGRESS_SERVING_KEY = 'regression' | |
_PREDICT_SERVING_KEY = 'predict' | |
def my_model(features, labels, mode, params): | |
"""DNN with three hidden layers, and dropout of 0.1 probability.""" | |
# Create three fully connected layers each layer having a dropout | |
# probability of 0.1. | |
net = tf.feature_column.input_layer(features, params['feature_columns']) | |
for units in params['hidden_units']: | |
net = tf.layers.dense(net, units=units, activation=tf.nn.relu) | |
# Compute logits (1 per class). | |
logits = tf.layers.dense(net, params['n_classes'], activation=None) | |
# Compute predictions. | |
predicted_classes = tf.argmax(logits, 1) | |
if mode == tf.estimator.ModeKeys.PREDICT: | |
pred_keys = prediction_keys.PredictionKeys | |
class_ids = math_ops.argmax(logits, 1, name=pred_keys.CLASS_IDS) | |
class_ids = array_ops.expand_dims(class_ids, axis=(1,)) | |
classes = string_ops.as_string(class_ids, name='str_classes') | |
probabilities = tf.nn.softmax(logits, name=pred_keys.PROBABILITIES) | |
predictions = { | |
pred_keys.LOGITS: logits, | |
pred_keys.PROBABILITIES: probabilities, | |
# Expand to [batch_size, 1] | |
pred_keys.CLASS_IDS: class_ids, | |
pred_keys.CLASSES: classes, | |
} | |
batch_size = array_ops.shape(probabilities)[0] | |
export_class_list = string_ops.as_string( | |
math_ops.range(params['n_classes'])) | |
export_output_classes = array_ops.tile( | |
input=array_ops.expand_dims(input=export_class_list, axis=0), | |
multiples=[batch_size, 1]) | |
classifier_output = export_output.ClassificationOutput( | |
scores=probabilities, | |
classes=export_output_classes) | |
export_outputs={ | |
_DEFAULT_SERVING_KEY: classifier_output, | |
_CLASSIFY_SERVING_KEY: classifier_output, | |
_PREDICT_SERVING_KEY: export_output.PredictOutput(predictions) | |
} | |
return model_fn.EstimatorSpec( | |
mode=model_fn.ModeKeys.PREDICT, | |
predictions=predictions, | |
export_outputs=export_outputs) | |
# Compute loss. | |
loss = tf.losses.sparse_softmax_cross_entropy(labels=labels, logits=logits) | |
# Compute evaluation metrics. | |
accuracy = tf.metrics.accuracy(labels=labels, | |
predictions=predicted_classes, | |
name='acc_op') | |
metrics = {'accuracy': accuracy} | |
tf.summary.scalar('accuracy', accuracy[1]) | |
if mode == tf.estimator.ModeKeys.EVAL: | |
return tf.estimator.EstimatorSpec( | |
mode, loss=loss, eval_metric_ops=metrics) | |
# Create training op. | |
assert mode == tf.estimator.ModeKeys.TRAIN | |
optimizer = tf.train.AdagradOptimizer(learning_rate=0.1) | |
train_op = optimizer.minimize(loss, global_step=tf.train.get_global_step()) | |
return tf.estimator.EstimatorSpec(mode, loss=loss, train_op=train_op) | |
def main(argv): | |
args = parser.parse_args(argv[1:]) | |
# Fetch the data | |
(train_x, train_y), (test_x, test_y) = iris_data.load_data() | |
# Feature columns describe how to use the input. | |
my_feature_columns = [] | |
for key in train_x.keys(): | |
my_feature_columns.append(tf.feature_column.numeric_column(key=key)) | |
# Build 2 hidden layer DNN with 10, 10 units respectively. | |
classifier = tf.estimator.Estimator( | |
model_fn=my_model, | |
params={ | |
'feature_columns': my_feature_columns, | |
# Two hidden layers of 10 nodes each. | |
'hidden_units': [10, 10], | |
# The model must choose between 3 classes. | |
'n_classes': 3, | |
}) | |
# Train the Model. | |
classifier.train( | |
input_fn=lambda:iris_data.train_input_fn(train_x, train_y, args.batch_size), | |
steps=args.train_steps) | |
# Evaluate the model. | |
eval_result = classifier.evaluate( | |
input_fn=lambda:iris_data.eval_input_fn(test_x, test_y, args.batch_size)) | |
print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result)) | |
# Generate predictions from the model | |
expected = ['Setosa', 'Versicolor', 'Virginica'] | |
predict_x = { | |
'SepalLength': [5.1, 5.9, 6.9], | |
'SepalWidth': [3.3, 3.0, 3.1], | |
'PetalLength': [1.7, 4.2, 5.4], | |
'PetalWidth': [0.5, 1.5, 2.1], | |
} | |
predictions = classifier.predict( | |
input_fn=lambda:iris_data.eval_input_fn(predict_x, | |
labels=None, | |
batch_size=args.batch_size)) | |
for pred_dict, expec in zip(predictions, expected): | |
template = ('\nPrediction is "{}" ({:.1f}%), expected "{}"') | |
class_id = pred_dict['class_ids'][0] | |
probability = pred_dict['probabilities'][class_id] | |
print(template.format(iris_data.SPECIES[class_id], | |
100 * probability, expec)) | |
print('========done========') | |
shutil.rmtree('./savedmodel', ignore_errors=True) | |
feature_spec = tf.feature_column.make_parse_example_spec(my_feature_columns) | |
export_input_fn = tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec) | |
classifier.export_savedmodel(export_dir_base='./savedmodel', | |
serving_input_receiver_fn=export_input_fn, | |
as_text=False) | |
if __name__ == '__main__': | |
tf.logging.set_verbosity(tf.logging.INFO) | |
tf.app.run(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment