anchors生成方式

文章目录

scales = (32, 64, 128, 256, 512)  # 定义了锚框的尺寸
ratios = [0.5, 1, 2]  # 定义了锚框的高宽比率
shape = (2, 2)  # 定义了输入feature map的尺寸,为了方便观测,设置为(2,2)
feature_stride = 16  # 定义了原图像尺寸与feature map尺寸的比率,这里设置原图像缩小16倍后生成了feature map
anchor_stride = 1  # 在feature map上每个锚框点之间的间距
    scales, ratios = np.meshgrid(np.array(scales), np.array(ratios))  # 将输入转化为ndarray
    scales = scales.flatten()  # 转换为一维数组
    ratios = ratios.flatten()  # 转换为一维数组
    # 获得所有框的长度和比例的组合
    heights = scales / np.sqrt(ratios)  
    print("heights:")
    print(heights)
    widths = scales * np.sqrt(ratios)
    print("widths:")
    print(widths)

如下图所示,生成的高宽俩俩组合,就获得了所有框的长度和比例的组合

在这里插入图片描述

将每个锚框的轴坐标点(一维的形式)通过meshgrid变换成二维的轴坐标点

    # 生成网格中心
    shifts_y = np.arange(0, shape[0], anchor_stride) * feature_stride
    shifts_x = np.arange(0, shape[1], anchor_stride) * feature_stride
    print("shifts_x:")
    print(shifts_x)
    print("shifts_y:")
    print(shifts_y)
    shifts_x, shifts_y = np.meshgrid(shifts_x, shifts_y)
    print("shifts_x:")
    print(shifts_x)
    print("shifts_y:")
    print(shifts_y)

在这里插入图片描述
在这里插入图片描述

通过meshgrid把15种锚框的尺寸和锚框的4个坐标相结合,则总共生成了4x15(4,15)个锚框,然后再利用np.stack把锚框的尺寸和坐标整合成(60,2)的数组,方便使用

    # 获得先验框的中心和宽高
    box_widths, box_centers_x = np.meshgrid(widths, shifts_x)
    box_heights, box_centers_y = np.meshgrid(heights, shifts_y)
    print("box_widths:")
    print(box_widths)
    print("box_centers_x:")
    print(box_centers_x)
    print("box_widths:")
    print(box_widths)
    print("box_centers_y:")
    print(box_centers_y)
    # 更变格式
    box_centers = np.stack([box_centers_y, box_centers_x], axis=2).reshape([-1, 2])
    box_sizes = np.stack([box_heights, box_widths], axis=2).reshape([-1, 2])
    print("box_centers:")
    print(box_centers)
    print("box_sizes:")
    print(box_sizes)

每个锚框的宽和中心点x坐标

在这里插入图片描述

每个锚框的高和中心点y坐标

在这里插入图片描述

    # 计算出(y1, x1, y2, x2)
    boxes = np.concatenate([box_centers - 0.5 * box_sizes, box_centers + 0.5 * box_sizes], axis=1)

最后,再将锚框的坐标转换为左上角和右下角的形式

import numpy as np

scales = 32  # 定义了锚框的尺寸
ratios = [0.5, 1, 2]  # 定义了锚框的高宽比率
shape = (2, 2)  # 定义了输入feature map的尺寸,为了方便观测,设置为(2,2)
feature_stride = 16  # 定义了原图像尺寸与feature map尺寸的比率,这里设置原图像缩小16倍后生成了feature map
anchor_stride = 1  # 在feature map上每个锚框点之间的间距

scales, ratios = np.meshgrid(np.array(scales), np.array(ratios))  # 将输入转化为ndarray
scales = scales.flatten()  # 转换为一维数组
ratios = ratios.flatten()  # 转换为一维数组
# 获得所有框的长度和比例的组合
heights = scales / np.sqrt(ratios)
print("heights:")
print(heights)
widths = scales * np.sqrt(ratios)
print("widths:")
print(widths)

# 生成网格中心
shifts_y = np.arange(0, shape[0], anchor_stride) * feature_stride
shifts_x = np.arange(0, shape[1], anchor_stride) * feature_stride
print("shifts_x:")
print(shifts_x)
print("shifts_y:")
print(shifts_y)
shifts_x, shifts_y = np.meshgrid(shifts_x, shifts_y)
print("shifts_x:")
print(shifts_x)
print("shifts_y:")
print(shifts_y)

# 获得先验框的中心和宽高
box_widths, box_centers_x = np.meshgrid(widths, shifts_x)
box_heights, box_centers_y = np.meshgrid(heights, shifts_y)
print("box_widths:")
print(box_widths)
print("box_centers_x:")
print(box_centers_x)
print("box_widths:")
print(box_widths)
print("box_centers_y:")
print(box_centers_y)
# 更变格式
box_centers = np.stack([box_centers_y, box_centers_x], axis=2).reshape([-1, 2])
box_sizes = np.stack([box_heights, box_widths], axis=2).reshape([-1, 2])
print("box_centers:")
print(box_centers)
print("box_sizes:")
print(box_sizes)

# 计算出(y1, x1, y2, x2)
boxes = np.concatenate([box_centers - 0.5 * box_sizes, box_centers + 0.5 * box_sizes], axis=1)
### Anchors索引的作用 Anchors索引在目标检测算法中扮演重要角色。这些预先设定的边界框用于预测物体的位置和大小,特别是在基于锚点的目标检测器中。通过定义一系列具有不同比例和宽高比的默认边界框,anchors能够覆盖图像中的各个位置,从而提高检测精度[^1]。 对于小、模糊以及部分遮挡的对象,如人脸,采用特定设计的anchors可以显著提升识别效果。例如,在一些研究工作中提到的方法中,不仅考虑了常规尺寸的人脸,也特别针对较小或不清晰的情况进行了优化处理[^2]。 ### Anchors索引的具体用法 具体来说,当构建一个基于anchor的目标检测模型时: - **初始化阶段**:设置一组固定的先验框作为初始anchors,它们分布在输入图片的不同位置,并拥有多种形状(宽度与高度的比例)。这组预设好的boxes会根据实际应用场景调整其数量及属性。 - **训练过程中**:每个ground truth box会被分配给最接近它的那个anchor,即两者之间IoU(Intersection over Union)最大的一对。接着利用回归损失函数指导网络学习如何微调这些选中的anchors使之更贴近真实标签;同时分类分支负责判断当前候选区域内是否存在目标对象及其类别信息。 - **推理期间**:经过充分训练后的神经网络可以直接输出修正过的proposals,再经由非极大抑制(NMS)去除冗余项最终得到精确定位的结果。 ```python import numpy as np def generate_anchors(base_size=16, ratios=[0.5, 1, 2], scales=2**np.arange(3, 6)): """ Generate anchor windows by enumerating aspect ratio, scales w.r.t a reference window. Args: base_size(int): Base size of the anchor relative to which widths and heights are defined. ratios(list[float]): Ratios between width and height of anchors. scales(array-like[int]): Scales used for scaling up/down the base anchor. Returns: array: An array containing all generated anchors with shape (num_ratios * num_scales, 4). Each row represents an anchor in format [x_center, y_center, w, h]. """ # ...省略具体的实现细节... ``` 此代码片段展示了如何生成一组基础anchors供后续操作使用。其中`base_size`指定了单个像素对应的实际物理单位长度,而`scales`则控制着整体缩放倍数范围内的变化情况。至于`ratios`参数,则决定了每类objects可能呈现出来的长宽关系特性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AoDeLuo

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

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

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

打赏作者

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

抵扣说明:

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

余额充值