Moo*_*dra 5 python deep-learning tensorflow
我正在尝试从已保存的模型中提取所有权重/偏差output_graph.pb。
我读了模型:
def create_graph(modelFullPath):
"""Creates a graph from saved GraphDef file and returns a saver."""
# Creates graph from saved graph_def.pb.
with tf.gfile.FastGFile(modelFullPath, 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
tf.import_graph_def(graph_def, name='')
GRAPH_DIR = r'C:\tmp\output_graph.pb'
create_graph(GRAPH_DIR)
Run Code Online (Sandbox Code Playgroud)
并尝试这种希望,我将能够提取每一层中的所有权重/偏差。
with tf.Session() as sess:
all_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES)
print (len(all_vars))
Run Code Online (Sandbox Code Playgroud)
但是,我得到的值为len。
最终目标是提取权重和偏差并将其保存到文本文件/np.arrays。
该tf.import_graph_def()函数没有足够的信息来重构tf.GraphKeys.TRAINABLE_VARIABLES集合(为此,您需要一个MetaGraphDef)。但是,如果output.pb包含“ frozen” GraphDef,则所有权重将存储在tf.constant()图中的节点中。要提取它们,可以执行以下操作:
create_graph(GRAPH_DIR)
constant_values = {}
with tf.Session() as sess:
constant_ops = [op for op in sess.graph.get_operations() if op.type == "Const"]
for constant_op in constant_ops:
constant_values[constant_op.name] = sess.run(constant_op.outputs[0])
Run Code Online (Sandbox Code Playgroud)
请注意,其中constant_values可能包含的值可能不只是权重,因此您可能需要根据op.name或其他条件进一步过滤。
| 归档时间: |
|
| 查看次数: |
3519 次 |
| 最近记录: |