Tensorflow中InceptionV3模型迁移学习获取tensor的name

InceptionV3模型迁移学习的过程中,因为需要获取不同层次tensor的值,所以需要获取各层的name。之后才可以在迁移学习的过程中根据不同的层次输出结果,重新训练相应的模型

以下代码可以实现的功能:

tensorflow重新载入google提供的inceptionV3的pb模型文件 (不是cpkt模型)

读取pb文件的结构并print出tensor的name和值

存入txt文件中

#读取pb模型并print出tensor的name和值到txt文件中
import tensorflow as tf
from tensorflow.python.framework import graph_util
tf.reset_default_graph()#重置计算图    
logdir="E:\DeepLearning\Git\cnn\inception_dec_2015\\"
output_graph_path = logdir +'tensorflow_inception_graph.pb'

doc=open('inceptionV3_tensorName.txt','w') #建立写入文件的文件,'w'为写入模式

with tf.Session() as sess:
    # with tf.gfile.FastGFile(output_graph_path, 'rb') as f:
    #     graph_def = tf.GraphDef()
    #     graph_def.ParseFromString(f.read())
    #     sess.graph.as_default()
    #     tf.import_graph_def(graph_def, name='')
    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)) #得到当前图有几个操作节点
        summaryWriter = tf.summary.FileWriter('log_graph/', graph) #在log_graph文件夹下生产日志文件,可以在tensorboard中可视化模型


        for op in graph.get_operations():
            print(op.name, op.values(),sep=',',file=doc) #直接写入txt文件中
            print(op.name, op.values(),sep=',')  #print出tensor的name和值
        doc.close


猜你喜欢

转载自blog.csdn.net/sunflower_sara/article/details/80686345
今日推荐