Chữ ký trong TensorFlow Lite

Xem trên TensorFlow.org Chạy trong Google Colab Xem nguồn trên GitHub Tải xuống sổ ghi chép

TensorFlow Lite hỗ trợ chuyển đổi thông số kỹ thuật đầu vào / đầu ra của mô hình TensorFlow sang mô hình TensorFlow Lite. Các đặc tả đầu vào / đầu ra được gọi là "chữ ký". Chữ ký có thể được chỉ định khi xây dựng SavedModel hoặc tạo các chức năng cụ thể.

Chữ ký trong TensorFlow Lite cung cấp các tính năng sau:

  • Họ chỉ định các đầu vào và đầu ra của mô hình TensorFlow Lite đã chuyển đổi bằng cách tôn trọng các chữ ký của mô hình TensorFlow.
  • Cho phép một mô hình TensorFlow Lite hỗ trợ nhiều điểm nhập.

Chữ ký bao gồm ba phần:

  • Đầu vào: Ánh xạ các đầu vào từ tên đầu vào trong chữ ký đến một tenxơ đầu vào.
  • Kết quả đầu ra: Ánh xạ để ánh xạ đầu ra từ tên đầu ra trong chữ ký đến một tenxơ đầu ra.
  • Khóa chữ ký: Tên xác định điểm vào của biểu đồ.

Thành lập

import tensorflow as tf

Mô hình ví dụ

Giả sử chúng ta có hai nhiệm vụ, ví dụ: mã hóa và giải mã, dưới dạng mô hình TensorFlow:

class Model(tf.Module):

  @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.float32)])
  def encode(self, x):
    result = tf.strings.as_string(x)
    return {
         "encoded_result": result
    }

  @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.string)])
  def decode(self, x):
    result = tf.strings.to_number(x)
    return {
         "decoded_result": result
    }

Nói một cách khôn ngoan, mô hình TensorFlow ở trên có thể được tóm tắt như sau:

  • Chữ ký

    • Chìa khóa: mã hóa
    • Đầu vào: {"x"}
    • Đầu ra: {"encoded_result"}
  • Chữ ký

    • Chìa khóa: giải mã
    • Đầu vào: {"x"}
    • Đầu ra: {"decoded_result"}

Chuyển đổi một mô hình với Chữ ký

API trình chuyển đổi TensorFlow Lite sẽ đưa thông tin chữ ký ở trên vào mô hình TensorFlow Lite đã chuyển đổi.

Chức năng chuyển đổi này có sẵn trên tất cả các API trình chuyển đổi bắt đầu từ phiên bản TensorFlow 2.7.0. Xem cách sử dụng ví dụ.

Từ mô hình đã lưu

model = Model()

# Save the model
SAVED_MODEL_PATH = 'content/saved_models/coding'

tf.saved_model.save(
    model, SAVED_MODEL_PATH,
    signatures={
      'encode': model.encode.get_concrete_function(),
      'decode': model.decode.get_concrete_function()
    })

# Convert the saved model using TFLiteConverter
converter = tf.lite.TFLiteConverter.from_saved_model(SAVED_MODEL_PATH)
converter.target_spec.supported_ops = [
    tf.lite.OpsSet.TFLITE_BUILTINS,  # enable TensorFlow Lite ops.
    tf.lite.OpsSet.SELECT_TF_OPS  # enable TensorFlow ops.
]
tflite_model = converter.convert()

