Surya Sharma

Machine Learning Applications | Computer Vision | This website may be my recipe book.

Use a frozen Tensorflow (keras) graph for predictions in PYTHON

This guide is a part of a series of articles that show you how to save a Keras model and use it for predictions in a Visual Studio C program.

In this guide we’ll use a frozen Keras (Tensorflow) graph to make predictions. As a note, this works only for graphs that are frozen with their weights. If the weights are not a part of the file, you will also have to load a checkpoint file (not covered in this guide). Additionally, make sure you don’t reset the graph after loading a file, otherwise your predictions won’t work.

Load the dataset and normalize

You will need the slope-dataset and the model saved in the previous guide (slopemodel.pb). Go ahead and load the dataset again (assuming you’re starting in a new file).

# imports
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Load the data
df = pd.read_csv("slope-lownoise.txt", sep='\t', header=None)
labels = np.asarray(df[0]).reshape((len(df[0]),-1))
data = np.asarray(df.iloc[:,1:])
# Check data
print(labels.shape)
print(data.shape)
# Normalize segments
normdata = np.empty_like(data).astype('float64')
for row in range(len(data)):
normdata[row] = (data[row] - min(data[row])) / (max(data[row]) - min(data[row]))
normdata[1]

We’re also going to pick a single sample to classify, this is the first sample i.e. normdata[1], which is a rising series (see above), and has a ground truth label label[1] = 1. This sample is forced into a (1,21) shape because tensorflow is expecting an input of shape (?, 21). Without specifying this parameter, this shape assigned would be (21,) which tensorflow doesn’t like.

sample = normdata[1].reshape((1,21))

Load the GRAPH

Lets load the .pb file, and read a list of all the operations available.

from tensorflow.python.platform import gfile
with tf.Session() as sess:
with gfile.FastGFile('slopemodel/slopemodel.pb', 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
sess.graph.as_default()
g_in = tf.import_graph_def(graph_def)
for op in tf.get_default_graph().get_operations():
print(str(op.name))
for n in tf.get_default_graph().as_graph_def().node:
print(n.name)
view raw TFgraph_list.py hosted with ❤ by GitHub

This should give us a long list of operations. I’ve listed some of them here:

import/dense_1_input
import/dense_1/kernel
import/dense_1/kernel/read
import/dense_1/bias
import/dense_1/bias/read
import/dense_1/MatMul
import/dense_1/BiasAdd
import/dense_1/Relu
import/dense_2/kernel
import/dense_2/kernel/read
import/dense_2/bias
import/dense_2/bias/read
import/dense_2/MatMul
import/dense_2/BiasAdd
import/dense_2/Sigmoid
import/Adam/iterations
import/Adam/lr
import/Adam/beta_1
import/Adam/beta_2
.
.
.

Call a prediction

Note these are operations. Tensorflow operates on graphs, so the code defines them differently. For prediction we want the output of the last layer, i.e. Sigmoid, so we’ll call the output tensor as : import/dense_2/Sigmoid:0

from tensorflow.python.platform import gfile
with tf.Session() as sess:
with gfile.FastGFile('slopemodel/slopemodel.pb', 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
sess.graph.as_default()
g_in = tf.import_graph_def(graph_def)
tensor_output = sess.graph.get_tensor_by_name('import/dense_2/Sigmoid:0')
tensor_input = sess.graph.get_tensor_by_name('import/dense_1_input:0')
predictions = sess.run(tensor_output, {tensor_input:sample})
print(predictions)

This should give you a prediction on the sample that was provided as input, and the output should be: [[0.698449]]

Wait a minute…

Shouldn’t the prediction be 1 (rising) or 0 (falling)? Yes and no.
The prediction should be 1, but it isn’t. What we’re seeing is the output of the sigmoid function, which is the probability of the class being 1. Keras has two functions, predict, and predict_label. What you’re getting is the output of the predict() function.

How do you get a label?

You predict the label yourself. Thankfully, predicting the label from a probability in the case of a binary classification problem is easy to implement, return a 1 if the value is greater than 0.5, otherwise return a 0:

int(predictions > 0.5)

This line of code first gets a True or False value, then converts the returned value (True or False) to a number (0 or 1) so that we can use that label.

Moving on…

Now that we know how to load a graph and run a prediction using that graph, we’re ready to do the same thing using libtensorflow and C. But first, we must get libtensorflow, create a .lib file, and setup our Visual Studio environment.

Next Post

Previous Post

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2026 Surya Sharma

Theme by Anders Norén