SoFunction
Updated on 2025-03-01

TensorFlow gets all tensor name codes in the loading model

The core code is as follows:

[ for tensor in tf.get_default_graph().as_graph_def().node]

Example code: (The model of Inceptino_v3 is loaded and the names of all nodes of the model are obtained)

# -*- coding: utf-8 -*-
 
import tensorflow as tf
import os
 
model_dir = 'C:/Inception_v3'
model_name = 'output_graph.pb'
 
# Read and create a graph to store the trained Inception_v3 model (function)def create_graph():
 with ((
   model_dir, model_name), 'rb') as f:
  # Use() to define an empty Graph  graph_def = ()
  graph_def.ParseFromString(())
  # Imports the graph from graph_def into the current default Graph.
  tf.import_graph_def(graph_def, name='')
 
# Create graphcreate_graph()
 
tensor_name_list = [ for tensor in tf.get_default_graph().as_graph_def().node]
for tensor_name in tensor_name_list:
 print(tensor_name,'\n')

Output result:

mixed_8/tower/conv_1/batchnorm/moving_variance 

mixed_8/tower/conv_1/batchnorm 

r_1/mixed/conv_1/batchnorm 

.

.

.

mixed_10/tower_1/mixed/conv_1/CheckNumerics 

mixed_10/tower_1/mixed/conv_1/control_dependency 

mixed_10/tower_1/mixed/conv_1 

pool_3 

pool_3/_reshape/shape 

pool_3/_reshape 

input/BottleneckInputPlaceholder 
.
.
.
.
final_training_ops/weights/final_weights 

final_training_ops/weights/final_weights/read 

final_training_ops/biases/final_biases 

final_training_ops/biases/final_biases/read 

final_training_ops/Wx_plus_b/MatMul 

final_training_ops/Wx_plus_b/add 

final_result

Because the result was too long, some were omitted.

If you don't want to print the output like this, you can also write it to txt to view it.

Write the txt code as follows:

tensor_name_list = [ for tensor in tf.get_default_graph().as_graph_def().node]
 
txt_path = './txt/node name'
full_path = txt_path+ '.txt'
 
for tensor_name in tensor_name_list:
 name = tensor_name + '\n'
 file = open(full_path,'a+')
(name)
()

The above article TensorFlow obtains all the tensor name codes in the loading model are all the content I share with you. I hope you can give you a reference and I hope you can support me more.