打印pb模型参数及可视化结构

import
tensorflow as tf from tensorflow.python.framework import graph_util tf.reset_default_graph() # 重置计算图 output_graph_path = '/home/huihua/NewDisk/stuff_detector_v1.pb' with tf.Session() as sess: tf.global_variables_initializer().run() output_graph_def = tf.GraphDef() # 获得默认的图 graph = tf.get_default_graph() with open(output_graph_path, "rb") as f: output_graph_def.ParseFromString(f.read()) _ = tf.import_graph_def(output_graph_def, name="") # 得到当前图有几个操作节点 print("%d ops in the final graph." % len(output_graph_def.node)) tensor_name = [tensor.name for tensor in output_graph_def.node] print(tensor_name) print('---------------------------') # 在log_graph文件夹下生产日志文件,可以在tensorboard中可视化模型 summaryWriter = tf.summary.FileWriter('log_pb/', graph) for op in graph.get_operations(): # print出tensor的name和值 print(op.name, op.values())

 

加载ckpt模型到tensorboard可视化

import tensorflow as tf
graph = tf.get_default_graph()
graphdef = graph.as_graph_def()

_ = tf.train.import_meta_graph("/home/huihua/NewDisk1/research/object_detection/ssd_model/eff_ssd_model/model.ckpt-100.meta")
summary_write = tf.summary.FileWriter("./log_ck" , graph)

 

打印模型参数

 

from tensorflow.python import pywrap_tensorflow
import os
import tensorflow as tf
checkpoint_path=os.path.join('./model.ckpt-300')
# 打印参数
reader=pywrap_tensorflow.NewCheckpointReader(checkpoint_path)
var_to_shape_map=reader.get_variable_to_shape_map()
for key in var_to_shape_map:
    print('tensor_name: ',key)

 

 

修改模型中的参数名称

import tensorflow as tf

def rename_var(ckpt_path, new_ckpt_path):
    with tf.Session() as sess:
        for var_name, _ in tf.contrib.framework.list_variables(ckpt_path):
            print(var_name)
            var = tf.contrib.framework.load_variable(ckpt_path, var_name)
            new_var_name = var_name.replace('IV','ssd_efficient_net_feature_extractor' )
            var = tf.Variable(var, name=new_var_name)

        saver = tf.train.Saver()
        sess.run(tf.global_variables_initializer())
        saver.save(sess, new_ckpt_path)

ckpt_path = './model.ckpt-200'

new_ckpt_path = './model.ckpt-300'
rename_var(ckpt_path, new_ckpt_path)