I think the ONNX file i.e. model.onnx
that you have given is corrupted I don't know what is the issue but it is not doing any inference on ONNX runtime.
Now you can run PyTorch Models directly on mobile phones. check out PyTorch Mobile's documentation here
This answer is for TensorFlow version 1,
For TensorFlow version 2 or higher click link
The best way to convert the model from protobuf freezeGraph to TFlite is to use the official TensorFlow lite converter documentation
According to TensorFlow Docs, TocoConverter has been deprecated
This class (tf.compat.v1.lite.TocoConverter) has been deprecated. Please use lite.TFLiteConverter instead.
Convert from PyTorch to ONNX model
The best practice to convert the model from Pytorch to Onnx is that you should add the following parameters to specify the names of the input and output layer of your model in torch.onnx.export() function
# Export the model from PyTorch to ONNX
torch_out = torch.onnx._export(model, # model being run
x, # model input (or a tuple for multiple inputs)
EXPORT_PATH + "mnist.onnx", # where to save the model (can be a file or file-like object)
export_params=True, # store the trained parameter weights inside the model file
input_names=['main_input'], # specify the name of input layer in onnx model
output_names=['main_output']) # specify the name of input layer in onnx model
So in your case:
Now export this model to TensorFlow protobuf FreezeGraph using onnx-tf
Please note that this method is only working when tensorflow_version < 2
Convert from ONNX to TensorFlow freezGraph
To convert the model please install onnx-tf version 1.5.0 from the below command
pip install onnx-tf==1.5.0
Now to convert .onnx model to TensorFlow freeze graph run this below command in shell
onnx-tf convert -i "mnist.onnx" -o "mnist.pb"
Convert from TensorFlow FreezeGraph .pb to TF
Now to convert this model from .pb file to tflite model use this code
import tensorflow as tf
# make a converter object from the saved tensorflow file
converter = tf.lite.TFLiteConverter.from_frozen_graph('mnist.pb', #TensorFlow freezegraph .pb model file
input_arrays=['main_input'], # name of input arrays as defined in torch.onnx.export function before.
output_arrays=['main_output'] # name of output arrays defined in torch.onnx.export function before.
)
# tell converter which type of optimization techniques to use
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# to view the best option for optimization read documentation of tflite about optimization
# go to this link https://www.tensorflow.org/lite/guide/get_started#4_optimize_your_model_optional
# convert the model
tf_lite_model = converter.convert()
# save the converted model
open('mnist.tflite', 'wb').write(tf_lite_model)
To choose which option is best for optimization for your model use case see this official guide about TensorFlow lite optimization
https://www.tensorflow.org/lite/guide/get_started#4_optimize_your_model_optional
Note: You can try my Jupyter Notebook Convert ONNX model to Tensorflow Lite on Google Colaboratory link