Logo
首页
前沿
AI热点新闻
36氦头条
博客
开源
极简简历
极简智库
瞬间
相册
友链
知识库
创作者
搜索文章 Ctrl K

动手学深度学习

返回 0
  • 开始之前
    • 导读
    • 关于本书
    • 术语表
  • 前言
    • 前言
  • 安装
    • 安装
  • 符号
    • 符号
  • 引言
    • 引言
  • 预备知识
    • 预备知识
    • 数据操作
    • 数据预处理
    • 线性代数
    • 微积分
    • 自动微分
    • 概率
    • 查阅文档
  • 线性神经网络
    • 线性神经网络
    • 线性回归
    • 线性回归的从零开始实现
    • 线性回归的简洁实现
    • softmax回归
    • 图像分类数据集
    • softmax回归的从零开始实现
    • softmax回归的简洁实现
  • 多层感知机
    • 多层感知机
    • 多层感知机
    • 多层感知机的从零开始实现
    • 多层感知机的简洁实现
    • 模型选择、欠拟合和过拟合
    • 权重衰减
    • 暂退法(Dropout)
    • 前向传播、反向传播和计算图
    • 数值稳定性和模型初始化
    • 环境和分布偏移
    • 实战Kaggle比赛:预测房价
  • 深度学习计算
    • 深度学习计算
    • 层和块
    • 参数管理
    • 延后初始化
    • 自定义层
    • 读写文件
    • GPU
  • 卷积神经网络
    • 卷积神经网络
    • 从全连接层到卷积
    • 图像卷积
    • 填充和步幅
    • 多输入多输出通道
    • 汇聚层
    • 卷积神经网络(LeNet)
  • 现代卷积神经网络
    • 现代卷积神经网络
    • 深度卷积神经网络(AlexNet)
    • 使用块的网络(VGG)
    • 网络中的网络(NiN)
    • 含并行连结的网络(GoogLeNet)
    • 批量规范化
    • 残差网络(ResNet)
    • 稠密连接网络(DenseNet)
  • 循环神经网络
    • 循环神经网络
    • 序列模型
    • 文本预处理
    • 语言模型和数据集
    • 循环神经网络
    • 循环神经网络的从零开始实现
    • 循环神经网络的简洁实现
    • 通过时间反向传播
  • 现代循环神经网络
    • 现代循环神经网络
    • 门控循环单元(GRU)
    • 长短期记忆网络(LSTM)
    • 深度循环神经网络
    • 双向循环神经网络
    • 机器翻译与数据集
    • 编码器-解码器架构
    • 序列到序列学习(seq2seq)
    • 束搜索
  • 注意力机制
    • 注意力机制
    • 注意力提示
    • 注意力汇聚:Nadaraya-Watson核回归
    • 注意力评分函数
    • Bahdanau注意力
    • 多头注意力
    • 自注意力和位置编码
    • Transformer
  • 优化算法
    • 优化算法
    • 优化和深度学习
    • 凸性
    • 梯度下降
    • 随机梯度下降
    • 小批量随机梯度下降
    • 动量法
    • AdaGrad算法
    • RMSProp算法
    • Adadelta
    • Adam算法
    • 学习率调度器
  • 计算性能
    • 计算性能
    • 编译器和解释器
    • 异步计算
    • 自动并行
    • 硬件
    • 多GPU训练
    • 多GPU的简洁实现
    • 参数服务器
  • 计算机视觉
    • 计算机视觉
    • 图像增广
    • 微调
    • 目标检测和边界框
    • 锚框
    • 多尺度目标检测
    • 目标检测数据集
    • 单发多框检测(SSD)
    • 区域卷积神经网络(R-CNN)系列
    • 语义分割和数据集
    • 转置卷积
    • 全卷积网络
    • 风格迁移
    • 实战Kaggle比赛:图像分类(CIFAR-10)
    • 实战Kaggle比赛:狗的品种识别(ImageNetDogs)
  • 自然语言处理预训练
    • 自然语言处理:预训练
    • 词嵌入(word2vec)
    • 近似训练
    • 用于预训练词嵌入的数据集
    • 预训练word2vec
    • 全局向量的词嵌入(GloVe)
    • 子词嵌入
    • 词的相似性和类比任务
    • 来自Transformers的双向编码器表示(BERT)
    • 用于预训练BERT的数据集
    • 预训练BERT
  • 自然语言处理应用
    • 自然语言处理:应用
    • 情感分析及数据集
    • 情感分析:使用循环神经网络
    • 情感分析:使用卷积神经网络
    • 自然语言推断与数据集
    • 自然语言推断:使用注意力
    • 针对序列级和词元级应用微调BERT
    • 自然语言推断:微调BERT
  • 附录深度学习工具
    • 附录:深度学习工具
    • 使用JupyterNotebook
    • 使用AmazonSageMaker
    • 使用AmazonEC2实例
    • 选择服务器和GPU
    • 为本书做贡献
    • `d2l`API文档
  • 参考文献
    • 参考文献
    • zreferences

锚框

目标检测算法通常会在输入图像中采样大量的区域,然后判断这些区域中是否包含我们感兴趣的目标,并调整区域边界从而更准确地预测目标的真实边界框(ground-truth bounding box)。 不同的模型使用的区域采样方法可能不同。 这里我们介绍其中的一种方法:以每个像素为中心,生成多个缩放比和宽高比(aspect ratio)不同的边界框。 这些边界框被称为锚框(anchor box)我们将在 (见上文图示)中设计一个基于锚框的目标检测模型。

首先,让我们修改输出精度,以获得更简洁的输出。

%matplotlib inline
from d2l import mxnet as d2l
from mxnet import gluon, image, np, npx

np.set_printoptions(2)  # 精简输出精度
npx.set_np()
# pytorch 版本
%matplotlib inline
from d2l import torch as d2l
import torch

torch.set_printoptions(2)  # 精简输出精度
# paddle 版本
%matplotlib inline
from d2l import paddle as d2l
import warnings
warnings.filterwarnings("ignore")
import paddle
import numpy as np

paddle.set_printoptions(2)  # 精简输出精度

生成多个锚框

假设输入图像的高度为hhh,宽度为www。 我们以图像的每个像素为中心生成不同形状的锚框:缩放比为s∈(0,1]s\in (0, 1]s∈(0,1],宽高比为r>0r > 0r>0。 那么[锚框的宽度和高度分别是hsrhs\sqrt{r}hsr​和hs/rhs/\sqrt{r}hs/r​。] 请注意,当中心位置给定时,已知宽和高的锚框是确定的。

