锚框及代码

一、基于锚框的目标检测算法

1、提出多个锚框区域(算法猜的)

2、预测每个锚框中是否有关注的物体

3、如果有,预测锚框到真是边缘框的偏移

二、IoU交并比

三、锚框标号

1、每个锚框是一个训练样本

2、将每个锚框标注为背景或者关联上一个真实边缘框

3、算法会生成大量锚框(会导致大量负类样本)

四、使用非极大值抑制(NMS)输出

五、总结

1、我们以图像的每个像素为中心生成不同形状的锚框。

2、交并比(IoU)也被称为杰卡德系数,用于衡量两个边界框的相似性。它是相交面积与相并面积的比率。

3、在训练集中,我们需要给每个锚框两种类型的标签。一个是与锚框中目标检测的类别,另一个是锚框真实相对于边界框的偏移量。

六、代码

1、生成多个锚框

#@save
def multibox_prior(data, sizes, ratios):
    """生成以每个像素为中心具有不同形状的锚框"""
    # data图片->(batch_size, channels, height, width)
    # sizes锚框的大小占图片的百分比
    # ratio锚框的高低比
    in_height, in_width = data.shape[-2:]
    device, num_sizes, num_ratios = data.device, len(sizes), len(ratios)
    # boxes_per_pixel每个像素生成的锚框数量
    boxes_per_pixel = (num_sizes + num_ratios - 1)

    size_tensor = torch.tensor(sizes, device=device)
    ratio_tensor = torch.tensor(ratios, device=device)

    # 为了将锚点移动到像素的中心,需要设置偏移量。
    # 因为一个像素的高和宽都是 1,所以设置偏移量为 0.5 将锚点移动到像素的中心
    offset_h, offset_w = 0.5, 0.5
    steps_h = 1.0 / in_height  # 在y轴上的步长
    steps_w = 1.0 / in_width  # 在x轴上的步长

    # 生成锚框的所有中心点
    center_h = (torch.arange(in_height, device=device) + offset_h) * steps_h
    center_w = (torch.arange(in_width, device=device) + offset_w) * steps_w
    #生成一个二维网格,尺寸由 center_h 和 center_w 决定,表示高度和宽度的张量或数组;参数 indexing='ij' 指第一个返回的数组是行坐标,第二个是列坐标
    shift_y, shift_x = torch.meshgrid(center_h, center_w, indexing='ij')
    #将每个网格中的行和列展平为一维张量
    shift_y, shift_x = shift_y.reshape(-1), shift_x.reshape(-1)

    #乘以 in_height / in_width 将这些宽度调整为适应输入图像的实际比例
    w = torch.cat((size_tensor * torch.sqrt(ratio_tensor[0]),
                   sizes[0] * torch.sqrt(ratio_tensor[1:])))\
                   * in_height / in_width  # 处理矩形输入
    h = torch.cat((size_tensor / torch.sqrt(ratio_tensor[0]),
                   sizes[0] / torch.sqrt(ratio_tensor[1:])))

    # 除以2来获得半高和半宽,复制 in_height * in_width次
    anchor_manipulations = torch.stack((-w, -h, w, h)).T.repeat(
                                        in_height * in_width, 1) / 2

    # 每个中心点都将有“boxes_per_pixel”个锚框,
    out_grid = torch.stack([shift_x, shift_y, shift_x, shift_y],
                dim=1).repeat_interleave(boxes_per_pixel, dim=0)
    output = out_grid + anchor_manipulations
    #返回所有的锚框
    return output.unsqueeze(0)

2、在图像上绘制多个边界框

