-
Notifications
You must be signed in to change notification settings - Fork 54
/
anchor.py
executable file
·62 lines (51 loc) · 1.65 KB
/
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
import itertools
import math
import tensorflow as tf
def generate_default_boxes(config):
""" Generate default boxes for all feature maps
Args:
config: information of feature maps
scales: boxes' size relative to image's size
fm_sizes: sizes of feature maps
ratios: box ratios used in each feature maps
Returns:
default_boxes: tensor of shape (num_default, 4)
with format (cx, cy, w, h)
"""
default_boxes = []
scales = config['scales']
fm_sizes = config['fm_sizes']
ratios = config['ratios']
for m, fm_size in enumerate(fm_sizes):
for i, j in itertools.product(range(fm_size), repeat=2):
cx = (j + 0.5) / fm_size
cy = (i + 0.5) / fm_size
default_boxes.append([
cx,
cy,
scales[m],
scales[m]
])
default_boxes.append([
cx,
cy,
math.sqrt(scales[m] * scales[m + 1]),
math.sqrt(scales[m] * scales[m + 1])
])
for ratio in ratios[m]:
r = math.sqrt(ratio)
default_boxes.append([
cx,
cy,
scales[m] * r,
scales[m] / r
])
default_boxes.append([
cx,
cy,
scales[m] / r,
scales[m] * r
])
default_boxes = tf.constant(default_boxes)
default_boxes = tf.clip_by_value(default_boxes, 0.0, 1.0)
return default_boxes