python - 如何从以 .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>}
请帮助我找到一种方法来获取在输出图像上生成掩码所需的值。
解决方案
推荐阅读
- architecture - 带有 api 网关的微服务授权模式
- python - 未找到“登录”的反向。“登录”不是有效的视图函数或模式名称
- ios - 如何在转场目标视图控制器中执行转场
- c - 使用回溯的最小路径查找
- java - kotlin 类中的访问字段并忽略 getter
- php - 有哪些可能的因素会阻止或破坏 Laravel 数组转换按应有的方式运行?
- laravel-6 - [Route: password.reset] [URI: {locale}/password/reset/{token}] 缺少必需的参数
- python - 我想使用云函数在 biq 查询表中导出 google sccfindings 但出现错误“客户端”对象没有属性“list_findings”
- python - 如何在多列上执行 TTest
- flutter - 如何递归地对 listview.builder 执行操作?