要生成多个不同形状的锚框,让我们设置许多缩放比(scale)取值s1,…,sns_1,\ldots, s_ns1​,…,sn​和许多宽高比(aspect ratio)取值r1,…,rmr_1,\ldots, r_mr1​,…,rm​。 当使用这些比例和长宽比的所有组合以每个像素为中心时,输入图像将总共有whnmwhnmwhnm个锚框。 尽管这些锚框可能会覆盖所有真实边界框,但计算复杂性很容易过高。 在实践中,(我们只考虑)包含s1s_1s1​或r1r_1r1​的(组合:)

(** (s1,r1),(s1,r2),…,(s1,rm),(s2,r1),(s3,r1),…,(sn,r1).(s_1, r_1), (s_1, r_2), \ldots, (s_1, r_m), (s_2, r_1), (s_3, r_1), \ldots, (s_n, r_1).(s1​,r1​),(s1​,r2​),…,(s1​,rm​),(s2​,r1​),(s3​,r1​),…,(sn​,r1​). **)

也就是说,以同一像素为中心的锚框的数量是n+m−1n+m-1n+m−1。 对于整个输入图像,将共生成wh(n+m−1)wh(n+m-1)wh(n+m−1)个锚框。

上述生成锚框的方法在下面的multibox_prior函数中实现。 我们指定输入图像、尺寸列表和宽高比列表,然后此函数将返回所有的锚框。

#@save
def multibox_prior(data, sizes, ratios):
    """生成以每个像素为中心具有不同形状的锚框"""
    in_height, in_width = data.shape[-2:]
    device, num_sizes, num_ratios = data.ctx, len(sizes), len(ratios)
    boxes_per_pixel = (num_sizes + num_ratios - 1)
    size_tensor = d2l.tensor(sizes, ctx=device)
    ratio_tensor = d2l.tensor(ratios, ctx=device)

    # 为了将锚点移动到像素的中心,需要设置偏移量。
    # 因为一个像素的高为1且宽为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 = (d2l.arange(in_height, ctx=device) + offset_h) * steps_h
    center_w = (d2l.arange(in_width, ctx=device) + offset_w) * steps_w
    shift_x, shift_y = d2l.meshgrid(center_w, center_h)
    shift_x, shift_y = shift_x.reshape(-1), shift_y.reshape(-1)

    # 生成“boxes_per_pixel”个高和宽,
    # 之后用于创建锚框的四角坐标(xmin,xmax,ymin,ymax)
    w = np.concatenate((size_tensor * np.sqrt(ratio_tensor[0]),
                        sizes[0] * np.sqrt(ratio_tensor[1:]))) \
                        * in_height / in_width  # 处理矩形输入
    h = np.concatenate((size_tensor / np.sqrt(ratio_tensor[0]),
                        sizes[0] / np.sqrt(ratio_tensor[1:])))
    # 除以2来获得半高和半宽
    anchor_manipulations = np.tile(np.stack((-w, -h, w, h)).T,
                                   (in_height * in_width, 1)) / 2

    # 每个中心点都将有“boxes_per_pixel”个锚框,
    # 所以生成含所有锚框中心的网格,重复了“boxes_per_pixel”次
    out_grid = d2l.stack([shift_x, shift_y, shift_x, shift_y],
                         axis=1).repeat(boxes_per_pixel, axis=0)
    output = out_grid + anchor_manipulations
    return np.expand_dims(output, axis=0)
# pytorch 版本
#@save
def multibox_prior(data, sizes, ratios):
    """生成以每个像素为中心具有不同形状的锚框"""
    in_height, in_width = data.shape[-2:]
    device, num_sizes, num_ratios = data.device, len(sizes), len(ratios)
    boxes_per_pixel = (num_sizes + num_ratios - 1)
    size_tensor = d2l.tensor(sizes, device=device)
    ratio_tensor = d2l.tensor(ratios, device=device)

    # 为了将锚点移动到像素的中心,需要设置偏移量。
    # 因为一个像素的高为1且宽为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
    shift_y, shift_x = torch.meshgrid(center_h, center_w, indexing='ij')
    shift_y, shift_x = shift_y.reshape(-1), shift_x.reshape(-1)

    # 生成“boxes_per_pixel”个高和宽,
    # 之后用于创建锚框的四角坐标(xmin,xmax,ymin,ymax)
    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来获得半高和半宽
    anchor_manipulations = torch.stack((-w, -h, w, h)).T.repeat(
                                        in_height * in_width, 1) / 2

    # 每个中心点都将有“boxes_per_pixel”个锚框,
    # 所以生成含所有锚框中心的网格,重复了“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)
# paddle 版本
#@save
def multibox_prior(data, sizes, ratios):
    """生成以每个像素为中心具有不同形状的锚框"""
    in_height, in_width = data.shape[-2:]
    place, num_sizes, num_ratios = data.place, len(sizes), len(ratios)
    boxes_per_pixel = (num_sizes + num_ratios - 1)
    size_tensor = paddle.to_tensor(sizes, place=place)
    ratio_tensor = paddle.to_tensor(ratios, place=place)

    # 为了将锚点移动到像素的中心,需要设置偏移量。
    # 因为一个像素的的高为1且宽为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 = (paddle.arange(in_height) + offset_h) * steps_h
    center_w = (paddle.arange(in_width) + offset_w) * steps_w
    shift_y, shift_x = paddle.meshgrid(center_h, center_w)
    shift_y, shift_x = shift_y.reshape([-1]), shift_x.reshape([-1])

    # 生成“boxes_per_pixel”个高和宽,
    # 之后用于创建锚框的四角坐标(xmin,xmax,ymin,ymax)
    w = paddle.concat((size_tensor * paddle.sqrt(ratio_tensor[0]),
                       sizes[0] * paddle.sqrt(ratio_tensor[1:])))\
                       * in_height / in_width  # 处理矩形输入
    h = paddle.concat((size_tensor / paddle.sqrt(ratio_tensor[0]),
                   sizes[0] / paddle.sqrt(ratio_tensor[1:])))
    # 除以2来获得半高和半宽
    anchor_manipulations = paddle.tile(paddle.stack((-w, -h, w, h)).T,
                                        (in_height * in_width, 1)) / 2

    # 每个中心点都将有“boxes_per_pixel”个锚框,
    # 所以生成含所有锚框中心的网格,重复了“boxes_per_pixel”次
    out_grid = paddle.stack([shift_x, shift_y, shift_x, shift_y], axis=1)
    out_grid = paddle.tile(out_grid, repeat_times=[boxes_per_pixel]).reshape((-1, out_grid.shape[1]))
    output = out_grid + anchor_manipulations
    return output.unsqueeze(0)

可以看到[返回的锚框变量Y的形状]是(批量大小,锚框的数量,4)。