# Print the signatures from the converted model
interpreter = tf.lite.Interpreter(model_content=tflite_model)
signatures = interpreter.get_signature_list()
print(signatures)
2021-11-15 12:17:48.388332: W tensorflow/python/util/util.cc:368] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
INFO:tensorflow:Assets written to: content/saved_models/coding/assets
2021-11-15 12:17:48.727484: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:363] Ignored output_format.
2021-11-15 12:17:48.727522: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:366] Ignored drop_control_dependency.
WARNING:absl:Buffer deduplication procedure will be skipped when flatbuffer library is not properly loaded
2021-11-15 12:17:48.727529: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:372] Ignored change_concat_input_ranges.
2021-11-15 12:17:48.767576: W tensorflow/compiler/mlir/lite/flatbuffer_export.cc:1891] TFLite interpreter needs to link Flex delegate in order to run the model since it contains the following Select TFop(s):
Flex ops: FlexAsString, FlexStringToNumber
Details:
    tf.AsString(tensor<?xf32>) -> (tensor<?x!tf_type.string>) : {device = "", fill = "", precision = -1 : i64, scientific = false, shortest = false, width = -1 : i64}
    tf.StringToNumber(tensor<?x!tf_type.string>) -> (tensor<?xf32>) : {device = "", out_type = f32}
See instructions: https://www.tensorflow.org/lite/guide/ops_select
{'decode': {'inputs': ['x'], 'outputs': ['decoded_result']}, 'encode': {'inputs': ['x'], 'outputs': ['encoded_result']} }
INFO: Created TensorFlow Lite delegate for select TF ops.
INFO: TfLiteFlexDelegate delegate: 1 nodes delegated out of 1 nodes with 1 partitions.

Của Keras Model

# Generate a Keras model.
keras_model = tf.keras.Sequential(
    [
        tf.keras.layers.Dense(2, input_dim=4, activation='relu', name='x'),
        tf.keras.layers.Dense(1, activation='relu', name='output'),
    ]
)

# Convert the keras model using TFLiteConverter.
# Keras model converter API uses the default signature automatically.
converter = tf.lite.TFLiteConverter.from_keras_model(keras_model)
tflite_model = converter.convert()

# Print the signatures from the converted model
interpreter = tf.lite.Interpreter(model_content=tflite_model)

signatures = interpreter.get_signature_list()
print(signatures)
INFO:tensorflow:Assets written to: /tmp/tmplhr7j714/assets
INFO:tensorflow:Assets written to: /tmp/tmplhr7j714/assets
2021-11-15 12:17:49.368226: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:363] Ignored output_format.
2021-11-15 12:17:49.368264: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:366] Ignored drop_control_dependency.
WARNING:absl:Buffer deduplication procedure will be skipped when flatbuffer library is not properly loaded
{'serving_default': {'inputs': ['x_input'], 'outputs': ['output']} }

Từ các chức năng bê tông

model = Model()

# Convert the concrete functions using TFLiteConverter
converter = tf.lite.TFLiteConverter.from_concrete_functions(
    [model.encode.get_concrete_function(),
     model.decode.get_concrete_function()], model)
converter.target_spec.supported_ops = [
    tf.lite.OpsSet.TFLITE_BUILTINS,  # enable TensorFlow Lite ops.
    tf.lite.OpsSet.SELECT_TF_OPS  # enable TensorFlow ops.
]
tflite_model = converter.convert()

# Print the signatures from the converted model
interpreter = tf.lite.Interpreter(model_content=tflite_model)
signatures = interpreter.get_signature_list()
print(signatures)
INFO:tensorflow:Assets written to: /tmp/tmpc14_l70o/assets
INFO:tensorflow:Assets written to: /tmp/tmpc14_l70o/assets
2021-11-15 12:17:49.538814: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:363] Ignored output_format.
2021-11-15 12:17:49.538850: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:366] Ignored drop_control_dependency.
WARNING:absl:Buffer deduplication procedure will be skipped when flatbuffer library is not properly loaded
{'decode': {'inputs': ['x'], 'outputs': ['decoded_result']}, 'encode': {'inputs': ['x'], 'outputs': ['encoded_result']} }
2021-11-15 12:17:49.572773: W tensorflow/compiler/mlir/lite/flatbuffer_export.cc:1891] TFLite interpreter needs to link Flex delegate in order to run the model since it contains the following Select TFop(s):
Flex ops: FlexAsString, FlexStringToNumber
Details:
    tf.AsString(tensor<?xf32>) -> (tensor<?x!tf_type.string>) : {device = "", fill = "", precision = -1 : i64, scientific = false, shortest = false, width = -1 : i64}
    tf.StringToNumber(tensor<?x!tf_type.string>) -> (tensor<?xf32>) : {device = "", out_type = f32}
