新功能!TensorFlow Lite 轉換器中的 TensorFlow 算子融合

2021-02-20 TensorFlow

開箱即用的 RNN 轉換

現在,我們支持將 Keras LSTM 和 Keras 雙向 LSTM 轉換為複合 TensorFlow 算子。如要獲取基於 RNN 的模型以利用 TensorFlow Lite 中的高效 LSTM 融合算子,這是最簡單的方式。請參閱此 Colab,了解如何通過 TensorFlow Lite 解釋器進行端到端 Keras LSTM 到 TensorFlow Lite 的轉換和執行。

Keras LSTM
https://tensorflow.google.cn/api_docs/python/tf/keras/layers/LSTM

Keras 雙向 LSTM
https://tensorflow.google.cn/api_docs/python/tf/keras/layers/Bidirectional

此 Colab
https://colab.sandbox.google.com/github/tensorflow/tensorflow/blob/master/tensorflow/lite/examples/experimental_new_converter/Keras_LSTM_fusion_Codelab.ipynb

此外,我們通過提供連接至基礎架構轉換的便捷接口,實現了到其他任意 TensorFlow RNN 實現的轉換。您也可查看此功能與使用 lingvo 的 LSTMCellSimple 和 LayerNormalizedLSTMCellSimple RNN 實現相關的數個示例。

lingvo
https://github.com/tensorflow/lingvo

LSTMCellSimple
https://github.com/tensorflow/tensorflow/blob/82abf0dbf316526cd718ae8cd7b11cfcb805805e/tensorflow/compiler/mlir/lite/transforms/prepare_composite_functions_tf.cc#L130

LayerNormalizedLSTMCellSimple
https://github.com/tensorflow/tensorflow/blob/c11d5d8881fd927165eeb09fd524a80ebaf009f2/tensorflow/compiler/mlir/lite/transforms/prepare_composite_functions_tf.cc#L137

如需獲取更多信息,請查看我們的 RNN 轉換文檔。

註:目前,我們致力於對 TensorFlow Lite 的 LSTM 算子添加量化支持。我們將在未來推出相關成果。

擴展至其他複合算子

我們擴展了 TensorFlow Lite 轉換器,以便將其他複合 TensorFlow 算子轉換為現有或自定義的 TensorFlow Lite 算子。

要實現 TensorFlow 算子融合至 TensorFlow Lite,需執行以下步驟:將複合算子打包至 tf.function 中。在 TensorFlow 模型原始碼中,使用 experimental_implements 函數注釋標識複合算子並將其抽象為 tf.function。編寫轉換代碼。從概念上看,轉換代碼用已融合算子替換了此接口的複合實現。在 prepare-composite-functions 傳遞中,插入轉換代碼。

調用 TensorFlow Lite 轉換器。使用 TFLiteConverter.from_saved_model API 轉換為 TensorFlow Lite。

experimental_implements
https://github.com/tensorflow/tensorflow/blob/c11d5d8881fd927165eeb09fd524a80ebaf009f2/tensorflow/python/eager/def_function.py#L470

轉換代碼
https://github.com/tensorflow/tensorflow/blob/c11d5d8881fd927165eeb09fd524a80ebaf009f2/tensorflow/compiler/mlir/lite/transforms/prepare_composite_functions_tf.cc#L115

TFLiteConverter.from_saved_model
https://tensorflow.google.cn/api_docs/python/tf/lite/TFLiteConverter#from_saved_model

有關此基礎架構的整體架構,請參閱此處[1]。有關代碼示例的詳細步驟,請參閱此處[2]。如需了解後臺中的算子融合工作方式,請參閱詳細文檔。

此處[1]
https://tensorflow.google.cn/lite/convert/operation_fusion#converting_from_composite_to_fused_operation

此處[2]
https://tensorflow.google.cn/lite/convert/operation_fusion#wrap_the_composite_operation_in_a_tffunction

詳細文檔
https://tensorflow.google.cn/lite/convert/operation_fusion#under_the_hood