img = image.imread('../img/catdog.jpg').asnumpy()
h, w = img.shape[:2]

print(h, w)
X = np.random.uniform(size=(1, 3, h, w))
Y = multibox_prior(X, sizes=[0.75, 0.5, 0.25], ratios=[1, 2, 0.5])
Y.shape
# pytorch 版本
img = d2l.plt.imread('../img/catdog.jpg')
h, w = img.shape[:2]

print(h, w)
X = torch.rand(size=(1, 3, h, w))
Y = multibox_prior(X, sizes=[0.75, 0.5, 0.25], ratios=[1, 2, 0.5])
Y.shape
# paddle 版本
img = d2l.plt.imread('../img/catdog.jpg')
h, w = img.shape[:2]

print(h, w)
X = paddle.rand(shape=(1, 3, h, w))
Y = multibox_prior(X, sizes=[0.75, 0.5, 0.25], ratios=[1, 2, 0.5])
Y.shape

将锚框变量Y的形状更改为(图像高度,图像宽度,以同一像素为中心的锚框的数量,4)后,我们可以获得以指定像素的位置为中心的所有锚框。 在接下来的内容中,我们[访问以(250,250)为中心的第一个锚框]。 它有四个元素:锚框左上角的(x,y)(x, y)(x,y)轴坐标和右下角的(x,y)(x, y)(x,y)轴坐标。 输出中两个轴的坐标各分别除以了图像的宽度和高度。

# mxnet, pytorch 版本
boxes = Y.reshape(h, w, 5, 4)
boxes[250, 250, 0, :]
# paddle 版本
boxes = Y.reshape([h, w, 5, 4])
boxes[250, 250, 0, :]

为了[显示以图像中以某个像素为中心的所有锚框],定义下面的show_bboxes函数来在图像上绘制多个边界框。

# all 版本
#@save
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(d2l.numpy(bbox), 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=9, color=text_color,
                      bbox=dict(facecolor=color, lw=0))

正如从上面代码中所看到的,变量boxes中xxx轴和yyy轴的坐标值已分别除以图像的宽度和高度。 绘制锚框时,我们需要恢复它们原始的坐标值。 因此,在下面定义了变量bbox_scale。 现在可以绘制出图像中所有以(250,250)为中心的锚框了。 如下所示,缩放比为0.75且宽高比为1的蓝色锚框很好地围绕着图像中的狗。

# all 版本
d2l.set_figsize()
bbox_scale = d2l.tensor((w, h, w, h))
fig = d2l.plt.imshow(img)
show_bboxes(fig.axes, boxes[250, 250, :, :] * bbox_scale,
            ['s=0.75, r=1', 's=0.5, r=1', 's=0.25, r=1', 's=0.75, r=2',
             's=0.75, r=0.5'])

[交并比(IoU)]

我们刚刚提到某个锚框“较好地”覆盖了图像中的狗。 如果已知目标的真实边界框,那么这里的“好”该如何如何量化呢? 直观地说,可以衡量锚框和真实边界框之间的相似性。 杰卡德系数(Jaccard)可以衡量两组之间的相似性。 给定集合A\mathcal{A}A和B\mathcal{B}B,他们的杰卡德系数是他们交集的大小除以他们并集的大小:

J(A,B)=∣A∩B∣∣A∪B∣.J(\mathcal{A},\mathcal{B}) = \frac{\left|\mathcal{A} \cap \mathcal{B}\right|}{\left| \mathcal{A} \cup \mathcal{B}\right|}.J(A,B)=∣A∪B∣∣A∩B∣​.

事实上,我们可以将任何边界框的像素区域视为一组像素。通 过这种方式,我们可以通过其像素集的杰卡德系数来测量两个边界框的相似性。 对于两个边界框,它们的杰卡德系数通常称为交并比(intersection over union,IoU),即两个边界框相交面积与相并面积之比,如 (见上文图示)所示。 交并比的取值范围在0和1之间:0表示两个边界框无重合像素,1表示两个边界框完全重合。

交并比是两个边界框相交面积与相并面积之比。

接下来部分将使用交并比来衡量锚框和真实边界框之间、以及不同锚框之间的相似度。 给定两个锚框或边界框的列表,以下box_iou函数将在这两个列表中计算它们成对的交并比。

#@save
def box_iou(boxes1, boxes2):
    """计算两个锚框或边界框列表中成对的交并比"""
    box_area = lambda boxes: ((boxes[:, 2] - boxes[:, 0]) *
                              (boxes[:, 3] - boxes[:, 1]))
    # boxes1,boxes2,areas1,areas2的形状:
    # boxes1:(boxes1的数量,4),
    # boxes2:(boxes2的数量,4),
    # areas1:(boxes1的数量,),
    # areas2:(boxes2的数量,)
    areas1 = box_area(boxes1)
    areas2 = box_area(boxes2)

    # inter_upperlefts,inter_lowerrights,inters的形状:
    # (boxes1的数量,boxes2的数量,2)
    inter_upperlefts = np.maximum(boxes1[:, None, :2], boxes2[:, :2])
    inter_lowerrights = np.minimum(boxes1[:, None, 2:], boxes2[:, 2:])
    inters = (inter_lowerrights - inter_upperlefts).clip(min=0)
    # inter_areasandunion_areas的形状:(boxes1的数量,boxes2的数量)
    inter_areas = inters[:, :, 0] * inters[:, :, 1]
    union_areas = areas1[:, None] + areas2 - inter_areas
    return inter_areas / union_areas
# pytorch 版本
#@save
def box_iou(boxes1, boxes2):
    """计算两个锚框或边界框列表中成对的交并比"""
    box_area = lambda boxes: ((boxes[:, 2] - boxes[:, 0]) *
                              (boxes[:, 3] - boxes[:, 1]))
    # boxes1,boxes2,areas1,areas2的形状:
    # boxes1:(boxes1的数量,4),
    # boxes2:(boxes2的数量,4),
    # areas1:(boxes1的数量,),
    # areas2:(boxes2的数量,)
    areas1 = box_area(boxes1)
    areas2 = box_area(boxes2)
    # inter_upperlefts,inter_lowerrights,inters的形状:
    # (boxes1的数量,boxes2的数量,2)
    inter_upperlefts = torch.max(boxes1[:, None, :2], boxes2[:, :2])
    inter_lowerrights = torch.min(boxes1[:, None, 2:], boxes2[:, 2:])
    inters = (inter_lowerrights - inter_upperlefts).clamp(min=0)
    # inter_areasandunion_areas的形状:(boxes1的数量,boxes2的数量)
    inter_areas = inters[:, :, 0] * inters[:, :, 1]
    union_areas = areas1[:, None] + areas2 - inter_areas
    return inter_areas / union_areas
