-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlevels.py
59 lines (44 loc) · 1.91 KB
/
levels.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
from itertools import product
import numpy as np
class Levels(object):
def __init__(self, anchor_aspect_ratios, anchor_scale_ratios):
self._anchor_aspect_ratios = anchor_aspect_ratios
self._anchor_scale_ratios = anchor_scale_ratios
self._levels = {
'P3': Level(32, self._anchor_aspect_ratios, self._anchor_scale_ratios),
'P4': Level(64, self._anchor_aspect_ratios, self._anchor_scale_ratios),
'P5': Level(128, self._anchor_aspect_ratios, self._anchor_scale_ratios),
'P6': Level(256, self._anchor_aspect_ratios, self._anchor_scale_ratios),
'P7': Level(512, self._anchor_aspect_ratios, self._anchor_scale_ratios)
}
@property
def num_anchors(self):
return len(self._anchor_aspect_ratios) * len(self._anchor_scale_ratios)
def keys(self):
return self._levels.keys()
def __getitem__(self, item):
return self._levels[item]
def __iter__(self):
return iter(self.keys())
class Level(object):
def __init__(self, anchor_size, anchor_aspect_ratios, anchor_scale_ratios):
self._anchor_size = anchor_size
self._anchor_aspect_ratios = anchor_aspect_ratios
self._anchor_scale_ratios = anchor_scale_ratios
@property
def anchor_sizes(self):
return np.stack([
compute_box_size(self._anchor_size, aspect_ratio, scale_ratio)
for aspect_ratio, scale_ratio in product(
self._anchor_aspect_ratios, self._anchor_scale_ratios)
], 0)
#
#
def compute_box_size(base_size, aspect_ratio, scale_ratio):
aspect_ratio = np.array(aspect_ratio)
size = np.sqrt(base_size**2 / aspect_ratio.prod()) * aspect_ratio * scale_ratio
return size
def build_levels():
anchor_aspect_ratios = [(1, 2), (1, 1), (2, 1)]
anchor_scale_ratios = [2**0, 2**(1 / 3), 2**(2 / 3)]
return Levels(anchor_aspect_ratios, anchor_scale_ratios)