相關焦點

  • 有道雲筆記是如何使用TensorFlow Lite的?
    如果只想編譯庫文件,可以編譯 "//tensorflow/contrib/lite/java:tensorflowlite" 這個 target,得到的是 libtensorflowlite_jni.so 庫和相應的 java 層接口。
  • 玩轉TensorFlow Lite:有道雲筆記實操案例分享
    如果只想編譯庫文件,可以編譯 "//tensorflow/contrib/lite/java:tensorflowlite" 這個 target,得到的是 libtensorflowlite_jni.so 庫和相應的 java 層接口。
  • 谷歌第一個TensorFlow中文教程發布!深度解析TensorFlow Lite
    結構體定義中,operator_codes 定義了整個模型的所有算子,subgraphs 定義了所有的子圖。剩下的operators 定義了子圖當中的算子。大家如果想要更好掌握 TensorFlow Lite 的技術細節,一定要閱讀以下文件:• lite/context.h• lite/model.h• lite/interpreter.h• lite/kernels/register.h希望我們的分享能讓廣大開發者們對 TensorFlow Lite 的代碼有一個初步認識
  • TensorFlow Lite 微控制器
    註:微控制器入門 連結https://tensorflow.google.cn/lite/microcontrollers/get_started微控制器通常是小型、低能耗的計算設備,經常嵌入在只需要進行基本運算的硬體中,包括家用電器和物聯網設備等。每年都有數十億個微控制器被生產出來。
  • 【免費教學】Tensorflow Lite極簡入門
    Lite 兼容的公開模型視頻中提到的兼容性指南的連結為:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/tf_ops_compatibility.md在 TensorFlow Lite 中兼容的模型是 Inception
  • TensorFlow Lite的 GPU 委託代理(Delegate)是什麼
    代碼,並切到最新release分支,略# 假設當前在tensorflow目錄下# 配置tensorflow lite的編譯安裝第三方等環境# android ndk、SDK需提前裝好下載好# 其他走默認選擇.
  • 社區分享 | TensorFlow Lite C++ API 開源案例教程
    ", urls = [ "https://github.com/tensorflow/tensorflow/archive/%s.tar.gz" % _TENSORFLOW_GIT_COMMIT, ], patches = [ "@//third_party:org_tensorflow_compatibility_fixes.diff
  • TensorFlow Lite 深度解析 | 中文教學視頻
    operator_codes 定義了整個模型的所有算子,subgraphs 定義了所有的子圖。剩下的operators 定義了子圖當中的算子。大家如果想要更好掌握 TensorFlow Lite 的技術細節,一定要閱讀以下文件:lite/context.hlite/model.hlite/interpreter.hlite/kernels/register.h希望我們的分享能讓廣大開發者們對 TensorFlow Lite 的代碼有一個初步認識,期待看到大家精彩的作品
  • 【免費教學】在嵌入式中使用 TensorFlow Lite
    今天與大家分享 Android 平臺上的一些 TensorFlow Lite 應用,先來一起看看視頻吧:TensorFlow Lite 在安卓中的應用 視頻中的 App 用了一個叫 MobileNet 的模型。這個模型的特點是體積小、速度快(延遲低)、能耗低。
  • TensorFlow Lite 助力產品落地
    在 TensorFlow Lite 中引入前沿研究模型的同時,我們還希望您能夠為自己的用例與需求輕鬆自定義這些模型。我們很高興地發布 TensorFlow Lite Model Maker,這款工具簡單易用。通過遷移學習,您可在您的數據集上應用前沿的機器學習模型。此工具將複雜的機器學習概念封裝在直觀的 API 中,無需機器學習專業知識,您也可以開啟機器學習之旅。
  • Google 第一個 TF 中文教學視頻發布 | TensorFlow Lite 深度解析
    operator_codes 定義了整個模型的所有算子,subgraphs 定義了所有的子圖。剩下的operators 定義了子圖當中的算子。大家如果想要更好掌握 TensorFlow Lite 的技術細節,一定要閱讀以下文件:lite/context.hlite/model.hlite/interpreter.hlite/kernels/register.h希望我們的分享能讓廣大開發者們對 TensorFlow Lite 的代碼有一個初步認識,期待看到大家精彩的作品
  • 學習教程 | 使用 TensorFlow Lite 在 Android App 中生成超分圖片
    /tensorflow/lite/c/c_api.h我們在預先編譯好的 AAR 文件中包含了 TFLite C API需要的頭文件和庫 (包括核心庫和 GPU 庫)。file_path=org%2Ftensorflow%2Ftensorflow-lite%2F2.3.0%2Ftensorflow-lite-2.3.0.aar" dest "${project.rootDir}/libraries/tensorflow-lite-2.3.0.aar" overwrite false retries 5
  • TensorFlow Lite Android部署介紹
    谷歌提供了多種轉換方式:tflight_convert跟tensorflow是一起下載的,筆者通過brew安裝的python,pip安裝tf-nightly後tflight_convert路徑如下:註:這裡只介紹了keras HDF5格式模型的轉換,其他模型轉換建議參考:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/tflite_convert/cmdline_examples.md4、Android
  • 中文視頻教學 | 在 iOS 中使用 TensorFlow Lite
    本期我們為大家帶來如何在 iOS
  • TensorFlow模型量化
    但是實際生活中在手機上使用該功能的場景數不勝數:人臉解鎖、人臉支付、短視頻與直播人臉特效都能看到它的身影。與PC不同,手持設備在運算能力、存儲容量、電能供應等方面無法與數百瓦特功率的臺式機相比。因此,深度學習模型在移動終端運行需要經過特別的改造。
  • Android中使用TensorFlow Lite實現圖像分類
    TensorFlow Lite的GitHub地址:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/lite轉換模型 手機上執行預測,首先需要一個訓練好的模型,這個模型不能是TensorFlow原來格式的模型,TensorFlow
  • Google發布TensorFlow Lite
    TensorFlow Lite 轉換器: 將模型轉換為 TensorFlow Lite 文件格式的程序。TensorFlow Lite 模型文件: 基於 FlatBuffers 的模型文件格式,已經針對最大速度和最小尺寸進行了優化。
  • 學習教程 | 用 TensorFlow Lite 和 SPICE 模型打造聽歌識譜 App
    該文件存儲在手機內部存儲器的 Pitch Detector 文件夾中,可用於與原始版本對比驗證移動端模型輸出的準確性。部署 SPICE 模型的第一步是複製位於 Assets 文件夾中的 .tflite 文件。
  • 跨越重重「障礙」,我從 PyTorch 轉換為了 TensorFlow Lite
    要求:TensorFlow == 2.2.0(這是 onnx-tensorflow 的先決條件。不過,它也適用於 tf-nightly 版本2.4.0-dev20200923)。tensorflow-addons == 0.11.2我也不知道為什麼,但這種轉換隻能用在我的 GPU 機器。
  • 中文教學視頻 | 在 Android 中使用 TensorFlow Lite
    第一期視頻中,我們分享了 TensorFlow Lite 的一些基本知識,還有它的模型結構及使用方法。