第一课:fasterRCNN之RPN初探&anchors的产生

代码:

# -*- coding: utf-8 -*-
'''
参考链接:https://www.bilibili.com/video/av29987414/?spm_id_from=333.788.videocard.5
1. anchor 和 feature map 是什么关系?
答:
2. anchor 和原始图片是什么关系?
答:
'''
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import cv2
import pylab

# feature map
size_x = 4
size_y = 4
# 下采样
rpn_stride = 5                 # input = 4*8 = 32×32
scales = [1, 2, 4] #[2, 4, 8]  # 变长   
ratios = [0.5, 1, 2]

def anchor_gen(size_x, size_y, rpn_stride, scales, ratios):
    
    scales, ratios = np.meshgrid(scales, ratios)      # 表示九种anchor,挨个的可能
    scales, ratios = scales.flatten(), ratios.flatten()     # 转换为行向量
    
    # 求anchor的长和宽
    # scales^2 = x *y;   y = x*ratios; -> scales^2 = x * x * ratios
    # scales^2  = x*y = y / ratios * y
    scales_y = scales * np.sqrt(ratios)    # 本来是面积的ratios , 现在是边路的
    scales_x = scales / np.sqrt(ratios)    # 1×9 

    # 原始图片上的anchor 的中心点坐标
    shiftx = np.arange(0, size_x) * rpn_stride
    shifty = np.arange(0, size_y) * rpn_stride
    #input 组合网格坐标
    shiftX, shiftY = np.meshgrid(shiftx, shifty)
    
    # 组合anchor中心点和边长
    centerX, anchorX = np.meshgrid(shiftX, scales_x)
    centerY, anchorY = np.meshgrid(shiftY, scales_y)
    # 把anchor的中心点的x和y坐标组合
    anchor_center = np.stack([centerY,centerX], axis=2).reshape(-1, 2)
    anchor_size = np.stack([anchorY,anchorX], axis=2).reshape(-1, 2)
    boxes = np.concatenate([anchor_center - 0.5*anchor_size, anchor_center + 0.5*anchor_size ], axis=1)
    
    return boxes


if __name__=='__main__':
    anchors = anchor_gen(size_x,size_y,rpn_stride,scales,ratios)
    plt.figure(figsize=(10,10))   # 图片显示大小
    #img = cv2.imread("./person.jpg", 1)
    img = np.ones((size_x*rpn_stride, size_x*rpn_stride, 3))
    #plt.imshow(img)
    # 打开接口
    Axs = plt.gca()
 
    for i in range(anchors.shape[0]):
        box = anchors[i]    #  (x_min,y_min,x_max,y_max)         
        # Rectangle(x_min,y_min, width, height, angle=0.0, **kwargs)
        rec = patches.Rectangle((box[0],box[1]),box[2]-box[0],box[3]-box[1],edgecolor="r",facecolor="none")
        Axs.add_patch(rec)
 
    plt.imshow(img)
    pylab.show()

'''
np.meshgrid():输入两个坐标向量返回两个坐标矩阵
作用:表示anchor 的坐标

f_x = np.arange(size_x) # feature    # *rpn_stride  input
f_y = np.arange(size_y) #            # *rpn_stride
F_x, F_y = np.meshgrid(f_x, f_y)     # 用来表示特征图上每一点的坐标
print(f_x)   
[0 1 2 3]
print(F_x)    
 [0 1 2 3]
 [0 1 2 3]
 [0 1 2 3]]
print(F_y)
[[0 0 0 0]
 [1 1 1 1]
 [2 2 2 2]
 [3 3 3 3]]
'''
'''
import numpy as np

a = np.array([[1, 2, 3],[4, 5, 6]])
b = np.array([[7, 8, 9],[10, 11, 12]])
zzz = np.stack([a,b], axis=2)
print(a)
print(zzz)
'''

结果:

运行结果

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值