<jupyter_start><jupyter_text>PytorchIt is easy to export a Pytorch model to ONNX because it is built into the API. The Pytorch documentation provides a good example on how to perform this conversion.This is a simplified example:<jupyter_code>import numpy as np
import torch
import torchvision
dummy_input = torch.randn(1,3,224,224, device='cpu')
model = torchvision.models.alexnet(pretrained=True)
input_names = ['input_alexnet'] + ['layer_%d' % i for i in range(16)]
output_name = ['output_alexnet'] 
torch.onnx.export(model, dummy_input, 'alexnet_onnx.onnx', verbose=True, input_names= input_names, output_names=output_name)
import torch.nn as nn
import torch.nn.functional as F

class LSTM(nn.Module):
    def __init__(self, in_ts, in_ch, out_dim, p=0.0):
        super(LSTM, self).__init__()
        self.lstm1 = nn.LSTM(in_ts,  128, 1, dropout=0.3)
        self.lstm2 = nn.LSTM(128, 256, 1, dropout=0.4)
        self.fc1   = nn.Linear(in_ch*256, out_dim)
        self.drop1 = nn.Dropout(p=p//2)

    def forward(self,x):
        x = F.tanh(self.lstm1(x)[0])
        x = F.tanh(self.lstm2(x)[0])
        x = torch.flatten(x, 1)
        x = self.drop1(x)
        x = self.fc1(x)
        return x

MODEL_LSTM = LSTM(100,1,100)
print(MODEL_LSTM)
dummy_input = torch.randn(1,1,100, device='cpu')
torch.onnx.export(MODEL_LSTM, dummy_input, 'lstm_onnx.onnx')<jupyter_output>C:\Users\lospa\anaconda3\envs\dl\lib\site-packages\torch\nn\functional.py:1698: UserWarning: nn.functional.tanh is deprecated. Use torch.tanh instead.
  warnings.warn("nn.functional.tanh is deprecated. Use torch.tanh instead.")
C:\Users\lospa\anaconda3\envs\dl\lib\site-packages\torch\onnx\symbolic_opset9.py:1932: UserWarning: Exporting a model to ONNX with a batch_size other than 1, with a variable length with LSTM can cause an error when running the ONNX model with a different batch size. Make sure to save the model with a batch size of 1, or define the initial states (h0/c0) as inputs of the model. 
  warnings.warn("Exporting a model to ONNX with a batch_size other than 1, " +<jupyter_text>TensorFlowExporting a TensorFlow neural network to ONNX takes a bit longer than with Pytorch, but it is still straightforward. Tensorflow and ONNX both define their own graph format to represent to model. You can use tensorflow-onnx to export a Tensorflow model to ONNX.> Procedures to convert tensorflow model>   - get tensorflow model>    - convert to ONNX>    - validateTensorflow uses several file formats to represent a model, such as checkpoint files, graph with weight(called frozen graph next) and saved_model, and it has APIs to generate these files. And tensorflow-onnx can accept all the three formats to represent a Tensorflow model, the format "saved_model" should be the preference since it doesn't require the user to specify input and output names of graph.*Resources:*https://github.com/onnx/tutorials<jupyter_code>import os
import tensorflow as tf
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
import onnxruntime

x = np.random.randn(1,224,224,3).astype(np.float32)
x = preprocess_input(x)
print(x.dtype)
model = ResNet50(weights='imagenet')
preds = model.predict(x)
print('Keras Predicted:', decode_predictions(preds, top=3)[0])
model.save('resnet_model')
import tf2onnx
import onnxruntime as rt

spec = (tf.TensorSpec((None, 224, 224, 3), tf.float32, name="input"),)
output_path = "resnet_model.onnx"

model_proto, _ = tf2onnx.convert.from_keras(model, input_signature=spec, opset=13, output_path=output_path)
output_names = [n.name for n in model_proto.graph.output]
providers = ['CPUExecutionProvider']
m = rt.InferenceSession(output_path, providers=providers)
onnx_pred = m.run(output_names, {"input": x})

print('ONNX Predicted:', decode_predictions(onnx_pred[0], top=3)[0])

# make sure ONNX and keras have the same results
np.testing.assert_allclose(preds, onnx_pred[0], rtol=1e-5)
# python -m tf2onnx.convert --opset 13 --saved-model {"resnet_model"} --output  {"resnet_model.onnx"}<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10597628841035567, 'fraction_numerical': 0.027098959593515607, 'mean_word_length': 4.948201438848921, 'pattern_counts': {'":': 1, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '202921', 'license': 'MIT', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1408, 'n_words': 411, 'path': 'Tensorflow_Pytorch_ONNX.ipynb', 'repo_name': 'patoalejor/Workshop2021', 'size': '4133'}