# axes: Matplotlib 的 Axes 对象,用于绘制图像和边界框。
# bboxes: 边界框列表,每个边界框通常表示为 [xmin, ymin, xmax, ymax] 的形式。
def show_bboxes(axes, bboxes, labels=None, colors=None):
    """显示所有边界框"""
    #将输入对象转换为列表或元组
    def _make_list(obj, default_values=None):
        if obj is None:
            obj = default_values
        elif not isinstance(obj, (list, tuple)):
            obj = [obj]
        return obj

    labels = _make_list(labels)
    colors = _make_list(colors, ['b', 'g', 'r', 'm', 'c'])
    #使用 enumerate 遍历 bboxes 列表中的边界框。
    for i, bbox in enumerate(bboxes):
        color = colors[i % len(colors)]
        #将边界框坐标转换为 Matplotlib 的 Rectangle 对象 rect,并添加到 axes 中
        rect = d2l.bbox_to_rect(bbox.detach().numpy(), color)
        axes.add_patch(rect)
        #确保在提供了标签且标签列表长度足够长时,才执行下面的绘制文本的操作
        if labels and len(labels) > i:
            #根据当前边界框的颜色 color 来选择文本的颜色 text_color
            text_color = 'k' if color == 'w' else 'w'
            # 在 (rect.xy[0], rect.xy[1]) 位置处绘制文本;labels[i] 当前边界框对应的标签;va='center' 和 ha='center'设置文本的垂直和水平对齐方式为居中; fontsize=9 设置文本的字体大小为 9; color=text_color 设置文本的颜色
            axes.text(rect.xy[0], rect.xy[1], labels[i],
                      va='center', ha='center', fontsize=9, color=text_color,
                      #文本框的样式,facecolor 是背景色,lw 是边框宽度
                      bbox=dict(facecolor=color, lw=0))

3、box_iou函数将在这两个列表中计算它们成对的交并比

#@save
def box_iou(boxes1, boxes2):
    """计算两组锚框中成对的交并比"""
    # box_area 计算面积,box形状为(N, 4)的张量,每行代表一个框,其四个值分别是(xmin,ymin,xmax,ymax)
    box_area = lambda boxes: ((boxes[:, 2] - boxes[:, 0]) *
                              (boxes[:, 3] - boxes[:, 1]))
    areas1 = box_area(boxes1)
    areas2 = box_area(boxes2)
    #计算了两组框中每对框的交集的左上角坐标
    inter_upperlefts = torch.max(boxes1[:, None, :2], boxes2[:, :2])
    #计算了每对框的交集的右下角坐标
    inter_lowerrights = torch.min(boxes1[:, None, 2:], boxes2[:, 2:])
    #.clamp(min=0)保证不为负数
    inters = (inter_lowerrights - inter_upperlefts).clamp(min=0)
    #一个形状为 (N, M, 2) 的张量,其中 N 是 boxes1 的数量,M 是 boxes2 的数量,2= 表示每个交集框的宽度和高度
    #计算了每对框的交集的面积,即交集的宽度乘以高度
    inter_areas = inters[:, :, 0] * inters[:, :, 1]
    #计算了每对框的并集的面积,即两个框各自的面积之和减去它们的交集面积
    union_areas = areas1[:, None] + areas2 - inter_areas
    return inter_areas / union_areas

4、将真实边界框分配给锚框

#@save
def assign_anchor_to_bbox(ground_truth, anchors, device, iou_threshold=0.5):
    #iou_threshold: IoU 阈值,用于确定何时将锚框分配给真实边界框
    num_anchors, num_gt_boxes = anchors.shape[0], ground_truth.shape[0]
    jaccard = box_iou(anchors, ground_truth)
    anchors_bbox_map = torch.full((num_anchors,), -1, dtype=torch.long,
                                  device=device)
    # 计算每个锚框的最大 IoU 和对应的真实边界框索引
    max_ious, indices = torch.max(jaccard, dim=1)
    #根据 IoU 阈值筛选锚框,返回满足条件的索引
    anc_i = torch.nonzero(max_ious >= iou_threshold).reshape(-1)
    # 获取符合条件的真实边界框索引
    box_j = indices[max_ious >= iou_threshold]
    #将锚框分配给真实边界框
    anchors_bbox_map[anc_i] = box_j
    #每次迭代中将已经分配的列(真实边界框)的 IoU 值设为 -1
    col_discard = torch.full((num_anchors,), -1)
    #在每次迭代中将已经分配的行(锚框)的 IoU 值设为 -1
    row_discard = torch.full((num_gt_boxes,), -1)
    #num_gt_boxes 次,即真实边界框的数量。
    for _ in range(num_gt_boxes):
        #找到最大 IoU 的索引
        max_idx = torch.argmax(jaccard)
        #计算对应的真实边界框和锚框的索引
        #行索引(锚框索引)
        box_idx = (max_idx % num_gt_boxes).long()
        #列索引(真实边界框索引)
        anc_idx = (max_idx / num_gt_boxes).long()
        #更新锚框-真实边界框映射
        anchors_bbox_map[anc_idx] = box_idx
        #更新 jaccard 矩阵,避免重复分配
        jaccard[:, box_idx] = col_discard
        jaccard[anc_idx, :] = row_discard
    return anchors_bbox_map

