anchor实践

自己敲了一遍才彻底明白anchor是咋操作的,一直想趁假期的时间把faster复现一下…

import torch
import numpy as np
ratios=[0.5,1,2]#长与宽之比 原图对应16*16 将其缩放的比例 有 11*23,16*16,23*11
scales=[8,16,32]#在原图上生成128*128,256*256,512*512的anchor,如128*128是16*16放大8倍,0.5比例则为88*184
size=16 #特征图上一点代表原图多大的区域
cx=size/2.
cy=size/2.#算出每个anchor的中心坐标
def generate_anchor(size,ratios,scales):#生成anchor
    anchor=np.zeros((len(ratios)*len(scales),4),dtype=float)
    for i in range(len(ratios)):
        for j in range(len(scales)):
            h=size*scales[j]*np.sqrt(ratios[i])
            w=size*scales[j]*np.sqrt(1./ratios[i])
            index=i*len(scales)+j
            anchor[index][0]=cy-h/2.
            anchor[index][1]=cx-w/2.
            anchor[index][2] = cy + h / 2.
            anchor[index][3] = cx + w / 2.
    return anchor.flatten() #返回的anchor是9*4的数组,存放着不同大小和比例anchor的左上、右下相对于中心点的坐标。
#给定中心点,就可以得到在原图中左上、右下的坐标
#传入上一步得到的anchor坐标,缩放比例size,和特征图长宽weight、height
def enumerate_anchor(anchor,size,f_height,f_weight):
    anchor_img_y=np.arange(size/2,f_height*size,size)#在原图上以缩放比例为间隔取点
    anchor_img_x = np.arange(size/2, f_weight * size, size)
    anchor_img_x,anchor_img_y=np.meshgrid(anchor_img_x,anchor_img_y)
    shift = np.stack((anchor_img_y.ravel(), anchor_img_x.ravel(),anchor_img_y.ravel(), anchor_img_x.ravel()), axis=1)
    a=np.shape(shift)[0]
    shift=np.tile(shift, (1, len(scales)*len(ratios)))
    anchor=np.tile(anchor,(a,1))
    anchor=anchor+shift
    #将原图上抽取的点坐标保存(保存两个,分别对应左上、右下),然后将原图上的坐标加上anchor中传入的坐标,就得到了anchor在原图上的左上、右下坐标
    return anchor
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值