如何从以.pb格式保存的mrcnn模型中获取掩码输出?

2024-04-18 22:54:10 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用以.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>}

请帮助我找到在输出图像上生成遮罩所需的值的方法