-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathuvdocBenchmark_eval.py
129 lines (103 loc) · 4.07 KB
/
uvdocBenchmark_eval.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
import argparse
import json
import multiprocessing as mp
import os
from utils import get_version
N_LINES = 25
def visual_metrics_process(queue, uvdoc_path, preds_path, verbose):
"""
Subprocess function that computes visual metrics (MS-SSIM, LD, and AD) based on a matlab script.
"""
import matlab.engine
eng = matlab.engine.start_matlab()
eng.cd(r"./eval/eval_code/", nargout=0)
mean_ms, mean_ad = eng.evalScriptUVDoc(uvdoc_path, preds_path, verbose, nargout=2)
queue.put(dict(ms=mean_ms, ad=mean_ad))
def ocr_process(queue, uvdoc_path, preds_path):
"""
Subprocess function that computes OCR metrics (CER and ED).
"""
from eval.ocr_eval.ocr_eval import OCR_eval_UVDoc
CERmean, EDmean, OCR_dict_results = OCR_eval_UVDoc(uvdoc_path, preds_path)
with open(os.path.join(preds_path, "ocr_res.json"), "w") as f:
json.dump(OCR_dict_results, f)
queue.put(dict(cer=CERmean, ed=EDmean))
def new_line_metric_process(queue, uvdoc_path, preds_path, n_lines):
"""
Subprocess function that computes the new line metrics on the UVDoc benchmark.
"""
from uvdocBenchmark_metric import compute_line_metric
hor_metric, ver_metric = compute_line_metric(uvdoc_path, preds_path, n_lines)
queue.put(dict(hor_line=hor_metric, ver_line=ver_metric))
def compute_metrics(uvdoc_path, pred_path, pred_type, verbose=False):
"""
Compute and save all metrics.
"""
if not pred_path.endswith("/"):
pred_path += "/"
q = mp.Queue()
# Create process to compute MS-SSIM, LD, AD
p1 = mp.Process(
target=visual_metrics_process,
args=(q, os.path.join(uvdoc_path, "texture_sample"), os.path.join(pred_path, pred_type), verbose),
)
p1.start()
# Create process to compute new line metrics
p2 = mp.Process(
target=new_line_metric_process,
args=(q, uvdoc_path, os.path.join(pred_path, "bm"), N_LINES),
)
p2.start()
# Create process to compute OCR metrics
p3 = mp.Process(
target=ocr_process, args=(q, os.path.join(uvdoc_path, "texture_sample"), os.path.join(pred_path, pred_type))
)
p3.start()
p1.join()
p2.join()
p3.join()
# Get results
res = {}
for _ in range(q.qsize()):
ret = q.get()
for k, v in ret.items():
res[k] = v
# Print and saves results
print("--- Results ---")
print(f" Mean MS-SSIM : {res['ms']}")
print(f" Mean AD : {res['ad']}")
print(f" Mean CER : {res['cer']}")
print(f" Mean ED : {res['ed']}")
print(f" Hor Line : {res['hor_line']}")
print(f" Ver Line : {res['ver_line']}")
with open(os.path.join(pred_path, pred_type, "resUVDoc.txt"), "w") as f:
f.write(f"Mean MS-SSIM : {res['ms']}\n")
f.write(f"Mean AD : {res['ad']}\n")
f.write(f"Mean CER : {res['cer']}\n")
f.write(f"Mean ED : {res['ed']}\n")
f.write(f"Hor Line : {res['hor_line']}\n")
f.write(f"Ver Line : {res['ver_line']}\n")
f.write("\n--- Module Version ---\n")
for module, version in get_version().items():
f.write(f"{module:25s}: {version}\n")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--uvdoc-path", type=str, default="./data/UVDoc_benchmark/", help="Path to the uvdoc benchmark dataset"
)
parser.add_argument("--pred-path", type=str, help="Path to the UVDoc benchmark predictions. Need to be absolute.")
parser.add_argument(
"--pred-type",
type=str,
default="uwp_texture",
choices=["uwp_texture", "uwp_img"],
help="Which type of prediction to compare. Either the unwarped textures or the unwarped litted images.",
)
parser.add_argument("-v", "--verbose", action="store_true")
args = parser.parse_args()
compute_metrics(
uvdoc_path=os.path.abspath(args.uvdoc_path),
pred_path=os.path.abspath(args.pred_path),
pred_type=args.pred_type,
verbose=args.verbose,
)