Converting tensorflow saved model to model.graphdef

Sanpreet Singh
2 min readJun 25, 2019

--

Converting tensorflow saved model to model.graphdef
Converting tensorflow saved model to model.graphdef

Introduction

To serve the tensorflow saved model with the TensorRT for fast inference, it is appropriate to convert checkpoints and other files such as meta, index and data to model.graphdef. If readers are facing any problem for understanding of these file, they would gain such vital (essential) information by reading from this article

Freezing tensorflow model

I am writing this article keeping in mind model is saved using tensorflow deep learning frame work. If you wish to use Pytorch, Caffe2, ONNX models. I would suggest user to please see the below for Framework Model Definition

model.plan for TensorRT models
model.graphdef for TensorFlow GraphDef models
model.savedmodel for TensorFlow SavedModel models
model.netdef and init_model.netdef for Caffe2 Netdef models
model.onnx for ONNX Runtime ONNX models
model.pt for PyTorch TorchScript models

I would also request readers that if they want to go for ample (more than enough) knowledge on “Should I go for TensorFlow or PyTorch?” they can read from the below link

https://qr.ae/TWtRxX

Code to convert tensorflow saved model to model.graphdef

import tensorflow as tf
import os
from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io
saver = tf.train.import_meta_graph('./simple_model.meta',
clear_devices=True
)
graph = tf.get_default_graph()
input_graph_def = graph.as_graph_def()
sess = tf.Session()
saver.restore(sess, "./simple_model")
output_node_names="init"
output_graph_def = graph_util.convert_variables_to_constants(
sess,
input_graph_def,
output_node_names.split(",")
)
# for getting the current path
path = os.path.dirname(os.path.realpath(__file__))
print("Please show the path.", path)
graph_io.write_graph(graph_or_graph_def=output_graph_def,
logdir=path + '/1',
name='model.graphdef',
as_text=False)

Important Note:
This code is in the continuation of the article

Freezing tensorflow model

So I would suggest you please read this article so that you can get tensorflow saved model and one can easily play with this code, otherwise this code will not run. Write the queries or any feedback in the comment section. Happy to serve you.

Originally published at http://ersanpreet.wordpress.com on June 25, 2019.

--

--

No responses yet