当前位置: 首页> 房产> 建筑 > anchors生成方式

anchors生成方式

时间:2025/7/12 6:16:11来源:https://blog.csdn.net/qq_30150579/article/details/140812635 浏览次数:0次

文章目录

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))  # 将输入转化为ndarrayscales = 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_strideshifts_x = np.arange(0, shape[1], anchor_stride) * feature_strideprint("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 npscales = 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生成方式

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: