首页 > 解决方案 > 如何从以 .pb 格式保存的 mrcnn 模型获取掩码输出?

问题描述

如何使用以 .pb 格式保存的冻结 mrcnn 模型提取在输出图像上绘制蒙版所需的值?

在 tensorflow 2x 中训练的模型。我加载模型如下:

model = tf.saved_model.load(model_location)
model_fn = model.signatures['serving_default']

然后使用枕头加载输入图像:

image = Image.open(r"<path>")

并将图像转换为张量并将其传递给模型,我执行了以下操作:

image_arr = np.array(image)
input_tensor = tf.convert_to_tensor(image_arr)
input_tensor = input_tensor[tf.newaxis, ...]
output_dict = model_fn(input_tensor)

但是,当我运行以下行时,num_detections = int(output_dict.pop('num_detections'))它给了我一个错误,上面写着:

TypeError                                 Traceback (most recent call last)
<ipython-input-22-c7671ab6b6fe> in <module>
----> 1 num_detections = int(output_dict.pop('num_detections'))

TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor'

仅供参考,output_dict具有以下值:

{'rpn_box_encodings': <tf.Tensor 'StatefulPartitionedCall:19' shape=(1, 49152, 4) dtype=float32>,
 'detection_anchor_indices': <tf.Tensor 'StatefulPartitionedCall:3' shape=(1, 100) dtype=float32>,
 'box_classifier_features': <tf.Tensor 'StatefulPartitionedCall:1' shape=(300, 9, 9, 1536) dtype=float32>,
 'detection_multiclass_scores': <tf.Tensor 'StatefulPartitionedCall:7' shape=(1, 100, 3) dtype=float32>,
 'mask_predictions': <tf.Tensor 'StatefulPartitionedCall:11' shape=(100, 2, 33, 33) dtype=float32>,
 'proposal_boxes_normalized': <tf.Tensor 'StatefulPartitionedCall:15' shape=(1, 300, 4) dtype=float32>,
 'rpn_objectness_predictions_with_background': <tf.Tensor 'StatefulPartitionedCall:20' shape=(1, 49152, 2) dtype=float32>,
 'detection_masks': <tf.Tensor 'StatefulPartitionedCall:6' shape=(1, 100, 33, 33) dtype=float32>,
 'refined_box_encodings': <tf.Tensor 'StatefulPartitionedCall:18' shape=(300, 2, 4) dtype=float32>,
 'raw_detection_boxes': <tf.Tensor 'StatefulPartitionedCall:16' shape=(1, 300, 4) dtype=float32>,
 'detection_boxes': <tf.Tensor 'StatefulPartitionedCall:4' shape=(1, 100, 4) dtype=float32>,
 'final_anchors': <tf.Tensor 'StatefulPartitionedCall:9' shape=(1, 300, 4) dtype=float32>,
 'raw_detection_scores': <tf.Tensor 'StatefulPartitionedCall:17' shape=(1, 300, 3) dtype=float32>,
 'image_shape': <tf.Tensor 'StatefulPartitionedCall:10' shape=(4,) dtype=float32>,
 'num_proposals': <tf.Tensor 'StatefulPartitionedCall:13' shape=(1,) dtype=float32>,
 'class_predictions_with_background': <tf.Tensor 'StatefulPartitionedCall:2' shape=(300, 3) dtype=float32>,
 'detection_classes': <tf.Tensor 'StatefulPartitionedCall:5' shape=(1, 100) dtype=float32>,
 'proposal_boxes': <tf.Tensor 'StatefulPartitionedCall:14' shape=(1, 300, 4) dtype=float32>,
 'anchors': <tf.Tensor 'StatefulPartitionedCall:0' shape=(?, 4) dtype=float32>,
 'detection_scores': <tf.Tensor 'StatefulPartitionedCall:8' shape=(1, 100) dtype=float32>}

请帮助我找到一种方法来获取在输出图像上生成掩码所需的值。

标签: pythontensorflow2.0object-detection-api

解决方案


推荐阅读