Google 第一個 TF 中文教學視頻發布 | TensorFlow Lite 深度解析

2021-02-26 TensorFlow

作者:TensorFlow 中國團隊,Google 開發技術推廣工程師 Renmin Gu.

最新的中文技術分享視頻來了!本期來自 Google 的工程師 Renmin 為大家帶來 TensorFlow Lite 的深度解析視頻,主要講述 TensorFlow Lite 模型文件格式,並可視化以幫助大家記憶理解,也包含 TensorFlow Lite 的具體加載運行過程,並給出關鍵的數據結構描述,同樣以可視化的形式呈現給大家:

看完視頻,我們不妨一起總結回顧一下:

首先,我們需要在臺式機上設計、訓練出目標模型,並將其轉化成 TensorFlow Lite 的格式。接著,此格式文件在 TensorFlow Lite 中會被內置了 Neon 指令集的解析器加載到內存,並執行相應的計算。由於 TensorFlow Lite 對硬體加速接口良好的支持,開發者可以設計出性能更優的 App 供用戶使用。

模型文件格式Model 結構體:模型的主結構

table Model {
   version: uint;
   operator_codes: [OperatorCode];
   subgraphs: [SubGraph];

   description: string;
   buffers: [Buffer]
}

在上面的 Model 結構體定義中,operator_codes 定義了整個模型的所有算子,subgraphs 定義了所有的子圖。子圖當中,第一個元素是主圖。buffers 屬性則是數據存儲區域,主要存儲的是模型的權重信息。

SubGraph 結構體:Model 中最重要的部分

table SubGraph {
   tensors: [Tensor];
   inputs: [int];
   outputs: [int];
   operators: [Operator];

   name: string;
}

類似的,tensors 屬性定義了子圖的各個 Tensor,而 inputs 和 outputs 用索引的維護著子圖中 Tensor 與輸入輸出之間的對應關係。剩下的operators 定義了子圖當中的算子。

Tensor 結構體:包含維度、數據類型、Buffer 位置等信息

table Tensor {
   shape: [int];
   type: TensorType;
   buffer: uint;

   name: string;
}

buffer 以索引量的形式,給出了這個 Tensor 需要用到子圖的哪一個 buffer。

Operator 結構體:SubGraph 中最重要的結構體

Operator 結構體定義了子圖的結構:

table Operator {
   opcode_index: uint;
   inputs: [int];
   outputs: [int];

   ...
}

opcode_index 用索引方式指明該 Operator 對應了哪個算子。 inputs 和 outputs 則是 Tensor 的索引值,指明該 Operator 的輸入輸出信息。

解析器概況

那麼 TensorFlow Lite 的解析器又是如何工作的呢?

一開始,終端設備會通過 mmap 以內存映射的形式將模型文件載入客戶端內存中,其中包含了所有的 Tensor,Operator 和 Buffer 等信息。出於數據使用的需要,TensorFlow Lite 會同時創建 Buffer 的只讀區域和分配可寫 Buffer 區域。

由於解析器中包含了集體執行計算的代碼,這一部分被稱為 Kernel。模型中的各個 Tensor 會被加載為 TfLiteTensor 的格式併集中存放在 TfLiteContext 中。

每個 Tensor 的指針都指向了內存中的只讀 Buffer 區域或是一開始新分配的可寫入 Buffer 區域。

模型中的 Operator 被重新加載為 TfLiteNode,它包含輸入輸出的 Tensor 索引值。這些 Node 對應的操作符存儲於 TfLiteRegistration 中,它包含了指向 Kernel 的函數指針。OpResolver 負責維護函數指針的對應關係。

TensorFlow Lite 加載模型的過程中會確定執行 Node 的順序,然後依次執行。

大家如果想要更好掌握 TensorFlow Lite 的技術細節,一定要閱讀以下文件:

lite/context.h

lite/model.h

lite/interpreter.h

lite/kernels/register.h

希望我們的分享能讓廣大開發者們對 TensorFlow Lite 的代碼有一個初步認識,期待看到大家精彩的作品!

另外,TensorFlow Lite 的代碼位置

https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/lite

模型的模式文件位於:

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/schema/schema.fbs

更多

在 Google 中國 Bilibili 頻道查看本視頻:

https://www.bilibili.com/video/av24219725/

谷歌開發者中文頻道查看本視頻:

https://www.youtube.com/watch?v=b45xt-tdI7M

谷歌開發者視頻 Youku 頻道查看本視頻:

http://v.youku.com/v_show/id_XMzYzODc1NDYyOA==.html

還想看到更多中文 TensorFlow 教學視頻嗎,歡迎留下你的評論和想法!