See instructions: https://www.tensorflow.org/lite/guide/ops_select

Chạy chữ ký

Các API suy luận TensorFlow hỗ trợ các thực thi dựa trên chữ ký:

  • Truy cập các bộ căng đầu vào / đầu ra thông qua tên của các đầu vào và đầu ra, được chỉ định bởi chữ ký.
  • Chạy từng điểm vào của biểu đồ riêng biệt, được xác định bằng khóa chữ ký.
  • Hỗ trợ thủ tục khởi tạo SavedModel.

Các ràng buộc ngôn ngữ Java, C ++ và Python hiện có sẵn. Xem ví dụ các phần bên dưới.

Java

try (Interpreter interpreter = new Interpreter(file_of_tensorflowlite_model)) {
  // Run encoding signature.
  Map<String, Object> inputs = new HashMap<>();
  inputs.put("x", input);
  Map<String, Object> outputs = new HashMap<>();
  outputs.put("encoded_result", encoded_result);
  interpreter.runSignature(inputs, outputs, "encode");

  // Run decoding signature.
  Map<String, Object> inputs = new HashMap<>();
  inputs.put("x", encoded_result);
  Map<String, Object> outputs = new HashMap<>();
  outputs.put("decoded_result", decoded_result);
  interpreter.runSignature(inputs, outputs, "decode");
}

C ++

SignatureRunner* encode_runner =
    interpreter->GetSignatureRunner("encode");
encode_runner->ResizeInputTensor("x", {100});
encode_runner->AllocateTensors();

TfLiteTensor* input_tensor = encode_runner->input_tensor("x");
float* input = input_tensor->data.f;
// Fill `input`.

encode_runner->Invoke();

const TfLiteTensor* output_tensor = encode_runner->output_tensor(
    "encoded_result");
float* output = output_tensor->data.f;
// Access `output`.

Python

# Load the TFLite model in TFLite Interpreter
interpreter = tf.lite.Interpreter(model_content=tflite_model)

# Print the signatures from the converted model
signatures = interpreter.get_signature_list()
print('Signature:', signatures)

# encode and decode are callable with input as arguments.
encode = interpreter.get_signature_runner('encode')
decode = interpreter.get_signature_runner('decode')

# 'encoded' and 'decoded' are dictionaries with all outputs from the inference.
input = tf.constant([1, 2, 3], dtype=tf.float32)
print('Input:', input)
encoded = encode(x=input)
print('Encoded result:', encoded)
decoded = decode(x=encoded['encoded_result'])
print('Decoded result:', decoded)
Signature: {'decode': {'inputs': ['x'], 'outputs': ['decoded_result']}, 'encode': {'inputs': ['x'], 'outputs': ['encoded_result']} }
Input: tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32)
Encoded result: {'encoded_result': array([b'1.000000', b'2.000000', b'3.000000'], dtype=object)}
Decoded result: {'decoded_result': array([1., 2., 3.], dtype=float32)}

Những hạn chế đã biết

  • Vì trình thông dịch TFLite không đảm bảo an toàn luồng, các trình chạy chữ ký từ cùng một trình thông dịch sẽ không được thực thi đồng thời.
  • Chưa có hỗ trợ cho C / iOS / Swift.

Cập nhật

  • Phiên bản 2.7
    • Tính năng nhiều chữ ký được triển khai.
    • Tất cả các API trình chuyển đổi từ phiên bản hai đều tạo ra các mô hình TensorFlow Lite hỗ trợ chữ ký.
  • Phiên bản 2.5
    • Tính năng chữ ký có sẵn thông qua from_saved_model API chuyển đổi.
,

