是否有某种方法可以在 tensorflow v2 上加载在 tf v1 中创建的 .pb 文件? [英] Is there some way of load a .pb file created in tf v1 on tensorflow v2?

查看:62
本文介绍了是否有某种方法可以在 tensorflow v2 上加载在 tf v1 中创建的 .pb 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to load a .pb file that was created in tf v1 on a tfv2 dist, my question is, the version 2 does have compatibility with older pb?

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

I already tried a few things, but none of them worked. Trying to load the pb file directly with:

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="")

运行上面代码的结果是:

The result when i run the code above is:

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 上的新模型实例中,以应用迁移学习/使用新模型结构保存?

If not, is there a way that i can save the weights of the old pb and place them in a new model instance on tensorflow v2 to apply transfer learning / save with the new model structure ?

推荐答案

使用此处的代码将其转换为 tf.saved_model 将图形原型 (pb/pbtxt) 转换为 SavedModel 以在 TensorFlow 中使用服务或云端机器学习引擎

Convert it to a tf.saved_model with the code from here 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.如果是这种情况,您可以将其加载为

I just noticed that your .pb name is saved_model.pb so perhaps it is already a tf.saved_model. If that's the case you can load it as

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

这篇关于是否有某种方法可以在 tensorflow v2 上加载在 tf v1 中创建的 .pb 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