html选择框打勾代码_为多目标图像生成多个锚框并选择最佳预测边界框的代码实现

编码的实现环境是Python3.8.3、torch1.5、Anaconda3(64-bit)、PyCharm2020.1。是《动手学深度学习》(PyTorch版)的练习及作业,个别代码有修改,仅供交流学习之用。

e3d5ecaf17e0f4b5192147c245c48cf7.png
# 9.4 锚框# 目标检测算法通常会在输入图像中采样大量的区域,然后判断这些区域中是否包含我们感兴趣的目标,并调整区域边缘从而更准确地预测目标的真实边界框(ground-truth bounding box)。# 不同的模型使用的区域采样方法可能不同。这里介绍其中一种方法:以每个像素为中心生成多个大小和宽高比(aspect ratio)不同的边界框。这些边界框被称为锚框(anchor box)。# 导入一下相关包。from PIL import Imageimport numpy as npimport mathimport torchimport syssys.path.append("..")import d2lzh as d2lprint(torch.__version__)# 1 生成多个锚框# 假设输入图像高为h,宽为w。分别以图像的每个像素为中心生成不同形状的锚框。假设大小为s∈(0,1]且宽高比为r>0,那么锚框的宽和高将分别为ws√r和hs/r。# 当中心位置给定时,已知宽和高的锚框是确定的。# 下面分别设定好一组大小s1,…,sn和一组宽高比r1,…,rm。如果以每个像素为中心时使用所有的大小与宽高比的组合,输入图像将一共得到whnm个锚框。# 虽然这些锚框可能覆盖了所有的真实边界框,但计算复杂度容易过高。# 因此,通常只对包含s1或r1的大小与宽高比的组合感兴趣,即(s1,r1),(s1,r2),…,(s1,rm),(s2,r1),(s3,r1),…,(sn,r1).# 也就是说,以相同像素为中心的锚框的数量为n+m−1。对于整个输入图像,将一共生成wh(n+m−1)个锚框。img = Image.open('timg.jpg')w, h = img.sizeprint("图像宽w=%d,图像高h=%d" % (w, h))   #w = 629, h = 488# 定义函数MultiBoxPrior(),实现生成wh(n+m-1)个锚框def MultiBoxPrior(feature_map, sizes=[0.75, 0.5, 0.25], ratios=[1, 2, 0.5]):    """    如上所述,anchor表示成(xmin, ymin, xmax, ymax)    :param feature_map: torch tensor, Shape: [N, C, H, W].    :param sizes: List of sizes (0~1) of generated MultiBoxPriores.    :param ratios: List of aspect ratios (non-negative) of generated MultiBoxPriores.    :return: 锚框形状为(1, num_anchors, 4). 由于batch里每个都一样, 所以第一维为1    """    pairs = []  #锚框尺寸大小和比例(size, sqrt(ration))    for r in ratios:        pairs.append([sizes[0], math.sqrt(r)])    for s in sizes[1:]:        pairs.append([s, math.sqrt(ratios[0])])    pairs = np.array(pairs)    ss1 = pairs[:, 0] * pairs[:, 1] #size * sqrt(ration)    ss2 = pairs[:, 0] / pairs[:, 1] #size / sqrt(ration)    base_anchors = np.stack([-ss1, -ss2, ss1, ss2], axis=1) / 2    h, w = feature_map.shape[-2:]    shifts_x = np.arange(0, w) / w  #变量boxes中xx和yy轴的坐标值分别已除以图像的宽和高。    shifts_y = np.arange(0, h) / h  #变量boxes中xx和yy轴的坐标值分别已除以图像的宽和高。    shifts_x, shifts_y = np.meshgrid(shifts_x, shifts_y)    shifts_x = shifts_x.reshape(-1) #由矩阵变成一个array    shifts_y = shifts_y.reshape(-1)    shifts = np.stack((shifts_x, shifts_y, shifts_x, shifts_y), axis=1)    anchors = shifts.reshape((-1, 1, 4)) + base_anchors.reshape((1, -1, 4))    return torch.tensor(anchors, dtype=torch.float32).view(1, -1, 4)X = torch.Tensor(1, 3, h, w)Y = MultiBoxPrior(X, sizes=[0.75, 0.5, 0.25], ratios=[1, 2, 0.5])print("Y的形状:", Y.shape)     #torch.Size([1, 1534760, 4])boxes = Y.reshape((h, w, 5, 4))print(boxes[250, 250, 0, :])    ## 定义show_bboxes函数以便在图像上画出多个边界框,这样可以描绘图像中以某个像素为中心的所有锚框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'])    for i, bbox in enumerate(bboxes):        color = colors[i % len(colors)]        rect = d2l.bbox_to_rect(bbox.detach().cpu().numpy(), color)        axes.add_patch(rect)        if labels and len(labels) > i:            text_color = 'k' if color == 'w' else 'w'            axes.text(rect.xy[0], rect.xy[1], labels[i], va='center', ha='center', fontsize=6, color=text_color,                      bbox=dict(facecolor=color, lw=0))# 变量boxes中xx和yy轴的坐标值分别已除以图像的宽和高。在绘图时,需要恢复锚框的原始坐标值,并因此定义了变量bbox_scale。# 现在,可以画出图像中以(250, 250)为中心的所有锚框了。可以看到,大小为0.75且宽高比为1的锚框较好地覆盖了图像中的狗。fig = d2l.plt.imshow(img)bbox_scale = torch.tensor([[w, h, w, h]], dtype=torch.float32)show_bboxes(fig.axes,            boxes[250, 250, :, :] * bbox_scale,            ['s=0.75, r=1', 's=0.75, r=2', 's=0.55, r=0.5', 's=0.5, r=1', 's=0.25, r=1'])d2l.plt.show()# 2 交并比(Intersection over Union,IoU)# 如何量化某个锚框较好地覆盖了图像中的狗呢?一种直观方法是衡量锚框和真实边界框之间的相似度。# Jaccard系数(Jaccard index)可以衡量两个集合的相似度,即二者交集大小除以二者并集大小。# 当衡量两个边界框的相似度时,通常将Jaccard系数称为交并比(Intersection over Union,IoU),即两个边界框相交面积与相并面积之比。# 交并比的取值范围在0和1之间:0表示两个边界框无重合像素,1表示两个边界框相等。# 定义交并比的实现函数def compute_intersection(set_1, set_2):    """    计算anchor之间的交集.    :param set_1: 张量维度为(n1, 4), anchor表示成(xmin, ymin, xmax, ymax)    :param set_2: 张量维度为(n2, 4), anchor表示成(xmin, ymin, xmax, ymax)    :return: 边界框set1和set2的交集(intersection),维度为(n1, n2)。    """    # Pythorch自动广播单个维度    lower_bounds = torch.max(set_1[:, :2].unsqueeze(1), set_2[:, :2].unsqueeze(0))    upper_bounds = torch.min(set_1[:, 2:].unsqueeze(1), set_2[:, 2:].unsqueeze(0))    intersection_dims = torch.clamp(upper_bounds - lower_bounds, min=0)    return intersection_dims[:, :, 0] * intersection_dims[:, :, 1]def compute_jaccard(set_1, set_2):    """    计算anchor之间的Jaccard系数(IoU).    :param set_1: 张量维度为(n1, 4), anchor表示成(xmin, ymin, xmax, ymax)    :param set_2: 张量维度为(n2, 4), anchor表示成(xmin, ymin, xmax, ymax)    :return: 边界框set1和set2的交并比系数(IoU),维度为(n1, n2)。    """    # 找到交集    intersection = compute_intersection(set_1, set_2)    # 找出每个集合中的每个边界框的区域    areas_set_1 = (set_1[:, 2] - set_1[:, 0]) * (set_1[:, 3] - set_1[:, 1])    areas_set_2 = (set_2[:, 2] - set_2[:, 0]) * (set_2[:, 3] - set_2[:, 1])    # 找出共同部分    union = areas_set_1.unsqueeze(1) + areas_set_2.unsqueeze(0) - intersection    return intersection / union# 3 标注训练集的锚框# 在训练集中,将每个锚框视为一个训练样本。为了训练目标检测模型,需要为每个锚框标注两类标签:# 一是锚框所含目标的类别,简称类别;二是真实边界框相对锚框的偏移量,简称偏移量(offset)。# 在目标检测时,首先生成多个锚框,然后为每个锚框预测类别以及偏移量,接着根据预测的偏移量调整锚框位置从而得到预测边界框,最后筛选需要输出的预测边界框。# 如何为锚框分配与其相似的真实边界框呢?# 假设图像中锚框分别为A1, A2,…, Ana,真实边界框分别为B1, B2,…, Bnb,且na≥nb。定义矩阵X∈Rna×nb,其中第i行第j列的元素xij为锚框Ai与真实边界框Bj的交并比。# 首先找出矩阵X中最大元素,丢弃对应的行和列上其他元素;其次找出剩余的最大元素,并丢弃对应行和列上的其余元素。依次类推。# 演示一个具体的例子。为读取的图像中的猫和狗定义真实边界框,其中第一个元素为类别(0为狗,1为猫),剩余4个元素分别为左上角的xx和yy轴坐标以及右下角的xx和yy轴坐标(值域在0到1之间)。# 这里通过左上角和右下角的坐标构造了5个需要标注的锚框,分别记为A0,…,A4。先画出这些锚框与真实边界框在图像中的位置。bbox_scale = torch.tensor((w, h, w, h), dtype=torch.float32)ground_truth = torch.tensor([[0, 0.11, 0.05, 0.51, 0.97],                             [1, 0.52, 0.38, 0.89, 0.97]])anchors = torch.tensor([[0, 0.1, 0.2, 0.3],                        [0.15, 0.2, 0.4, 0.4],                        [0.63, 0.05, 0.88, 0.98],                        [0.66, 0.45, 0.8, 0.8],                        [0.57, 0.3, 0.92, 0.9]])fig = d2l.plt.imshow(img)show_bboxes(fig.axes, ground_truth[:, 1:] * bbox_scale, ['dog', 'cat'], 'k')show_bboxes(fig.axes, anchors * bbox_scale, ['0', '1', '2', '3', '4'])d2l.plt.show()# 定义函数MultiBoxTarget(),实现为锚框标注类别和偏移量。该函数将背景类别设为0,并令从零开始的目标类别的整数索引自加1(1为狗,2为猫)。def assign_anchor(bb, anchor, jaccard_threshold=0.5):    """    为每个anchor分配真实的bb, anchor表示成归一化(xmin, ymin, xmax, ymax)。    :param bb: 真实边界框(bounding box), shape:(nb, 4)    :param anchor: 待分配的anchor, shape:(na, 4)    :param jaccard_threshold: 预先设定的阈值    :return: assigned_idx: shape: (na, ), 每个anchor分配的真实bb对应的索引, 若未分配任何bb则为-1    """    na = anchor.shape[0]    nb = bb.shape[0]    jaccard = compute_jaccard(anchor, bb).detach().cpu().numpy()    #shape: (na, nb)    assigned_idx = np.ones(na) * -1 #初始值为-1    # 先为每个bb分配一个anchor(不要求满足jaccard_threshold)    jaccard_cp = jaccard.copy()    for j in range(nb):        i = np.argmax(jaccard_cp[:, j])        assigned_idx[i] = j        jaccard_cp[i, :] = float("-inf")    #赋值为负无穷, 相当于去掉这一行    # 处理还未被分配的anchor, 要求满足jaccard_threshold    for i in range(na):        if assigned_idx[i] == -1:            j = np.argmax(jaccard[i, :])            if jaccard[i, j] >= jaccard_threshold:                assigned_idx[i] = j    return torch.tensor(assigned_idx, dtype=torch.long)def xy_to_cxcy(xy):    """    将(x_min, y_min, x_max, y_max)形式的anchor转换成(center_x, center_y, w, h)形式.    :param xy: bounding boxes in boundary coordinates, 张量尺寸为(n_boxes, 4)    :return: bounding boxes in center-size coordinates, 张量尺寸为(n_boxes, 4)    """    return torch.cat([(xy[:, 2:] + xy[:, :2]) / 2,  #c_x, c_y                      xy[:, 2:] - xy[:, :2]],       #w, h                     1)def MultiBoxTarget(anchor, label):    """    anchor表示成归一化(xmin, ymin, xmax, ymax)。    :param anchor: 输入的锚框, 一般是通过MultiBoxPrior生成, 维度(shape)为(1,锚框总数,4)    :param label: 真实标签, 维度为(bn, 每张图片最多的真实锚框数, 5);                  第二维中,如果给定图片没有这么多锚框, 可以先用-1填充空白, 最后一维中的元素为[类别标签, 四个坐标值]    Returns: 列表, [bbox_offset, bbox_mask, cls_labels]        bbox_offset: 每个锚框的标注偏移量,形状为(bn,锚框总数*4)        bbox_mask: 形状同bbox_offset, 每个锚框的掩码, 一一对应上面的偏移量, 负类锚框(背景)对应的掩码均为0, 正类锚框的掩码均为1        cls_labels: 每个锚框的标注类别, 其中0表示为背景, 形状为(bn,锚框总数)    """    assert len(anchor.shape) == 3 and len(label.shape) == 3    bn = label.shape[0]    def MultiBoxTarget_one(anc, lab, eps=1e-6):        """        MultiBoxTarget函数的辅助函数, 处理batch中的一个。        :param anc: 维度为(锚框总数, 4)        :param lab: 维度为(真实锚框数, 5), 5代表[类别标签, 四个坐标值]        :param eps: 一个极小值, 防止log0        :return:            offset: (锚框总数*4, )            bbox_mask: (锚框总数*4, ), 0代表背景, 1代表非背景            cls_labels: (锚框总数, 4), 0代表背景        """        an = anc.shape[0]        assigned_idx = assign_anchor(lab[:, 1:], anc)   # (锚框总数, )        bbox_mask = ((assigned_idx >= 0).float().unsqueeze(-1)).repeat(1, 4)    # (锚框总数, 4)        cls_labels = torch.zeros(an, dtype=torch.long)  # 0表示背景        assigned_bb = torch.zeros((an, 4), dtype=torch.float32)  # 所有anchor对应的bb坐标        for i in range(an):            bb_idx = assigned_idx[i]            if bb_idx >= 0:     # 即非背景                cls_labels[i] = lab[bb_idx, 0].long().item() + 1    # 注意要加一                assigned_bb[i, :] = lab[bb_idx, 1:]        center_anc = xy_to_cxcy(anc)    # (center_x, center_y, w, h)        center_assigned_bb = xy_to_cxcy(assigned_bb)        offset_xy = 10.0 * (center_assigned_bb[:, :2] - center_anc[:, :2]) / center_anc[:, 2:]        offset_wh = 5.0 * torch.log(eps + center_assigned_bb[:, 2:] / center_anc[:, 2:])        offset = torch.cat([offset_xy, offset_wh], dim=1) * bbox_mask   # (锚框总数, 4)        return offset.view(-1), bbox_mask.view(-1), cls_labels    batch_offset = []    batch_mask = []    batch_cls_labels = []    for b in range(bn):        offset, bbox_mask, cls_labels = MultiBoxTarget_one(anchor[0, :, :], label[b, :, :])        batch_offset.append(offset)        batch_mask.append(bbox_mask)        batch_cls_labels.append(cls_labels)    bbox_offset = torch.stack(batch_offset)    bbox_mask = torch.stack(batch_mask)    cls_labels = torch.stack(batch_cls_labels)    return [bbox_offset, bbox_mask, cls_labels]# 通过unsqueeze函数为锚框和真实边界框添加样本维。/labels = MultiBoxTarget(anchors.unsqueeze(dim=0), ground_truth.unsqueeze(dim=0))# 返回结果里的第三项表示为锚框标注的类别。print(labels[2])# 返回值的第二项为掩码(mask)变量,形状为(批量大小, 锚框个数的四倍)。掩码变量中的元素与每个锚框的4个偏移量一一对应。print(labels[1])# 返回结果的第一项是为每个锚框标注的四个偏移量,其中负类锚框的偏移量标注为0。print(labels[0])# 4 输出预测边界框# 在模型预测阶段,先为图像生成多个锚框,并为这些锚框一一预测类别和偏移量。随后,我们根据锚框及其预测偏移量得到预测边界框。# 当锚框数量较多时,同一个目标上可能会输出较多相似的预测边界框。为了使结果更加简洁,可以移除相似的预测边界框。常用的方法叫作非极大值抑制(non-maximum suppression,NMS)。# 下面来看一个具体的例子。先构造4个锚框。简单起见,我们假设预测偏移量全是0:预测边界框即锚框。最后,我们构造每个类别的预测概率。anchors = torch.tensor([[0.1, 0.08, 0.52, 0.92],                        [0.08, 0.2, 0.56, 0.95],                        [0.15, 0.3, 0.62, 0.91],                        [0.55, 0.2, 0.9, 0.88]])offset_preds = torch.tensor([0.0] * (4 * len(anchors)))cls_probs = torch.tensor([[0., 0., 0., 0.,],        # 背景的预测概率                          [0.9, 0.8, 0.7, 0.1],     # 狗的预测概率                          [0.1, 0.2, 0.3, 0.9]])    # 猫的预测概率# 在图像上打印预测边界框和它们的置信度。fig = d2l.plt.imshow(img)show_bboxes(fig.axes, anchors * bbox_scale, ['dog=0.9', 'dog=0.8', 'dog=0.7', 'cat=0.9'])d2l.plt.show()# 下面实现MultiBoxDetection函数来执行非极大值抑制。from collections import namedtuplePred_BB_Info = namedtuple('Pred_BB_Info', ['index', 'class_id', 'confidence', 'xyxy'])def non_max_suppression(bb_info_list, nms_threshold=0.5):    """    非极大抑制处理预测的边界框。    :param bb_info_list: Pred_BB_Info的列表, 包含预测类别、置信度等信息    :param nms_threshold: 阈值    :return:        output: Pred_BB_Info的列表, 只保留过滤后的边界框信息    """    output = []    # 先根据置信度从高到低排序    sorted_bb_info_list = sorted(bb_info_list, key=lambda x: x.confidence, reverse=True)    # 循环遍历删除冗余输出,按置信度从大到小遍历每一个框    while len(sorted_bb_info_list) != 0:        best = sorted_bb_info_list.pop(0)   #pop(0)移除列表中的第一个元素,并返回该元素的值        output.append(best)        if len(sorted_bb_info_list) == 0:            break        bb_xyxy = []        for bb in sorted_bb_info_list:  #取出当前回合最大置信度的框之后,把剩余的框组合成一个列表            bb_xyxy.append(bb.xyxy)        # 计算当前回合最大置信度的框与其余所有框的交并比,来看它们之间的相似性。        iou = compute_jaccard(torch.tensor([best.xyxy]), torch.tensor(bb_xyxy))[0]  # shape: (len(sorted_bb_info_list), )        n = len(sorted_bb_info_list)        sorted_bb_info_list = [sorted_bb_info_list[i] for i in range(n) if iou[i] <= nms_threshold]    return output# MultiBoxDetection是一个总的生成阈值框的函数,是在训练好的模型之后的。def MultiBoxDetection(cls_prob, loc_pred, anchor, nms_threshold=0.5):    """    anchor表示成归一化(xmin, ymin, xmax, ymax)。    :param cls_prob: 经过softmax后得到的各个锚框的预测概率, shape:(bn, 预测总类别数+1, 锚框个数)    :param loc_pred: 预测的各个锚框的偏移量, shape:(bn, 锚框个数*4)    :param anchor: MultiBoxPrior输出的默认锚框, shape: (1, 锚框个数, 4)    :param nms_threshold: 非极大抑制中的阈值    :return:        所有锚框的信息, shape: (bn, 锚框个数, 6)        每个锚框信息由[class_id, confidence, xmin, ymin, xmax, ymax]表示        class_id=-1 表示背景或在非极大值抑制中被移除了    """    assert len(cls_prob.shape) == 3 and len(loc_pred.shape) == 2 and len(anchor.shape) == 3    bn = cls_prob.shape[0]    def MultiBoxDetection_one(c_p, l_p, anc, nms_threshold=0.5):        """        MultiBoxDetection的辅助函数, 处理batch中的一个        :param c_p:  (预测总类别数+1, 锚框个数)        :param l_p: (锚框个数*4, )        :param anc: (锚框个数, 4)        :param nms_threshold: 非极大抑制中的阈值        :return:            output: (锚框个数, 6)        """        pred_bb_num = c_p.shape[1]        anc = (anc + l_p.view(pred_bb_num, 4)).detach().cpu().numpy()   # 加上偏移量        confidence, class_id = torch.max(c_p, 0)        confidence = confidence.detach().cpu().numpy()  #置信度计算        class_id = class_id.detach().cpu().numpy()        pred_bb_info = [Pred_BB_Info(index=i,                                     class_id=class_id[i] - 1,    # 正类label从0开始                                     confidence=confidence[i],                                     xyxy=[*anc[i]])      # xyxy是个列表                        for i in range(pred_bb_num)]        # 正类的index        obj_bb_idx = [bb.index for bb in non_max_suppression(pred_bb_info, nms_threshold)]        output = []        for bb in pred_bb_info:            output.append([(bb.class_id if bb.index in obj_bb_idx else -1.0),                           bb.confidence,                           *bb.xyxy])        return torch.tensor(output)     # shape: (锚框个数, 6)    batch_output = []    for b in range(bn):        batch_output.append(MultiBoxDetection_one(cls_prob[b], loc_pred[b], anchor[0], nms_threshold))    return torch.stack(batch_output)# 运行MultiBoxDetection函数并设阈值为0.5。这里为输入都增加了样本维。# 我们看到,返回的结果的形状为(批量大小, 锚框个数, 6)。其中每一行的6个元素代表同一个预测边界框的输出信息。# 第一个元素是索引从0开始计数的预测类别(0为狗,1为猫),其中-1表示背景或在非极大值抑制中被移除。第二个元素是预测边界框的置信度。# 剩余的4个元素分别是预测边界框左上角的xx和yy轴坐标以及右下角的xx和yy轴坐标(值域在0到1之间)。output = MultiBoxDetection(cls_probs.unsqueeze(dim=0),                           offset_preds.unsqueeze(dim=0),                           anchors.unsqueeze(dim=0),                           nms_threshold=0.5)print("MultiBoxDetection函数运行结果:", output)# 移除掉类别为-1的预测边界框,并可视化非极大值抑制保留的结果。fig = d2l.plt.imshow(img)for i in output[0].detach().cpu().numpy():    if i[0] == -1:        continue    label = ('dog=', 'cat=')[int(i[0])] + str(i[1])    show_bboxes(fig.axes, [torch.tensor(i[2:]) * bbox_scale], label)d2l.plt.show()
00411fbcd4ec0826a094b3740e266ff5.png
8077ea3633a4b7d07f05819d0e42fa48.png
2de44a85f9bb75c492b265af801531bb.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值