forked from bubbliiiing/faster-rcnn-tf2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvision_for_anchor.py
114 lines (98 loc) · 4.55 KB
/
vision_for_anchor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
from tensorflow import keras
import matplotlib.pyplot as plt
import numpy as np
#---------------------------------------------------#
# 生成基础的先验框
#---------------------------------------------------#
def generate_anchors(sizes = [128, 256, 512], ratios = [[1, 1], [1, 2], [2, 1]]):
num_anchors = len(sizes) * len(ratios)
anchors = np.zeros((num_anchors, 4))
anchors[:, 2:] = np.tile(sizes, (2, len(ratios))).T
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]
anchors[:, 0::2] -= np.tile(anchors[:, 2] * 0.5, (2, 1)).T
anchors[:, 1::2] -= np.tile(anchors[:, 3] * 0.5, (2, 1)).T
return anchors
#---------------------------------------------------#
# 对基础的先验框进行拓展获得全部的建议框
#---------------------------------------------------#
def shift(shape, anchors, stride=16):
#---------------------------------------------------#
# [0,1,2,3,4,5……37]
# [0.5,1.5,2.5……37.5]
# [8,24,……]
#---------------------------------------------------#
shift_x = (np.arange(0, shape[0], dtype=keras.backend.floatx()) + 0.5) * stride
shift_y = (np.arange(0, shape[1], dtype=keras.backend.floatx()) + 0.5) * stride
shift_x, shift_y = np.meshgrid(shift_x, shift_y)
shift_x = np.reshape(shift_x, [-1])
shift_y = np.reshape(shift_y, [-1])
# print(shift_x,shift_y)
shifts = np.stack([
shift_x,
shift_y,
shift_x,
shift_y
], axis=0)
shifts = np.transpose(shifts)
number_of_anchors = np.shape(anchors)[0]
k = np.shape(shifts)[0]
shifted_anchors = np.reshape(anchors, [1, number_of_anchors, 4]) + np.array(np.reshape(shifts, [k, 1, 4]), keras.backend.floatx())
shifted_anchors = np.reshape(shifted_anchors, [k * number_of_anchors, 4])
#---------------------------------------------------#
# 进行图像的绘制
#---------------------------------------------------#
fig = plt.figure()
ax = fig.add_subplot(111)
plt.ylim(-300,900)
plt.xlim(-300,900)
# plt.ylim(0,600)
# plt.xlim(0,600)
plt.scatter(shift_x,shift_y)
box_widths = shifted_anchors[:, 2] - shifted_anchors[:, 0]
box_heights = shifted_anchors[:, 3] - shifted_anchors[:, 1]
initial = 0
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()
return shifted_anchors
#---------------------------------------------------#
# 获得resnet50对应的baselayer大小
#---------------------------------------------------#
def get_resnet50_output_length(height, width):
def get_output_length(input_length):
filter_sizes = [7, 3, 1, 1]
padding = [3, 1, 0, 0]
stride = 2
for i in range(4):
input_length = (input_length + 2 * padding[i] - filter_sizes[i]) // stride + 1
return input_length
return get_output_length(height), get_output_length(width)
#---------------------------------------------------#
# 获得vgg对应的baselayer大小
#---------------------------------------------------#
def get_vgg_output_length(height, width):
def get_output_length(input_length):
filter_sizes = [2, 2, 2, 2]
padding = [0, 0, 0, 0]
stride = 2
for i in range(4):
input_length = (input_length + 2 * padding[i] - filter_sizes[i]) // stride + 1
return input_length
return get_output_length(height), get_output_length(width)
def get_anchors(input_shape, backbone, sizes = [128, 256, 512], ratios = [[1, 1], [1, 2], [2, 1]], stride=16):
if backbone == 'vgg':
feature_shape = get_vgg_output_length(input_shape[0], input_shape[1])
print(feature_shape)
else:
feature_shape = get_resnet50_output_length(input_shape[0], input_shape[1])
anchors = generate_anchors(sizes = sizes, ratios = ratios)
anchors = shift(feature_shape, anchors, stride = stride)
anchors[:, ::2] /= input_shape[1]
anchors[:, 1::2] /= input_shape[0]
anchors = np.clip(anchors, 0, 1)
return anchors
if __name__ == "__main__":
get_anchors([600, 600], 'resnet50')