# paddle 版本
#@save
def box_iou(boxes1, boxes2):
    """计算两个锚框或边界框列表中成对的交并比"""
    box_area = lambda boxes: ((boxes[:, 2] - boxes[:, 0]) *
                              (boxes[:, 3] - boxes[:, 1]))
    # boxes1,boxes2,areas1,areas2的形状:
    # boxes1:(boxes1的数量,4),
    # boxes2:(boxes2的数量,4),
    # areas1:(boxes1的数量,),
    # areas2:(boxes2的数量,)
    areas1 = box_area(boxes1)
    areas2 = box_area(boxes2)
    # inter_upperlefts,inter_lowerrights,inters的形状:
    # (boxes1的数量,boxes2的数量,2)
    inter_upperlefts = paddle.maximum(boxes1[:, None, :2], boxes2[:, :2])
    inter_lowerrights = paddle.minimum(boxes1[:, None, 2:], boxes2[:, 2:])
    inters = (inter_lowerrights - inter_upperlefts).clip(min=0)
    # inter_areasandunion_areas的形状:(boxes1的数量,boxes2的数量)
    inter_areas = inters[:, :, 0] * inters[:, :, 1]
    union_areas = areas1[:, None] + areas2 - inter_areas
    return inter_areas / union_areas

在训练数据中标注锚框

在训练集中,我们将每个锚框视为一个训练样本。 为了训练目标检测模型,我们需要每个锚框的类别(class)和偏移量(offset)标签,其中前者是与锚框相关的对象的类别,后者是真实边界框相对于锚框的偏移量。 在预测时,我们为每个图像生成多个锚框,预测所有锚框的类别和偏移量,根据预测的偏移量调整它们的位置以获得预测的边界框,最后只输出符合特定条件的预测边界框。

目标检测训练集带有真实边界框的位置及其包围物体类别的标签。 要标记任何生成的锚框,我们可以参考分配到的最接近此锚框的真实边界框的位置和类别标签。 下文将介绍一个算法,它能够把最接近的真实边界框分配给锚框。

[将真实边界框分配给锚框]

给定图像,假设锚框是A1,A2,…,AnaA_1, A_2, \ldots, A_{n_a}A1​,A2​,…,Ana​​,真实边界框是B1,B2,…,BnbB_1, B_2, \ldots, B_{n_b}B1​,B2​,…,Bnb​​,其中na≥nbn_a \geq n_bna​≥nb​。 让我们定义一个矩阵X∈Rna×nb\mathbf{X} \in \mathbb{R}^{n_a \times n_b}X∈Rna​×nb​,其中第iii行、第jjj列的元素xijx_{ij}xij​是锚框AiA_iAi​和真实边界框BjB_jBj​的IoU。 该算法包含以下步骤。

  1. 在矩阵X\mathbf{X}X中找到最大的元素,并将它的行索引和列索引分别表示为i1i_1i1​和j1j_1j1​。然后将真实边界框Bj1B_{j_1}Bj1​​分配给锚框Ai1A_{i_1}Ai1​​。这很直观,因为Ai1A_{i_1}Ai1​​和Bj1B_{j_1}Bj1​​是所有锚框和真实边界框配对中最相近的。在第一个分配完成后,丢弃矩阵中i1th{i_1}^\mathrm{th}i1​th行和j1th{j_1}^\mathrm{th}j1​th列中的所有元素。
  2. 在矩阵X\mathbf{X}X中找到剩余元素中最大的元素,并将它的行索引和列索引分别表示为i2i_2i2​和j2j_2j2​。我们将真实边界框Bj2B_{j_2}Bj2​​分配给锚框Ai2A_{i_2}Ai2​​,并丢弃矩阵中i2th{i_2}^\mathrm{th}i2​th行和j2th{j_2}^\mathrm{th}j2​th列中的所有元素。
  3. 此时,矩阵X\mathbf{X}X中两行和两列中的元素已被丢弃。我们继续,直到丢弃掉矩阵X\mathbf{X}X中nbn_bnb​列中的所有元素。此时已经为这nbn_bnb​个锚框各自分配了一个真实边界框。
  4. 只遍历剩下的na−nbn_a - n_bna​−nb​个锚框。例如,给定任何锚框AiA_iAi​,在矩阵X\mathbf{X}X的第ithi^\mathrm{th}ith行中找到与AiA_iAi​的IoU最大的真实边界框BjB_jBj​,只有当此IoU大于预定义的阈值时,才将BjB_jBj​分配给AiA_iAi​。

下面用一个具体的例子来说明上述算法。 如 (见上文图示)(左)所示,假设矩阵X\mathbf{X}X中的最大值为x23x_{23}x23​,我们将真实边界框B3B_3B3​分配给锚框A2A_2A2​。 然后,我们丢弃矩阵第2行和第3列中的所有元素,在剩余元素(阴影区域)中找到最大的x71x_{71}x71​,然后将真实边界框B1B_1B1​分配给锚框A7A_7A7​。 接下来,如 (见上文图示)(中)所示,丢弃矩阵第7行和第1列中的所有元素,在剩余元素(阴影区域)中找到最大的x54x_{54}x54​,然后将真实边界框B4B_4B4​分配给锚框A5A_5A5​。 最后,如 (见上文图示)(右)所示,丢弃矩阵第5行和第4列中的所有元素,在剩余元素(阴影区域)中找到最大的x92x_{92}x92​,然后将真实边界框B2B_2B2​分配给锚框A9A_9A9​。 之后,我们只需要遍历剩余的锚框A1,A3,A4,A6,A8A_1, A_3, A_4, A_6, A_8A1​,A3​,A4​,A6​,A8​,然后根据阈值确定是否为它们分配真实边界框。

将真实边界框分配给锚框。

此算法在下面的assign_anchor_to_bbox函数中实现。

#@save
def assign_anchor_to_bbox(ground_truth, anchors, device, iou_threshold=0.5):
    """将最接近的真实边界框分配给锚框"""
    num_anchors, num_gt_boxes = anchors.shape[0], ground_truth.shape[0]
    # 位于第i行和第j列的元素x_ij是锚框i和真实边界框j的IoU
    jaccard = box_iou(anchors, ground_truth)
    # 对于每个锚框,分配的真实边界框的张量
    anchors_bbox_map = np.full((num_anchors,), -1, dtype=np.int32, ctx=device)
    # 根据阈值,决定是否分配真实边界框
    max_ious, indices = np.max(jaccard, axis=1), np.argmax(jaccard, axis=1)
    anc_i = np.nonzero(max_ious >= iou_threshold)[0]
    box_j = indices[max_ious >= iou_threshold]
    anchors_bbox_map[anc_i] = box_j
    col_discard = np.full((num_anchors,), -1)
    row_discard = np.full((num_gt_boxes,), -1)
    for _ in range(num_gt_boxes):
        max_idx = np.argmax(jaccard)
        box_idx = (max_idx % num_gt_boxes).astype('int32')
        anc_idx = (max_idx / num_gt_boxes).astype('int32')
        anchors_bbox_map[anc_idx] = box_idx
        jaccard[:, box_idx] = col_discard
        jaccard[anc_idx, :] = row_discard
    return anchors_bbox_map
