Tensorflow Lite之編譯產生tflite檔案

來源:互聯網
上載者:User
這是tensorflow產生的各種模型檔案: GraphDef (.pb) - a protobuf that represents the TensorFlow training and or computation graph. This contains operators, tensors, and variables definitions. CheckPoint (.ckpt) - Serialized variables from a TensorFlow graph. Note, this does not contain the graph structure, so alone it cannot typically be interpreted. FrozenGraphDef - a subclass of GraphDef that contains no variables. A GraphDef can be converted to a frozen graphdef by taking a checkpoint and a graphdef and converting every variable into a constant with the value looked up in the checkpoint. SavedModel - A collection of GraphDef and CheckPoint together with a signature that labels input and output arguments to a model. A GraphDef and Checkpoint can be extracted from a saved model. TensorFlow lite model (.lite) - a serialized flatbuffer, containing TensorFlow lite operators and Tensors for the TensorFlow lite interpreter. This is most analogous to TensorFlow frozen GraphDefs. 其中關注的主要三種檔案格式: .pb檔案,儲存的是圖模型的計算流程圖,包括圖中的常量,但不儲存變數,可通過以下兩個方法擷取: (1):tf.train.write_graph(sess.graph_def,'','graph.pb',as_text=False) #直接儲存圖模型,但沒有圖中變數的值 (2):graph = convert_variables_to_constants(sess, sess.graph_def, ["output_image"]) tf.train.write_graph(graph, '.', 'graph.pb', as_text=False) #這樣通過將模型裡面的所有變數都變為常量,那麼就可以直接使用.pb檔案做成介面,無需.ckpt檔案再次匯入變數的值.
.ckpt檔案,儲存的是圖模型中的變數的值,要使用.ckpt檔案的話,要重構圖的結構和初始化圖中變數.可通過以下方式擷取: saver=tf.train.Saver() saver.save(sess,"model.ckpt")
.lite檔案:裡面是包含圖模型的計算流程圖和圖模型中的變數的值,可以直接給android系統或者ios系統的tensorflowLite調用讀取.
接下來是產生.lite檔案的方法: 首先是,產生lite檔案支援的操作和不支援的操作:(如若圖中有不支援的操作,將在產生.lite檔案時會報錯) https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/g3doc/tf_ops_compatibility.md
產生.lite檔案有兩種方式: 第一種,是通過.pb檔案和.ckpt檔案,進行圖中的變數的固化,再產生.lite檔案.執行方法如下 (1).要先安裝tensorflow源碼和bazel方法,編譯tensorflow源碼產生tensorflow (2).cd到源碼目錄下: (3).bazel build tensorflow/python/tools:freeze_graph #執行產生一個固化的指令碼.每次執行下一步之前都要執行這一步. (4).bazel-bin/tensorflow/python/tools/freeze_graph --input_graph=graph.pb --input_checkpoint=model.ckpt --input_binary=true --output_graph=graphRes.pb --output_node_names=output #--output_node_names 對應的是輸出tensor的name (5).bazel run --config=opt tensorflow/contrib/lite/toco:toco -- --input_file=graphRes.pb --input_format=TENSORFLOW_GRAPHDEF --output_format=TFLITE --output_file=model.lite --inference_type=FLOAT --input_type=FLOAT --input_arrays=input_image --output_arrays=output --input_shapes=1,256,256,3 --allow_custom_ops # --input_arrays 和 --output_arrays 對應的是輸入輸出tensor的name 注意--input_shapes 必須確定,不可以填None --allow_custom_ops 是允許一些傳統方法 可參考: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/toco/g3doc/cmdline_examples.md 裡麵包含了混合輸入等多種方法.
第二種.是直接在代碼中通過代碼直接產生.lite檔案. 如果途中沒有變數 import tensorflow as tfimg = tf.placeholder(name="img", dtype=tf.float32, shape=(1, 64, 64, 3))val = img + tf.constant([1., 2., 3.]) + tf.constant([1., 4., 4.])out = tf.identity(val, name="out")with tf.Session() as sess: tflite_model = tf.contrib.lite.toco_convert(sess.graph_def, [img], [out]) open("converteds_model.tflite", "wb").write(tflite_model) 如果圖中有變數的話,需要將變數固化 frozen_graphdef = tf.graph_util.convert_variables_to_constants(sess, sess.graph_def, ['output']) #這裡 ['output']是輸出tensor的名字 tflite_model = tf.contrib.lite.toco_convert(frozen_graphdef, [input], [out]) #這裡[input], [out]這裡分別是輸入tensor或者輸出tensor的集合,是變數實體不是名字 open("model.tflite", "wb").write(tflite_model) 可參考 https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/toco/g3doc/python_api.md

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.