TF Model Conversion to TF Lite (Part 2)

Learn to convert TF models obtained from concrete functions to the TF Lite format.

Eager and graph execution

In TF2, eager execution is the default mode instead of graph executionEager_and_Graph_Execution. However, we can use the tf.function to run our function code inside a graph. The tf.function converts regular code to callable TF graph functions to make a Python-independent dataflow graph tf.Graph from our regular code. These graph data structures contain tf.Operation and tf.Tensor objects. We can save and run tf.Graph data structures without the original Python code. This makes graphs machine-independent. TF uses graphs as a format for SavedModel.

What is a concrete function?

Functions in TF are polymorphic, so they can work with inputs of different shapes and data types. Each time we call a TF function with new input shapes and types, it creates a new concrete function with its unique graph specific to the given input combination (also known as input signature). The specifications of the input type are known as the input signature.

In Python, a wrapper function (or a function decorator) extends a function’s behavior without modifying its original functionality. A concrete function is a wrapper function that allows us to execute graphs in the eager mode of TF. A tf.Graph is specific to the inputs of a specific size and dtype. When we use tf.function to run our function with different input sizes and data types, it creates a new tf.Graph specific to the new inputs.

The @tf.function

The tf.function stores tf.Graph corresponding to the input signature in a concrete function that’s a wrapper around the tf.Graph. The @tf.function decorator applies the tf.function transformation to a Python function. When we call a TF function with an already used input signature, it reuses the already generated concrete function.

Get hands-on with 1400+ tech skills courses.