【问题标题】:DNNClassifier model to TensorFlow Serving modelDNNClassifier 模型到 TensorFlow Serving 模型
【发布时间】:2018-08-03 01:14:32
【问题描述】:

我是 ML 和 TF 的新手,我正在尝试使用 TensorFlow Serving 在 GCP 上托管原始 TensorFlow 模型。为此,我需要将 DNNClassifier 模型转换为 TensorFlow Serving 模型。根据Get Started指南我需要使用 SavedModelBuilder 方法,但我不知道如何在 Iris Flower example 的情况下定义输入/输出。

有人可以发布此案例的示例代码吗?

完整代码:

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

【问题讨论】:

    标签: python tensorflow google-cloud-platform tensorflow-serving


    【解决方案1】:

    在训练和评估您的模型之后,您就可以保存模型。

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

    根据您的应用程序,您还可以将signature_def_map 添加到 builder.add_meta_graph_and_variables() 函数中。

    请注意,对于分类器,输入是 feature_columns,输出是三个类之一。对于 Builder,输入为 'tf session,tag_constants.SERVINGandsignature_def_map`,输出为 'Desired_Directory/saved_model.pb'

    【讨论】:

    • 感谢您的回答,但在这种情况下,我无法弄清楚如何为该模型定义输入/输出。我的意思是我不明白如何在导出之前调用这个模型
    • classifierBuilder 的输入/输出?对于分类器,输入是 feature_columns,输出是三个类之一。对于 Builder,输入是 'tf session` 、 tag_constants.SERVINGsignature_def_map ,输出是 'Desired_Directory/saved_model.pb'
    【解决方案2】:

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

    【讨论】:

    • 感谢您的回复,您有任何示例链接吗?
    猜你喜欢
    • 2018-02-01
    • 2018-05-16
    • 2018-02-26
    • 1970-01-01
    • 2017-09-24
    • 2017-09-10
    • 1970-01-01
    • 2019-03-08
    • 2019-04-21
    相关资源
    最近更新 更多