如何获取TensorFlow保存模型中BatchNorm层的epsilon值?
Retrieving Epsilon from BatchNorm Layers in a Frozen TensorFlow .pb Model
Got it, I’ve run into this exact issue before when working with pre-trained frozen .pb models in TensorFlow—since these aren’t Keras models, we can’t use the handy get_layer() method to grab epsilon directly. Here’s how you can pull that value out:
Step 1: Load the Frozen Graph
First, we need to load the .pb file into a TensorFlow graph. For TF 2.x, we’ll use compatibility mode to handle the frozen graph format:
import tensorflow as tf def load_frozen_graph(pb_file_path): graph = tf.Graph() with graph.as_default(): graph_def = tf.compat.v1.GraphDef() with tf.io.gfile.GFile(pb_file_path, 'rb') as f: graph_def.ParseFromString(f.read()) tf.import_graph_def(graph_def, name='') return graph
Step 2: Extract Epsilon from BatchNorm Operations
Unlike gamma, beta, or moving averages (which are trainable variables), epsilon is stored as an attribute of the BatchNorm operation node in the frozen graph. We’ll iterate through all operations to find BatchNorm nodes and read their epsilon attribute:
# Load your model model_graph = load_frozen_graph("your_model_file.pb") # Iterate through all operations in the graph for op in model_graph.get_operations(): # Match common BatchNorm operation names/types is_batchnorm = ( "batchnorm" in op.name.lower() or "BatchNorm" in op.name or op.type in ["FusedBatchNorm", "FusedBatchNormV3", "BatchNormWithGlobalNormalization"] ) if is_batchnorm: print(f"Found BatchNorm node: {op.name} (type: {op.type})") # Fetch the epsilon attribute (varies slightly by operation type) try: epsilon_val = op.get_attr("epsilon") print(f"Epsilon value: {epsilon_val}\n") except AttributeError: print("Could not retrieve epsilon for this node—check operation type.\n")
Key Notes:
- TensorFlow uses different BatchNorm operation types depending on the version and how the model was exported:
- Modern models often use
FusedBatchNormorFusedBatchNormV3 - Older models might use
BatchNormWithGlobalNormalization
- Modern models often use
- Epsilon is a fixed attribute set during model training, not a trainable parameter—so it won’t show up alongside gamma/beta when you list variables.
内容的提问来源于stack exchange,提问作者Jiarong Li
相关产品推荐
相关产品推荐

