tensorflow从pb文件中导出权重

import tensorflow as tf
from tensorflow.python.platform import gfile
#path to your .pb file
GRAPH_PB_PATH = './model/tensorflow_inception_v3_stripped_optimized_quantized.pb' 
with tf.Session(config=config) as sess:
  print("load graph")
  with gfile.FastGFile(GRAPH_PB_PATH,'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    sess.graph.as_default()
    tf.import_graph_def(graph_def, name='')
    graph_nodes=[n for n in graph_def.node]
wts = [n for n in graph_nodes if n.op=='Const']
from tensorflow.python.framework import tensor_util

for n in wts:
    print("Name of the node - %s" % n.name)
    print("Value - ")
    print(tensor_util.MakeNdarray(n.attr['value'].tensor))

参考链接:

1)How to restore Tensorflow model from .pb file in python

2)How to get weights from .pb model in Tensorflow

posted @ 2022-11-13 22:35  dlhl  阅读(31)  评论(0)    收藏  举报