TensorFlow
FLOPs Calculation
Deep Learning
Model Optimization
PB File

How to calculate the flops of a tensorflow model loaded from pb file

Master System Design with Codemia

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

Introduction

If you have a frozen TensorFlow graph stored in a .pb file, the usual way to estimate FLOPs is to import the graph and run the TensorFlow profiler on it. The important caveat is that FLOPs are only meaningful when tensor shapes are known, so dynamic or missing shapes can make the count incomplete or misleading.

Load the Graph Definition

A .pb file typically stores a serialized GraphDef. You can load it into a TensorFlow graph like this:

python
1import tensorflow as tf
2
3
4def load_graph(pb_path: str) -> tf.Graph:
5    graph = tf.Graph()
6    graph_def = tf.compat.v1.GraphDef()
7
8    with open(pb_path, "rb") as f:
9        graph_def.ParseFromString(f.read())
10
11    with graph.as_default():
12        tf.import_graph_def(graph_def, name="")
13
14    return graph

This gives you a TensorFlow graph object that the profiler can inspect.

Profile Floating-Point Operations

Once the graph is loaded, use the TensorFlow v1 profiler API:

python
1import tensorflow as tf
2
3
4def calculate_flops(pb_path: str):
5    graph = load_graph(pb_path)
6
7    with graph.as_default():
8        run_meta = tf.compat.v1.RunMetadata()
9        opts = tf.compat.v1.profiler.ProfileOptionBuilder.float_operation()
10        flops = tf.compat.v1.profiler.profile(graph=graph, run_meta=run_meta, cmd="op", options=opts)
11
12    return flops.total_float_ops if flops is not None else 0
13
14
15print(calculate_flops("model.pb"))

This returns the estimated number of floating-point operations counted by the profiler for supported ops in the graph.

If the graph imports successfully and shapes are available, this is usually the most direct answer.

Why Shapes Matter

FLOPs are tied to tensor sizes. A convolution on a 224 x 224 image costs far more than the same layer on a 32 x 32 image. If the graph contains placeholders or tensors with unknown dimensions, TensorFlow may not be able to compute a useful number for every operation.

That means a FLOPs report can be:

  • correct for a fixed input shape
  • incomplete for dynamic shapes
  • misleading if the graph was frozen without the dimensions you care about

For serious comparison work, make sure the graph represents the real inference shape you plan to use.

Understand What the Number Means

A FLOPs count is an estimate of arithmetic work, not actual runtime speed. Two models with similar FLOPs can have different latency because of:

  • memory access patterns
  • operator fusion
  • device-specific kernels
  • precision mode such as FP32 versus FP16
  • framework overhead outside the core ops

So use FLOPs as one metric, not the only one.

Common Workflow for Frozen Models

A practical workflow is:

  1. load the .pb graph
  2. confirm the input and output node names
  3. verify tensor shapes
  4. profile floating-point ops
  5. compare against measured runtime on the target hardware

This avoids the common mistake of trusting the FLOPs number without checking whether the graph actually matches the deployed inference path.

Common Pitfalls

The biggest mistake is profiling a graph with unknown or wrong input shapes. FLOPs without the intended tensor dimensions are often not useful.

Another issue is assuming the profiler counts every possible operation the same way. Some ops may be skipped, fused, or represented differently than you expect.

Developers also sometimes confuse FLOPs with parameter count. Parameters measure model size, while FLOPs estimate computation.

Finally, remember that a .pb file can describe different kinds of TensorFlow graphs. If the graph is not frozen cleanly or depends on missing assets or signatures, import and profiling may need extra cleanup first.

Summary

  • Load the .pb file as a TensorFlow GraphDef and import it into a graph.
  • Use tf.compat.v1.profiler.profile with float_operation() to estimate FLOPs.
  • FLOPs are meaningful only when tensor shapes are known.
  • Treat FLOPs as a compute estimate, not a direct latency measurement.
  • Validate the graph structure and input dimensions before comparing models by FLOPs.

Course illustration
Course illustration

All Rights Reserved.