简体   繁体   English

DNNClassifier 模型到 TensorFlow Serving 模型

[英]DNNClassifier model to TensorFlow Serving model

I am a newbie in ML and TF and I am trying to host primitive TensorFlow model on GCP using TensorFlow Serving.我是 ML 和 TF 的新手,我正在尝试使用 TensorFlow Serving 在 GCP 上托管原始 TensorFlow 模型。 For do that I need to convert DNNClassifier model to TensorFlow Serving model.为此,我需要将DNNClassifier模型转换为 TensorFlow Serving 模型。 According to Get Started guide I need to use SavedModelBuilder method but I can't figure out how to define input/outputs in the case with Iris Flower example .根据入门指南,我需要使用SavedModelBuilder方法,但我无法弄清楚如何在使用Iris Flower 示例的情况下定义输入/输出。

Could anybody post an example code for this case?有人可以为此案例发布示例代码吗?

Full code:完整代码:

(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.DNNClassifier(
    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))

Just right after training and evaluating your model, you are able to save the model.在训练和评估模型之后,您就可以保存模型。

(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.DNNClassifier(
    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))

export_path = 'Your Desired new Path '
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
sess = tf.InteractiveSession()
builder.add_meta_graph_and_variables(
  sess, [tf.saved_model.tag_constants.SERVING]
builder.save()

Depending on your application, you can also add signature_def_map to builder.add_meta_graph_and_variables() function.根据您的应用程序,您还可以将signature_def_map添加到 builder.add_meta_graph_and_variables() 函数。

Please note that for the classifier the input is feature_columns and the output is one of the three classes.请注意,对于分类器,输入是 feature_columns,输出是三个类之一。 For Builder, the input is 'tf session , tag_constants.SERVING and signature_def_map` and the output is 'Desired_Directory/saved_model.pb'对于 Builder,输入是 'tf session , tag_constants.SERVING and signature_def_map`,输出是 'Desired_Directory/saved_model.pb'

只需将 arythmic 模式更改为张量样式,可能必须交叉合并样式,然后使用格式均衡器进行调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM