How to convert a frozen TensorFlow model to ONNX model ready for Windows ML

In Artificial Intelligence, Mobile Development by Christian HissibiniLeave a Comment

Here you will find an example of how to convert a model from a frozen tensorflow model by using WinMLTools. To get possible output names of a tensorflow model, you can use summarize_graph tool

WinMLTools enables you to convert ML models created with different training frameworks into ONNX. It is extension of ONNXMLTools and TF2ONNX to convert models to ONNX for use with Windows ML.

import winmltools
import tensorflow

filename = 'frozen-model.pb'
output_names = ['output:0']

graph_def = graph_pb2.GraphDef()
with open(filename, 'rb') as file:
  graph_def.ParseFromString(file.read())
g = tf.import_graph_def(graph_def, name='')

with tf.Session(graph=g) as sess:
  converted_model = winmltools.convert_tensorflow(sess.graph, 7, output_names=['output:0'])
  winmltools.save_model(converted_model)

Leave a Comment