Vengineerの妄想(準備期間)

人生は短いけど、長いです。人生を楽しみましょう!

TensorFlow Lite で、TensorFlowのOpを使える



引用
    This experimental feature allows the use of certain TensorFlow ops from within the TensorFlow Lite runtime. 
    Using these ops requires opting in to TF op usage during model conversion, 
    as well as adding an additional dependency to the client's target.

    See `lite/g3doc/using_select_tf_ops.md` for more details. Note that this feature is under active development 
    and is still in the experimental stage.

ということで、TensorFlow Liteの中で、TensorFlowのOpが使えると。

このドキュメントは、r1.12 には無かったもの。r1.13から入ったぽい。

TFLiteConverterのPython APIでは、
下記のように target_ops に、TensorFlow LiteのBuiltInなOpに加えて、SELECT_TF_OPSを追加している。
import tensorflow as tf

converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
converter.target_ops = [tf.lite.OpsSet.TFLITE_BUILTINS,
                        tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

tflte_convertツールを使う場合は、
tflite_convert \
  --output_file=/tmp/foo.tflite \
  --graph_def_file=/tmp/foo.pb \
  --input_arrays=input \
  --output_arrays=MobilenetV1/Predictions/Reshape_1 \
  --target_ops=TFLITE_BUILTINS,SELECT_TF_OPS

--target_ops に、TFLITE_BUILTINSとSELECT_TF_OPSを指定する。

bazelにて、tflite_convertをビルドする場合は、下記のように、--define=with_select_tf_ops=true を指定する。
bazel run --define=with_select_tf_ops=true tflite_convert -- \
  --output_file=/tmp/foo.tflite \
  --graph_def_file=/tmp/foo.pb \
  --input_arrays=input \
  --output_arrays=MobilenetV1/Predictions/Reshape_1 \
  --target_ops=TFLITE_BUILTINS,SELECT_TF_OPS

ここで指定して、--define=with_select_tf_ops=true は、lite/BUILD にて次のようになっています。
# Enables inclusion of select TensorFlow kernels via the TF Lite Flex delegate.
# WARNING: This build flag is experimental and subject to change.
config_setting(
    name = "with_select_tf_ops",
    define_values = {"with_select_tf_ops": "true"},
    visibility = ["//visibility:public"],
)

r1.12では、微妙に違う。
# Enables inclusion of TensorFlow kernels via the TF Lite Flex delegate.
# WARNING: This build flag is experimental and subject to change.
config_setting(
    name = "with_tflite_flex",
    define_values = {"with_tflite_flex": "true"},
    visibility = ["//visibility:public"],
)

AndroidiOSでなくても、C++のライブラリとしても使える。bazelでbuildするときに、--config=monolithicにすると。
C++
When building TensorFlow Lite libraries using the bazel pipeline, the additional TensorFlow ops library can be included and enabled as follows:

Enable monolithic builds if necessary by adding the --config=monolithic build flag.
Do one of the following:
Include the --define=with_select_tf_ops=true build flag in the bazel build invocation when building TensorFlow Lite.
Add the TensorFlow ops delegate library dependency to the build dependencies: tensorflow/lite/delegates/flex:delegate.
Note that the necessary TfLiteDelegate will be installed automatically when creating the interpreter at runtime as long as the delegate is linked into the client library. It is not necessary to explicitly install the delegate instance as is typically required with other delegate types.