# pytorch 版本
#@save
def assign_anchor_to_bbox(ground_truth, anchors, device, iou_threshold=0.5):
    """将最接近的真实边界框分配给锚框"""
    num_anchors, num_gt_boxes = anchors.shape[0], ground_truth.shape[0]
    # 位于第i行和第j列的元素x_ij是锚框i和真实边界框j的IoU
    jaccard = box_iou(anchors, ground_truth)
    # 对于每个锚框,分配的真实边界框的张量
    anchors_bbox_map = torch.full((num_anchors,), -1, dtype=torch.long,
                                  device=device)
    # 根据阈值,决定是否分配真实边界框
    max_ious, indices = torch.max(jaccard, dim=1)
    anc_i = torch.nonzero(max_ious >= iou_threshold).reshape(-1)
    box_j = indices[max_ious >= iou_threshold]
    anchors_bbox_map[anc_i] = box_j
    col_discard = torch.full((num_anchors,), -1)
    row_discard = torch.full((num_gt_boxes,), -1)
    for _ in range(num_gt_boxes):
        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[:, box_idx] = col_discard
        jaccard[anc_idx, :] = row_discard
    return anchors_bbox_map
# paddle 版本
#@save
def assign_anchor_to_bbox(ground_truth, anchors, place, iou_threshold=0.5):
    """将最接近的真实边界框分配给锚框"""
    num_anchors, num_gt_boxes = anchors.shape[0], ground_truth.shape[0]
    # 位于第i行和第j列的元素x_ij是锚框i和真实边界框j的IoU
    jaccard = box_iou(anchors, ground_truth)
    # 对于每个锚框,分配的真实边界框的张量
    anchors_bbox_map = paddle.full((num_anchors,), -1, dtype=paddle.int64)
    # 根据阈值,决定是否分配真实边界框
    max_ious = paddle.max(jaccard, axis=1)
    indices = paddle.argmax(jaccard, axis=1)
    anc_i = paddle.nonzero(max_ious >= 0.5).reshape([-1])
    box_j = indices[max_ious >= 0.5]
    anchors_bbox_map[anc_i] = box_j
    col_discard = paddle.full((num_anchors,), -1)
    row_discard = paddle.full((num_gt_boxes,), -1)
    for _ in range(num_gt_boxes):
        max_idx = paddle.argmax(jaccard)
        box_idx = paddle.cast((max_idx % num_gt_boxes), dtype='int64')
        anc_idx = paddle.cast((max_idx / num_gt_boxes), dtype='int64')
        anchors_bbox_map[anc_idx] = box_idx
        jaccard[:, box_idx] = col_discard
        jaccard[anc_idx, :] = row_discard
    return anchors_bbox_map

标记类别和偏移量

现在我们可以为每个锚框标记类别和偏移量了。 假设一个锚框AAA被分配了一个真实边界框BBB。 一方面,锚框AAA的类别将被标记为与BBB相同。 另一方面,锚框AAA的偏移量将根据BBB和AAA中心坐标的相对位置以及这两个框的相对大小进行标记。 鉴于数据集内不同的框的位置和大小不同,我们可以对那些相对位置和大小应用变换,使其获得分布更均匀且易于拟合的偏移量。 这里介绍一种常见的变换。 [**给定框AAA和BBB,中心坐标分别为(xa,ya)(x_a, y_a)(xa​,ya​)和(xb,yb)(x_b, y_b)(xb​,yb​),宽度分别为waw_awa​和wbw_bwb​,高度分别为hah_aha​和hbh_bhb​,可以将AAA的偏移量标记为:

(xb−xawa−μxσx,yb−yaha−μyσy,log⁡wbwa−μwσw,log⁡hbha−μhσh),\left( \frac{ \frac{x_b - x_a}{w_a} - \mu_x }{\sigma_x}, \frac{ \frac{y_b - y_a}{h_a} - \mu_y }{\sigma_y}, \frac{ \log \frac{w_b}{w_a} - \mu_w }{\sigma_w}, \frac{ \log \frac{h_b}{h_a} - \mu_h }{\sigma_h}\right),(σx​wa​xb​−xa​​−μx​​,σy​ha​yb​−ya​​−μy​​,σw​logwa​wb​​−μw​​,σh​logha​hb​​−μh​​), **] 其中常量的默认值为 μx=μy=μw=μh=0,σx=σy=0.1\mu_x = \mu_y = \mu_w = \mu_h = 0, \sigma_x=\sigma_y=0.1μx​=μy​=μw​=μh​=0,σx​=σy​=0.1 , σw=σh=0.2\sigma_w=\sigma_h=0.2σw​=σh​=0.2。 这种转换在下面的 offset_boxes 函数中实现。

# all 版本
#@save
def offset_boxes(anchors, assigned_bb, eps=1e-6):
    """对锚框偏移量的转换"""
    c_anc = d2l.box_corner_to_center(anchors)
    c_assigned_bb = d2l.box_corner_to_center(assigned_bb)
    offset_xy = 10 * (c_assigned_bb[:, :2] - c_anc[:, :2]) / c_anc[:, 2:]
    offset_wh = 5 * d2l.log(eps + c_assigned_bb[:, 2:] / c_anc[:, 2:])
    offset = d2l.concat([offset_xy, offset_wh], axis=1)
    return offset

如果一个锚框没有被分配真实边界框,我们只需将锚框的类别标记为背景(background)。 背景类别的锚框通常被称为负类锚框,其余的被称为正类锚框。 我们使用真实边界框(labels参数)实现以下multibox_target函数,来[标记锚框的类别和偏移量](anchors参数)。 此函数将背景类别的索引设置为零,然后将新类别的整数索引递增一。

