问题
我正在使用TF的对象检测API来训练和部署一个实例分割模型。我能够成功地训练模型,将其打包到TF服务码头映像(截至2020年10月的latest标记),并通过REST接口处理推理请求。但是,从推理请求返回的数据量非常大(数百Mb)。当推理请求和处理不发生在同一台机器上时,这是一个大问题,因为所有返回的数据都必须经过网络。
是否有办法减少输出的数量(无论是在模型导出期间还是在TF服务映像中),以便在推理期间允许更快的往返时间?
详细信息
我正在使用TF (与TF2一起)来训练一个Mask模型,这是此配置的一个修改版本。我相信输出的完整列表是用代码这里描述的。我在推理过程中得到的项目列表也贴在下面。对于一个有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,所以当通过网络时,请求有效负载不会太大。只是与之相比,推理反应是巨大的。我只需要这个响应中的4或5项,所以最好排除其余的内容,避免在网络上传递这么大的比特包。
我试过的东西
发布于 2020-12-30 04:48:18
我找到了一个麻烦的解决办法。在导出过程(这里)中,将删除预测dict的一些组件。我向non_tensor_predictions列表中添加了其他项,该列表包含在postprocess步骤中将被删除的所有键。增加这个列表将我的推理输出从200 my减少到12 my。
if self._number_of_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服务映像的过程中,有一种更“适当”的方法来使用签名定义来处理这个问题,但是这对于快速和肮脏的修复是有效的。
发布于 2020-10-12 10:48:08
我也遇到过同样的问题。在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问题可以找到这里
发布于 2021-04-16 04:07:27
如果您使用exporter_main_v2.py文件导出您的模型,您可以尝试这种黑客方式来解决这个问题。
只需在函数_run_inference_on_images of exporter_lib_v2.py文件中添加以下代码:
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的值。
如果这能解决你的问题,请告诉我。
https://stackoverflow.com/questions/64200782
复制相似问题