簡體   English   中英

DNNClassifier 模型到 TensorFlow Serving 模型

[英]DNNClassifier model to TensorFlow Serving model

我是 ML 和 TF 的新手,我正在嘗試使用 TensorFlow Serving 在 GCP 上托管原始 TensorFlow 模型。 為此,我需要將DNNClassifier模型轉換為 TensorFlow Serving 模型。 根據入門指南,我需要使用SavedModelBuilder方法,但我無法弄清楚如何在使用Iris Flower 示例的情況下定義輸入/輸出。

有人可以為此案例發布示例代碼嗎?

完整代碼:

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

在訓練和評估模型之后,您就可以保存模型。

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