-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathtools.py
44 lines (35 loc) · 1.15 KB
/
tools.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
import numpy as np
def custom_print(context, log_file, mode):
#custom print and log out function
if mode == 'w':
fp = open(log_file, mode)
fp.write(context + '\n')
fp.close()
elif mode == 'a+':
print(context)
fp = open(log_file, mode)
print(context, file=fp)
fp.close()
else:
raise Exception('other file operation is unimplemented !')
def generate_binary_map(pred, type,th=0.5):
if type == '2mean':
threshold = np.mean(pred) * 2
if threshold > th:
threshold = th
binary_map = pred > threshold
return binary_map.astype(np.float32)
if type == 'mean+std':
threshold = np.mean(pred) + np.std(pred)
if threshold > th:
threshold = th
binary_map = pred > threshold
return binary_map.astype(np.float32)
def calc_precision_and_jaccard(pred, gt,th=0.5):
bin_pred = generate_binary_map(pred, 'mean+std',th)
tp = (bin_pred == gt).sum()
precision = tp / (pred.size)
i = (bin_pred * gt).sum()
u = bin_pred.sum() + gt.sum() - i
jaccard = i / (u + 1e-10)
return precision, jaccard