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