目标检测中生成锚框函数详解

发布于:2023-09-16 ⋅ 阅读:(72) ⋅ 点赞:(0)
%matplotlib inline
import torch
from d2l import torch as d2l
torch.set_printoptions(2) # 让pytorch打印张量时,只打印到小数点后两位

将设一张图片,宽和高为2,2

X = torch.rand(size=(1,3,2,2))
Y = generate_anchors(X,sizes=[0.75,0.5,0.25],ratios=[1,2,0.5])

锚框中心点的设置

# 为每个像素可以生成 n+m-1个锚框,整个图像生成 wh(n+m-1)
def generate_anchors(data,sizes,ratios): # 书上的名字是 multibox_prior
    '''
    data:输入图像,sizes:缩放比 rations:宽高比
    :return: (批量数,锚框数量,4)
    '''
    '''1.数据准备'''
    # 图片的shape为(样本数,h,w),取出图片的h,w
    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)
    # 把缩放列表和宽高比列表转换为tensor格式
    size_tensor = torch.tensor(sizes,device=device)
    ratio_tensor = torch.tensor(ratios,device=device)

    '''设置锚框中心坐标 和 步长'''
    # 因为1像素的宽和高都是1,所以1像素的中心点是(0.5,0.5)
    offset_h,offset_w=0.5,0.5
    # 缩放步长
    steps_h = 1/in_height
    steps_w = 1/in_width
	# 不乘以步长,垂直方向上锚框的中心点
	center_h = (torch.arange(in_height,device=device) + offset_h)
	print(center_h)

tensor([0.50, 1.50])

    # 乘以步长时,垂直方向上锚框的中心点。
    center_h = (torch.arange(in_height,device=device) + offset_h) * steps_h
    print(center_h)

tensor([0.25, 0.75])

	# 不乘以步长,水平方向上锚框的中心点
	center_w = (torch.arange(in_width,device=device) + offset_w)
	print(center_w)

tensor([0.50, 1.50])

	# 乘以步长,水平方向上锚框的中心点
	center_w = (torch.arange(in_width,device=device) + offset_w) * steps_w
	print(center_w)

tensor([0.25, 0.75])

乘以步长和不乘步长,锚框中心点的区别

之所以要乘以步长,是为了对应/w,/h归一化后的锚框形状。

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

	# 生成锚框的所有中心点
	shift_y,shift_x = torch.meshgrid(center_h,center_w)
	print(f'shift_y = {shift_y}')
	print(f'shift_x = {shift_x}')

shift_y = tensor([[0.25, 0.25], [0.75, 0.75]])
shift_x = tensor([[0.25, 0.75], [0.25, 0.75]])

 #把tensor变成一维
	shift_y,shift_x = shift_y.reshape(-1),shift_x.reshape(-1)
	print(shift_y, shift_x)

tensor([0.25, 0.25, 0.75, 0.75]) tensor([0.25, 0.75, 0.25, 0.75])
请添加图片描述在这里插入图片描述

参考链接

https://zhuanlan.zhihu.com/p/455807888

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到