Xem trên TensorFlow.org Chạy trong Google Colab Xem nguồn trên GitHub Tải xuống sổ ghi chép

TensorFlow Lite hỗ trợ chuyển đổi thông số kỹ thuật đầu vào / đầu ra của mô hình TensorFlow sang mô hình TensorFlow Lite. Các đặc tả đầu vào / đầu ra được gọi là "chữ ký". Chữ ký có thể được chỉ định khi xây dựng SavedModel hoặc tạo các chức năng cụ thể.

Chữ ký trong TensorFlow Lite cung cấp các tính năng sau:

  • Họ chỉ định các đầu vào và đầu ra của mô hình TensorFlow Lite đã chuyển đổi bằng cách tôn trọng các chữ ký của mô hình TensorFlow.
  • Cho phép một mô hình TensorFlow Lite hỗ trợ nhiều điểm nhập.

Chữ ký bao gồm ba phần:

  • Đầu vào: Ánh xạ các đầu vào từ tên đầu vào trong chữ ký đến một tenxơ đầu vào.
  • Kết quả đầu ra: Ánh xạ để ánh xạ đầu ra từ tên đầu ra trong chữ ký đến một tenxơ đầu ra.
  • Khóa chữ ký: Tên xác định điểm vào của biểu đồ.

Thành lập

import tensorflow as tf

Mô hình ví dụ

Giả sử chúng ta có hai nhiệm vụ, ví dụ: mã hóa và giải mã, dưới dạng mô hình TensorFlow:

class Model(tf.Module):

  @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.float32)])
  def encode(self, x):
    result = tf.strings.as_string(x)
    return {
         "encoded_result": result
    }

  @tf.function(input_signature=[tf.TensorSpec(shape=[None], dtype=tf.string)])
  def decode(self, x):
    result = tf.strings.to_number(x)
    return {
         "decoded_result": result
    }

Nói một cách khôn ngoan, mô hình TensorFlow ở trên có thể được tóm tắt như sau:

  • Chữ ký

    • Chìa khóa: mã hóa
    • Đầu vào: {"x"}
    • Đầu ra: {"encoded_result"}
  • Chữ ký

    • Chìa khóa: giải mã
    • Đầu vào: {"x"}
    • Đầu ra: {"decoded_result"}

Chuyển đổi một mô hình với Chữ ký

API trình chuyển đổi TensorFlow Lite sẽ đưa thông tin chữ ký ở trên vào mô hình TensorFlow Lite đã chuyển đổi.

Chức năng chuyển đổi này có sẵn trên tất cả các API trình chuyển đổi bắt đầu từ phiên bản TensorFlow 2.7.0. Xem cách sử dụng ví dụ.

Từ mô hình đã lưu

model = Model()

# Save the model
SAVED_MODEL_PATH = 'content/saved_models/coding'

tf.saved_model.save(
    model, SAVED_MODEL_PATH,
    signatures={
      'encode': model.encode.get_concrete_function(),
      'decode': model.decode.get_concrete_function()
    })

# Convert the saved model using TFLiteConverter
converter = tf.lite.TFLiteConverter.from_saved_model(SAVED_MODEL_PATH)
converter.target_spec.supported_ops = [
    tf.lite.OpsSet.TFLITE_BUILTINS,  # enable TensorFlow Lite ops.
    tf.lite.OpsSet.SELECT_TF_OPS  # enable TensorFlow ops.
]
tflite_model = converter.convert()

