gpt4 book ai didi

python-3.x - 是否有某种方法可以在 tensorflow v2 上加载在 tf v1 中创建的 .pb 文件?

转载 作者:行者123 更新时间:2023-12-04 15:38:54 25 4
gpt4 key购买 nike

我正在尝试在 tfv2 dist 上加载在 tf v1 中创建的 .pb 文件,我的问题是,版本 2 是否与旧 pb 兼容?

我已经尝试了一些东西,但没有一个奏效。尝试直接加载 pb 文件:

with tf.compat.v1.gfile.GFile("./saved_model.pb", "rb") as f:
graph_def = tf.compat.v1.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def, name="")

当我运行上面的代码时的结果是:
Traceback (most recent call last):
File "read_tfv1_pb.py", line 7, in <module>
graph_def.ParseFromString(f.read())
File "D:\Anaconda3\envs\tf2\lib\site-packages\google\protobuf\message.py", line 187, in ParseFromString
return self.MergeFromString(serialized)
File "D:\Anaconda3\envs\tf2\lib\site-packages\google\protobuf\internal\python_message.py", line 1128, in MergeFromString
if self._InternalParse(serialized, 0, length) != length:
File "D:\Anaconda3\envs\tf2\lib\site-packages\google\protobuf\internal\python_message.py", line 1193, in InternalParse
pos = field_decoder(buffer, new_pos, end, self, field_dict)
File "D:\Anaconda3\envs\tf2\lib\site-packages\google\protobuf\internal\decoder.py", line 968, in _SkipFixed32
raise _DecodeError('Truncated message.')
google.protobuf.message.DecodeError: Truncated message.

如果没有,有没有办法可以保存旧 pb 的权重并将它们放在 tensorflow v2 上的新模型实例中,以应用转移学习/使用新模型结构保存?

最佳答案

将其转换为 tf.saved_model使用这里的代码 Convert a graph proto (pb/pbtxt) to a SavedModel for use in TensorFlow Serving or Cloud ML Engine

我刚注意到你的 .pb姓名是 saved_model.pb所以也许它已经是 tf.saved_model .如果是这种情况,您可以将其加载为

func = tf.saved_model.load('.').signatures["serving_default"] 
out = func( tf.constant(10,tf.float32) )

关于python-3.x - 是否有某种方法可以在 tensorflow v2 上加载在 tf v1 中创建的 .pb 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58737712/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com