首页 > 解决方案 > 如何优化/准备用于推理/部署的 TensorFlow 2 对象检测模型?

问题描述

我想尝试一个来自 TFHub 的预训练对象检测模型,并使用第三方供应商工具将其导入,让它在 CNN 加速器上运行。此工具需要冻结推理图作为输入。

TFHub 模型以 SavedModel 的形式提供。作为模型,我选择带有 FPN 的 SSD MobileNet v1: https ://tfhub.dev/tensorflow/ssd_mobilenet_v1/fpn_640x640/1

试用 Tensorflow 图形转换工具(https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/graph_transforms/README.md)我遇到了以下问题。

如何freeze_graph正确参数化方法调用?(https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph.py

model_dir=./ssd_mobilenet_v1-fpn_640x640/
saved_model_cli show \
    --dir ./mobilenet_v2_ssd/ \
    --tag_set serve --signature_def serving_default

输出

The given SavedModel SignatureDef contains the following input(s):
  inputs['input_tensor'] tensor_info:
      dtype: DT_UINT8
      shape: (1, -1, -1, 3)
      name: serving_default_input_tensor:0
The given SavedModel SignatureDef contains the following output(s):
  outputs['detection_anchor_indices'] tensor_info:
      dtype: DT_FLOAT
      shape: (1, 100)
      name: StatefulPartitionedCall:0
  outputs['detection_boxes'] tensor_info:
      dtype: DT_FLOAT
      shape: (1, 100, 4)
      name: StatefulPartitionedCall:1
  outputs['detection_classes'] tensor_info:
      dtype: DT_FLOAT
      shape: (1, 100)
      name: StatefulPartitionedCall:2
  outputs['detection_multiclass_scores'] tensor_info:
      dtype: DT_FLOAT
      shape: (1, 100, 91)
      name: StatefulPartitionedCall:3
  outputs['detection_scores'] tensor_info:
      dtype: DT_FLOAT
      shape: (1, 100)
      name: StatefulPartitionedCall:4
  outputs['num_detections'] tensor_info:
      dtype: DT_FLOAT
      shape: (1)
      name: StatefulPartitionedCall:5
  outputs['raw_detection_boxes'] tensor_info:
      dtype: DT_FLOAT
      shape: (1, 1917, 4)
      name: StatefulPartitionedCall:6
  outputs['raw_detection_scores'] tensor_info:
      dtype: DT_FLOAT
      shape: (1, 1917, 91)
      name: StatefulPartitionedCall:7
Method name is: tensorflow/serving/predict

我在某处读到这ouput_node_names是一个逗号分隔的输出名称列表,没有该:<number>部分。但是所有输出都将具有相同的名称StatefulPartitionedCall?有没有办法仅使用以下输出来冻结模型:

之后我想使用bazel-bin/tensorflow/tools/graph_transforms/transform_graph. 有人可以解释或举例说明如何为上述模型正确配置它吗?

如果这两种工具都不是简化部署模型的正确方法(没有任何量化),那么正确的方法是什么?

标签: tensorflowtensorflow2.0

解决方案


推荐阅读