# Print the signatures from the converted model
interpreter = tf.lite.Interpreter(model_content=tflite_model)
signatures = interpreter.get_signature_list()
print(signatures)
2021-11-15 12:17:48.388332: W tensorflow/python/util/util.cc:368] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
INFO:tensorflow:Assets written to: content/saved_models/coding/assets
2021-11-15 12:17:48.727484: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:363] Ignored output_format.
2021-11-15 12:17:48.727522: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:366] Ignored drop_control_dependency.
WARNING:absl:Buffer deduplication procedure will be skipped when flatbuffer library is not properly loaded
2021-11-15 12:17:48.727529: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:372] Ignored change_concat_input_ranges.
2021-11-15 12:17:48.767576: W tensorflow/compiler/mlir/lite/flatbuffer_export.cc:1891] TFLite interpreter needs to link Flex delegate in order to run the model since it contains the following Select TFop(s):
Flex ops: FlexAsString, FlexStringToNumber
Details:
    tf.AsString(tensor<?xf32>) -> (tensor<?x!tf_type.string>) : {device = "", fill = "", precision = -1 : i64, scientific = false, shortest = false, width = -1 : i64}
    tf.StringToNumber(tensor<?x!tf_type.string>) -> (tensor<?xf32>) : {device = "", out_type = f32}
See instructions: https://www.tensorflow.org/lite/guide/ops_select
{'decode': {'inputs': ['x'], 'outputs': ['decoded_result']}, 'encode': {'inputs': ['x'], 'outputs': ['encoded_result']} }
INFO: Created TensorFlow Lite delegate for select TF ops.
INFO: TfLiteFlexDelegate delegate: 1 nodes delegated out of 1 nodes with 1 partitions.

Của Keras Model

# Generate a Keras model.
keras_model = tf.keras.Sequential(
    [
        tf.keras.layers.Dense(2, input_dim=4, activation='relu', name='x'),
        tf.keras.layers.Dense(1, activation='relu', name='output'),
    ]
)

# Convert the keras model using TFLiteConverter.
# Keras model converter API uses the default signature automatically.
converter = tf.lite.TFLiteConverter.from_keras_model(keras_model)
tflite_model = converter.convert()

# Print the signatures from the converted model
interpreter = tf.lite.Interpreter(model_content=tflite_model)

signatures = interpreter.get_signature_list()
print(signatures)
INFO:tensorflow:Assets written to: /tmp/tmplhr7j714/assets
INFO:tensorflow:Assets written to: /tmp/tmplhr7j714/assets
2021-11-15 12:17:49.368226: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:363] Ignored output_format.
2021-11-15 12:17:49.368264: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:366] Ignored drop_control_dependency.
WARNING:absl:Buffer deduplication procedure will be skipped when flatbuffer library is not properly loaded
{'serving_default': {'inputs': ['x_input'], 'outputs': ['output']} }

Từ các chức năng bê tông

model = Model()

# Convert the concrete functions using TFLiteConverter
converter = tf.lite.TFLiteConverter.from_concrete_functions(
    [model.encode.get_concrete_function(),
     model.decode.get_concrete_function()], model)
converter.target_spec.supported_ops = [
    tf.lite.OpsSet.TFLITE_BUILTINS,  # enable TensorFlow Lite ops.
    tf.lite.OpsSet.SELECT_TF_OPS  # enable TensorFlow ops.
]
tflite_model = converter.convert()

# Print the signatures from the converted model
interpreter = tf.lite.Interpreter(model_content=tflite_model)
signatures = interpreter.get_signature_list()
print(signatures)
INFO:tensorflow:Assets written to: /tmp/tmpc14_l70o/assets
INFO:tensorflow:Assets written to: /tmp/tmpc14_l70o/assets
2021-11-15 12:17:49.538814: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:363] Ignored output_format.
2021-11-15 12:17:49.538850: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:366] Ignored drop_control_dependency.
WARNING:absl:Buffer deduplication procedure will be skipped when flatbuffer library is not properly loaded
{'decode': {'inputs': ['x'], 'outputs': ['decoded_result']}, 'encode': {'inputs': ['x'], 'outputs': ['encoded_result']} }
2021-11-15 12:17:49.572773: W tensorflow/compiler/mlir/lite/flatbuffer_export.cc:1891] TFLite interpreter needs to link Flex delegate in order to run the model since it contains the following Select TFop(s):
Flex ops: FlexAsString, FlexStringToNumber
Details:
    tf.AsString(tensor<?xf32>) -> (tensor<?x!tf_type.string>) : {device = "", fill = "", precision = -1 : i64, scientific = false, shortest = false, width = -1 : i64}
    tf.StringToNumber(tensor<?x!tf_type.string>) -> (tensor<?xf32>) : {device = "", out_type = f32}
