-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathtest_math.py
More file actions
255 lines (205 loc) · 8.67 KB
/
Copy pathtest_math.py
File metadata and controls
255 lines (205 loc) · 8.67 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# Use this only when running the test locally.
# import sys
# sys.path.append(".") # Adds the module to path.
import unittest
import array_api_compat as apc
import numpy as np
from scipy.ndimage import uniform_filter
from deeptrack import math
from deeptrack.backend import OPENCV_AVAILABLE, TORCH_AVAILABLE, xp
from deeptrack.tests import BackendTestBase
if TORCH_AVAILABLE:
import torch
class TestMath_Numpy(BackendTestBase):
BACKEND = "numpy"
def test_Average(self):
input_image0 = xp.ones((10, 30, 20)) * 2
input_image1 = xp.ones((10, 30, 20)) * 4
feature = math.Average(axis=0)
average = feature.resolve([input_image0, input_image1])
self.assertTrue(xp.all(average == 3), True)
self.assertEqual(average.shape, (10, 30, 20))
def test_Clip(self):
input_image = xp.asarray([[10, 4], [4, -10]])
feature = math.Clip(min=-5, max=5)
clipped_feature = feature.resolve(input_image)
self.assertTrue(
xp.all(clipped_feature == xp.asarray([[5, 4], [4, -5]]))
)
input_image = xp.asarray(np.array([[5, 6], [7, 8]]))
feature = math.Clip(min=0, max=10)
clipped_feature = feature.resolve(input_image)
self.assertTrue(
xp.all(clipped_feature == xp.asarray([[5, 6], [7, 8]]))
)
def test_NormalizeMinMax(self):
input_image = xp.asarray([[10, 4], [4, -10]])
feature = math.NormalizeMinMax(min=-5, max=5)
normalized_image = feature.resolve(input_image)
self.assertTrue(
xp.all(normalized_image == xp.asarray([[5, 2], [2, -5]]))
)
def test_NormalizeStandard(self):
input_image = xp.asarray([[1, 2], [3, 4]], dtype=float)
feature = math.NormalizeStandard()
normalized_image = feature.resolve(input_image)
self.assertEqual(xp.mean(normalized_image), 0)
if apc.is_torch_array(normalized_image):
# By default, torch.std() is unbiased, i.e., divides by N-1
self.assertEqual(torch.std(normalized_image, unbiased=False), 1)
else:
self.assertEqual(xp.std(normalized_image), 1)
def test_NormalizeQuantile(self):
input_image = xp.asarray([[1, 2], [3, 100]], dtype=float)
feature = math.NormalizeQuantile(quantiles=(0.25, 0.75))
output = feature.resolve(input_image)
self.assertAlmostEqual(xp.quantile(output, 0.5), 0, places=5)
def test_Blur(self):
# TODO: check this test with torch
pass
#input_image = xp.asarray(np.array([[1, 2], [3, 4]], dtype=float))
#expected_output = xp.asarray(np.array([[1, 1.5], [2, 2.5]]))
#feature = math.Blur(filter_function=uniform_filter, size=2)
#blurred_image = feature.resolve(input_image)
#self.assertTrue(xp.all(blurred_image == expected_output))
def test_AveragePooling(self):
input_image = xp.asarray([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=float)
feature = math.AveragePooling(ksize=2)
pooled_image = feature.resolve(input_image)
expected = xp.asarray([[3.5, 5.5]])
self.assertTrue(xp.all(pooled_image == expected))
self.assertEqual(pooled_image.shape, (1, 2))
def test_MaxPooling(self):
input_image = xp.asarray([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=float)
feature = math.MaxPooling(ksize=2)
pooled_image = feature.resolve(input_image)
expected = xp.asarray([[6.0, 8.0]], dtype=float)
self.assertTrue(xp.all(pooled_image == expected))
self.assertEqual(pooled_image.shape, (1, 2))
def test_MinPooling(self):
input_image = xp.asarray([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=float)
feature = math.MinPooling(ksize=2)
pooled_image = feature.resolve(input_image)
expected = xp.asarray([[1.0, 3.0]], dtype=float)
self.assertEqual(pooled_image.shape, (1, 2))
self.assertTrue(xp.all(pooled_image == expected))
# Extending the test and setting the backend to torch
@unittest.skipUnless(TORCH_AVAILABLE, "PyTorch is not installed.")
class TestMath_Torch(TestMath_Numpy):
BACKEND = "torch"
pass
class TestMath(unittest.TestCase):
def test_GaussianBlur(self):
input_image = np.array([[1, 2], [3, 4]], dtype=float)
feature = math.GaussianBlur(sigma=0)
blurred_image = feature.resolve(input_image)
self.assertTrue(np.all(blurred_image == [[1, 2], [3, 4]]))
input_image = np.array([[1, 2], [3, 4]], dtype=float)
feature = math.GaussianBlur(sigma=1000)
blurred_image = feature.resolve(input_image)
self.assertTrue(np.all(blurred_image - [[2.5, 2.5], [2.5, 2.5]] <= 0.01))
def test_AveragePooling(self):
input_image = np.array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=float)
feature = math.AveragePooling(ksize=2)
pooled_image = feature.resolve(input_image)
self.assertTrue(np.all(pooled_image == [[3.5, 5.5]]))
def test_MaxPooling(self):
input_image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
feature = math.MaxPooling(ksize=2)
pooled_image = feature.resolve(input_image)
self.assertTrue(xp.all(pooled_image == xp.asarray([[5, 6], [8, 9]]) ) )
def test_MinPooling(self):
input_image = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
feature = math.MinPooling(ksize=2)
pooled_image = feature.resolve(input_image)
self.assertTrue(np.all(pooled_image == [[1, 3]]))
def test_MedianBlur(self):
input_image = np.random.rand(32, 32)
feature = math.MedianBlur(ksize=3)
output = feature.resolve(input_image)
self.assertEqual(output.shape, input_image.shape)
def test_MedianPooling(self):
input_image = np.array([[1, 3, 2, 4], [5, 7, 6, 8]], dtype=float)
feature = math.MedianPooling(ksize=2)
pooled = feature.resolve(input_image)
self.assertEqual(pooled.shape, (1, 2))
@unittest.skipUnless(OPENCV_AVAILABLE, "OpenCV is not installed.")
def test_Resize(self):
input_image = np.random.rand(16, 16)
feature = math.Resize(dsize=(8, 8))
resized = feature.resolve(input_image)
self.assertEqual(resized.shape, (8, 8))
@unittest.skipUnless(OPENCV_AVAILABLE, "OpenCV is not installed.")
def test_BlurCV2_GaussianBlur(self):
import cv2
input_image = np.random.rand(32, 32).astype(np.float32)
expected_output = cv2.GaussianBlur(
input_image, ksize=(5, 5), sigmaX=1, borderType=cv2.BORDER_REFLECT
)
feature = math.BlurCV2(
filter_function=cv2.GaussianBlur, ksize=(5, 5), sigmaX=1, mode="reflect"
)
output_image = feature.resolve(input_image)
self.assertTrue(output_image.shape == expected_output.shape)
self.assertIsNone(
np.testing.assert_allclose(
output_image,
expected_output,
rtol=1e-5,
atol=1e-6,
)
)
@unittest.skipUnless(OPENCV_AVAILABLE, "OpenCV is not installed.")
def test_BlurCV2_bilateralFilter(self):
import cv2
input_image = np.random.rand(32, 32).astype(np.float32)
expected_output = cv2.bilateralFilter(
input_image,
d=9,
sigmaColor=75,
sigmaSpace=75,
borderType=cv2.BORDER_REFLECT,
)
feature = math.BlurCV2(
filter_function=cv2.bilateralFilter,
d=9,
sigmaColor=75,
sigmaSpace=75,
mode="reflect",
)
output_image = feature.resolve(input_image)
self.assertTrue(output_image.shape == expected_output.shape)
self.assertIsNone(
np.testing.assert_allclose(
output_image,
expected_output,
rtol=1e-5,
atol=1e-6,
)
)
@unittest.skipUnless(OPENCV_AVAILABLE, "OpenCV is not installed.")
def test_BilateralBlur(self):
import cv2
input_image = np.random.rand(32, 32).astype(np.float32)
expected_output = cv2.bilateralFilter(
input_image,
d=9,
sigmaColor=75,
sigmaSpace=75,
borderType=cv2.BORDER_REFLECT,
)
feature = math.BilateralBlur(
d=9, sigma_color=75, sigma_space=75, mode="reflect"
)
output_image = feature.resolve(input_image)
self.assertTrue(output_image.shape == expected_output.shape)
self.assertIsNone(
np.testing.assert_allclose(
output_image,
expected_output,
rtol=1e-5,
atol=1e-6,
)
)
if __name__ == "__main__":
unittest.main()