<jupyter_start><jupyter_text>To convert a TensorFlow model, tf2onnx prefers a frozen TensorFlow graph with specified inputs and outputs for the graph, alongside a source checkpoint file. More details on usage can be found in the [tf2onnx](https://github.com/onnx/tensorflow-onnxusage) repository.```python3 -m tf2onnx.convert     --input SOURCE_GRAPHDEF_PB    --graphdef SOURCE_GRAPHDEF_PB    --checkpoint SOURCE_CHECKPOINT    --saved-model SOURCE_SAVED_MODEL    [--inputs GRAPH_INPUTS]    [--outputs GRAPH_OUTPUS]    [--inputs-as-nchw inputs_provided_as_nchw]    [--target TARGET]    [--output TARGET_ONNX_GRAPH]    [--target TARGET]    [--continue_on_error]    [--verbose]    [--custom-ops list-of-custom-ops]    [--opset OPSET]    [--fold_const]```<jupyter_code># savedmodel is the name of the directory from your tf.saved_model output
# model.onnx is the name of your desired output model

!python3 -m tf2onnx.convert \
        --saved_model savedmodel \
        --output model.onnx \
        --target rs6 \
        --fold_const \
        --verbose
# model.pb is the name of your checkpoint
# graph_inputs are the names of your input nodes
# graph_outputs are the names of your output nodes 
# model.onnx is the name of your desired output model

# If you know your graph input / output node names, then you can use the freeze_graph method instead
# input and output node names can be discovered through Netron, tensorboard, or the TF summarize_graph tool

!python3 -m tf2onnx.convert \
        --input model.pb \
        --inputs GRAPH_INPUTS \
        --outputs GRAPH_OUTPUTS \
        --output model.onnx \
        --fold_const \
        --verbose<jupyter_output><empty_output><jupyter_text>Alternatively, you could use a model from an active Tensorflow session in Python, freeze the graph, and then convert it, as demonstrated below.<jupyter_code># Replace this with your desired input TF model name
input_model_name = "tf_model"

# Replace this with your desired output ONNX model name
output_onnx_model = "model.onnx"
with tf.Session() as sess:
    # Note: this is a simple example Tensorflow model
    x = tf.placeholder(tf.float32, [2, 3], name="input")
    x_ = tf.add(x, x)
    _ = tf.identity(x_, name="output")
    onnx_graph = tf2onnx.tfonnx.process_tf_graph(sess.graph, input_names=["input:0"], output_names=["output:0"])
    onnx_model = onnx_graph.make_model(input_model_name)
# Save as protobuf

onnxmltools.utils.save_model(onnx_model, output_onnx_model)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09337589784517158, 'fraction_numerical': 0.006384676775738229, 'mean_word_length': 3.5664845173041893, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 1}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '54708', 'license': 'MIT', 'n_tokens_mistral': 806, 'n_tokens_neox': 785, 'n_words': 250, 'path': 'onnx-ecosystem/converter_scripts/tf_onnx.ipynb', 'repo_name': 'pridkett/onnx-docker', 'size': '2506'}