基于pytorch计算IoU

IoU 是目标检测里面的一个基本的环节,这里看到别人的代码,感觉还是挺高效的,就记录一下:

torch.Tensor.expand

torch.Tensor.expand(*sizes) → Tensor
这是一个pytorch的函数,sizes是你想要扩展后的shape,其中原来tensor大小为1的维度可以扩展成任意值,并且这个操作不会分配新的内存。
栗子:

>>> x = torch.tensor([[1], [2], [3]])
>>> x.size()
torch.Size([3, 1])
>>> x.expand(3, 4)
tensor([[ 1,  1,  1,  1],
        [ 2,  2,  2,  2],
        [ 3,  3,  3,  3]])
>>> x.expand(-1, 4)   # -1 means not changing the size of that dimension
tensor([[ 1,  1,  1,  1],
        [ 2,  2,  2,  2],
        [ 3,  3,  3,  3]])

torch.unsqueeze()

torch.unsqueeze(input, dim, out=None) → Tensor
这个函数就是在输入input的指定dim维度插入一个单一的维度,out可以指定输出的tensor,其中dim的范围是:[-input.dim() - 1, input.dim() + 1)。如果dim是一个负值的话,事实上起作用的是dim = dim + input.dim() + 1,也就是我们平常用的-1代表最后面。一般建议只用正数,以免混淆。
栗子:

>>> x = torch.tensor([1, 2, 3, 4])
>>> torch.unsqueeze(x, 0)
tensor([[ 1,  2,  3,  4]])
>>> torch.unsqueeze(x, -2)
tensor([[1, 2, 3, 4]])
>>> torch.unsqueeze(x, 1)
tensor([[ 1],
        [ 2],
        [ 3],
        [ 4]])
>>> torch.unsqueeze(x, -1)
tensor([[1],
        [2],
        [3],
        [4]])

IoU计算

主要有intersect、jaccard 两个函数,需要注意的一点就是这里喂入的bbox都是以
( x m i n , y m i n , x m a x , y m a x x_{min}, y_{min}, x_{max}, y_{max} xmin,ymin,xmax,ymax)给出的。

def intersect(box_a, box_b):
    """ We resize both tensors to [A,B,2] without new malloc:
    [A,2] -> [A,1,2] -> [A,B,2]
    [B,2] -> [1,B,2] -> [A,B,2]
    Then we compute the area of intersect between box_a and box_b.
    Args:
      box_a: (tensor) bounding boxes, Shape: [A,4].
      box_b: (tensor) bounding boxes, Shape: [B,4].
    Return:
      (tensor) intersection area, Shape: [A,B].
    """
    A = box_a.size(0)
    B = box_b.size(0)
    max_xy = torch.min(box_a[:, 2:].unsqueeze(1).expand(A, B, 2),
                       box_b[:, 2:].unsqueeze(0).expand(A, B, 2))
    min_xy = torch.max(box_a[:, :2].unsqueeze(1).expand(A, B, 2),
                       box_b[:, :2].unsqueeze(0).expand(A, B, 2))
    inter = torch.clamp((max_xy - min_xy), min=0)
    return inter[:, :, 0] * inter[:, :, 1]
    # inter[:, :, 0] is the width of intersection and inter[:, :, 1] is height


def jaccard(box_a, box_b):
    """Compute the jaccard overlap of two sets of boxes.  The jaccard overlap
    is simply the intersection over union of two boxes.  Here we operate on
    ground truth boxes and default boxes.
    E.g.:
        A ∩ B / A ∪ B = A ∩ B / (area(A) + area(B) - A ∩ B)
    Args:
        box_a: (tensor) Ground truth bounding boxes, Shape: [A,4]
        box_b: (tensor) Prior boxes from priorbox layers, Shape: [B,4]
    Return:
        jaccard overlap: (tensor) Shape: [A, B]
    """
    inter = intersect(box_a, box_b)
    area_a = ((box_a[:, 2]-box_a[:, 0]) *
              (box_a[:, 3]-box_a[:, 1])).unsqueeze(1).expand_as(inter)  # [A,B]
    area_b = ((box_b[:, 2]-box_b[:, 0]) *
              (box_b[:, 3]-box_b[:, 1])).unsqueeze(0).expand_as(inter)  # [A,B]
    union = area_a + area_b - inter
    return inter / union  # [A,B]

你们见到过的IoU计算方法都是怎样的呢?

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
在多标签分割任务中,通常使用交并比(IoU)或者Dice系数作为评价指标来衡量模型的性能。对于多标签分割任务,我们需要对每个类别分别计算评价指标,然后对所有类别的评价指标进行平均,得到最终的评价指标。 下面是一个基于PyTorch的多标签分割accuracy计算的示例代码: ```python import torch def iou(outputs: torch.Tensor, labels: torch.Tensor): """ 计算交并比(IoU) """ intersection = (outputs & labels).float().sum((1, 2)) # 计算交集 union = (outputs | labels).float().sum((1, 2)) # 计算并集 iou = (intersection + 1e-15) / (union + 1e-15) # 计算IoU,加上一个极小值防止分母为0 return iou.mean().item() # 对所有类别的IoU取平均值作为最终的IoU评价指标 def dice(outputs: torch.Tensor, labels: torch.Tensor): """ 计算Dice系数 """ intersection = (outputs & labels).float().sum((1, 2)) # 计算交集 dice = (2. * intersection + 1e-15) / (outputs.float().sum((1, 2)) + labels.float().sum((1, 2)) + 1e-15) # 计算Dice系数,加上一个极小值防止分母为0 return dice.mean().item() # 对所有类别的Dice系数取平均值作为最终的Dice评价指标 # 示例用法 outputs = torch.tensor([[1, 0, 1], [0, 1, 0], [1, 1, 0]]) # 模型输出 labels = torch.tensor([[1, 0, 0], [0, 1, 1], [1, 0, 1]]) # 标签 iou_score = iou(outputs, labels) dice_score = dice(outputs, labels) print(f"IoU: {iou_score:.4f}") print(f"Dice: {dice_score:.4f}") ``` 上述代码中,我们定义了两个评价指标函数:`iou`和`dice`。这两个函数的输入参数是模型输出`outputs`和标签`labels`,输出的是对应的评价指标。在这个示例中,`outputs`和`labels`都是$3 \times 3$的张量,每个元素表示一个像素的标签。我们计算了`outputs`和`labels`的IoU和Dice系数,并将结果打印出来。注意,这里的IoU和Dice系数都是针对所有类别的评价指标,因此没有分别计算每个类别的指标。如果需要分别计算每个类别的指标,可以在函数中增加一个类别维度,并在计算时对这个维度进行循环。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

laizi_laizi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值