aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/toco/g3doc/python_api.md
diff options
context:
space:
mode:
authorGravatar Nupur Garg <nupurgarg@google.com>2018-05-30 17:54:02 -0700
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-05-30 17:56:47 -0700
commit316549d36f6ab3d250ce9e33b768bbfb1a4d7362 (patch)
treecef32a4c8ace3dedac532c14fd39944d5bc4ed2b /tensorflow/contrib/lite/toco/g3doc/python_api.md
parent2a484497062677f5cf0205ee3b9c28a64f03fe04 (diff)
Enable TOCO pip command line binding.
PiperOrigin-RevId: 198649827
Diffstat (limited to 'tensorflow/contrib/lite/toco/g3doc/python_api.md')
-rw-r--r--tensorflow/contrib/lite/toco/g3doc/python_api.md49
1 files changed, 26 insertions, 23 deletions
diff --git a/tensorflow/contrib/lite/toco/g3doc/python_api.md b/tensorflow/contrib/lite/toco/g3doc/python_api.md
index 29a83bd26f..e5f6a0b500 100644
--- a/tensorflow/contrib/lite/toco/g3doc/python_api.md
+++ b/tensorflow/contrib/lite/toco/g3doc/python_api.md
@@ -12,8 +12,8 @@ Table of contents:
* [High-level overview](#high-level-overview)
* [API](#api)
* [Basic examples](#basic)
- * [Exporting a GraphDef with constants](#basic-graphdef-const)
- * [Exporting a GraphDef with variables](#basic-graphdef-var)
+ * [Exporting a GraphDef from tf.Session](#basic-graphdef-sess)
+ * [Exporting a GraphDef from file](#basic-graphdef-file)
* [Exporting a SavedModel](#basic-savedmodel)
* [Complex examples](#complex)
* [Exporting a quantized GraphDef](#complex-quant)
@@ -50,17 +50,17 @@ possible.
The following section shows examples of how to convert a basic float-point model
from each of the supported data formats into a TensorFlow Lite FlatBuffers.
-### Exporting a GraphDef with constants <a name="basic-graphdef-const"></a>
+### Exporting a GraphDef from tf.Session <a name="basic-graphdef-sess"></a>
-The following example shows how to convert a TensorFlow GraphDef with constants
-into a TensorFlow Lite FlatBuffer.
+The following example shows how to convert a TensorFlow GraphDef into a
+TensorFlow Lite FlatBuffer from a `tf.Session` object.
```python
import tensorflow as tf
img = tf.placeholder(name="img", dtype=tf.float32, shape=(1, 64, 64, 3))
-const = tf.constant([1., 2., 3.]) + tf.constant([1., 4., 4.])
-val = img + const
+var = tf.get_variable("weights", dtype=tf.float32, shape=(1, 64, 64, 3))
+val = img + var
out = tf.identity(val, name="out")
with tf.Session() as sess:
@@ -69,25 +69,28 @@ with tf.Session() as sess:
open("converted_model.tflite", "wb").write(tflite_model)
```
-### Exporting a GraphDef with variables <a name="basic-graphdef-var"></a>
+### Exporting a GraphDef from file <a name="basic-graphdef-file"></a>
-If a model has variables, they need to be turned into constants through a
-process known as freezing. It can be accomplished by setting `freeze_variables`
-to `True` as shown in the example below.
+The following example shows how to convert a TensorFlow GraphDef into a
+TensorFlow Lite FlatBuffer when the GraphDef is stored in a file. Both `.pb` and
+`.pbtxt` files are accepted.
+
+The example uses
+[Mobilenet_1.0_224](https://storage.googleapis.com/download.tensorflow.org/models/mobilenet_v1_1.0_224_frozen.tgz).
+The function only supports GraphDefs frozen via
+[freeze_graph.py](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py).
```python
import tensorflow as tf
-img = tf.placeholder(name="img", dtype=tf.float32, shape=(1, 64, 64, 3))
-var = tf.get_variable("weights", dtype=tf.float32, shape=(1, 64, 64, 3))
-val = img + var
-out = tf.identity(val, name="out")
+graph_def_file = "/path/to/Downloads/mobilenet_v1_1.0_224/frozen_graph.pb"
+input_arrays = ["input"]
+output_arrays = ["MobilenetV1/Predictions/Softmax"]
-with tf.Session() as sess:
- converter = tf.contrib.lite.TocoConverter.from_session(sess, [img], [out],
- freeze_variables=True)
- tflite_model = converter.convert()
- open("converted_model.tflite", "wb").write(tflite_model)
+converter = tf.contrib.lite.TocoConverter.from_flatbuffer_file(
+ graph_def_file, input_arrays, output_arrays)
+tflite_model = converter.convert()
+open("converted_model.tflite", "wb").write(tflite_model)
```
### Exporting a SavedModel <a name="basic-savedmodel"></a>
@@ -111,8 +114,8 @@ available by running `help(tf.contrib.lite.TocoConverter)`.
## Complex examples <a name="complex"></a>
For models where the default value of the attributes is not sufficient, the
-variables values should be set before calling `convert()`. In order to call any
-constants use `tf.contrib.lite.constants.<CONSTANT_NAME>` as seen below with
+attribute's values should be set before calling `convert()`. In order to call
+any constants use `tf.contrib.lite.constants.<CONSTANT_NAME>` as seen below with
`QUANTIZED_UINT8`. Run `help(tf.contrib.lite.TocoConverter)` in the Python
terminal for detailed documentation on the attributes.
@@ -135,7 +138,7 @@ out = tf.fake_quant_with_min_max_args(val, min=0., max=1., name="output")
with tf.Session() as sess:
converter = tf.contrib.lite.TocoConverter.from_session(sess, [img], [out])
converter.inference_type = tf.contrib.lite.constants.QUANTIZED_UINT8
- converter.quantized_input_stats = [(0., 1.)] # mean, std_dev
+ converter.quantized_input_stats = {"img" : (0., 1.)} # mean, std_dev
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
```