TensorFlow Lite Build Tflite file

Source: Internet
Author: User
Tags unsupported
This is the various model files generated by 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. This is does not contain the graph structure, so alone it cannot typically is interpreted. Frozengraphdef-a subclass of Graphdef that contains no variables. A graphdef can is converted to a frozen graphdef by taking a checkpoint and a graphdef and converting every A constant with the value looked up in the checkpoint. Savedmodel-a collection of Graphdef and CheckPoint together with a signature so 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 TENSORFL OW Lite interpreter. This is most analogous to TensorFlow frozen graphdefs. Among the main three file formats:. pb files, savedIs the diagram model's calculation flowchart, including the constants in the diagram, but does not save the variable, can be obtained by the following two methods: (1): Tf.train.write_graph (Sess.graph_def, ', ' GRAPH.PB ', As_text=false) # Save the diagram model directly, but not the value of the variable in the diagram (2): graph = convert_variables_to_constants (Sess, Sess.graph_def, ["Output_image"]) Tf.train.write_ Graph (graph, '. ', ' GRAPH.PB ', as_text=false) #这样通过将模型里面的所有变量都变为常量, you can use the. pb file directly as an interface without the. ckpt file to import the value of the variable again.
The. ckpt file, which holds the values of the variables in the diagram model, to use the. ckpt file, to refactor the structure of the composition and the variables in the initialization diagram. Can be obtained by: Saver=tf.train.saver () Saver.save (Sess, "model.ckpt ")
. Lite file: The value of a variable in the calculation flowchart and the diagram model that contains the diagram model can be read directly to the Android system or the Tensorflowlite call to the iOS system.
The next step is to generate a. lite file: First, generate Lite file-supported operations and unsupported operations: (if there are unsupported operations in the diagram, an error will be generated when generating the. lite file) https://github.com/tensorflow/ Tensorflow/blob/master/tensorflow/contrib/lite/g3doc/tf_ops_compatibility.md
There are two ways to build a. lite file: The first is to use the. pb file and the. ckpt file to solidify the variables in the diagram and then generate the. lite file. The following methods are implemented (1). To install the TensorFlow source code and the Bazel method first, Compile TensorFlow source code generation TensorFlow (2). CD to Source directory: (3). Bazel Build Tensorflow/python/tools:freeze_graph # Execute to generate a cured script. This step is performed each time the next steps are performed. (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 corresponds to the name (5) of the output tensor. 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 and--output_arrays correspond to the input and output tensor name note--input_shapes must determine that the none can be filled in--allow_custom_ops is allowed some traditional methods to refer to: https:// Github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/toco/g3doc/cmdline_examples.md It contains mixed input and many other methods.
The second type is to directly generate the. lite file directly in your code. If there are no variables on the way import tensorflow as Tfimg = Tf.placeholder (name= "img", Dtype=tf.float32, shape= (1 , 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) if there are variables in the diagram, you need to solidify the variable frozen_graphdef = tf.graph_util.convert_variables_to_ Constants (Sess, Sess.graph_def, [' Output ']) #这里 [' Output '] is the name of the output tensor Tflite_model = Tf.contrib.lite.toco_convert ( Frozen_graphdef, [input], [out]) #这里 [input], [out] here is the input tensor or output tensor set, is the variable entity is not the name open ("Model.tflite", "WB"). Write (Tflite_model) can refer to https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/lite/toco/g3doc/ Python_api.md

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.