See instructions: https://www.tensorflow.org/lite/guide/ops_select

Chạy chữ ký

Các API suy luận TensorFlow hỗ trợ các thực thi dựa trên chữ ký:

  • Truy cập các bộ căng đầu vào / đầu ra thông qua tên của các đầu vào và đầu ra, được chỉ định bởi chữ ký.
  • Chạy từng điểm vào của biểu đồ riêng biệt, được xác định bằng khóa chữ ký.
  • Hỗ trợ thủ tục khởi tạo SavedModel.

Các ràng buộc ngôn ngữ Java, C ++ và Python hiện có sẵn. Xem ví dụ các phần bên dưới.

Java

try (Interpreter interpreter = new Interpreter(file_of_tensorflowlite_model)) {
  // Run encoding signature.
  Map<String, Object> inputs = new HashMap<>();
  inputs.put("x", input);
  Map<String, Object> outputs = new HashMap<>();
  outputs.put("encoded_result", encoded_result);
  interpreter.runSignature(inputs, outputs, "encode");

  // Run decoding signature.
  Map<String, Object> inputs = new HashMap<>();
  inputs.put("x", encoded_result);
  Map<String, Object> outputs = new HashMap<>();
  outputs.put("decoded_result", decoded_result);
  interpreter.runSignature(inputs, outputs, "decode");
}

C ++

SignatureRunner* encode_runner =
    interpreter->GetSignatureRunner("encode");
encode_runner->ResizeInputTensor("x", {100});
encode_runner->AllocateTensors();

TfLiteTensor* input_tensor = encode_runner->input_tensor("x");
float* input = input_tensor->data.f;
// Fill `input`.

encode_runner->Invoke();

const TfLiteTensor* output_tensor = encode_runner->output_tensor(
    "encoded_result");
float* output = output_tensor->data.f;
// Access `output`.

Python

# Load the TFLite model in TFLite Interpreter
interpreter = tf.lite.Interpreter(model_content=tflite_model)

# Print the signatures from the converted model
signatures = interpreter.get_signature_list()
print('Signature:', signatures)

# encode and decode are callable with input as arguments.
encode = interpreter.get_signature_runner('encode')
decode = interpreter.get_signature_runner('decode')

# 'encoded' and 'decoded' are dictionaries with all outputs from the inference.
input = tf.constant([1, 2, 3], dtype=tf.float32)
print('Input:', input)
encoded = encode(x=input)
print('Encoded result:', encoded)
decoded = decode(x=encoded['encoded_result'])
print('Decoded result:', decoded)
Signature: {'decode': {'inputs': ['x'], 'outputs': ['decoded_result']}, 'encode': {'inputs': ['x'], 'outputs': ['encoded_result']} }
Input: tf.Tensor([1. 2. 3.], shape=(3,), dtype=float32)
Encoded result: {'encoded_result': array([b'1.000000', b'2.000000', b'3.000000'], dtype=object)}
Decoded result: {'decoded_result': array([1., 2., 3.], dtype=float32)}

Những hạn chế đã biết

  • Vì trình thông dịch TFLite không đảm bảo an toàn luồng, các trình chạy chữ ký từ cùng một trình thông dịch sẽ không được thực thi đồng thời.
  • Chưa có hỗ trợ cho C / iOS / Swift.

Cập nhật

  • Phiên bản 2.7
    • Tính năng nhiều chữ ký được triển khai.
    • Tất cả các API trình chuyển đổi từ phiên bản hai đều tạo ra các mô hình TensorFlow Lite hỗ trợ chữ ký.
  • Phiên bản 2.5
    • Tính năng chữ ký có sẵn thông qua from_saved_model API chuyển đổi.