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.结果:

本文深入探讨了区域提议网络(RPN)的概念,其作为目标检测的重要组成部分,能够从任意大小的输入图像中生成一系列矩形对象提议,每个提议都附带一个对象得分。文章详细介绍了RPN的架构,包括通过n×n卷积层和两个并行的1×1卷积层实现的区域提议生成过程。此外,还提供了RPN在Python中的代码实现示例,展示了如何生成锚框(anchor),并将其可视化在输入图像上。
259

被折叠的 条评论
为什么被折叠?



