diff options
author | A. Unique TensorFlower <gardener@tensorflow.org> | 2017-11-14 10:54:28 -0800 |
---|---|---|
committer | TensorFlower Gardener <gardener@tensorflow.org> | 2017-11-14 10:57:46 -0800 |
commit | c674e27bfd68a6c990e694b6afd901bfeeaa006d (patch) | |
tree | bf5bcf0f0432db68a2c951347d01ee03f69a4ca7 /tensorflow/contrib/lite/toco/g3doc/python_api.md | |
parent | f5669d905a28893c71ff44245da6ed5e13d55d1c (diff) |
Merged commit includes the following changes:
175703479 by yifeif:
Internal change.
--
175695370 by A. Unique TensorFlower:
Implements _shared_embedding_columns and adds some tests.
--
175695349 by A. Unique TensorFlower:
Implements tf.metrics.true_negatives, adds missing tests and does some cleanup in tf.contrib.metrics.
--
PiperOrigin-RevId: 175703479
Diffstat (limited to 'tensorflow/contrib/lite/toco/g3doc/python_api.md')
-rw-r--r-- | tensorflow/contrib/lite/toco/g3doc/python_api.md | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/tensorflow/contrib/lite/toco/g3doc/python_api.md b/tensorflow/contrib/lite/toco/g3doc/python_api.md new file mode 100644 index 0000000000..440f9c367c --- /dev/null +++ b/tensorflow/contrib/lite/toco/g3doc/python_api.md @@ -0,0 +1,62 @@ +# TensorFlow Lite Optimizing Converter (TOCO) Python API reference + +## High-level overview + +While the TensorFlow Lite Optimizing Converter can be used from the command +line, it is often convenient to use it as part of Python model build and +training script. This is so that conversion can be part of your model +development pipeline. This allows you to know early and often that you are +designing a model that can be targeted to devices with mobile. + +## API + +In Python you can run `help(tf.contrib.lite)` to get documentation on functions. +In particular, `tf.contrib.lite.toco_convert` presents a simple API and +`tf.contrib.lite.toco_from_protos` allows more detailed control of TOCO using +the protobuf interface to TOCO. + +## Example + +In particular, here we show creating a simple model and converting it to a +TensorFlow Lite Model. + +```python +import tensorflow as tf + +img = 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("test.tflite", "wb").write(tflite_modeL) +``` + +**NOTE** Currently, the TOCO command will cause a fatal error to the Python +interpreter when TOCO conversion fails. This will be remedied as soon as +possible. + +## Example 2: Export with variables + +If a model has variables, they need to be turned into constants. This process is +known as freezing, and it can actually be accomplished with + +```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 + +def canonical_name(x): + return x.name.split(":")[0] + +out = tf.identity(val, name="out") +with tf.Session() as sess: + sess.run(tf.global_variables_initializer()) + out_tensors = [out] + frozen_graphdef = tf.graph_util.convert_variables_to_constants( + sess, sess.graph_def, map(canonical_name, out_tensors)) + tflite_model = tf.contrib.lite.toco_convert( + frozen_graphdef, [img], out_tensors) + open("converted_model.tflite", "wb").write(tflite_model) +``` |