tensorflow pb模型获得权重wts

该博客主要展示了如何使用TensorFlow加载.pb模型文件,并提取其中的权重。通过读取模型定义,打印出节点名称及其对应的权重值,揭示了模型内部的参数细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

tensorflow pb模型获得权重wts

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))

运行出现报错

Traceback (most recent call last):
  File "gen_pb_wts.py", line 5, in <module>
    with tf.Session(config=config) as sess:
NameError: name 'config' is not defined

将with tf.Session(config=config) as sess: 修改为tf.Session()as sess: 重新运行部分结果如下

Name of the node - BoxPredictor_4/ClassPredictor_depthwise/BatchNorm/gamma
Value -
[1.0076598  0.600361   0.64767396 0.5886829  0.6474589  0.57899725
 0.7420581  0.43999094 0.9291541  0.6562006  0.6434446  0.62888956
 0.7723971  1.013845   0.5593387  0.5763445  0.60916996 0.66238034
 1.0512784  0.58268934 0.52825654 0.55082077 0.4491668  0.6486317
 0.717447   0.9524743  0.7800959  0.42979443 0.55822694 0.60574436
 1.0343778  0.5154323  0.85758215 1.0833062  0.5687403  0.5752864
 0.84560686 0.62819284 0.6473139  0.58606607 0.6454761  0.6270443
 0.5428621  0.673155   0.9136668  0.62957346 0.855056   0.80063677
 0.55059046 0.941908   0.6897346  0.5215224  0.64170074 1.0129
 0.63292354 0.7850904  0.44446874 0.7453388  0.6934488  0.5419593
 0.70664483 0.48354548 0.60087705 0.6893038  0.45109886 0.62180865
 0.6481066  0.67009485 0.6451158  0.5812356  0.8095073  0.40086615
 0.50798595 0.49360913 0.51697165 0.5107985  0.53685313 0.9088723
 0.76556355 0.36509857 0.367629   0.80191237 0.6855312  0.970307
 1.0197186  0.671655   0.57405996 0.76866794 0.74245185 0.51531
 0.6980881  0.8520622  0.54880375 0.91214556 0.710224   0.41681576
 0.6096677  0.36241293 0.56075    0.5863032  0.6354018  0.5120003
 1.0113248  0.8348537  0.7205713  0.67849016 0.97358465 0.44886646
 0.8159403  0.64450437 0.5997353  0.54739535 0.6758225  0.61986154
 0.6395207  0.5936247  0.5463965  0.5821391  0.6760462  1.0680252
 0.55302185 0.7000822  0.8675259  0.6098902  0.74904156 0.5757958
 0.2752119  0.64663106 0.56606585 0.5852428  0.82287776 0.5280436
 0.6185344  0.6950132  1.0274562  0.7259649  1.0736766  0.602673
 0.8857293  0.59187907 0.78937036 0.4410297  0.7101182  0.8159739
 0.55530983 0.6026916  0.7655125  0.63132054 0.52071196 0.57674795
 0.919869   0.5550313  0.55495024 1.0107807  0.6631691  0.7698098
 0.6434384  0.6741949  1.0766468  0.8970208  0.6370691  0.48358867
 0.9154165  0.5827703  0.9874309  1.0530525  0.600562   0.8497894
 0.6070317  0.5207939  0.7410717  0.75982136 0.57712764 0.539803
 0.72110265 1.1149868  0.6521509  0.86586046 0.49901482 0.6168051
 0.52331764 0.65172225 1.0159032  0.6780148  0.51903594 0.84838635
 0.6718971  0.8696038  0.4443711  0.48789653 0.7989498  0.6552638
 0.4824569  0.4769425  0.63836104 0.54260814 0.49502477 0.808891
 0.7465959  0.586385   0.63334984 0.63899726 0.6576707  0.99794906
 0.8469274  0.72964466 0.76935375 0.7605871  0.7410051  0.6782605
 0.4853573  0.90893656 0.835856   0.6664103  0.6316711  0.71951354
 0.7788348  0.58414733 0.85619235 0.87070334 0.6793145  0.9028463
 0.7173049  0.64837295 0.44326815 0.82141757 0.65800077 0.8714189
 0.90920067 0.7825849  0.5547908  1.0501481  0.6167052  0.80019474
 0.56159836 0.7303601  0.6019257  0.9394907  0.57830775 0.6206071
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值