来自 .meta .info .data 的 Tensorflow 冻结推理图并组合冻结推理图

2024-04-23

我是张量流新手,目前正在努力解决一些问题:

  1. 如何在没有管道配置的情况下从 .meta .data .info 获取冻结推理图

    我想实时检查预先训练的交通标志检测模型。模型包含 3 个文件 - .meta .data .info,但我找不到信息,如何在没有管道配置的情况下将它们转换为冻结推理图。我发现的所有内容要么已过时,要么需要管道配置。

    另外,我尝试自己训练模型,但我认为问题是 .ppa 文件(GTSDB 数据集),因为使用 .png 或 .jpg 一切都工作得很好。

  2. 如何组合两个或多个冻结推理图

    我已经在自己的数据集上成功训练了模型(检测某些特定对象),但我希望该模型能够与一些预先训练的模型一起使用,例如更快的 rcnn inception 或 SSD mobilenet。我知道我必须加载两个模型,但我不知道如何使它们同时工作,这甚至可能吗?

UPDATE

我在第一个问题上已经完成了一半 - 现在我有 freeze_model.pb,问题出在输出节点名称中,我很困惑,不知道该放什么,所以经过几个小时的“调查”,得到了工作代码:

import os, argparse

import tensorflow as tf

# The original freeze_graph function
# from tensorflow.python.tools.freeze_graph import freeze_graph

dir = os.path.dirname(os.path.realpath(__file__))

def freeze_graph(model_dir):
    """Extract the sub graph defined by the output nodes and convert
    all its variables into constant
    Args:
        model_dir: the root folder containing the checkpoint state file
        output_node_names: a string, containing all the output node's names,
                            comma separated
    """
    if not tf.gfile.Exists(model_dir):
        raise AssertionError(
            "Export directory doesn't exists. Please specify an export "
            "directory: %s" % model_dir)

    # if not output_node_names:
    #     print("You need to supply the name of a node to --output_node_names.")
    #     return -1

    # We retrieve our checkpoint fullpath
    checkpoint = tf.train.get_checkpoint_state(model_dir)
    input_checkpoint = checkpoint.model_checkpoint_path

    # We precise the file fullname of our freezed graph
    absolute_model_dir = "/".join(input_checkpoint.split('/')[:-1])
    output_graph = absolute_model_dir + "/frozen_model.pb"
    # We clear devices to allow TensorFlow to control on which device it will load operations
    clear_devices = True

    # We start a session using a temporary fresh Graph
    with tf.Session(graph=tf.Graph()) as sess:

        # We import the meta graph in the current default Graph
        saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices)

        # We restore the weights
        saver.restore(sess, input_checkpoint)

        # We use a built-in TF helper to export variables to constants
        output_graph_def = tf.graph_util.convert_variables_to_constants(
            sess, # The session is used to retrieve the weights
            tf.get_default_graph().as_graph_def(), # The graph_def is used to retrieve the nodes
            [n.name for n in tf.get_default_graph().as_graph_def().node] # The output node names are used to select the usefull nodes
        )

        # Finally we serialize and dump the output graph to the filesystem
        with tf.gfile.GFile(output_graph, "wb") as f:
            f.write(output_graph_def.SerializeToString())
        print("%d ops in the final graph." % len(output_graph_def.node))

    return output_graph_def

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--model_dir", type=str, default="", help="Model folder to export")
    # parser.add_argument("--output_node_names", type=str, default="", help="The name of the output nodes, comma separated.")
    args = parser.parse_args()

    freeze_graph(args.model_dir)

我必须更改几行 - 删除 --output_node_names 并将 output_graph_def 中的 output_node_names 更改为[n.name for n in tf.get_default_graph().as_graph_def().node]现在我遇到了新问题 - 我无法将 .pb 转换为 .pbtxt,错误是:

ValueError: Input 0 of node prefix/Variable/Assign was passed float from prefix/Variable:0 incompatible with expected float_ref.

再一次,关于这个问题的信息已经过时了——我发现的所有东西都至少有一年了。我开始认为对 freeze_graph 的修复不正确,这就是我遇到新错误的原因。

我真的很感激有关此事的一些建议。


如果你写

[n.name for n in tf.get_default_graph().as_graph_def().node]

在您的convert_variables_to_constants函数中,您将图形的每个节点定义为输出节点,这当然是行不通的。 (这可能是您的 ValueError 的原因)

您需要找到真实输出节点的名称,最好的方法通常是查看张量板中经过训练的模型并分析那里的图,或者打印出图的每个节点。通常打印出的最后一个节点是您的输出节点(忽略名称中包含“梯度”或“Adam”的所有内容,如果您已将其用作优化器)

执行此操作的简单方法(在恢复会话后插入):

gd = sess.graph.as_graph_def()
for node in gd.node:
    print(node.name)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

来自 .meta .info .data 的 Tensorflow 冻结推理图并组合冻结推理图 的相关文章

