-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathtest_features.py
195 lines (161 loc) · 7.22 KB
/
test_features.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import numpy
import features
class TestFeaturesColorHistogram:
def setup_method(self, method = None, w = 10, h = 10):
self.h, self.w = h, w
image = numpy.zeros((self.h, self.w, 3), dtype=numpy.uint8)
label = numpy.zeros((self.h, self.w), dtype=int)
self.f = features.Features(image, label, 1)
def test_1region_1color(self):
hist = self.f._Features__init_color(1)
assert len(hist) == 1
assert hist[0].shape == (75,)
r_expected = [0.333333333] + [0] * 24
g_expected = [0.333333333] + [0] * 24
b_expected = [0.333333333] + [0] * 24
numpy.testing.assert_array_almost_equal(hist[0].ravel(), r_expected + g_expected + b_expected)
def test_1region_255color(self):
self.setup_method(self, w = 1, h = 256)
for y in range(self.h):
self.f.image[y, :, :] = y
hist = self.f._Features__init_color(1)
assert len(hist) == 1
assert hist[0].shape == (75,)
r_expected = [11] * 23 + [3, 0] # because bin width equals 11
g_expected = [11] * 23 + [3, 0]
b_expected = [11] * 23 + [3, 0]
expected = numpy.array(r_expected + g_expected + b_expected)
numpy.testing.assert_array_almost_equal(hist[0].ravel(), expected / numpy.sum(expected))
def test_2region_1color(self):
self.setup_method(self, w = 1, h = 2)
for y in range(self.h):
self.f.label[y, :] = y
hist = self.f._Features__init_color(2)
assert len(hist) == 2
assert hist[0].shape == (75,)
r1_expected = ([1.0/3] + [0] * 24) + ([1.0/3] + [0] * 24) + ([1.0/3] + [0] * 24)
r2_expected = ([1.0/3] + [0] * 24) + ([1.0/3] + [0] * 24) + ([1.0/3] + [0] * 24)
numpy.testing.assert_array_almost_equal(hist[0].ravel(), r1_expected)
numpy.testing.assert_array_almost_equal(hist[1].ravel(), r2_expected)
class TestFeaturesSize:
def setup_method(self, method):
image = numpy.zeros((10, 10, 3), dtype=numpy.uint8)
label = numpy.zeros((10, 10), dtype=int)
self.f = features.Features(image, label, 1)
def test_1region(self):
sizes = self.f._Features__init_size(1)
assert len(sizes) == 1
assert sizes[0] == 100
def test_2region(self):
self.f.label[:5, :] = 1
sizes = self.f._Features__init_size(2)
assert len(sizes) == 2
assert sizes[0] == 50
assert sizes[1] == 50
class TestFeaturesBoundingBox:
def setup_method(self, method):
image = numpy.zeros((10, 10, 3), dtype=numpy.uint8)
label = numpy.zeros((10, 10), dtype=int)
self.f = features.Features(image, label, 1)
def test_1region(self):
bb = self.f._Features__init_bounding_box(1)
assert len(bb) == 1
assert bb[0] == (0, 0, 9, 9)
def test_4region(self):
self.f.label[:5, :5] = 0
self.f.label[:5, 5:] = 1
self.f.label[5:, :5] = 2
self.f.label[5:, 5:] = 3
bb = self.f._Features__init_bounding_box(4)
assert len(bb) == 4
assert bb[0] == (0, 0, 4, 4)
assert bb[1] == (0, 5, 4, 9)
assert bb[2] == (5, 0, 9, 4)
assert bb[3] == (5, 5, 9, 9)
class TestSimilarity:
def setup_method(self, method):
self.dummy_image = numpy.zeros((10, 10, 3), dtype=numpy.uint8)
self.dummy_label = numpy.zeros((10, 10), dtype=int)
self.f = features.Features(self.dummy_image, self.dummy_label, 1)
def test_similarity_size(self):
self.f.size = {0 : 10, 1 : 20}
s = self.f._Features__sim_size(0, 1)
assert s == 0.7
def test_similarity_color_simple(self):
self.f.color[0] = numpy.array([1] * 75)
self.f.color[1] = numpy.array([2] * 75)
s = self.f._Features__sim_color(0, 1)
assert s == 75
def test_similarity_color_complex(self):
# build 75-dimensional arrays as color histogram
self.f.color[0] = numpy.array([1, 2, 1, 2, 1] * 15)
self.f.color[1] = numpy.array([2, 1, 2, 1, 2] * 15)
s = self.f._Features__sim_color(0, 1)
assert s == 75
def test_similarity_texture(self):
# build 240-dimensional arrays as texture histogram
self.f.texture[0] = numpy.array([1, 2, 1, 2, 1, 2] * 40)
self.f.texture[1] = numpy.array([2, 1, 2, 1, 2, 1] * 40)
s = self.f._Features__sim_texture(0, 1)
assert s == 240
def test_similarity_fill(self):
self.f.bbox[0] = numpy.array([10, 10, 20, 20])
self.f.size[0] = 100
self.f.bbox[1] = numpy.array([20, 20, 30, 30])
self.f.size[1] = 100
s = self.f._Features__sim_fill(0, 1)
assert s == 1. - float(400 - 200) / 100
def test_similarity_user_all(self, monkeypatch):
monkeypatch.setattr(features.Features, '_Features__sim_size', lambda self, i, j: 1)
monkeypatch.setattr(features.Features, '_Features__sim_texture',lambda self, i, j: 1)
monkeypatch.setattr(features.Features, '_Features__sim_color', lambda self, i, j: 1)
monkeypatch.setattr(features.Features, '_Features__sim_fill', lambda self, i, j: 1)
w = features.SimilarityMask(1, 1, 1, 1)
f = features.Features(self.dummy_image, self.dummy_label, 1, w)
assert f.similarity(0, 1) == 4
class TestMerge:
def setup_method(self, method):
dummy_image = numpy.zeros((10, 10, 3), dtype=numpy.uint8)
dummy_label = numpy.zeros((10, 10), dtype=int)
self.f = features.Features(dummy_image, dummy_label, 1)
def test_merge_size(self):
self.f.size = {0: 10, 1: 20}
self.f._Features__merge_size(0, 1, 2)
assert self.f.size[2] == 30
def test_merge_color(self):
self.f.color[0] = numpy.array([1.] * 75)
self.f.size[0] = 100
self.f.color[1] = numpy.array([2.] * 75)
self.f.size[1] = 50
self.f._Features__merge_color(0, 1, 2)
expected = (100 * 1. + 50 * 2.) / (100 + 50)
assert numpy.array_equal(self.f.color[2], [expected] * 75)
def test_merge_texture(self):
self.f.texture[0] = numpy.array([1.] * 240)
self.f.size[0] = 100
self.f.texture[1] = numpy.array([2.] * 240)
self.f.size[1] = 50
self.f._Features__merge_texture(0, 1, 2)
expected = (100 * 1. + 50 * 2.) / (100 + 50)
assert numpy.array_equal(self.f.texture[2], [expected] * 240)
def test_merge_bbox(self):
self.f.bbox[0] = numpy.array([10, 10, 20, 20])
self.f.size[0] = 100
self.f.bbox[1] = numpy.array([20, 20, 30, 30])
self.f.size[1] = 50
self.f.imsize = 1000
self.f._Features__merge_bbox(0, 1, 2)
assert numpy.array_equal(self.f.bbox[2], [10, 10, 30, 30])
def test_merge(self):
self.f.imsize = 1000
self.f.size = {0: 10, 1: 20}
self.f.color = {0: numpy.array([1.] * 75), 1: numpy.array([2.] * 75)}
self.f.texture = {0: numpy.array([1.] * 240), 1: numpy.array([2.] * 240)}
self.f.bbox = {0: numpy.array([10, 10, 20, 20]), 1: numpy.array([20, 20, 30, 30])}
assert self.f.merge(0, 1) == 2
assert len(self.f.size) == 3
assert len(self.f.color) == 3
assert len(self.f.texture) == 3
assert len(self.f.bbox) == 3