相關焦點

  • 谷歌第一個TensorFlow中文教程發布!深度解析TensorFlow Lite
    )作者 :Renmin Gu【新智元導讀】本期來自 Google 的工程師 Renmin 為大家帶來 TensorFlow Lite 的深度解析視頻,主要講述 TensorFlow Lite 模型文件格式,並可視化以幫助大家記憶理解,也包含 TensorFlow Lite 的具體加載運行過程
  • TensorFlow Lite 深度解析 | 中文教學視頻
    最新的中文技術分享視頻來了!本期來自 Google 的工程師 Renmin 為大家帶來 TensorFlow Lite 的深度解析視頻,主要講述 TensorFlow Lite 模型文件格式,並可視化以幫助大家記憶理解,也包含 TensorFlow Lite 的具體加載運行過程,並給出關鍵的數據結構描述,同樣以可視化的形式呈現給大家。
  • 發布新的中文系列視頻 | TensorFlow Lite 概述和模型轉化簡介
    我們將於本周一、三、五更新「Coding TensorFlow 系列」的前三個中文視頻,並將在 TensorFlow 微信公眾號(ID: tensorflowers)首發,歡迎轉發和關注!示例代碼如下:with tf.Session() as sess:  tflite_model = tf.contrib.lite.toco_convert(sess.graph_def, [img], [out])  open("converted_model.tflite","wb").write(tflite_model)TensorFlow Lite
  • 【免費教學】Tensorflow Lite極簡入門
    示例代碼如下:with tf.Session() as sess:  tflite_model = tf.contrib.lite.toco_convert(sess.graph_def, [img], [out])  open("converted_model.tflite","wb").write(tflite_model)TensorFlow
  • TensorFlow Lite 轉換器中的 TensorFlow 算子融合
    Keras LSTMhttps://tensorflow.google.cn/api_docs/python/tf/keras/layers/LSTMKeras 雙向 LSTMhttps://tensorflow.google.cn/api_docs/python/tf/keras/layers/Bidirectional此 Colab
  • 社區分享 | TensorFlow Lite C++ API 開源案例教程
    :workspace.bzl", "tf_workspace")tf_workspace(tf_repo_name = "org_tensorflow")上面是 image-classifier 的 WORKSPACE 配置,他導入 versions 對象檢查 Bazel 版本,加載 http_archive 函數管理 org_tensorflow、opencv、abseil
  • TensorFlow Lite 助力產品落地
    我們很高興地發布 TensorFlow Lite Model Maker,這款工具簡單易用。通過遷移學習,您可在您的數據集上應用前沿的機器學習模型。此工具將複雜的機器學習概念封裝在直觀的 API 中,無需機器學習專業知識,您也可以開啟機器學習之旅。
  • TensorFlow Lite Android部署介紹
    /mnist_cnn.tflite到此,我們已經得到一個可以運行的TensorFlow Lite模型了,即mnist_cnn.tflite。註:這裡只介紹了keras HDF5格式模型的轉換,其他模型轉換建議參考:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/tflite_convert/cmdline_examples.md4、Android
  • 用樹莓派4b構建深度學習應用(六)TensorFlow Lite篇
    TensorFlow Lite 包括兩個主要組件:轉換器一般在主電腦上完成,主要是為了靜態化計算圖,轉換權重類型後,生成 .tflite 文件。而解釋器主要在嵌入式設備上運行,我們這裡先在樹莓派上安裝一下 tensorflow lite。
  • Google發布TensorFlow Lite
    今天,我們很高興地發布 TensorFlow Lite 的開發者預覽版,它是 TensorFlow 針對移動和嵌入式設備的輕量級解決方案
  • 中文教學視頻 | 在 Android 中使用 TensorFlow Lite
    第一期視頻中,我們分享了 TensorFlow Lite 的一些基本知識,還有它的模型結構及使用方法。
  • TensorFlow Lite 微控制器
    註:構建和轉換模型 連結https://tensorflow.google.cn/lite/microcontrollers/build_convert#%E8%BD%AC%E6%8D%A2%E6%A8%A1%E5%9E%8B理解 C++ 庫 連結https://tensorflow.google.cn/lite/microcontrollers
  • 跨越重重「障礙」,我從 PyTorch 轉換為了 TensorFlow Lite
    要求:TensorFlow == 2.2.0(這是 onnx-tensorflow 的先決條件。不過,它也適用於 tf-nightly 版本2.4.0-dev20200923)。tensorflow-addons == 0.11.2我也不知道為什麼,但這種轉換隻能用在我的 GPU 機器。
  • 有道雲筆記是如何使用TensorFlow Lite的?
    如果只想編譯庫文件,可以編譯 "//tensorflow/contrib/lite/java:tensorflowlite" 這個 target,得到的是 libtensorflowlite_jni.so 庫和相應的 java 層接口。
  • 使用Tensorflow進行實時移動視頻對象檢測
    為減少障礙,Google發布了Tensorflow對象檢測API和Tensorflow Hub等開源工具,使人們能夠利用那些已經廣泛使用的預先訓練的模型(例如Faster R-CNN,R-FCN和SSD)來快速構建自定義模型,遷移學習。
  • Android中使用TensorFlow Lite實現圖像分類
    TensorFlow Lite的GitHub地址:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/lite轉換模型 手機上執行預測,首先需要一個訓練好的模型,這個模型不能是TensorFlow原來格式的模型,TensorFlow
  • 玩轉TensorFlow Lite:有道雲筆記實操案例分享
    如果只想編譯庫文件,可以編譯 "//tensorflow/contrib/lite/java:tensorflowlite" 這個 target,得到的是 libtensorflowlite_jni.so 庫和相應的 java 層接口。
  • Keras vs tf.keras: 在TensorFlow 2.0中有什麼區別?
    Francois於2015年3月27日承諾將Keras的第一個版本發布到他的GitHub。最初,Francois開發了Keras,以促進他自己的研究和實驗。但是,隨著深度學習的普及,許多開發人員,程式設計師和機器學習從業人員都因其易於使用的API而蜂擁而至Keras。那時,可用的深度學習庫還不多,熱門的庫包括Torch,Theano和Caffe。
  • TensorFlow Lite的 GPU 委託代理(Delegate)是什麼
    代碼,並切到最新release分支,略# 假設當前在tensorflow目錄下# 配置tensorflow lite的編譯安裝第三方等環境# android ndk、SDK需提前裝好下載好# 其他走默認選擇.
  • 業界 谷歌發布tfdbg:讓TensorFlow機器學習模型調試更簡單
    隨著 2 月 16 日谷歌開發者大會上 TensorFlow1.0 的發布,這一最流行的深度學習框架邁進了新的時代。