-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
executable file
·114 lines (90 loc) · 3.77 KB
/
run.py
File metadata and controls
executable file
·114 lines (90 loc) · 3.77 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
#!/usr/bin/env python3
import importlib
import time
import hashlib
import os.path
from argparse import ArgumentParser
import numpy as np
from PIL import Image
def run_algorithm(f, img_array, limit=None, hash=None, **kwargs):
print(f"testing {f.__module__.split('.')[-1]}")
# record the start time before executing the algorithm
start_time = time.time()
# execute the algorithm while applying any given limit
img_out_array = f(img_array[:limit, :limit], **kwargs)
# save the elapsed time
elapsed_time = time.time() - start_time
# only display the image if we've processed the entire image
if limit is None:
img_out = Image.fromarray(img_out_array, 'L')
img_out.show()
# initialize our log prefix
log_prefix = "elapsed time"
# if we only processed a limited image, then calculate an estimate of what the total calculation time shoul be
if limit is not None:
elapsed_time *= (img_array.size / (limit ** 2))
log_prefix = "estimated elapsed time"
# calculate the hash of the output image
h = hashlib.sha1(img_out_array.view(np.uint8)).hexdigest()
# check if the hash is OK
hash_ok = h == hash
# print a log message
msg = f"{log_prefix} for {f.__module__.split('.')[-1]}: {elapsed_time:0.2f}s"
if hash is not None:
msg += f"; hash: {h} ({'ok' if hash_ok else 'bad'})"
print(msg)
# return the image array
return img_out_array
if __name__ == '__main__':
parser = ArgumentParser(
description="executes the various implementations of FLCLAHE and prints the processing time"
)
parser.add_argument("-i", "--image",
choices=("f16", "cosmic_cliffs", "realme"),
default="f16",
help="the source image file to process")
parser.add_argument("-c", "--clip-limit",
type=int,
default=8,
help="the FLCLAHE clip limit to use")
parser.add_argument("-w", "--window",
type=int,
default=64,
help="the FLCLAHE window size to use")
parser.add_argument("algorithms",
nargs="*",
default=(
"python",
"python_refactored",
"numpy_precomputed_bins",
"numpy_as_strided",
"numpy_bincount",
"numpy_skip",
"numpy_vectorized",
"numpy_threads",
"numpy_threads_vectorized",
"numba",
"numba_skip",
"numba_vectorized",
"numba_threads",
"numba_threads_nogil",
"numba_parallel",
"numba_cuda",
),
help="the FLCLAHE algorithm implementations to execute")
args = parser.parse_args()
img_path = "images"
img = Image.open(os.path.join(img_path, f"{args.image}.tif"))
sha1 = open(os.path.join(img_path, f"{args.image}.sha1")).readline().strip()
# noinspection PyTypeChecker
img_array = np.array(img)
for algorithm in args.algorithms:
a = importlib.import_module(f"flclahe.{algorithm}")
if not hasattr(a, 'flclahe'):
print(f"WARNING: could not locate implementation for algorithm '{algorithm}")
continue
run_algorithm(a.flclahe,
img_array,
hash=sha1,
window=args.window,
clip_limit=args.clip_limit)