test_anchors.py
8.61 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import numpy as np
import configparser
from tensorflow import keras
from keras_retinanet.utils.anchors import anchors_for_shape, AnchorParameters
from keras_retinanet.utils.config import read_config_file, parse_anchor_parameters
def test_config_read():
config = read_config_file('tests/test-data/config/config.ini')
assert 'anchor_parameters' in config
assert 'sizes' in config['anchor_parameters']
assert 'strides' in config['anchor_parameters']
assert 'ratios' in config['anchor_parameters']
assert 'scales' in config['anchor_parameters']
assert config['anchor_parameters']['sizes'] == '32 64 128 256 512'
assert config['anchor_parameters']['strides'] == '8 16 32 64 128'
assert config['anchor_parameters']['ratios'] == '0.5 1 2 3'
assert config['anchor_parameters']['scales'] == '1 1.2 1.6'
def create_anchor_params_config():
config = configparser.ConfigParser()
config['anchor_parameters'] = {}
config['anchor_parameters']['sizes'] = '32 64 128 256 512'
config['anchor_parameters']['strides'] = '8 16 32 64 128'
config['anchor_parameters']['ratios'] = '0.5 1'
config['anchor_parameters']['scales'] = '1 1.2 1.6'
return config
def test_parse_anchor_parameters():
config = create_anchor_params_config()
anchor_params_parsed = parse_anchor_parameters(config)
sizes = [32, 64, 128, 256, 512]
strides = [8, 16, 32, 64, 128]
ratios = np.array([0.5, 1], keras.backend.floatx())
scales = np.array([1, 1.2, 1.6], keras.backend.floatx())
assert sizes == anchor_params_parsed.sizes
assert strides == anchor_params_parsed.strides
np.testing.assert_equal(ratios, anchor_params_parsed.ratios)
np.testing.assert_equal(scales, anchor_params_parsed.scales)
def test_anchors_for_shape_dimensions():
sizes = [32, 64, 128]
strides = [8, 16, 32]
ratios = np.array([0.5, 1, 2, 3], keras.backend.floatx())
scales = np.array([1, 1.2, 1.6], keras.backend.floatx())
anchor_params = AnchorParameters(sizes, strides, ratios, scales)
pyramid_levels = [3, 4, 5]
image_shape = (64, 64)
all_anchors = anchors_for_shape(image_shape, pyramid_levels=pyramid_levels, anchor_params=anchor_params)
assert all_anchors.shape == (1008, 4)
def test_anchors_for_shape_values():
sizes = [12]
strides = [8]
ratios = np.array([1, 2], keras.backend.floatx())
scales = np.array([1, 2], keras.backend.floatx())
anchor_params = AnchorParameters(sizes, strides, ratios, scales)
pyramid_levels = [3]
image_shape = (16, 16)
all_anchors = anchors_for_shape(image_shape, pyramid_levels=pyramid_levels, anchor_params=anchor_params)
# using almost_equal for floating point imprecisions
np.testing.assert_almost_equal(all_anchors[0, :], [
strides[0] / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2,
strides[0] / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2,
strides[0] / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2,
strides[0] / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2,
], decimal=6)
np.testing.assert_almost_equal(all_anchors[1, :], [
strides[0] / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2,
strides[0] / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2,
strides[0] / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2,
strides[0] / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2,
], decimal=6)
np.testing.assert_almost_equal(all_anchors[2, :], [
strides[0] / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2,
strides[0] / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2,
strides[0] / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2,
strides[0] / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2,
], decimal=6)
np.testing.assert_almost_equal(all_anchors[3, :], [
strides[0] / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2,
strides[0] / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2,
strides[0] / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2,
strides[0] / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2,
], decimal=6)
np.testing.assert_almost_equal(all_anchors[4, :], [
strides[0] * 3 / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2,
strides[0] / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2,
strides[0] * 3 / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2,
strides[0] / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2,
], decimal=6)
np.testing.assert_almost_equal(all_anchors[5, :], [
strides[0] * 3 / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2,
strides[0] / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2,
strides[0] * 3 / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2,
strides[0] / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2,
], decimal=6)
np.testing.assert_almost_equal(all_anchors[6, :], [
strides[0] * 3 / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2,
strides[0] / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2,
strides[0] * 3 / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2,
strides[0] / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2,
], decimal=6)
np.testing.assert_almost_equal(all_anchors[7, :], [
strides[0] * 3 / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2,
strides[0] / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2,
strides[0] * 3 / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2,
strides[0] / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2,
], decimal=6)
np.testing.assert_almost_equal(all_anchors[8, :], [
strides[0] / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2,
strides[0] * 3 / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2,
strides[0] / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2,
strides[0] * 3 / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2,
], decimal=6)
np.testing.assert_almost_equal(all_anchors[9, :], [
strides[0] / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2,
strides[0] * 3 / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2,
strides[0] / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2,
strides[0] * 3 / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2,
], decimal=6)
np.testing.assert_almost_equal(all_anchors[10, :], [
strides[0] / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2,
strides[0] * 3 / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2,
strides[0] / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2,
strides[0] * 3 / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2,
], decimal=6)
np.testing.assert_almost_equal(all_anchors[11, :], [
strides[0] / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2,
strides[0] * 3 / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2,
strides[0] / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2,
strides[0] * 3 / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2,
], decimal=6)
np.testing.assert_almost_equal(all_anchors[12, :], [
strides[0] * 3 / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2,
strides[0] * 3 / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2,
strides[0] * 3 / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[0])) / 2,
strides[0] * 3 / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[0])) / 2,
], decimal=6)
np.testing.assert_almost_equal(all_anchors[13, :], [
strides[0] * 3 / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2,
strides[0] * 3 / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2,
strides[0] * 3 / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[0])) / 2,
strides[0] * 3 / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[0])) / 2,
], decimal=6)
np.testing.assert_almost_equal(all_anchors[14, :], [
strides[0] * 3 / 2 - (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2,
strides[0] * 3 / 2 - (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2,
strides[0] * 3 / 2 + (sizes[0] * scales[0] / np.sqrt(ratios[1])) / 2,
strides[0] * 3 / 2 + (sizes[0] * scales[0] * np.sqrt(ratios[1])) / 2,
], decimal=6)
np.testing.assert_almost_equal(all_anchors[15, :], [
strides[0] * 3 / 2 - (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2,
strides[0] * 3 / 2 - (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2,
strides[0] * 3 / 2 + (sizes[0] * scales[1] / np.sqrt(ratios[1])) / 2,
strides[0] * 3 / 2 + (sizes[0] * scales[1] * np.sqrt(ratios[1])) / 2,
], decimal=6)