python - 如何在所有列组合的两个指标之间进行 numpy 计算?
问题描述
我有以下尝试计算我从人脸检测器获得的几个检测的 IOU。
import numpy as np
class BboxUtils(object):
@staticmethod
def ious(bboxes1, bboxes2):
left1, top1, right1, bottom1 = BboxUtils.bbox_to_perimiters(bboxes1)
left2, top2, right2, bottom2 = BboxUtils.bbox_to_perimiters(bboxes2)
area1 = (right1 - left1) * (top1 - bottom1)
area2 = (right2 - left2) * (top2 - bottom2)
intersection_left = np.maximum(left1, left2)
intersection_right = np.minimum(right1, right2)
intersection_top = np.maximum(top1, top2)
intersection_bottom = np.minimum(bottom1, bottom2)
intersection_area = (intersection_right - intersection_left) * (intersection_top - intersection_bottom)
intersection_area[intersection_area < 0] = 0
iou = intersection_area / (area1 + area2 - intersection_area)
return iou
@staticmethod
def bbox_to_perimiters(bboxes):
left, w, top, h = np.split(bboxes.reshape(-1), 4)
right = left + w
bottom = top + h
return left, right, top, bottom
# example usage:
detections1 = my_detector.detect(frame1) #np.array of shape (n1, 4)
detections2 = my_detector.detect(frame2) #np.array of shape (n2, 4)
ious = BboxUtils.ious(detections1, detections2)
此代码假定:
- 2 帧 (
bboxes1
和bboxes2
) 上的检测长度相同 - 每个检测的索引在
bboxes1
和bboxes2
我想用与上面代码相同的逻辑来计算 IOU,但要避免 for loops。
注意bboxes1
和bboxes2
可以是形状矩阵(n1, 4)
和(n2, 4)
,其中n1
不一定相等n2
。
如何才能做到这一点?
最后,可能有一个库已经完成了所有这些工作。如果它确实存在,请参考我。
解决方案
推荐阅读
- string - 按分隔符拆分字符串并包含分隔符 - Common Lisp
- node.js - 数据未持久化/保存到 MongoDB 数据库中
- python - 使用递归查找最多 n 项的总和时得到“shell返回-1073741571”
- batch-file - 对数组中的数字进行排序 [批处理脚本]
- angular - 尝试将图像上传到 Imgur 中的特定用户帐户时卡住
- jquery - jQuery UI自动完成向上/向下箭头不显示项目选择
- amazon-web-services - 从 REST API 返回 413 响应时的最佳实践
- c - 如果它们是结构的成员,可以比较 C 中的指针吗?
- swift - 不能在其他派生属性 @count 上使用派生属性 @sum
- python - 在树上聚合节点的值