2 回答

TA贡献1805条经验 获得超10个赞
每个正在寻找我的问题的答案的人,请看下面。
注意:我想您已经将模型保存在其中checkpoint_dir并希望以图形模式获取此模型,以便您可以将其另存为.pb文件。
model = ContextExtractor()
predictions = model(images, training=False)
checkpoint = tf.train.Checkpoint(model=model, global_step=tf.train.get_or_create_global_step())
status = checkpoint.restore(tf.train.latest_checkpoint(checkpoint_dir))
status.assert_consumed()
with tf.Session() as sess:
status.initialize_or_restore(sess) # this is the main line for loading
# Actually, I don't know it is necessary to pass one batch for creating graph or not
img_batch = get_image(...)
ans = sess.run(predictions, feed_dict={images: img_batch})
frozen_graph = freeze_session(sess, output_names=[out.op.name for out in model.outputs])
# save your model
tf.train.write_graph(frozen_graph, "where/to/save", "tf_model.pb", as_text=False)

TA贡献1810条经验 获得超4个赞
你应该得到会话:
tf.keras.backend.get_session()
然后冻结模型,例如这里做的https://www.dlology.com/blog/how-to-convert-trained-keras-model-to-tensorflow-and-make-prediction/
def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
"""
Freezes the state of a session into a pruned computation graph.
Creates a new computation graph where variable nodes are replaced by
constants taking their current value in the session. The new graph will be
pruned so subgraphs that are not necessary to compute the requested
outputs are removed.
@param session The TensorFlow session to be frozen.
@param keep_var_names A list of variable names that should not be frozen,
or None to freeze all the variables in the graph.
@param output_names Names of the relevant graph outputs.
@param clear_devices Remove the device directives from the graph for better portability.
@return The frozen graph definition.
"""
from tensorflow.python.framework.graph_util import convert_variables_to_constants
graph = session.graph
with graph.as_default():
freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
output_names = output_names or []
output_names += [v.op.name for v in tf.global_variables()]
# Graph -> GraphDef ProtoBuf
input_graph_def = graph.as_graph_def()
if clear_devices:
for node in input_graph_def.node:
node.device = ""
frozen_graph = convert_variables_to_constants(session, input_graph_def,
output_names, freeze_var_names)
return frozen_graph
frozen_graph = freeze_session(K.get_session(),
output_names=[out.op.name for out in model.outputs])
然后将模型另存为.pb(也显示在链接中):
tf.train.write_graph(frozen_graph, "model", "tf_model.pb", as_text=False)
如果这太麻烦,请尝试将 keras 模型另存为.h5(HDF5 类型文件),然后按照提供的链接中的说明进行操作。
从张量流文档:
编写兼容代码 为 Eager Execution 编写的相同代码也将在图执行期间构建图。为此,只需在未启用 Eager Execution 的新 Python 会话中运行相同的代码即可。
同样来自同一页面:
为了保存和加载模型,tf.train.Checkpoint 存储对象的内部状态,而不需要隐藏变量。要记录模型、优化器和全局步骤的状态,请将它们传递给 tf.train.Checkpoint:
checkpoint_dir = tempfile.mkdtemp()
checkpoint_prefix = os.path.join(checkpoint_dir, "ckpt")
root = tf.train.Checkpoint(optimizer=optimizer,
model=model,
optimizer_step=tf.train.get_or_create_global_step())
root.save(checkpoint_prefix)
root.restore(tf.train.latest_checkpoint(checkpoint_dir))
我向您推荐本页的最后一部分:https : //www.tensorflow.org/guide/eager
希望这可以帮助。
添加回答
举报