问题
我正在使用 TF 的对象检测 API 来训练和部署实例分割模型。我能够成功训练模型,将其打包到 TF Serving Docker 镜像(截至 2020 年 10 月的最新
标签),并通过 REST 接口(interface)处理推理请求。然而,推理请求返回的数据量非常大(数百Mb)。当推理请求和处理不在同一台机器上发生时,这是一个大问题,因为所有返回的数据都必须通过网络。
有没有办法减少输出数量(无论是在模型导出期间还是在 TF Serving 图像内),以便在推理过程中实现更快的往返时间?
详细信息
我正在使用 TF OD API(带有 TF2)来训练 Mask RCNN 模型,该模型是 this config 的修改版本。我相信代码 here 中描述了完整的输出列表。 。我在推理过程中得到的项目列表也粘贴在下面。对于具有 100 个对象建议的模型,如果我将返回的推理以 json 形式写入磁盘,则该信息约为 270 Mb。
inference_payload['outputs'].keys()
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,因此在通过网络传输时请求负载不会太大。只是相比之下,推理 react 是巨大的。我只需要此响应中的 4 或 5 个项目,因此最好排除其余项目并避免通过网络传递如此大的位包。
我尝试过的事情
- 我尝试在导出 ( code example here ) 期间将
score_threshold
设置为更高的值,以减少输出数量。然而,这似乎只是检测分数的阈值。所有无关的推理信息仍然会返回。 - 我还尝试通过添加要删除的键名称来手动排除其中一些推理输出 here 。这似乎也没有任何效果,我担心这是一个坏主意,因为在评分/评估过程中可能需要其中一些键。
- 我还在此处和
tensorflow/models
存储库上进行了搜索,但没有找到任何内容。
最佳答案
我也遇到了同样的问题。在exporter_main_v2代码中声明输出应该是:
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问题可以找到here
关于tensorflow - TF物体检测: return subset of inference payload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64200782/