【问题标题】:How to use `transform_graph` in Tensorflow如何在 Tensorflow 中使用“transform_graph”
【发布时间】:2019-02-20 23:55:10
【问题描述】:

我想优化我的冷冻训练 Tensorflow 模型。但是,我发现optimize_for_inference 库不再可用。

import tensorflow as tf

from tensorflow.python.tools import freeze_graph
from tensorflow.python.tools import optimize_for_inference_lib

input_graph_def = tf.GraphDef()
with tf.gfile.Open("./inference_graph/frozen_model.pb", "rb") as f:
    data = f.read()
    input_graph_def.ParseFromString(data)

output_graph_def = optimize_for_inference_lib.optimize_for_inference(
        input_graph_def,
        ["image_tensor"], ## input  
        ["'detection_boxes, detection_scores, detection_classes, num_detections"], ## outputs
        tf.float32.as_datatype_enum)

f = tf.gfile.FastGFile("./optimized_model.pb", "wb")
f.write(output_graph_def.SerializeToString())

我从https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/graph_transforms/README.md#strip_unused_nodes 中找到了transform_graph 来优化我的冻结模型。我能够成功地为我的对象检测模型生成一个有效的优化模型。生成模型的优化版本的目的是提高模型的推理速度。我在 bash(/tensorflow 根目录)中输入了这段代码:

bazel-bin/tensorflow/tools/graph_transforms/transform_graph \
--in_graph=/Users/cvsanbuenaventura/Documents/tensorflow_fastlog/models/research/object_detection/inference_graph/frozen_inference_graph.pb \
--out_graph=/Users/cvsanbuenaventura/Documents/tensorflow_fastlog/models/research/object_detection/inference_graph/optimized_inference_graph-transform_graph-manyoutputs-planA2-v2.pb \
--inputs='image_tensor' \
--outputs='detection_boxes, detection_scores, detection_classes, num_detections' \
--transforms='fold_batch_norms
fold_old_batch_norms
fold_constants(ignore_errors=true)'

所以我的问题是:

  1. 转换有什么作用? fold_batch_norms, fold_old_batch_norms, fold_constants(ignore_errors=true)
  2. 我能够使用上述三个转换成功地生成优化模型。但还有其他转换(例如strip_unused_nodes(type=float, shape="1,299,299,3"))。这是做什么的?我应该在这里放什么形状?
  3. optimize_for_inference 库是否不再存在?

【问题讨论】:

    标签: python macos tensorflow


    【解决方案1】:

    我有点想和你一样

    1. 关于解释,找到了这个介绍,细节有点多;幻灯片 14 和 15 似乎有你想知道的,在 SimplifyGraph() https://web.stanford7edu/class/cs245/slides/TFGraphOptimizationsStanford.pdf

    2. 这似乎“1,299,299,3”对应于 SSD-300x300 模型,所以我想如果有一些与强制数据调整大小相关的东西。我读过优化的想法是删除完全训练而不是推理所需的节点。 就我而言,我使用的是 1920x1080 FRCNN 模型,所以我想我必须删除“1,1080,1920,3”。

    3. 很可能不会……必须查看 TensorFlow 团队的变更日志。

    编辑:

    1. 终于完成了我的测试。似乎使用 Faster-RCNN(可能还有 R-FCN)我在 GPU 上使用“推理优化”模型进行推理没有任何好处(我的参考卡是 GTX Titan X Maxwell,但我也有一个 AGX泽维尔测试)。使用此指令尝试了“量化”模型:

      ~/build/tensorflow/tf_1.12.3-cpu/bazel-bin/tensorflow/tools/graph_transforms/transform_graph --in_graph='model.cas.f01-v2_aug_frcnn-1920-1080-dia.pb' --out_graph='opt-for-inf/opt_2q_model.cas.f01-v2_aug_frcnn-1920-1080-dia.pb' --inputs="image_tensor" -- output="detection_boxes,detection_scores,detection_classes,num_detections" --transforms='add_default_attributes strip_unused_nodes(type=float, shape="1,1080,1920,3") remove_nodes(op=Identity, op=CheckNumerics) fold_constants(ignore_errors=true) fold_batch_norms fold_old_batch_norms merge_duplicate_nodes quantize_weights sort_by_execution_order'

    它并没有使推理时间变得更好(比如说,在 Xavier 中从每次推理的 1.2 秒到 0.8 秒左右)。添加“quantize_nodes”让我在模型的层上不匹配,这使得它无法使用。 也许这种拓扑的工作方式不同,需要我进行更多探索以了解如何优化此模型以进行推理。不过,它似乎适用于 SSD;将测试我自己的,并发布结果。

    1. 据我所知,如果您至少可以使用 Volta 架构 GPU(Titan-V 或 Tesla V100)或 RTX 卡进行训练,则可以使用 env var 并在模型的混合数据类型(如果可能,FP16,在 FP32 中有一些)。如果您真的不需要精确度,那将是一个更好的推理模型。 这将取决于用例:对于医学图像,可能的精度最高。车辆的物体检测左右,我想你可以牺牲速度的精度。 使用 nVidia-CUDA 进行混合精度训练:https://docs.nvidia.com/deeplearning/sdk/mixed-precision-training/index.html#tensorflow-amp

    2. 我的另一种方法是尝试将模型转换为 TF-Lite,然后看看如何在那里使用推理。它还在我的积压工作中。

    我用 bazel v0.19.x 编译了 tensorflow。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-26
      • 2017-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 2019-09-09
      相关资源
      最近更新 更多