-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_prior_box.py
87 lines (73 loc) · 3.65 KB
/
create_prior_box.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
import pickle
import numpy as np
import pdb
#create (7308,8) for 300x300
img_width, img_height = 300, 300
box_configs = [
{'layer_width': 38, 'layer_height': 38, 'num_prior': 3, 'min_size': 30.0,
'max_size': None, 'aspect_ratios': [1.0, 2.0, 1/2.0]},
{'layer_width': 19, 'layer_height': 19, 'num_prior': 6, 'min_size': 60.0,
'max_size': 114.0, 'aspect_ratios': [1.0, 1.0, 2.0, 1/2.0, 3.0, 1/3.0]},
{'layer_width': 10, 'layer_height': 10, 'num_prior': 6, 'min_size': 114.0,
'max_size': 168.0, 'aspect_ratios': [1.0, 1.0, 2.0, 1/2.0, 3.0, 1/3.0]},
{'layer_width': 5, 'layer_height': 5, 'num_prior': 6, 'min_size': 168.0,
'max_size': 222.0, 'aspect_ratios': [1.0, 1.0, 2.0, 1/2.0, 3.0, 1/3.0]},
{'layer_width': 3, 'layer_height': 3, 'num_prior': 6, 'min_size': 222.0,
'max_size': 276.0, 'aspect_ratios': [1.0, 1.0, 2.0, 1/2.0, 3.0, 1/3.0]},
{'layer_width': 1, 'layer_height': 1, 'num_prior': 6, 'min_size': 276.0,
'max_size': 330.0, 'aspect_ratios': [1.0, 1.0, 2.0, 1/2.0, 3.0, 1/3.0]},
]
variance = [0.1, 0.1, 0.2, 0.2]
boxes_paras = []
def create_prior_box():
for layer_config in box_configs:
layer_width, layer_height = layer_config["layer_width"], layer_config["layer_height"]
num_priors = layer_config["num_prior"]
aspect_ratios = layer_config["aspect_ratios"]
min_size = layer_config["min_size"]
max_size = layer_config["max_size"]
step_x = float(img_width) / float(layer_width)
step_y = float(img_height) / float(layer_height)
linx = np.linspace(0.5 * step_x, img_width - 0.5 * step_x, layer_width)
liny = np.linspace(0.5 * step_y, img_height - 0.5 * step_y, layer_height)
centers_x, centers_y = np.meshgrid(linx, liny)
centers_x = centers_x.reshape(-1, 1)
centers_y = centers_y.reshape(-1, 1)
assert(num_priors == len(aspect_ratios))
prior_boxes = np.concatenate((centers_x, centers_y), axis=1)
prior_boxes = np.tile(prior_boxes, (1, 2 * num_priors))
box_widths = []
box_heights = []
for ar in aspect_ratios:
if ar == 1 and len(box_widths) == 0:
box_widths.append(min_size)
box_heights.append(min_size)
elif ar == 1 and len(box_widths) > 0:
box_widths.append(np.sqrt(min_size * max_size))
box_heights.append(np.sqrt(min_size * max_size))
elif ar != 1:
box_widths.append(min_size * np.sqrt(ar))
box_heights.append(min_size / np.sqrt(ar))
box_widths = 0.5 * np.array(box_widths)
box_heights = 0.5 * np.array(box_heights)
# Normalize to 0-1
prior_boxes[:, ::4] -= box_widths #x1
prior_boxes[:, 1::4] -= box_heights #y1
prior_boxes[:, 2::4] += box_widths #x2
prior_boxes[:, 3::4] += box_heights #y2
prior_boxes[:, ::2] /= img_width #xn normalize
prior_boxes[:, 1::2] /= img_height #yn normalize
prior_boxes = prior_boxes.reshape(-1, 4)
# clip to 0-1
prior_boxes = np.minimum(np.maximum(prior_boxes, 0.0), 1.0)
#https://github.com/rykov8/ssd_keras/issues/53
piror_variances = np.tile(variance, (len(prior_boxes),1))
boxes_para = np.concatenate((prior_boxes, piror_variances), axis=1)
boxes_paras.append(boxes_para)
return np.concatenate(boxes_paras, axis=0)
if __name__ == "__main__":
boxes_paras = create_prior_box()
priors = pickle.load(open('prior_boxes_ssd300.pkl', 'rb'))
diff = boxes_paras - priors
# pdb.set_trace()
print("simi {}, max value {}, min value {}".format(diff.shape, diff.max(), diff.min()))