Part-III : Convert Keras Model to TensorFlow frozen graph model

4 Part blog series : Documenting the learnings of my experimentation on Object Detection using Keras, Tensorflow

Karan Singh
2 min readMar 28, 2021

In part-I we learned the process of using a pre-trained keras model with separate model weight and mode configuration files. As a follow-on to that in part-II, we discussed about saving the model weight and configuration files into a single hdf5 format keras model file.

In this blog post, we will learn how to convert a keras model file into a TensorFlow (2.x) frozen graph model format (*.pb) and its text representation (*.pbtxt) .

Introduction

TF Saved Model Format : saved_model.pb may represent multiple graph definitions as MetaGraphDef protocol buffers. Weights and other variables usually aren’t stored inside the file during training. Instead, they’re held in separate checkpoint files

TF Frozen Graph Model : has its variables converted into inline constants so everything’s in one file and ready for serving on any platform including mobile. Freezing process includes loading the GraphDef, pull in the values for all the variables from the latest checkpoint file, and then replace each Variable op with a Const that has the numerical data for the weights stored in its attributes It then strips away all the extraneous nodes that aren’t used for forward inference. Reference: Rohit Sharma

Implementation

If you like to follow along, refer to 3_convert_keras_model_to_tensorflow_frozen_graph.ipynb jupyter notebook from my repository. Below are the implementation steps, the following program loads up keras model files and convert that to tensorflow frozen graph / text representation file format *.pb and *.pbtxt

  • Once keras model is converted to TF format, list the frozen_models directory. Here you will find the files simple_frozen_graph.pb and simple_frozen_graph.pbtxt
  • Finally, let’s use this newly created TensorFlow model file and try to do inferencing (detect license plate).
  • You should get results like below

Summary

  • In this blog, we have learned how to converted keras h5 model file to tensorflow frozen model and its text representation
  • We have validated that the newly created TF frozen graph model works by detecting license plate from input image

--

--

Karan Singh

Co-Founder & CTO @ Scogo ♦ I Love to solve problems using Tech