-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance_test.py
More file actions
345 lines (299 loc) · 11.7 KB
/
performance_test.py
File metadata and controls
345 lines (299 loc) · 11.7 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import pickle
import timeit
from itertools import product
import cv2 as cv
import numba
import numpy as np
from numba.typed import List as nList
from scipy import ndimage
def fix_array_or_list(matrix):
if isinstance(matrix, np.ndarray):
return matrix
if not matrix:
raise ValueError("List must contain at least one element")
new_list = nList()
for r in matrix:
new_list.append(r)
return new_list
def run_pure_simple_python(matrix: list, h: int, w: int, kernel: int = 3):
kernel_half = kernel // 2
# create result array
res = [[0 for _ in range(w)] for _ in range(h)]
# iterate over height/rows
for r in range(0, h):
# iterate over width/col
for c in range(0, w):
# prepare result calculation
sums = 0
cnt = 0
# iterate kernel size height/rows
for r_offset in range(0, kernel):
# iterate kernel size width/col
for c_offset in range(0, kernel):
# calc final position
pos_r = r + r_offset - kernel_half
pos_c = c + c_offset - kernel_half
# check if position is valid
if 0 <= pos_r < h and 0 <= pos_c < w:
# sum up values
sums += matrix[pos_r][pos_c]
cnt += 1
# calculate final result for position
res[r][c] = sums // cnt
return res
@numba.jit(nopython=True)
def run_pure_simple_python_with_numba(matrix: list, h: int, w: int, kernel: int = 3):
kernel_half = kernel // 2
# create result array
res = [[0 for _ in range(w)] for _ in range(h)]
# iterate over height/rows
for r in range(0, h):
# iterate over width/col
for c in range(0, w):
# prepare result calculation
sums = 0
cnt = 0
# iterate kernel size height/rows
for r_offset in range(0, kernel):
# iterate kernel size width/col
for c_offset in range(0, kernel):
# calc final position
pos_r = r + r_offset - kernel_half
pos_c = c + c_offset - kernel_half
# check if position is valid
if 0 <= pos_r < h and 0 <= pos_c < w:
# sum up values
sums = sums + matrix[pos_r][pos_c]
cnt = cnt + 1
# calculate final result for position
res[r][c] = sums // cnt
return res
@numba.jit(nopython=True, parallel=True)
def run_pure_simple_python_with_numba_parallel(matrix: list, h: int, w: int, kernel: int = 3):
kernel_half = kernel // 2
# create result array
res = [[0 for _ in range(w)] for _ in range(h)]
# iterate over height/rows
for r in numba.prange(h):
# iterate over width/col
for c in numba.prange(w):
# prepare result calculation
sums = 0
cnt = 0
# iterate kernel size height/rows
for r_offset in range(0, kernel):
# iterate kernel size width/col
for c_offset in range(0, kernel):
# calc final position
pos_r = r + r_offset - kernel_half
pos_c = c + c_offset - kernel_half
# check if position is valid
if 0 <= pos_r < h and 0 <= pos_c < w:
# sum up values
sums = sums + matrix[pos_r][pos_c]
cnt = cnt + 1
# calculate final result for position
res[r][c] = sums // cnt
return res
def run_with_itertools(matrix: list, h: int, w: int, kernel: int = 3):
res = [[0 for _ in range(w)] for _ in range(h)]
kernel_half = kernel // 2
filter_pos = list(product(range(kernel), repeat=2))
image_pos = product(range(h), range(w))
for r, c in image_pos:
sums = 0
cnt = 0
for r_offset, c_offset in filter_pos:
pos_r = r + r_offset - kernel_half
pos_c = c + c_offset - kernel_half
# check if position is valid
if 0 <= pos_r < h and 0 <= pos_c < w:
# sum up values
sums = sums + matrix[pos_r][pos_c]
cnt = cnt + 1
res[r][c] = sums // cnt
return res
@numba.jit(nopython=True, parallel=True)
def _run_with_itertools_with_numba(_matrix: list, _h: int, _w: int, f_pos, i_pos, _kernel: int = 3):
res = [[0 for _ in range(_w)] for _ in range(_h)]
kernel_half = _kernel // 2
for r, c in i_pos:
sums = 0
cnt = 0
for r_offset, c_offset in f_pos:
pos_r = r + r_offset - kernel_half
pos_c = c + c_offset - kernel_half
# check if position is valid
if 0 <= pos_r < _h and 0 <= pos_c < _w:
# sum up values
sums += _matrix[pos_r][pos_c]
cnt += 1
res[r][c] = sums // cnt
return res
def run_with_itertools_with_numba(matrix: list, h: int, w: int, kernel: int = 3):
filter_pos = fix_array_or_list(list(product(range(kernel), repeat=2)))
image_pos = fix_array_or_list(product(range(h), range(w)))
return _run_with_itertools_with_numba(matrix, h, w, filter_pos, image_pos, kernel)
def run_with_numpy(matrix: list, h: int, w: int, kernel: int = 3):
kernel_half = kernel // 2
h_end = h
w_end = w
# create result array
res = np.zeros((h, w), dtype=int)
# iterate over height/rows
for r in range(0, h):
# iterate over width/col
for c in range(0, w):
# prepare result calculation
r_start = r - kernel_half
r_end = r + kernel_half + 1
c_start = c - kernel_half
c_end = c + kernel_half + 1
if r_start < 0:
r_start = 0
if r_end > h_end:
r_end = h_end
if c_start < 0:
c_start = 0
if c_end > w_end:
c_end = w_end
sub = matrix[r_start:r_end, c_start:c_end]
res[r, c] = np.sum(sub) // sub.size
return res
@numba.jit(nopython=True)
def run_with_numpy_with_numba(matrix: list, h: int, w: int, kernel: int = 3):
kernel_half = kernel // 2
h_end = h
w_end = w
# create result array
res = np.zeros((h, w), dtype=numba.int64)
# iterate over height/rows
for r in range(0, h):
# iterate over width/col
for c in range(0, w):
# prepare result calculation
r_start = r - kernel_half
r_end = r + kernel_half + 1
c_start = c - kernel_half
c_end = c + kernel_half + 1
if r_start < 0:
r_start = 0
if r_end > h_end:
r_end = h_end
if c_start < 0:
c_start = 0
if c_end > w_end:
c_end = w_end
sub = matrix[r_start:r_end, c_start:c_end]
res[r, c] = np.sum(sub) // sub.size
return res
@numba.jit(nopython=True, parallel=True)
def run_with_numpy_with_numba_parallel(matrix: list, h: int, w: int, kernel: int = 3):
kernel_half = kernel // 2
h_end = h
w_end = w
# create result array
res = np.zeros((h, w), dtype=numba.int64)
# iterate over height/rows
for r in numba.prange(0, h):
# iterate over width/col
for c in numba.prange(0, w):
# prepare result calculation
r_start = r - kernel_half
r_end = r + kernel_half + 1
c_start = c - kernel_half
c_end = c + kernel_half + 1
if r_start < 0:
r_start = 0
if r_end > h_end:
r_end = h_end
if c_start < 0:
c_start = 0
if c_end > w_end:
c_end = w_end
sub = matrix[r_start:r_end, c_start:c_end]
res[r, c] = np.sum(sub) // sub.size
return res
def run_with_numpy_and_scipy(matrix: list, h: int, w: int, kernel: int = 3):
_matrix = np.asarray(matrix)
conv = np.ones((kernel, kernel), dtype=int) / (kernel * kernel)
return ndimage.convolve(_matrix, conv, mode='constant', cval=0)
def run_with_opencv(matrix: list, h: int, w: int, kernel: int = 3):
conv = np.ones((kernel, kernel), dtype=np.float32) / (kernel * kernel)
return cv.filter2D(matrix, -1, conv)
runs = [(run_pure_simple_python, 0, 25, 100), # 0
(run_with_itertools, 0, 25, 100), # 1
(run_pure_simple_python_with_numba, 1, 100, 1000), # 2
(run_pure_simple_python_with_numba_parallel, 1, 100, 1000), # 3
(run_with_itertools_with_numba, 1, 100, 1000), # 4
(run_with_numpy, 2, 25, 100), # 5
(run_with_numpy_with_numba, 2, 100, 1000), # 6
(run_with_numpy_with_numba_parallel, 2, 100, 1000), # 7
(run_with_numpy_and_scipy, 2, 100, 1000), # 8
(run_with_opencv, 3, 100, 1000) # 9
]
def build_run(run_version: int):
return runs[run_version]
def run_timed(func, matrix: list, h: int, w: int, warmpup:int = 100, measurement:int = 1000, kernel: int = 3):
# warmup
warm_up_times = timeit.repeat(lambda: func(matrix, h, w, kernel), number=1, repeat=warmpup)
run_times = timeit.repeat(lambda: func(matrix, h, w, kernel), number=1, repeat=measurement)
print(f"WarmUp: {warm_up_times}")
for x in run_times:
print(str(x*1000))
def comp(v1, v2):
with open(v1, 'rb') as f:
res_v1 = pickle.load(f)
with open(v2, 'rb') as f:
res_v2 = pickle.load(f)
for r in range(len(res_v1)):
for c in range(len(res_v1[r])):
if res_v1[r][c] != res_v2[r][c]:
print(f"Error @ r={r} c={c}")
if __name__ == '__main__':
# V1 - classic python
debug_run = False
width = 1920 # 20 # 1920
height = 1080 # 20 # 1080
kernel_size = 5
arr = list(np.zeros((height, width)).tolist())
v = 0
for r in range(0, height):
for c in range(0, width):
arr[r][c] = v
v += 1
v %= 255
n_arr = fix_array_or_list(arr)
np_arr = np.asarray(arr)
np_uint_arr = np.asarray(np_arr, dtype=np.uint8)
if debug_run:
for i in range(0, len(runs)):
print(f"Run Timed: {i}")
run, converted, warmup, measurement = build_run(i)
if converted == 3:
run_timed(run, np_uint_arr, height, width, warmup, measurement, kernel_size)
if converted == 2:
res = run(np_arr, height, width, warmup, measurement, kernel_size)
elif converted == 1:
res = run(n_arr, height, width, warmup, measurement, kernel_size)
elif converted == 0:
res = run(arr, height, width, warmup, measurement, kernel_size)
else:
raise ValueError("Not implemented")
with open(f'rev_v{i}.pickle', 'wb') as f:
pickle.dump(res, f)
comp('rev_v0.pickle', f'rev_v{i}.pickle')
if not debug_run:
for i in range(0, len(runs)):
run, converted, warmup, measurement = build_run(i)
print(f"Run: {i} - {run.__name__}")
if converted == 3:
run_timed(run, np_uint_arr, height, width, warmup, measurement, kernel_size)
elif converted == 2:
run_timed(run, np_arr, height, width, warmup, measurement, kernel_size)
elif converted == 1:
run_timed(run, n_arr, height, width, warmup, measurement, kernel_size)
elif converted == 0:
run_timed(run, arr, height, width, warmup, measurement, kernel_size)
else:
raise ValueError("Not implemented")