5、标记类别和偏移量

#@save
def offset_boxes(anchors, assigned_bb, eps=1e-6):
    """对锚框偏移量的转换,offset_boxes 函数计算每个锚框与其分配的真实边界框之间的偏移量。这个偏移量表示为中心坐标的偏移量和宽高的偏移量,用于训练目标检测模型时的回归任务"""
    #anc锚框,assigned_bb 是与每个锚框对应的真实边界框
    c_anc = d2l.box_corner_to_center(anchors)
    c_assigned_bb = d2l.box_corner_to_center(assigned_bb)
    #(c_assigned_bb[:, :2] - c_anc[:, :2]) 计算分配的真实边界框与锚框的中心坐标差
    #/ c_anc[:, 2:]将中心坐标差除以锚框的宽度和高度(w, h),进行归一化,10.5用于加速训练
    offset_xy = 10 * (c_assigned_bb[:, :2] - c_anc[:, :2]) / c_anc[:, 2:]
    offset_wh = 5 * torch.log(eps + c_assigned_bb[:, 2:] / c_anc[:, 2:])
    #将中心坐标偏移量 offset_xy和宽度高度偏移量 offset_wh合并为一个张量 offset
    offset = torch.cat([offset_xy, offset_wh], axis=1)
    return offset
#@save
def multibox_target(anchors, labels):
    """使用真实边界框标记锚框"""

    #1. 初始化
    # anchors.squeeze(0)去掉第一个维度(通常是batchsizes 1)的锚框张量
    batch_size, anchors = labels.shape[0], anchors.squeeze(0)
    #偏移量、掩码和类别标签;掩码:计算锚框偏移量的损失时,只考虑那些与真实边界框有匹配的锚框
    batch_offset, batch_mask, batch_class_labels = [], [], []
    device, num_anchors = anchors.device, anchors.shape[0]

    #2. 遍历每张图像
    for i in range(batch_size):
        label = labels[i, :, :]
        # 将每个锚框分配给最接近的真实边界框,返回每个锚框分配到的真实边界框的索引
        anchors_bbox_map = assign_anchor_to_bbox(label[:, 1:], anchors, device)

        #3. 创建掩码
        #如果锚框有分配的真实边界框,则值为1,否则为0
        #生成布尔掩码->转换为浮点型->增加维度->将张量沿着最后一个维度重复 4 次
        bbox_mask = ((anchors_bbox_map >= 0).float().unsqueeze(-1)).repeat(1, 4)

        #4. 初始化类别标签和分配的边界框坐标
        # 将类标签(背景类)和分配的边界框坐标(无分配的边界框)初始化为零
        class_labels = torch.zeros(num_anchors, dtype=torch.long,device=device)
        assigned_bb = torch.zeros((num_anchors, 4), dtype=torch.float32,device=device)

        #5. 标记类别和分配边界框
        # 使用真实边界框来标记锚框的类别,如果一个锚框没有被分配,标记其为背景(值为零)
        #indices_true找到所有有分配的锚框的索引
        indices_true = torch.nonzero(anchors_bbox_map >= 0)
        # bb_idx 找到这些锚框分配到的真实边界框的索引
        bb_idx = anchors_bbox_map[indices_true]
        #将有分配的锚框的类别标签标记为对应的真实边界框的类别
        class_labels[indices_true] = label[bb_idx, 0].long() + 1
        #将有分配的锚框的坐标标记为对应的真实边界框的坐标
        assigned_bb[indices_true] = label[bb_idx, 1:]

        #6. 计算偏移量并存储
        # 计算每个锚框相对于分配的真实边界框的偏移量,并应用掩码
        offset = offset_boxes(anchors, assigned_bb) * bbox_mask
        batch_offset.append(offset.reshape(-1))
        batch_mask.append(bbox_mask.reshape(-1))
        batch_class_labels.append(class_labels)

    #7.返回批量数据
    bbox_offset = torch.stack(batch_offset)
    bbox_mask = torch.stack(batch_mask)
    class_labels = torch.stack(batch_class_labels)
    return (bbox_offset, bbox_mask, class_labels)

6、将锚框和偏移量预测作为输入,并应用逆偏移变换来返回预测的边界框坐标

