我可以从.pb文件中获得Keras模型吗?

时间:2019-11-08 10:34:21

标签: tensorflow keras model protocol-buffers

我有一个用于VGG模型的ptocolbuffer文件,我需要生成keras模型。我可以使用.pb文件生成Keras模型,以便使用摘要等Keras功能吗?

我将.pb文件加载到tf中,并生成模型cofigs和图层权重。

import tensorflow as tf
from tensorflow.python.platform import gfile
from tensorflow.python.framework import tensor_util


GRAPH_PB_PATH = './small_vgg_tf_graph.pb'
with tf.Session() 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]
   names = []
   for t in graph_nodes:
      names.append(t.name)
   with open('names.txt','w') as fh:
        for i in names:
         fh.write(str(i)+'\n')

   weight_nodes = [n for n in graph_def.node if n.op == 'Const']
   with open('weights.txt','w') as fh:
      for n in weight_nodes:
               fh.write("Name of the node - %s" % n.name+'\n')
               fh.write("Value - " )
               fh.write(str(tensor_util.MakeNdarray(n.attr['value'].tensor))+'\n')


   with open('ops.txt','w') as fh:               
      for op in sess.graph.get_operations():
         fh.write(str(op)+'\n')

我想生成摘要文件之类的Keras

0 个答案:

没有答案