#@save
def multibox_target(anchors, labels):
    """使用真实边界框标记锚框"""
    batch_size, anchors = labels.shape[0], anchors.squeeze(0)
    batch_offset, batch_mask, batch_class_labels = [], [], []
    device, num_anchors = anchors.ctx, anchors.shape[0]
    for i in range(batch_size):
        label = labels[i, :, :]
        anchors_bbox_map = assign_anchor_to_bbox(
            label[:, 1:], anchors, device)
        bbox_mask = np.tile((np.expand_dims((anchors_bbox_map >= 0),
                                            axis=-1)), (1, 4)).astype('int32')
        # 将类标签和分配的边界框坐标初始化为零
        class_labels = d2l.zeros(num_anchors, dtype=np.int32, ctx=device)
        assigned_bb = d2l.zeros((num_anchors, 4), dtype=np.float32,
                                ctx=device)
        # 使用真实边界框来标记锚框的类别。
        # 如果一个锚框没有被分配,标记其为背景(值为零)
        indices_true = np.nonzero(anchors_bbox_map >= 0)[0]
        bb_idx = anchors_bbox_map[indices_true]
        class_labels[indices_true] = label[bb_idx, 0].astype('int32') + 1
        assigned_bb[indices_true] = label[bb_idx, 1:]
        # 偏移量转换
        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)
    bbox_offset = d2l.stack(batch_offset)
    bbox_mask = d2l.stack(batch_mask)
    class_labels = d2l.stack(batch_class_labels)
    return (bbox_offset, bbox_mask, class_labels)
# pytorch 版本
#@save
def multibox_target(anchors, labels):
    """使用真实边界框标记锚框"""
    batch_size, anchors = labels.shape[0], anchors.squeeze(0)
    batch_offset, batch_mask, batch_class_labels = [], [], []
    device, num_anchors = anchors.device, anchors.shape[0]
    for i in range(batch_size):
        label = labels[i, :, :]
        anchors_bbox_map = assign_anchor_to_bbox(
            label[:, 1:], anchors, device)
        bbox_mask = ((anchors_bbox_map >= 0).float().unsqueeze(-1)).repeat(
            1, 4)
        # 将类标签和分配的边界框坐标初始化为零
        class_labels = torch.zeros(num_anchors, dtype=torch.long,
                                   device=device)
        assigned_bb = torch.zeros((num_anchors, 4), dtype=torch.float32,
                                  device=device)
        # 使用真实边界框来标记锚框的类别。
        # 如果一个锚框没有被分配,标记其为背景(值为零)
        indices_true = torch.nonzero(anchors_bbox_map >= 0)
        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:]
        # 偏移量转换
        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)
    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)
# paddle 版本
#@save
def multibox_target(anchors, labels):
    """使用真实边界框标记锚框"""
    batch_size, anchors = labels.shape[0], anchors.squeeze(0)
    batch_offset, batch_mask, batch_class_labels = [], [], []
    place, num_anchors = anchors.place, anchors.shape[0]
    for i in range(batch_size):
        label = labels[i, :, :]
        anchors_bbox_map = assign_anchor_to_bbox(
            label[:, 1:], anchors, place)
        bbox_mask = paddle.tile(paddle.to_tensor((anchors_bbox_map >= 0), dtype='float32').unsqueeze(-1), (1, 4))
        # 将类标签和分配的边界框坐标初始化为零
        class_labels = paddle.zeros(paddle.to_tensor(num_anchors), dtype=paddle.int64)
        assigned_bb = paddle.zeros(paddle.to_tensor((num_anchors, 4)), dtype=paddle.float32)
        # 使用真实边界框来标记锚框的类别。
        # 如果一个锚框没有被分配,我们标记其为背景(值为零)
        indices_true = paddle.nonzero(anchors_bbox_map >= 0).numpy()
        bb_idx = anchors_bbox_map[indices_true].numpy()
        class_labels[indices_true] = label.numpy()[bb_idx, 0][:] + 1
        assigned_bb[indices_true] = label.numpy()[bb_idx, 1:]
        class_labels = paddle.to_tensor(class_labels)
        assigned_bb = paddle.to_tensor(assigned_bb)
        # 偏移量转换
        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)
    bbox_offset = paddle.stack(batch_offset)
    bbox_mask = paddle.stack(batch_mask)
    class_labels = paddle.stack(batch_class_labels)
    return (bbox_offset, bbox_mask, class_labels)

一个例子

下面通过一个具体的例子来说明锚框标签。 我们已经为加载图像中的狗和猫定义了真实边界框,其中第一个元素是类别(0代表狗,1代表猫),其余四个元素是左上角和右下角的(x,y)(x, y)(x,y)轴坐标(范围介于0和1之间)。 我们还构建了五个锚框,用左上角和右下角的坐标进行标记:A0,…,A4A_0, \ldots, A_4A0​,…,A4​(索引从0开始)。 然后我们[在图像中绘制这些真实边界框和锚框]。

# all 版本
ground_truth = d2l.tensor([[0, 0.1, 0.08, 0.52, 0.92],
                         [1, 0.55, 0.2, 0.9, 0.88]])
anchors = d2l.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']);

使用上面定义的multibox_target函数,我们可以[根据狗和猫的真实边界框,标注这些锚框的分类和偏移量]。 在这个例子中,背景、狗和猫的类索引分别为0、1和2。 下面我们为锚框和真实边界框样本添加一个维度。

labels = multibox_target(np.expand_dims(anchors, axis=0),
                         np.expand_dims(ground_truth, axis=0))
# pytorch 版本
labels = multibox_target(anchors.unsqueeze(dim=0),
                         ground_truth.unsqueeze(dim=0))
# paddle 版本
labels = multibox_target(anchors.unsqueeze(axis=0),
                         ground_truth.unsqueeze(axis=0))

返回的结果中有三个元素,都是张量格式。第三个元素包含标记的输入锚框的类别。

让我们根据图像中的锚框和真实边界框的位置来分析下面返回的类别标签。 首先,在所有的锚框和真实边界框配对中,锚框A4A_4A4​与猫的真实边界框的IoU是最大的。 因此,A4A_4A4​的类别被标记为猫。 去除包含A4A_4A4​或猫的真实边界框的配对,在剩下的配对中,锚框A1A_1A1​和狗的真实边界框有最大的IoU。 因此,A1A_1A1​的类别被标记为狗。 接下来,我们需要遍历剩下的三个未标记的锚框:A0A_0A0​、A2A_2A2​和A3A_3A3​。 对于A0A_0A0​,与其拥有最大IoU的真实边界框的类别是狗,但IoU低于预定义的阈值(0.5),因此该类别被标记为背景; 对于A2A_2A2​,与其拥有最大IoU的真实边界框的类别是猫,IoU超过阈值,所以类别被标记为猫; 对于A3A_3A3​,与其拥有最大IoU的真实边界框的类别是猫,但值低于阈值,因此该类别被标记为背景。

# all 版本
labels[2]

