TensorFlow
Python
C++
Machine Learning
Model Export

Export Python TensorFlow model and import it in C

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Exporting a TensorFlow model from Python and using it in C is mainly an interface problem, not a modeling problem. The typical workflow is to save the model in a standard format from Python, inspect the exported input and output signatures, and then load that artifact from C through the TensorFlow C API or, for lighter deployments, TensorFlow Lite.

Export the Model from Python

The most common full TensorFlow export format is SavedModel. Here is a minimal Python example:

python
1import tensorflow as tf
2
3class DoubleModel(tf.Module):
4    @tf.function(input_signature=[tf.TensorSpec(shape=[None, 1], dtype=tf.float32)])
5    def __call__(self, x):
6        return {"output": x * 2.0}
7
8model = DoubleModel()
9tf.saved_model.save(model, "saved_model")

This creates a saved_model directory containing the graph, variables, and serving metadata. The important part is the input signature. If you do not define it clearly, using the model from C becomes much harder because tensor shapes and entry points are less obvious.

Inspect the Exported Signature

Before writing C code, inspect the exported model so you know the signature names:

bash
saved_model_cli show --dir saved_model --all

This reveals the serving signatures, input tensor names, output tensor names, and expected shapes. Do not guess these names in C. Read them from the exported model first.

Load the Model from C with the TensorFlow C API

A minimal load step in C looks like this:

c
1#include <stdio.h>
2#include <tensorflow/c/c_api.h>
3
4int main() {
5    TF_Status* status = TF_NewStatus();
6    TF_Graph* graph = TF_NewGraph();
7    TF_SessionOptions* session_opts = TF_NewSessionOptions();
8    const char* tags[] = {"serve"};
9
10    TF_Session* session = TF_LoadSessionFromSavedModel(
11        session_opts,
12        NULL,
13        "saved_model",
14        tags,
15        1,
16        graph,
17        NULL,
18        status
19    );
20
21    if (TF_GetCode(status) != TF_OK) {
22        fprintf(stderr, "Load failed: %s\n", TF_Message(status));
23        return 1;
24    }
25
26    printf("Model loaded successfully\n");
27
28    TF_CloseSession(session, status);
29    TF_DeleteSession(session, status);
30    TF_DeleteSessionOptions(session_opts);
31    TF_DeleteGraph(graph);
32    TF_DeleteStatus(status);
33    return 0;
34}

This does not run inference yet, but it proves the export and load path is valid. The next step is creating input tensors, looking up the input and output operations by name, and calling TF_SessionRun.

Know When TensorFlow Lite Is Simpler

If your target is plain C in a lightweight application, TensorFlow Lite is often a better deployment path than the full TensorFlow runtime. It has a smaller footprint and a simpler inference story. The tradeoff is that not every TensorFlow model converts cleanly, so the right choice depends on your model and runtime constraints.

As a rule:

  • use full SavedModel plus the TensorFlow C API when you need broader TensorFlow runtime support
  • consider TensorFlow Lite when the goal is compact inference in C on constrained targets

Common Pitfalls

The biggest mistake is exporting a model from Python without checking the signature names and then guessing tensor names in C. That usually leads to frustrating runtime failures.

Another issue is forgetting that preprocessing must match exactly. If the Python code normalizes inputs before inference, the C side must do the same or the predictions will be wrong even if the model loads successfully.

Developers also often underestimate the build and linking work required for the TensorFlow C runtime. Loading the model is only one part of the integration task.

Finally, if plain C is a hard requirement, make sure you choose a deployment format that matches that requirement early instead of exporting one format and discovering too late that the runtime is too heavy.

Summary

  • Export the model from Python in a standard format such as SavedModel.
  • Inspect the exported signatures before writing the C integration.
  • Load the model in C through the TensorFlow C API when full TensorFlow runtime support is needed.
  • Consider TensorFlow Lite when the deployment target is smaller and pure inference-focused.
  • Keep preprocessing and tensor names aligned between Python and C.

Course illustration
Course illustration

All Rights Reserved.