【问题标题】:Tensorflow: how to convert a frozen model to saved modelTensorflow:如何将冻结的模型转换为保存的模型
【发布时间】:2018-10-27 21:50:36
【问题描述】:
【问题讨论】:
标签:
python
python-3.x
tensorflow
model
【解决方案1】:
首先回答,这样做是可能的,但这样做很麻烦,因为在将 ckpt 冻结为 pb 模型期间,SavedModel 所需的所有变量都会转换为常量或其他类型。在 tf 中,revert 'Constant' to 'Variable' 有点复杂,比如:
# node is a constant
node = tf.Constant([1, 2])
output_node = tf.NodeDef()
output_node.op = "Variable"
output_node.name = node.name
dtype = node.attr["dtype"].type
data = node.attr["value"].tensor
output_node.attr["dtype"].type = dtype
output_node.attr["value"].CopyFrom(tf.AttrValue(tensor=data))
有关将常量节点转换为其他类型的详细信息,请参阅此answer。
因此,从 ckpt 文件中冻结 SavedModel 是最简单、最直接的方法。