返回的第二个元素是掩码(mask)变量,形状为(批量大小,锚框数的四倍)。 掩码变量中的元素与每个锚框的4个偏移量一一对应。 由于我们不关心对背景的检测,负类的偏移量不应影响目标函数。 通过元素乘法,掩码变量中的零将在计算目标函数之前过滤掉负类偏移量。

# all 版本
labels[1]

返回的第一个元素包含了为每个锚框标记的四个偏移值。 请注意,负类锚框的偏移量被标记为零。

# all 版本
labels[0]

使用非极大值抑制预测边界框

在预测时,我们先为图像生成多个锚框,再为这些锚框一一预测类别和偏移量。 一个预测好的边界框则根据其中某个带有预测偏移量的锚框而生成。 下面我们实现了offset_inverse函数,该函数将锚框和偏移量预测作为输入,并[应用逆偏移变换来返回预测的边界框坐标]。

# all 版本
#@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 = d2l.exp(offset_preds[:, 2:] / 5) * anc[:, 2:]
    pred_bbox = d2l.concat((pred_bbox_xy, pred_bbox_wh), axis=1)
    predicted_bbox = d2l.box_center_to_corner(pred_bbox)
    return predicted_bbox

当有许多锚框时,可能会输出许多相似的具有明显重叠的预测边界框,都围绕着同一目标。 为了简化输出,我们可以使用非极大值抑制(non-maximum suppression,NMS)合并属于同一目标的类似的预测边界框。

以下是非极大值抑制的工作原理。 对于一个预测边界框BBB,目标检测模型会计算每个类别的预测概率。 假设最大的预测概率为ppp,则该概率所对应的类别BBB即为预测的类别。 具体来说,我们将ppp称为预测边界框BBB的置信度(confidence)。 在同一张图像中,所有预测的非背景边界框都按置信度降序排序,以生成列表LLL。然后我们通过以下步骤操作排序列表LLL。

  1. 从LLL中选取置信度最高的预测边界框B1B_1B1​作为基准,然后将所有与B1B_1B1​的IoU超过预定阈值ϵ\epsilonϵ的非基准预测边界框从LLL中移除。这时,LLL保留了置信度最高的预测边界框,去除了与其太过相似的其他预测边界框。简而言之,那些具有非极大值置信度的边界框被抑制了。
  2. 从LLL中选取置信度第二高的预测边界框B2B_2B2​作为又一个基准,然后将所有与B2B_2B2​的IoU大于ϵ\epsilonϵ的非基准预测边界框从LLL中移除。
  3. 重复上述过程,直到LLL中的所有预测边界框都曾被用作基准。此时,LLL中任意一对预测边界框的IoU都小于阈值ϵ\epsilonϵ;因此,没有一对边界框过于相似。
  4. 输出列表LLL中的所有预测边界框。

[以下nms函数按降序对置信度进行排序并返回其索引]。

#@save
def nms(boxes, scores, iou_threshold):
    """对预测边界框的置信度进行排序"""
    B = scores.argsort()[::-1]
    keep = []  # 保留预测边界框的指标
    while B.size > 0:
        i = B[0]
        keep.append(i)
        if B.size == 1: break
        iou = box_iou(boxes[i, :].reshape(-1, 4),
                      boxes[B[1:], :].reshape(-1, 4)).reshape(-1)
        inds = np.nonzero(iou <= iou_threshold)[0]
        B = B[inds + 1]
    return np.array(keep, dtype=np.int32, ctx=boxes.ctx)
# pytorch 版本
#@save
def nms(boxes, scores, iou_threshold):
    """对预测边界框的置信度进行排序"""
    B = torch.argsort(scores, dim=-1, descending=True)
    keep = []  # 保留预测边界框的指标
    while B.numel() > 0:
        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 d2l.tensor(keep, device=boxes.device)
# paddle 版本
#@save
def nms(boxes, scores, iou_threshold):
    """对预测边界框的置信度进行排序"""
    B = paddle.argsort(scores, axis=-1, descending=True)
    keep = []  # 保留预测边界框的指标
    while B.numel().item() > 0:
        i = B[0]
        keep.append(i.item())
        if B.numel().item() == 1: break
        iou = box_iou(boxes[i.numpy(), :].reshape([-1, 4]),
                      paddle.to_tensor(boxes.numpy()[B[1:].numpy(), :]).reshape([-1, 4])).reshape([-1])
        inds = paddle.nonzero(iou <= iou_threshold).numpy().reshape([-1])
        B = paddle.to_tensor(B.numpy()[inds + 1])
    return paddle.to_tensor(keep, place=boxes.place, dtype='int64')

我们定义以下multibox_detection函数来[将非极大值抑制应用于预测边界框]。 这里的实现有点复杂,请不要担心。我们将在实现之后,马上用一个具体的例子来展示它是如何工作的。

#@save
def multibox_detection(cls_probs, offset_preds, anchors, nms_threshold=0.5,
                       pos_threshold=0.009999999):
    """使用非极大值抑制来预测边界框"""
    device, batch_size = cls_probs.ctx, cls_probs.shape[0]
    anchors = np.squeeze(anchors, axis=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)
        conf, class_id = np.max(cls_prob[1:], 0), np.argmax(cls_prob[1:], 0)
        predicted_bb = offset_inverse(anchors, offset_pred)
        keep = nms(predicted_bb, conf, nms_threshold)

        # 找到所有的non_keep索引,并将类设置为背景
        all_idx = np.arange(num_anchors, dtype=np.int32, ctx=device)
        combined = d2l.concat((keep, all_idx))
        unique, counts = np.unique(combined, return_counts=True)
        non_keep = unique[counts == 1]
        all_id_sorted = d2l.concat((keep, non_keep))
        class_id[non_keep] = -1
        class_id = class_id[all_id_sorted].astype('float32')
        conf, predicted_bb = conf[all_id_sorted], predicted_bb[all_id_sorted]
        # pos_threshold是一个用于非背景预测的阈值
        below_min_idx = (conf < pos_threshold)
        class_id[below_min_idx] = -1
        conf[below_min_idx] = 1 - conf[below_min_idx]
        pred_info = d2l.concat((np.expand_dims(class_id, axis=1),
                                np.expand_dims(conf, axis=1),
                                predicted_bb), axis=1)
        out.append(pred_info)
    return d2l.stack(out)
# pytorch 版本
#@save
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)
        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))
        uniques, counts = combined.unique(return_counts=True)
        non_keep = uniques[counts == 1]
        all_id_sorted = torch.cat((keep, non_keep))
        class_id[non_keep] = -1
        class_id = class_id[all_id_sorted]
        conf, predicted_bb = conf[all_id_sorted], predicted_bb[all_id_sorted]
        # pos_threshold是一个用于非背景预测的阈值
        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 d2l.stack(out)
