onnxruntime/docs/tutorials/tf-get-started.md
Cassie a0f3e30de6
Docs update: updated nav, get started sections, home page, apis (#9060)
* initial setup and rename "how to" to "setup"

* move API to main nav

* move api to main nav

* add get starated, rework nav order

* rename to install move mds out of install section

* update api nav and home page

* add install docs and python qs updates

* python get started work

* remove c and obj c for now

* move java, python, and obj-c docs under api folder

* move java api html to iframe (ugh)

* remove api docs w/o details, move api text getstar

* remove api docs wo detail updates get started

* remvoe iframes

* move eco system to main nav

* fix api buttons

* added more examples moved intro to ORT

* fix links

* fix get started titles

* fix get started titles

* fix more links

* fix more links

* more link fixes

* fix nav remove inferencing and training subnav

* fix top nav remove inference and training nav

* fix title

* fix tutorials nav hierarchy

* fix python api button

* add tenorflow keras example

* fix quickstart toc

* add imports fix spacing

* fix links

* update nav and python get started page

* move ort training example, add coming soon for iot

* update C# get started

* fix spacing on quantization

* Add some js get started content

* fix formatting

* fix typo

* removed onnx-pytorch and onnx-tf

* updated pip install torch and added links iot page

* added pytorch tutorial heirarchy

* updated web to docs soon added release blog link

* add web link
2021-09-15 16:23:42 -05:00

103 lines
3.4 KiB
Markdown

---
title: Getting Started - TensorFlow
nav_exclude: true
parent: Accelerate TensorFlow
grand_parent: Inferencing
---
# Getting Started Converting TensorFlow to ONNX
TensorFlow models (including keras and TFLite models) can be converted to ONNX using the [tf2onnx](https://github.com/onnx/tensorflow-onnx) tool.
Full code for this tutorial is available [here](https://github.com/onnx/tensorflow-onnx/blob/master/examples/getting_started.py).
## Installation
First install tf2onnx in a python environment that already has TensorFlow installed.
`pip install tf2onnx` (stable)
**OR**
`pip install git+https://github.com/onnx/tensorflow-onnx` (latest from GitHub)
## Converting a Model
### Keras models and tf functions
Keras models and tf functions and can be converted directly within python:
```python
import tensorflow as tf
import tf2onnx
import onnx
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(4, activation="relu"))
input_signature = [tf.TensorSpec([3, 3], tf.float32, name='x')]
# Use from_function for tf functions
onnx_model, _ = tf2onnx.convert.from_keras(model, input_signature, opset=13)
onnx.save(onnx_model, "dst/path/model.onnx")
```
See the [Python API Reference](https://github.com/onnx/tensorflow-onnx#python-api-reference) for full documentation.
### SavedModel
Convert a TensorFlow saved model with the command:
`python -m tf2onnx.convert --saved-model path/to/savedmodel --output dst/path/model.onnx --opset 13`
`path/to/savedmodel` should be the **path to the directory containing** `saved_model.pb`
See the [CLI Reference](https://github.com/onnx/tensorflow-onnx#cli-reference) for full documentation.
### TFLite
tf2onnx has support for converting tflite models.
`python -m tf2onnx.convert --tflite path/to/model.tflite --output dst/path/model.onnx --opset 13`
### NOTE: Opset number
Some TensorFlow ops will fail to convert if the ONNX opset used is too low. **Use the largest opset compatible with your application.** For full conversion instructions, please refer to the [tf2onnx README](https://github.com/onnx/tensorflow-onnx#cli-reference).
## Verifying a Converted Model
Install onnxruntime with:
`pip install onnxruntime`
Test your model in python using the template below:
```python
import onnxruntime as ort
import numpy as np
# Change shapes and types to match model
input1 = np.zeros((1, 100, 100, 3), np.float32)
sess = ort.InferenceSession("dst/path/model.onnx")
# Set first argument of sess.run to None to use all model outputs in default order
# Input/output names are printed by the CLI and can be set with --rename-inputs and --rename-outputs
# If using the python API, names are determined from function arg names or TensorSpec names.
results_ort = sess.run(["output1", "output2"], {"input1": input1})
import tensorflow as tf
model = tf.saved_model.load("path/to/savedmodel")
results_tf = model(input1)
for ort_res, tf_res in zip(results_ort, results_tf):
np.testing.assert_allclose(ort_res, tf_res, rtol=1e-5, atol=1e-5)
print("Results match")
```
## Conversion Failures
If your model fails to convert please read our [README](https://github.com/onnx/tensorflow-onnx#readme) and [Troubleshooting guide](https://github.com/onnx/tensorflow-onnx/blob/master/Troubleshooting.md). If that fails feel free to [open an issue on GitHub](https://github.com/onnx/tensorflow-onnx/issues). Contributions to tf2onnx are welcome!
## Next Steps
- [More tutorials: accelerate Tensorflow models](../tensorflow)