iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 13
0
DevOps

30 Days of MLOps系列 第 13

輸出可以 Serving 的 Model

這篇看錯方向,看到 tf v1 的文件去了,之後會再把內容更新成 v2 的。

步驟

  • 定義 Serving interface
  • 存成 Save Model 物件

tf.compat.v1.saved_model

  • tf.compat.v1.saved_model.utils.build_tensor_info
  • tf.compat.v1.saved_model.signature_def_utils.build_signature_def
  • tf.compat.v1.saved_model.tag_constants.SERVING

tf.compat.v1.saved_model.builder.SavedModelBuilder

  • builder.add_meta_graph_and_variables
  • builder.save()

tf.compat.v1.saved_model.signature_constants

  • CLASSIFY_INPUTS
  • CLASSIFY_OUTPUT_CLASSES
  • CLASSIFY_OUTPUT_SCORES
  • CLASSIFY_METHOD_NAME
  • PREDICT_METHOD_NAME
  • DEFAULT_SERVING_SIGNATURE_DEF_KEY
  • DEFAULT_SERVING_SIGNATURE_DEF_KEY

範例

builder = tf.compat.v1.saved_model.builder.SavedModelBuilder(export_path)

# Build the signature_def_map.
classification_inputs = tf.compat.v1.saved_model.utils.build_tensor_info(
    serialized_tf_example)
classification_outputs_classes = tf.compat.v1.saved_model.utils.build_tensor_info(
    prediction_classes)
classification_outputs_scores = tf.compat.v1.saved_model.utils.build_tensor_info(
    values)

classification_signature = (
    tf.compat.v1.saved_model.signature_def_utils.build_signature_def(
        inputs={
            tf.compat.v1.saved_model.signature_constants.CLASSIFY_INPUTS:
                classification_inputs
        },
        outputs={
            tf.compat.v1.saved_model.signature_constants
            .CLASSIFY_OUTPUT_CLASSES:
                classification_outputs_classes,
            tf.compat.v1.saved_model.signature_constants
            .CLASSIFY_OUTPUT_SCORES:
                classification_outputs_scores
        },
        method_name=tf.compat.v1.saved_model.signature_constants
        .CLASSIFY_METHOD_NAME))

tensor_info_x = tf.compat.v1.saved_model.utils.build_tensor_info(x)
tensor_info_y = tf.compat.v1.saved_model.utils.build_tensor_info(y)

prediction_signature = (
    tf.compat.v1.saved_model.signature_def_utils.build_signature_def(
        inputs={'images': tensor_info_x},
        outputs={'scores': tensor_info_y},
        method_name=tf.compat.v1.saved_model.signature_constants
        .PREDICT_METHOD_NAME))

builder.add_meta_graph_and_variables(
    sess, [tf.compat.v1.saved_model.tag_constants.SERVING],
    signature_def_map={
        'predict_images':
            prediction_signature,
        tf.compat.v1.saved_model.signature_constants
        .DEFAULT_SERVING_SIGNATURE_DEF_KEY:
            classification_signature,
    },
    main_op=tf.compat.v1.tables_initializer(),
    strip_default_attrs=True)

builder.save()

上一篇
訓練 Model
下一篇
SignatureDefs
系列文
30 Days of MLOps23
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言