使用 TF1 读取由 TF2 创建的 protobuf

reading a protobuf created with TF2 using TF1

我有一个存储为 hdf5 的模型,我使用 saved_model.save 将其导出到 protobuf (PB) 文件,如下所示:

from tensorflow import keras
import tensorflow as tf
model = keras.models.load_model("model.hdf5")
tf.saved_model.save(model, './output_dir/')

这很好用,结果是一个 saved_model.pb 文件,以后我可以使用其他软件查看它,没有任何问题。

但是,当我尝试使用 TensorFlow1 导入此 PB 文件时,我的代码失败了。由于PB应该是一种通用格式,这让我很困惑。

我用来读取PB文件的代码是这样的:

import tensorflow as tf
curr_graph = tf.Graph()
curr_sess = tf.InteractiveSession(graph=curr_graph)
f = tf.gfile.GFile('model.hdf5','rb')
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
f.close()

这是我得到的异常:

Traceback (most recent call last): File "read_pb.py", line 14, in graph_def.ParseFromString(f.read()) google.protobuf.message.DecodeError: Error parsing message

我有一个不同的模型存储为 PB 文件,读取代码在上面工作正常。

怎么回事?

***** 编辑 1 *****

在使用下面 Andrea Angeli 的代码时,我遇到了以下错误:

Encountered Error: NodeDef mentions attr 'exponential_avg_factor' not in Op y:T, batch_mean:U, batch_variance:U, reserve_space_1:U, reserve_space_2:U, reserve_space_3:U; attr=T:type,allowed=[DT_HALF, DT_BFLOAT16, DT_FLOAT]; attr=U:type,allowed=[DT_FLOAT]; attr=epsilon:float,default=0.0001; attr=data_format:string,default="NHWC",allowed=["NHWC", "NCHW"]; attr=is_training:bool,default=true>; NodeDef: {node u-mobilenetv2/bn_Conv1/FusedBatchNormV3}. (Check whether your GraphDef-interpreting binary is up to date with your GraphDef-generating binary.).

有解决办法吗?

您正在尝试读取 hdf5 文件,而不是您使用 tf.saved_model.save(..) 保存的 protobuf 文件。另请注意,TF2 导出的 protobuf 与 TF 1 的冻结图不同,因为它只包含计算图。

编辑 1: 如果要从 TF 2 模型导出 TF 1 样式的冻结图,可以使用以下代码片段完成:

from tensorflow.python.framework import convert_to_constants

def export_to_frozen_pb(model: tf.keras.models.Model, path: str) -> None:
    """
    Creates a frozen graph from a keras model.

    Turns the weights of a model into constants and saves the resulting graph into a protobuf file.

    Args:
        model: tf.keras.Model to convert into a frozen graph
        path: Path to save the profobuf file
    """
    inference_func = tf.function(lambda input: model(input))

    concrete_func = inference_func.get_concrete_function(tf.TensorSpec(model.inputs[0].shape, model.inputs[0].dtype))
    output_func = convert_to_constants.convert_variables_to_constants_v2(concrete_func)

    graph_def = output_func.graph.as_graph_def()
    graph_def.node[-1].name = 'output'

    with open(os.path.join(path, 'saved_model.pb'), 'wb') as freezed_pb:
        freezed_pb.write(graph_def.SerializeToString())

这将在您在 path 参数中指定的位置生成一个 protobuf 文件 (saved_model.pb)。您的图的输入节点将具有名称 "input:0"(这是由 lambda 实现的)和输出节点 "output:0"。