Tensorflow TF对象检测:返回推断有效负载的子集 问题
我正在使用TF的对象检测API培训和部署一个实例分割模型。我能够成功地训练模型,将其打包成TF服务Docker映像(Tensorflow TF对象检测:返回推断有效负载的子集 问题,tensorflow,tensorflow-serving,object-detection-api,Tensorflow,Tensorflow Serving,Object Detection Api,我正在使用TF的对象检测API培训和部署一个实例分割模型。我能够成功地训练模型,将其打包成TF服务Docker映像(最新的标签,自2020年10月起),并通过REST接口处理推理请求。但是,从推断请求返回的数据量非常大(数百Mb)。当推理请求和处理不在同一台机器上发生时,这是一个大问题,因为所有返回的数据都必须通过网络传输 是否有办法减少输出的数量(在模型导出期间或TF服务映像内),以便在推断期间允许更快的往返时间? 细节 我正在使用TFOD API(使用TF2)来训练掩码RCNN模型,它是的一
最新的
标签,自2020年10月起),并通过REST接口处理推理请求。但是,从推断请求返回的数据量非常大(数百Mb)。当推理请求和处理不在同一台机器上发生时,这是一个大问题,因为所有返回的数据都必须通过网络传输
是否有办法减少输出的数量(在模型导出期间或TF服务映像内),以便在推断期间允许更快的往返时间?
细节
我正在使用TFOD API(使用TF2)来训练掩码RCNN模型,它是的一个修改版本。我相信完整的输出列表是用代码描述的。我在推理过程中得到的项目列表也粘贴在下面。对于一个有100个对象建议的模型,如果我只是将返回的推断作为json写入磁盘,那么该信息大约为270MB
推理\u有效载荷['outputs'].keys()
我已经将推理请求中的图像编码为base64,因此当通过网络时,请求负载不会太大。只是相比之下,推理反应是巨大的。我只需要这个响应中的4到5个条目,所以最好排除其余条目,避免通过网络传递如此大的数据包
我尝试过的事情
score\u阈值设置为更高的值,以减少输出的数量。然而,这似乎只是检测分数的阈值。所有无关的推断信息仍将返回
tensorflow/models
repo上搜索过,但没有找到任何东西我遇到了同样的问题。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 repo的问题,我希望我们能从tensorflow开发团队得到反馈
github问题可以找到我遇到了同样的问题。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 repo的问题,我希望我们能从tensorflow开发团队得到反馈
github问题可以找到我找到了一个解决方法。在导出过程()中,删除了预测dict的一些组件。我在
非张量预测
列表中添加了其他项,其中包含在后处理步骤中将被删除的所有键。增加这个列表将我的推理输出从~200MB减少到~12MB
if self.\u stages==3时的完整代码
块:
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服务映像的过程中,有一种更“合适”的方法可以使用签名定义来处理此问题,但这是一种快速而肮脏的修复方法。我找到了一种黑客解决方法。在导出过程()中,删除了预测dict的一些组件。我在
非张量预测
列表中添加了其他项,其中包含在后处理步骤中将被删除的所有键。增加这个列表将我的推理输出从~200MB减少到~12MB
if self.\u stages==3时的完整代码
块:
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服务映像的过程中,有一种更“合适”的方法可以使用签名定义来处理此问题,但这是一种快速而肮脏的修复方法。如果您使用
exporter\u main\u v2.py
文件导出模型,您可以尝试这种黑客方法来解决此问题
只需将以下代码添加到exporter\u lib\u v2.py
文件的函数\u run\u interference\u on\u images
:
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)
因此,生成的模型将不会输出忽略的值\u model\u output\u names
请让我知道这是否可以解决您的问题。如果您使用
导出器\u main\u v2.py
文件导出您的模型,您可以尝试此黑客方法来解决此问题
只需将以下代码添加到exporter\u lib\u v2.py
文件的函数\u run\u interference\u on\u images
:
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)
因此,生成的模型将不会输出忽略的值\u model\u output\u names
请让我知道这是否能解决您的问题