# paddle 版本
#@save
def multibox_detection(cls_probs, offset_preds, anchors, nms_threshold=0.5,
                       pos_threshold=0.009999999):
    """使用非极大值抑制来预测边界框"""
    batch_size = 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])
        conf = paddle.max(cls_prob[1:], 0)
        class_id = paddle.argmax(cls_prob[1:], 0)
        predicted_bb = offset_inverse(anchors, offset_pred)
        keep = nms(predicted_bb, conf, nms_threshold)

        # 找到所有的non_keep索引,并将类设置为背景
        all_idx = paddle.arange(num_anchors, dtype='int64')
        combined = paddle.concat((keep, all_idx))
        uniques, counts = combined.unique(return_counts=True)
        non_keep = uniques[counts == 1]
        all_id_sorted = paddle.concat([keep, non_keep])
        class_id[non_keep.numpy()] = -1
        class_id = class_id[all_id_sorted]
        conf, predicted_bb = conf[all_id_sorted], predicted_bb[all_id_sorted]
        # pos_threshold是一个用于非背景预测的阈值
        below_min_idx = (conf < pos_threshold)
        class_id[below_min_idx.numpy()] = -1
        conf[below_min_idx.numpy()] = 1 - conf[below_min_idx.numpy()]
        pred_info = paddle.concat((paddle.to_tensor(class_id, dtype='float32').unsqueeze(1),
                               paddle.to_tensor(conf, dtype='float32').unsqueeze(1),
                               predicted_bb), axis=1)
        out.append(pred_info)
    return paddle.stack(out)

现在让我们[将上述算法应用到一个带有四个锚框的具体示例中]。 为简单起见,我们假设预测的偏移量都是零,这意味着预测的边界框即是锚框。 对于背景、狗和猫其中的每个类,我们还定义了它的预测概率。

# mxnet, pytorch 版本
anchors = d2l.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 = d2l.tensor([0] * d2l.size(anchors))
cls_probs = d2l.tensor([[0] * 4,  # 背景的预测概率
                      [0.9, 0.8, 0.7, 0.1],  # 狗的预测概率
                      [0.1, 0.2, 0.3, 0.9]])  # 猫的预测概率
# paddle 版本
anchors = d2l.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 = d2l.tensor([0] * anchors.numel().item())
cls_probs = d2l.tensor([[0] * 4,  # 背景的预测概率
                      [0.9, 0.8, 0.7, 0.1],  # 狗的预测概率
                      [0.1, 0.2, 0.3, 0.9]])  # 猫的预测概率

我们可以[在图像上绘制这些预测边界框和置信度]。

# all 版本
fig = d2l.plt.imshow(img)
show_bboxes(fig.axes, anchors * bbox_scale,
            ['dog=0.9', 'dog=0.8', 'dog=0.7', 'cat=0.9'])

现在我们可以调用multibox_detection函数来执行非极大值抑制,其中阈值设置为0.5。 请注意,我们在示例的张量输入中添加了维度。

我们可以看到[返回结果的形状是(批量大小,锚框的数量,6)]。 最内层维度中的六个元素提供了同一预测边界框的输出信息。 第一个元素是预测的类索引,从0开始(0代表狗,1代表猫),值-1表示背景或在非极大值抑制中被移除了。 第二个元素是预测的边界框的置信度。 其余四个元素分别是预测边界框左上角和右下角的(x,y)(x, y)(x,y)轴坐标(范围介于0和1之间)。

output = multibox_detection(np.expand_dims(cls_probs, axis=0),
                            np.expand_dims(offset_preds, axis=0),
                            np.expand_dims(anchors, axis=0),
                            nms_threshold=0.5)
output
# pytorch 版本
output = multibox_detection(cls_probs.unsqueeze(dim=0),
                            offset_preds.unsqueeze(dim=0),
                            anchors.unsqueeze(dim=0),
                            nms_threshold=0.5)
output
# paddle 版本
output = multibox_detection(cls_probs.unsqueeze(axis=0),
                            offset_preds.unsqueeze(axis=0),
                            anchors.unsqueeze(axis=0),
                            nms_threshold=0.5)
output

删除-1类别(背景)的预测边界框后,我们可以[输出由非极大值抑制保存的最终预测边界框]。

# all 版本
fig = d2l.plt.imshow(img)
for i in d2l.numpy(output[0]):
    if i[0] == -1:
        continue
    label = ('dog=', 'cat=')[int(i[0])] + str(i[1])
    show_bboxes(fig.axes, [d2l.tensor(i[2:]) * bbox_scale], label)

实践中,在执行非极大值抑制前,我们甚至可以将置信度较低的预测边界框移除,从而减少此算法中的计算量。 我们也可以对非极大值抑制的输出结果进行后处理。例如,只保留置信度更高的结果作为最终输出。

小结

  • 我们以图像的每个像素为中心生成不同形状的锚框。
  • 交并比(IoU)也被称为杰卡德系数,用于衡量两个边界框的相似性。它是相交面积与相并面积的比率。
  • 在训练集中,我们需要给每个锚框两种类型的标签。一个是与锚框中目标检测的类别,另一个是锚框真实相对于边界框的偏移量。
  • 预测期间可以使用非极大值抑制(NMS)来移除类似的预测边界框,从而简化输出。

练习

  1. 在multibox_prior函数中更改sizes和ratios的值。生成的锚框有什么变化?
  2. 构建并可视化两个IoU为0.5的边界框。它们是怎样重叠的?
  3. 在 subsec_labeling-anchor-boxes和 subsec_predicting-bounding-boxes-nms中修改变量anchors,结果如何变化?
  4. 非极大值抑制是一种贪心算法,它通过移除来抑制预测的边界框。是否存在一种可能,被移除的一些框实际上是有用的?如何修改这个算法来柔和地抑制?可以参考Soft-NMS。
  5. 如果非手动,非最大限度的抑制可以被学习吗?

Discussions

目录
    共 165 篇 动手学深度学习 / 锚框 0 字 0 行 cosolar
    创建 2026-11-08T12:24:22.311943388Z 更新 2026-11-08T12:29:17.395305386Z
    Logo

    以极简为笔,以求索为墨。 简单书写,深度思考,每一次记录,都是与内心的对话。

    30,291 访问量
    219 点赞数
    0 评论数
    162 文章数 13 分类数 243 标签数
    2026-06-06 建站时间
    中国·北京 博主地址

    订阅更新

    订阅我们的技术文章,每周精选内容送达你的邮箱

    隐私政策 | 使用条款 | 友情链接 | 关于我们
    本主题由 腾讯云 提供云服务
    © 极简笔记 版权所有 津ICP备2026156702号-1
    Powered by Halo Theme halo-theme-cosolar