tensorflow1.X版本中打印训练好模型各个节点名,并保存在txt,根据输入输出节点保存‘.pb’文件

tensorflow模型训练好后有几个文件,其中".ckpt"是模型参数数据,“.meta”存有网络结构;

如何打印显示网络中各个节点的名称(op),代码如下:

import tensorflow as tf

ckpt_path = './model.ckpt'
saver = tf.train.import_meta_graph(ckpt_path+'.meta',clear_devices=True)
graph = tf.get_default_graph()  # 默认图
path = './'

with tf.Session(graph=graph) as sess:  # 在默认图上打开会话
    sess.run(tf.global_variables_initializer())  # 运行变量初始化
    saver.restore(sess.ckpt_path)                # 从模型中恢复网络

#--------Test 1 以下是往txt中写入网络结构的op-----------#
g = tf.get_default_graph()   # 打开一个默认图
with tf.Session(graph=g) as sess:   # 在图g上打开会话
    with open('node.txt','w+') as w:  # 往node.txt中写数据
        OPS = graph.get_operations():  # 获取图中所有的操作op
        for op in OPS:
            txt = str([v.name for v in op.inputs])+'---->' + op.type +'---->'+str([v.name for v in op.outputs])  # 获取每个op的输入节点名或输出节点名,以及op的操作类型
            w.write(txt+'\n')


#-------Test2 以下是根据查看模型图中输出节点名,将模型由ckpt存成pb文件便于部署------------#
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "3"  # 设置GPU设备号

import tensorflow as tf

#from tensorflow.python.framework.graph_util import convert_variables_to_constants  #将ckpt中模型变量的类型由变量变为常量

ckpt_path='./model.ckpt-1'  # 模型路径
saver=tf.train.import_meta_graph(ckpt_path+'.meta',clear_devices=True)
graph =tf.get_default_graph()
path='./'
with tf.Session(graph=graph)as sess:
    sess.run(tf.global_variables_initializer())
    saver.restore(sess,ckpt_path)
g=tf.get_default_graph()
'''                                # 将op写到txt中
with tf.Session(graph=g) as sess:
    with open('node.txt','w+')as w:
        OPS=graph.get_operations()
        for op in OPS:
            txt=str([v.name for v in op.inputs])+'---->'+op.type+'-->'+str([v.name for v in op.outputs])
            w.write(txt+'\n')
'''
#'''
from tensorflow.python.tools import inspect_checkpoint as chkp
outputs_nodes = ['yolo_0/concat','yolo_1/concat','yolo_2/concat']  # 假设输出的op节点名已知,存在list中

with tf.Session(graph = tf.get_default_graph()) as sess:
    input_graph_def = sess.graph.as_graph_def()
    #saver.restore(sess.ckpt_path)
    #sess.run(tf.initialize_all_variables())
    sess.run(tf.global_variables_initializer())
    output_graph_def = tf.graph_util.convert_variables_to_constants(sess,
                                                                   input_graph_def,
                                                                   outputs_nodes)   #传入图结构,输出节点名,将图由变量构成的图转为常量构成的图,剔除不必要的op节点

    with open("frezen_model.pb","wb") as f:
        f.write(output_graph_def.SerializeToString())   # 将上述图存成pb文件,这个文件包含了模型的图结构也包含了模型的参数值,便于部署和inference

tensorflow1.X版本中打印训练好模型各个节点名,并保存在txt,根据输入输出节点保存‘.pb’文件_第1张图片

 对于tensorflow版本的yolov3,将其op写到node.txt中,内容如下:

[]---->Placeholder-->['Placeholder:0']
[]---->Const-->['detector/transpose/perm:0']
['Placeholder:0', 'detector/transpose/perm:0']---->Transpose-->['detector/transpose:0']
[]---->Const-->['detector/truediv/y:0']
['detector/transpose:0', 'detector/truediv/y:0']---->RealDiv-->['detector/truediv:0']
[]---->Const-->['detector/darknet-53/Conv/weights/Initializer/random_uniform/shape:0']
[]---->Const-->['detector/darknet-53/Conv/weights/Initializer/random_uniform/min:0']
[]---->Const-->['detector/darknet-53/Conv/weights/Initializer/random_uniform/max:0']
['detector/darknet-53/Conv/weights/Initializer/random_uniform/shape:0']---->RandomUniform-->['detector/darknet-53/Conv/weights/Initializer/random_uniform/RandomUniform:0']
['detector/darknet-53/Conv/weights/Initializer/random_uniform/max:0', 'detector/darknet-53/Conv/weights/Initializer/random_uniform/min:0']---->Sub-->['detector/darknet-53/Conv/weights/Initializer/random_uniform/sub:0']
['detector/darknet-53/Conv/weights/Initializer/random_uniform/RandomUniform:0', 'detector/darknet-53/Conv/weights/Initializer/random_uniform/sub:0']---->Mul-->['detector/darknet-53/Conv/weights/Initializer/random_uniform/mul:0']
['detector/darknet-53/Conv/weights/Initializer/random_uniform/mul:0', 'detector/darknet-53/Conv_36/weights/Assign:0']
['detector/darknet-53/Conv_36/weights:0']---->Identity-->['detector/darknet-53/Conv_36/weights/read:0']
[]---->Const-->['detector/darknet-53/Conv_36/dilation_rate:0']
['detector/darknet-53/Conv_35/LeakyRelu:0', 'detector/darknet-53/Conv_36/weights/read:0']---->Conv2D-->['detector/darknet-53/Conv_36/Conv2D:0']
[]---->Const-->['detector/darknet-53/Conv_36/BatchNorm/gamma/Initializer/ones:0']
[]---->VariableV2-->['detector/darknet-53/Conv_36/BatchNorm/gamma:0']
['detector/darknet-53/Conv_36/BatchNorm/gamma:0', 'detector/darknet-53/Conv_36/BatchNorm/gamma/Initializer/ones:0']---->Assign-->['detector/darknet-53/Conv_36/BatchNorm/gamma/Assign:0']
['detector/darknet-53/Conv_36/BatchNorm/gamma:0']---->Identity-->['detector/darknet-53/Conv_36/BatchNorm/gamma/read:0']
[]---->Const-->['detector/darknet-53/Conv_36/BatchNorm/beta/Initializer/zeros:0']
[]---->VariableV2-->['detector/darknet-53/Conv_36/BatchNorm/beta:0']
['detector/darknet-53/Conv_36/BatchNorm/beta:0', 'detector/darknet-53/Conv_36/BatchNorm/beta/Initializer/zeros:0']---->Assign-->['detector/darknet-53/Conv_36/BatchNorm/beta/Assign:0']
['detector/darknet-53/Conv_36/BatchNorm/beta:0']---->Identity-->['detector/darknet-53/Conv_36/BatchNorm/beta/read:0']
………………
……………………
……………………
['detector/darknet-53/Conv_9/BatchNorm/moving_mean:0', 'save/RestoreV2:257']---->Assign-->['save/Assign_257:0']
['detector/darknet-53/Conv_9/BatchNorm/moving_variance:0', 'save/RestoreV2:258']---->Assign-->['save/Assign_258:0']
['detector/darknet-53/Conv_9/weights:0', 'save/RestoreV2:259']---->Assign-->['save/Assign_259:0']
['detector/yolo-v3/Conv/BatchNorm/beta:0', 'save/RestoreV2:260']---->Assign-->['save/Assign_260:0']
['detector/yolo-v3/Conv/BatchNorm/gamma:0', 'save/RestoreV2:261']---->Assign-->['save/Assign_261:0']
['detector/yolo-v3/Conv/BatchNorm/moving_mean:0', 'save/RestoreV2:262']---->Assign-->['save/Assign_262:0']
['detector/yolo-v3/Conv/BatchNorm/moving_variance:0', 'save/RestoreV2:263']---->Assign-->['save/Assign_263:0']
['detector/yolo-v3/Conv/weights:0', 'save/RestoreV2:264']---->Assign-->['save/Assign_264:0']
['detector/yolo-v3/Conv_1/BatchNorm/beta:0', 'save/RestoreV2:265']---->Assign-->['save/Assign_265:0']
['detector/yolo-v3/Conv_1/BatchNorm/gamma:0', 'save/RestoreV2:266']---->Assign-->['save/Assign_266:0']
['detector/yolo-v3/Conv_1/BatchNorm/moving_mean:0', 'save/RestoreV2:267']---->Assign-->['save/Assign_267:0']
['detector/yolo-v3/Conv_1/BatchNorm/moving_variance:0', 'save/RestoreV2:268']---->Assign-->['save/Assign_268:0']
['detector/yolo-v3/Conv_1/weights:0', 'save/RestoreV2:269']---->Assign-->['save/Assign_269:0']
['detector/yolo-v3/Conv_10/BatchNorm/beta:0', 'save/RestoreV2:270']---->Assign-->['save/Assign_270:0']
['detector/yolo-v3/Conv_10/BatchNorm/gamma:0', 'save/RestoreV2:271']---->Assign-->['save/Assign_271:0']
['detector/yolo-v3/Conv_10/BatchNorm/moving_mean:0', 'save/RestoreV2:272']---->Assign-->['save/Assign_272:0']
['detector/yolo-v3/Conv_10/BatchNorm/moving_variance:0', 'save/RestoreV2:273']---->Assign-->['save/Assign_273:0']
['detector/yolo-v3/Conv_10/weights:0', 'save/RestoreV2:274']---->Assign-->['save/Assign_274:0']
['detector/yolo-v3/Conv_11/BatchNorm/beta:0', 'save/RestoreV2:275']---->Assign-->['save/Assign_275:0']
['detector/yolo-v3/Conv_11/BatchNorm/gamma:0', 'save/RestoreV2:276']---->Assign-->['save/Assign_276:0']
['detector/yolo-v3/Conv_11/BatchNorm/moving_mean:0', 'save/RestoreV2:277']---->Assign-->['save/Assign_277:0']
['detector/yolo-v3/Conv_11/BatchNorm/moving_variance:0', 'save/RestoreV2:278']---->Assign-->['save/Assign_278:0']
['detector/yolo-v3/Conv_11/weights:0', 'save/RestoreV2:279']---->Assign-->['save/Assign_279:0']
['detector/yolo-v3/Conv_12/BatchNorm/beta:0', 'save/RestoreV2:280']---->Assign-->['save/Assign_280:0']
['detector/yolo-v3/Conv_12/BatchNorm/gamma:0', 'save/RestoreV2:281']---->Assign-->['save/Assign_281:0']
['detector/yolo-v3/Conv_12/BatchNorm/moving_mean:0', 'save/RestoreV2:282']---->Assign-->['save/Assign_282:0']
['detector/yolo-v3/Conv_12/BatchNorm/moving_variance:0', 'save/RestoreV2:283']---->Assign-->['save/Assign_283:0']
['detector/yolo-v3/Conv_12/weights:0', 'save/RestoreV2:284']---->Assign-->['save/Assign_284:0']
['detector/yolo-v3/Conv_13/BatchNorm/beta:0', 'save/RestoreV2:285']---->Assign-->['save/Assign_285:0']
['detector/yolo-v3/Conv_13/BatchNorm/gamma:0', 'save/RestoreV2:286']---->Assign-->['save/Assign_286:0']
['detector/yolo-v3/Conv_13/BatchNorm/moving_mean:0', 'save/RestoreV2:287']---->Assign-->['save/Assign_287:0']
['detector/yolo-v3/Conv_13/BatchNorm/moving_variance:0', 'save/RestoreV2:288']---->Assign-->['save/Assign_288:0']
['detector/yolo-v3/Conv_13/weights:0', 'save/RestoreV2:289']---->Assign-->['save/Assign_289:0']
['detector/yolo-v3/Conv_14/biases:0', 'save/RestoreV2:290']---->Assign-->['save/Assign_290:0']
['detector/yolo-v3/Conv_14/weights:0', 'save/RestoreV2:291']---->Assign-->['save/Assign_291:0']
['detector/yolo-v3/Conv_15/BatchNorm/beta:0', 'save/RestoreV2:292']---->Assign-->['save/Assign_292:0']
['detector/yolo-v3/Conv_15/BatchNorm/gamma:0', 'save/RestoreV2:293']---->Assign-->['save/Assign_293:0']
['detector/yolo-v3/Conv_15/BatchNorm/moving_mean:0', 'save/RestoreV2:294']---->Assign-->['save/Assign_294:0']
['detector/yolo-v3/Conv_15/BatchNorm/moving_variance:0', 'save/RestoreV2:295']---->Assign-->['save/Assign_295:0']
['detector/yolo-v3/Conv_15/weights:0', 'save/RestoreV2:296']---->Assign-->['save/Assign_296:0']
['detector/yolo-v3/Conv_16/BatchNorm/beta:0', 'save/RestoreV2:297']---->Assign-->['save/Assign_297:0']
['detector/yolo-v3/Conv_16/BatchNorm/gamma:0', 'save/RestoreV2:298']---->Assign-->['save/Assign_298:0']
['detector/yolo-v3/Conv_16/BatchNorm/moving_mean:0', 'save/RestoreV2:299']---->Assign-->['save/Assign_299:0']
['detector/yolo-v3/Conv_16/BatchNorm/moving_variance:0', 'save/RestoreV2:300']---->Assign-->['save/Assign_300:0']
['detector/yolo-v3/Conv_16/weights:0', 'save/RestoreV2:301']---->Assign-->['save/Assign_301:0']
['detector/yolo-v3/Conv_17/BatchNorm/beta:0', 'save/RestoreV2:302']---->Assign-->['save/Assign_302:0']
['detector/yolo-v3/Conv_17/BatchNorm/gamma:0', 'save/RestoreV2:303']---->Assign-->['save/Assign_303:0']
['detector/yolo-v3/Conv_17/BatchNorm/moving_mean:0', 'save/RestoreV2:304']---->Assign-->['save/Assign_304:0']
['detector/yolo-v3/Conv_17/BatchNorm/moving_variance:0', 'save/RestoreV2:305']---->Assign-->['save/Assign_305:0']
['detector/yolo-v3/Conv_17/weights:0', 'save/RestoreV2:306']---->Assign-->['save/Assign_306:0']
['detector/yolo-v3/Conv_18/BatchNorm/beta:0', 'save/RestoreV2:307']---->Assign-->['save/Assign_307:0']
['detector/yolo-v3/Conv_18/BatchNorm/gamma:0', 'save/RestoreV2:308']---->Assign-->['save/Assign_308:0']
['detector/yolo-v3/Conv_18/BatchNorm/moving_mean:0', 'save/RestoreV2:309']---->Assign-->['save/Assign_309:0']
['detector/yolo-v3/Conv_18/BatchNorm/moving_variance:0', 'save/RestoreV2:310']---->Assign-->['save/Assign_310:0']
['detector/yolo-v3/Conv_18/weights:0', 'save/RestoreV2:311']---->Assign-->['save/Assign_311:0']
['detector/yolo-v3/Conv_19/BatchNorm/beta:0', 'save/RestoreV2:312']---->Assign-->['save/Assign_312:0']
['detector/yolo-v3/Conv_19/BatchNorm/gamma:0', 'save/RestoreV2:313']---->Assign-->['save/Assign_313:0']
['detector/yolo-v3/Conv_19/BatchNorm/moving_mean:0', 'save/RestoreV2:314']---->Assign-->['save/Assign_314:0']
['detector/yolo-v3/Conv_19/BatchNorm/moving_variance:0', 'save/RestoreV2:315']---->Assign-->['save/Assign_315:0']
['detector/yolo-v3/Conv_19/weights:0', 'save/RestoreV2:316']---->Assign-->['save/Assign_316:0']
['detector/yolo-v3/Conv_2/BatchNorm/beta:0', 'save/RestoreV2:317']---->Assign-->['save/Assign_317:0']
['detector/yolo-v3/Conv_2/BatchNorm/gamma:0', 'save/RestoreV2:318']---->Assign-->['save/Assign_318:0']
['detector/yolo-v3/Conv_2/BatchNorm/moving_mean:0', 'save/RestoreV2:319']---->Assign-->['save/Assign_319:0']
['detector/yolo-v3/Conv_2/BatchNorm/moving_variance:0', 'save/RestoreV2:320']---->Assign-->['save/Assign_320:0']
['detector/yolo-v3/Conv_2/weights:0', 'save/RestoreV2:321']---->Assign-->['save/Assign_321:0']
['detector/yolo-v3/Conv_20/BatchNorm/beta:0', 'save/RestoreV2:322']---->Assign-->['save/Assign_322:0']
['detector/yolo-v3/Conv_20/BatchNorm/gamma:0', 'save/RestoreV2:323']---->Assign-->['save/Assign_323:0']
['detector/yolo-v3/Conv_20/BatchNorm/moving_mean:0', 'save/RestoreV2:324']---->Assign-->['save/Assign_324:0']
['detector/yolo-v3/Conv_20/BatchNorm/moving_variance:0', 'save/RestoreV2:325']---->Assign-->['save/Assign_325:0']
['detector/yolo-v3/Conv_20/weights:0', 'save/RestoreV2:326']---->Assign-->['save/Assign_326:0']
['detector/yolo-v3/Conv_21/BatchNorm/beta:0', 'save/RestoreV2:327']---->Assign-->['save/Assign_327:0']
['detector/yolo-v3/Conv_21/BatchNorm/gamma:0', 'save/RestoreV2:328']---->Assign-->['save/Assign_328:0']
['detector/yolo-v3/Conv_21/BatchNorm/moving_mean:0', 'save/RestoreV2:329']---->Assign-->['save/Assign_329:0']
['detector/yolo-v3/Conv_21/BatchNorm/moving_variance:0', 'save/RestoreV2:330']---->Assign-->['save/Assign_330:0']
['detector/yolo-v3/Conv_21/weights:0', 'save/RestoreV2:331']---->Assign-->['save/Assign_331:0']
['detector/yolo-v3/Conv_22/biases:0', 'save/RestoreV2:332']---->Assign-->['save/Assign_332:0']
['detector/yolo-v3/Conv_22/weights:0', 'save/RestoreV2:333']---->Assign-->['save/Assign_333:0']
['detector/yolo-v3/Conv_3/BatchNorm/beta:0', 'save/RestoreV2:334']---->Assign-->['save/Assign_334:0']
['detector/yolo-v3/Conv_3/BatchNorm/gamma:0', 'save/RestoreV2:335']---->Assign-->['save/Assign_335:0']
['detector/yolo-v3/Conv_3/BatchNorm/moving_mean:0', 'save/RestoreV2:336']---->Assign-->['save/Assign_336:0']
['detector/yolo-v3/Conv_3/BatchNorm/moving_variance:0', 'save/RestoreV2:337']---->Assign-->['save/Assign_337:0']
['detector/yolo-v3/Conv_3/weights:0', 'save/RestoreV2:338']---->Assign-->['save/Assign_338:0']
['detector/yolo-v3/Conv_4/BatchNorm/beta:0', 'save/RestoreV2:339']---->Assign-->['save/Assign_339:0']
['detector/yolo-v3/Conv_4/BatchNorm/gamma:0', 'save/RestoreV2:340']---->Assign-->['save/Assign_340:0']
['detector/yolo-v3/Conv_4/BatchNorm/moving_mean:0', 'save/RestoreV2:341']---->Assign-->['save/Assign_341:0']
['detector/yolo-v3/Conv_4/BatchNorm/moving_variance:0', 'save/RestoreV2:342']---->Assign-->['save/Assign_342:0']
['detector/yolo-v3/Conv_4/weights:0', 'save/RestoreV2:343']---->Assign-->['save/Assign_343:0']
['detector/yolo-v3/Conv_5/BatchNorm/beta:0', 'save/RestoreV2:344']---->Assign-->['save/Assign_344:0']
['detector/yolo-v3/Conv_5/BatchNorm/gamma:0', 'save/RestoreV2:345']---->Assign-->['save/Assign_345:0']
['detector/yolo-v3/Conv_5/BatchNorm/moving_mean:0', 'save/RestoreV2:346']---->Assign-->['save/Assign_346:0']
['detector/yolo-v3/Conv_5/BatchNorm/moving_variance:0', 'save/RestoreV2:347']---->Assign-->['save/Assign_347:0']
['detector/yolo-v3/Conv_5/weights:0', 'save/RestoreV2:348']---->Assign-->['save/Assign_348:0']
['detector/yolo-v3/Conv_6/biases:0', 'save/RestoreV2:349']---->Assign-->['save/Assign_349:0']
['detector/yolo-v3/Conv_6/weights:0', 'save/RestoreV2:350']---->Assign-->['save/Assign_350:0']
['detector/yolo-v3/Conv_7/BatchNorm/beta:0', 'save/RestoreV2:351']---->Assign-->['save/Assign_351:0']
['detector/yolo-v3/Conv_7/BatchNorm/gamma:0', 'save/RestoreV2:352']---->Assign-->['save/Assign_352:0']
['detector/yolo-v3/Conv_7/BatchNorm/moving_mean:0', 'save/RestoreV2:353']---->Assign-->['save/Assign_353:0']
['detector/yolo-v3/Conv_7/BatchNorm/moving_variance:0', 'save/RestoreV2:354']---->Assign-->['save/Assign_354:0']
['detector/yolo-v3/Conv_7/weights:0', 'save/RestoreV2:355']---->Assign-->['save/Assign_355:0']
['detector/yolo-v3/Conv_8/BatchNorm/beta:0', 'save/RestoreV2:356']---->Assign-->['save/Assign_356:0']
['detector/yolo-v3/Conv_8/BatchNorm/gamma:0', 'save/RestoreV2:357']---->Assign-->['save/Assign_357:0']
['detector/yolo-v3/Conv_8/BatchNorm/moving_mean:0', 'save/RestoreV2:358']---->Assign-->['save/Assign_358:0']
['detector/yolo-v3/Conv_8/BatchNorm/moving_variance:0', 'save/RestoreV2:359']---->Assign-->['save/Assign_359:0']
['detector/yolo-v3/Conv_8/weights:0', 'save/RestoreV2:360']---->Assign-->['save/Assign_360:0']
['detector/yolo-v3/Conv_9/BatchNorm/beta:0', 'save/RestoreV2:361']---->Assign-->['save/Assign_361:0']
['detector/yolo-v3/Conv_9/BatchNorm/gamma:0', 'save/RestoreV2:362']---->Assign-->['save/Assign_362:0']
['detector/yolo-v3/Conv_9/BatchNorm/moving_mean:0', 'save/RestoreV2:363']---->Assign-->['save/Assign_363:0']
['detector/yolo-v3/Conv_9/BatchNorm/moving_variance:0', 'save/RestoreV2:364']---->Assign-->['save/Assign_364:0']
['detector/yolo-v3/Conv_9/weights:0', 'save/RestoreV2:365']---->Assign-->['save/Assign_365:0']
[]---->NoOp-->[]
[]---->NoOp-->[]

这个在部署模型时很有用,因为需要获取输入输出节点op名~

你可能感兴趣的:(常用工具,TensorFlow,深度学习/机器学习)