文章目录
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)