fasterRcnn实现(二)

get_anchors,先验框设计

# -*- coding: UTF-8 -*-


import numpy as np
import matplotlib.pyplot as plt

import config


"""anchors即先验框"""

#shape--共享特征图的大小,width--原图宽,height--原图高
def get_anchors(shape,width,height):
    #因为一开始anchor框scale给的是原图上的,所以一系列的处理都是转换到原图上做,最后归一化就得到了共享特征层上的anchors
    anchors = generate_anchors()
    network_anchors = shift(shape,anchors)
    network_anchors[:,0] = network_anchors[:,0]/width
    network_anchors[:,1] = network_anchors[:,1]/height
    network_anchors[:,2] = network_anchors[:,2]/width
    network_anchors[:,3] = network_anchors[:,3]/height
    network_anchors = np.clip(network_anchors,0,1)
    return network_anchors
    
# sizes--anchorbox面积,这里是宽的尺寸;retios--anchorbox长宽比
def generate_anchors(sizes=None,ratios=None):
    if sizes is None:
        sizes = config.anchor_box_scales
        
    if ratios is None:
        ratios = config.anchor_box_ratios
        
    num_anchors = len(sizes) * len(ratios)
    
    anchors = np.zeros((num_anchors,4))
    
    anchors[:,2:] = np.tile(sizes,(2,len(ratios))).T
    #print(anchors)
    
    for i in range(len(ratios)):
        anchors[3*i:3*i+3,2] = anchors[3*i:3*i+3,2]*ratios[i][0]
        anchors[3*i:3*i+3,3] = anchors[3*i:3*i+3,3]*ratios[i][1]
    #print('生成的9个不同尺度不同长宽比的anchors:\n',anchors)
    
    anchors[:,0:2] = anchors[:,0:2] - anchors[:,2:] * 0.5
    anchors[:,2:] = anchors[:,2:] * 0.5
    print('生成的9个不同尺度不同长宽比的anchors:\n',anchors)
    
    return anchors
    
def shift(shape,anchors,stride=config.rpn_stride):
    #[0,1,2,3, ...... ,37]
    #[0.5,1.5,2.5,...... ,37.5] -> * stride
    #[8,24,....] 得到先验框在原图像上的中心点坐标
    shift_x = (np.arange(0,shape[0],dtype=float) + 0.5) * stride
    shift_y = (np.arange(0,shape[1],dtype=float) + 0.5) * stride

    shift_x, shift_y = np.meshgrid(shift_x,shift_y) #返回形成的网格点上的(x,y)
    
    shift_x = np.reshape(shift_x,[-1])
    shift_y = np.reshape(shift_y,[-1])
    #print(shift_x[100:200],shift_y[100:200])
    
    #np.stack(array,axis),axis等与几,就相当于对第几维打包,再堆叠。
    shifts = np.stack([shift_x,
                       shift_y,
                       shift_x,
                       shift_y],axis=0)
    shifts = np.transpose(shifts)
   
    num_of_anchors = np.shape(anchors)[0]
    k = np.shape(shifts)[0]
    
    shifted_anchors = np.reshape(anchors,[1,num_of_anchors,4]) + np.array(np.reshape(shifts,[k,1,4]))# 广播,会先把不同维度变成相同纬度再相加
    shifted_anchors = np.reshape(shifted_anchors,[k * num_of_anchors,4])
    print(shifted_anchors,np.shape(shifted_anchors))
    
    
    #可视化
    '''fig = plt.figure()
    ax = fig.add_subplot(111)
    plt.ylim(-300,900)
    plt.xlim(-300,900)
    plt.scatter(shift_x,shift_y)
    box_widths = shifted_anchors[:,2] - shifted_anchors[:,0]
    box_heights = shifted_anchors[:,3] - shifted_anchors[:,1]
    initial = int(shape[0]*shape[1]/2*9+shape[0]/2*9)
    for i in [initial+0,initial+1,initial+2,initial+3,initial+4,initial+5,initial+6,initial+7,initial+8]:
        rect = plt.Rectangle([shifted_anchors[i,0],shifted_anchors[i,1]],box_widths[i],box_heights[i],color='r',fill=False)
        ax.add_patch(rect)
    plt.show()'''
    
    #得到了所有的anchor框的左上角坐标,及右下角坐标(在原图上的)。
    return shifted_anchors
     

if __name__ == '__main__':
    network_anchors = get_anchors((38,38),600,600)
    print(np.shape(network_anchors))
    print(network_anchors)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值