Detection---RPN

1.定义:

What’s RPN?

A Region Proposal Network(PRN) takes an image (of any size) as input and outputs a set of 
rectangular Object proposals , each with an objectness score.

What’s the architecture of RPN?

This architecture is naturally implemented with an n×n convolutional layer followed
by two sibling 1 × 1 convolutional layers (for reg and cls, respectively).

2.代码实现:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patches as patches
import pylab
import cv2

# feature map size
size_Y = 53
size_X = 80
# Scale ratio between input_image to feature map
rpn_stride = 8
# Definition anchor
scales = [1,2,4]
ratios = [0.5,1,2]

def anchor_gen(size_X,size_Y,rpn_stride,scales,ratios):
    scales,ratios = np.meshgrid(scales,ratios)
    scales,ratios = scales.flatten(),ratios.flatten()
    scalesY = scales*np.sqrt(ratios)
    scalesX = scales/np.sqrt(ratios)


    shiftX = np.arange(0, size_X) * rpn_stride
    shiftY = np.arange(0, size_Y) * rpn_stride
    shiftX,shiftY = np.meshgrid(shiftX,shiftY)
    centerX,anchorX = np.meshgrid(shiftX,scalesX)
    centerY,anchorY = np.meshgrid(shiftY,scalesY)
    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 = cv2.resize(img,None,fx=0.125,fy=0.125,interpolation=cv2.INTER_CUBIC)
    print(img.shape)
    Axs = plt.gca()

    for i in range(anchors.shape[0]):
        box = anchors[i]
        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()

3.结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值