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:
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:
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:
- load the
.pbgraph - confirm the input and output node names
- verify tensor shapes
- profile floating-point ops
- 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
.pbfile as a TensorFlowGraphDefand import it into a graph. - Use
tf.compat.v1.profiler.profilewithfloat_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.