#@save
def offset_inverse(anchors, offset_preds):
    """根据带有预测偏移量的锚框来预测边界框"""
    anc = d2l.box_corner_to_center(anchors)
    #计算预测的边界框中心坐标
    # 计算预测的边界框中心坐标 = (中心坐标的偏移 * 提取锚框的宽和高 / 缩放比例)(偏移量) + 中心点坐标
    pred_bbox_xy = (offset_preds[:, :2] * anc[:, 2:] / 10) + anc[:, :2]
    #计算预测的边界框宽度和高度
    pred_bbox_wh = torch.exp(offset_preds[:, 2:] / 5) * anc[:, 2:]

    pred_bbox = torch.cat((pred_bbox_xy, pred_bbox_wh), axis=1)
    predicted_bbox = d2l.box_center_to_corner(pred_bbox)
    return predicted_bbox

7、按降序对置信度进行排序并返回其索引

#@save
def nms(boxes, scores, iou_threshold):
    #boxes边界框: (N, 4);scores置信度得分: (N,);iou_threshold: 用于过滤重叠边界框的 IoU 阈值,只有 IoU 小于该阈值的边界框才会被保留
    """对预测边界框的置信度进行排序"""
    #按照 scores 从高到低对边界框进行排序,返回排序后的索引
    B = torch.argsort(scores, dim=-1, descending=True)
    keep = []  # 保留预测边界框的指标
    while B.numel() > 0:
        #选择当前置信最高的边界框加入到keep中
        i = B[0]
        keep.append(i)
        #如果只剩最后一个边界框,跳出循环
        if B.numel() == 1: break
        iou = box_iou(boxes[i, :].reshape(-1, 4),
                      boxes[B[1:], :].reshape(-1, 4)).reshape(-1)
        inds = torch.nonzero(iou <= iou_threshold).reshape(-1)
        B = B[inds + 1]
    return torch.tensor(keep, device=boxes.device)

8、非极大值抑制应用于预测边界框

#@save
#cls_probs每个类别的置信度概率: (batch_size, num_classes, num_anchors)
#offset_preds每个边界框的偏移预测:  (batch_size, 4 * num_anchors)
#nms_threshold: 非极大值抑制中的 IoU 阈值
#pos_threshold: 用于过滤背景类别的置信度阈值
def multibox_detection(cls_probs, offset_preds, anchors, nms_threshold=0.5,
                       pos_threshold=0.009999999):
    """使用非极大值抑制来预测边界框"""
    device, batch_size = cls_probs.device, cls_probs.shape[0]
    anchors = anchors.squeeze(0)
    num_classes, num_anchors = cls_probs.shape[1], cls_probs.shape[2]
    out = []

    for i in range(batch_size):
        #取类别概率及偏移预测值
        cls_prob, offset_pred = cls_probs[i], offset_preds[i].reshape(-1, 4)
        #计算除背景类别外的最高置信度和对应的类别索引,这里假设背景类别是第一个类别(索引为 0)
        conf, class_id = torch.max(cls_prob[1:], 0)
        #将偏移预测应用于锚框,得到预测的边界框坐标
        predicted_bb = offset_inverse(anchors, offset_pred)
        # 根据预测的边界框和置信度进行非极大值抑制,得到保留的边界框索引
        keep = nms(predicted_bb, conf, nms_threshold)

        # 找到所有的non_keep索引,并将类设置为背景
        all_idx = torch.arange(num_anchors, dtype=torch.long, device=device)
        combined = torch.cat((keep, all_idx))
        #通过 unique 方法找到不重复的索引找到唯一的元素,non_keep将包含那些在原数组中只出现了一次的元素,即背景
        uniques, counts = combined.unique(return_counts=True)
        non_keep = uniques[counts == 1]
        #合并保留框和非保留框的索引
        all_id_sorted = torch.cat((keep, non_keep))
        #将非保留框的类别 ID 设置为 -1(背景)
        class_id[non_keep] = -1
        #根据排序结果更新类别 ID、置信度和边界框
        class_id = class_id[all_id_sorted]
        conf, predicted_bb = conf[all_id_sorted], predicted_bb[all_id_sorted]
        # pos_threshold是一个用于非背景预测的阈值,过滤出置信度低于 pos_threshold 的框,并将其类别 ID 设置为 -1
        below_min_idx = (conf < pos_threshold)
        class_id[below_min_idx] = -1
        conf[below_min_idx] = 1 - conf[below_min_idx]

        pred_info = torch.cat((class_id.unsqueeze(1),
                               conf.unsqueeze(1),
                               predicted_bb), dim=1)
        out.append(pred_info)
    return torch.stack(out)
  • 21
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值