首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TF对象检测:推理有效载荷的返回子集

TF对象检测:推理有效载荷的返回子集
EN

Stack Overflow用户
提问于 2020-10-04 23:20:20
回答 4查看 430关注 0票数 2

问题

我正在使用TF的对象检测API来训练和部署一个实例分割模型。我能够成功地训练模型,将其打包到TF服务码头映像(截至2020年10月的latest标记),并通过REST接口处理推理请求。但是,从推理请求返回的数据量非常大(数百Mb)。当推理请求和处理不发生在同一台机器上时,这是一个大问题,因为所有返回的数据都必须经过网络。

是否有办法减少输出的数量(无论是在模型导出期间还是在TF服务映像中),以便在推理期间允许更快的往返时间?

详细信息

我正在使用TF (与TF2一起)来训练一个Mask模型,这是此配置的一个修改版本。我相信输出的完整列表是用代码这里描述的。我在推理过程中得到的项目列表也贴在下面。对于一个有100个对象建议的模型,如果我将返回的推理作为json写入磁盘,则该信息为270 Mb。

inference_payload['outputs'].keys()

代码语言:javascript
运行
复制
dict_keys(['detection_masks', 'rpn_features_to_crop', 'detection_anchor_indices', 'refined_box_encodings', 'final_anchors', 'mask_predictions', 'detection_classes', 'num_detections', 'rpn_box_predictor_features', 'class_predictions_with_background', 'proposal_boxes', 'raw_detection_boxes', 'rpn_box_encodings', 'box_classifier_features', 'raw_detection_scores', 'proposal_boxes_normalized', 'detection_multiclass_scores', 'anchors', 'num_proposals', 'detection_boxes', 'image_shape', 'rpn_objectness_predictions_with_background', 'detection_scores'])

我已经将推理请求中的图像编码为base64,所以当通过网络时,请求有效负载不会太大。只是与之相比,推理反应是巨大的。我只需要这个响应中的4或5项,所以最好排除其余的内容,避免在网络上传递这么大的比特包。

我试过的东西

  1. 在导出期间,我尝试将score_threshold设置为更高的值(这里的代码示例),以减少输出的数量。然而,这似乎只是门槛的detection_scores。所有无关的推理信息仍然返回。
  2. 我还尝试手动删除其中的一些推断输出,方法是添加键名以删除这里。这似乎也没有任何效果,我担心这是一个坏主意,因为其中一些关键可能需要在评分/评估。
  3. 我还搜索了这里和tensorflow/models回购,但我没有找到任何东西。
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2020-12-30 04:48:18

我找到了一个麻烦的解决办法。在导出过程(这里)中,将删除预测dict的一些组件。我向non_tensor_predictions列表中添加了其他项,该列表包含在postprocess步骤中将被删除的所有键。增加这个列表将我的推理输出从200 my减少到12 my。

if self._number_of_stages == 3块的完整代码:

代码语言:javascript
运行
复制
    if self._number_of_stages == 3:

      non_tensor_predictions = [
          k for k, v in prediction_dict.items() if not isinstance(v, tf.Tensor)]

      # Add additional keys to delete during postprocessing
      non_tensor_predictions = non_tensor_predictions + ['raw_detection_scores', 'detection_multiclass_scores', 'anchors', 'rpn_objectness_predictions_with_background', 'detection_anchor_indices', 'refined_box_encodings', 'class_predictions_with_background', 'raw_detection_boxes', 'final_anchors', 'rpn_box_encodings', 'box_classifier_features']
      
      for k in non_tensor_predictions:
        tf.logging.info('Removing {0} from prediction_dict'.format(k))
        prediction_dict.pop(k)

      return prediction_dict

我认为在创建TF服务映像的过程中,有一种更“适当”的方法来使用签名定义来处理这个问题,但是这对于快速和肮脏的修复是有效的。

票数 2
EN

Stack Overflow用户

发布于 2020-10-12 10:48:08

我也遇到过同样的问题。在exporter_main_v2代码中指出,输出应该是:

代码语言:javascript
运行
复制
and the following output nodes returned by the model.postprocess(..):
  * `num_detections`: Outputs float32 tensors of the form [batch]
      that specifies the number of valid boxes per image in the batch.
  * `detection_boxes`: Outputs float32 tensors of the form
      [batch, num_boxes, 4] containing detected boxes.
  * `detection_scores`: Outputs float32 tensors of the form
      [batch, num_boxes] containing class scores for the detections.
  * `detection_classes`: Outputs float32 tensors of the form
      [batch, num_boxes] containing classes for the detections.

我已经提交了一个关于tensorflow对象检测github回购的问题,我希望我们能从tensorflow开发团队那里得到反馈。

github问题可以找到这里

票数 1
EN

Stack Overflow用户

发布于 2021-04-16 04:07:27

如果您使用exporter_main_v2.py文件导出您的模型,您可以尝试这种黑客方式来解决这个问题。

只需在函数_run_inference_on_images of exporter_lib_v2.py文件中添加以下代码:

代码语言:javascript
运行
复制
    detections[classes_field] = (
        tf.cast(detections[classes_field], tf.float32) + label_id_offset)

############# START ##########
    ignored_model_output_names = ["raw_detection_boxes", "raw_detection_scores"]
    for key in ignored_model_output_names:
        if key in detections.keys(): del detections[key]
############# END ##########

    for key, val in detections.items():
      detections[key] = tf.cast(val, tf.float32)

因此,生成的模型不会输出ignored_model_output_names的值。

如果这能解决你的问题,请告诉我。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64200782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档