随机推荐

  • 转换运算符的移动语义

    可移动转换运算符的语法是什么 我有一个可以包裹的包装纸obj 其中有一个obj转换运算符 class wrap public operator obj private obj data 我怎样才能知道是否data 应该复制还是移动 其语法如
  • AWS IAM——使用条件

    我是 AWS 中的 IAM 新手 而且 我希望将各种用户的查询限制为仅主键与 cognito id 匹配的表条目 为了实现这一目标 我制定了政策 Version 2012 10 17 Statement Sid AllowAccessToO
  • “流不包含有效的 UTF-8”是什么意思?

    我正在创建一个简单的 HTTP 服务器 我需要读取请求的图像并将其发送到浏览器 我正在使用这段代码 fn read file mut file name String gt String file name file name replac
  • 这个使用 的简单 C++ 程序正确吗?

    这段代码似乎在 gcc 和 clang 的 ubuntu 可信 版本中工作正常 并且通过 mingw 在 VM 上的 Win 7 中工作正常 最近我升级到 Wily 并使用 clang 构建的版本始终崩溃 include
  • 使用 bootstrap 更改表标题颜色

    我有一个使用引导程序的 MVC5 应用程序 表列名称为黑色 在白色背景上 我想将其更改为蓝色背景和列 名字将是白色的 我该怎么做 我尝试使用 CSS 类但没有成功
  • 安装 rpy2 时 Visual Studio cl.exe 错误

    我在 Django 中安装 rpy2 并收到以下错误 C Program Files x86 Microsoft Visual Studio 2017 Enterprise VC Tools MSVC 14 15 26726 bin Hos
  • Django 是否有立即 http 响应的异常?

    Django Tastypie 有ImmediateHttpResponse允许立即向客户端返回响应的异常 raise ImmediateHttpResponse response a message 姜戈有Http404 https do
  • 如何读取长度未知的输入字符串?

    如果我不知道这个词有多长 我就无法写char m 6 这个词的长度可能有十到二十长 我该如何使用scanf从键盘获取输入 include
  • Postgres 物化路径 - 使用 ltree 有什么好处?

    物化路径是一种在 SQL 中表示层次结构的方法 每个节点包含路径本身及其所有祖先 grandparent parent self The django treebeard实施MP docs https django treebeard re
  • 如何在 angularjs 单元测试中触发 keyup/keydown 事件?

    我想对模拟占位符的指令进行单元测试 其中输入值仅在 keyup down 事件上清除 您需要以编程方式创建一个事件并触发它 为此 使用 jQuery 进行单元测试是非常有用的 例如 您可以编写一个像这样的简单实用程序 var trigger
  • Drive Rest API V3 中的断点续传上传

    我正在尝试使用 Android 中的 Drive Rest API 创建可恢复上传会话 根据文档 需要遵循的 3 个步骤是 启动可恢复会话 保存可恢复会话 URI 上传文件 第 1 步 我使用以下代码来启动可恢复会话 File body n
  • 如何获取十六进制02的STX字符

    我有一个设备试图通过套接字连接 根据手册 我需要 十六进制 02 的 STX 字符 我如何使用 C 来做到这一点 只是对 GeoffM 的答案的评论 我没有足够的观点来以正确的方式发表评论 您绝对不应该仅使用两位数字来嵌入 STX 或其他字
  • windows 7下如何强制删除硬盘上的文件

    我有一个硬盘 但里面有一个坏文件 当我想删除文件时 窗口说 您指定的文件名无效或太长 指定不同的文件名 但文件没有重命名 我该怎么办 当正在运行的进程拥有资源句柄时 您无法修改该资源 解决方案是结束所有拥有您的资源句柄的进程 如下所示 1
  • Ant Design Collapse - 关闭按钮

    我是 Ant Design 的初学者 在使用 Ant Design 库中的 Collapse 和 Form 时遇到了这个问题 我已经设置了我的页面 其中添加新项目的表单位于折叠中 并且折叠下方有一个项目列表
  • 在ios中使用Google Plus进行图像共享

    我想在 google plus 上分享图片 我使用过 google api AppDelegate m GPPSignIn sharedInstance clientID MyClientID GPPDeepLink setDelegate
  • 重命名access vba中的字段

    我需要重命名 VBA 访问代码中的字段名称以实现自动化 以便运行得更快 我正在使用一个按钮来移动一些数据 因此需要更改列名称来清理数据 我正在使用表格 但我需要更改的表格根本没有链接到表格 我尝试使用一些 VBA 代码 但不断收到变量未定义
  • 需要在Windows Phone 7屏幕上显示大量文字

    我想要在屏幕上显示大约 800 KB 的文本 有人可以让我知道这个问题的可能解决方案吗 由于文本块的 2048X2048 限制 我已经尝试将文本拆分为多个文本块 并且也尝试过http blogs msdn com b priozersk a
  • 在 FFI 中使用 ptr::NonNull 是否有效?

    铁锈具有ptr NonNull https doc rust lang org beta std ptr struct NonNull html代表非的类型NULL指针 在 FFI 中使用这种类型安全吗 是否保证具有相同的二进制表示形式 忽
  • 捕获文件似乎在数据包中间被缩短 - 如何防止此错误?

    在我的应用程序中 我打开 Tshark 进程并开始捕获 当我想完成捕获时 我会终止 Tshark 进程 因此有时捕获文件已损坏 当我尝试打开此文件时 我收到错误捕获文件似乎在数据包中间被剪短 如何防止此错误有没有更好的方法来关闭 Tshar
  • 来自 .meta .info .data 的 Tensorflow 冻结推理图并组合冻结推理图

    我是张量流新手 目前正在努力解决一些问题 如何在没有管道配置的情况下从 meta data info 获取冻结推理图 我想实时检查预先训练的交通标志检测模型 模型包含 3 个文件 meta data info 但我找不到信息 如何在没有管道