forked from firasl/CWCC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prediction_utilities.py
159 lines (112 loc) · 5.19 KB
/
prediction_utilities.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Dec 26 13:52:52 2019
@author: laakom
"""
import numpy as np
import cv2
from sklearn.feature_extraction import image
from tqdm import tqdm
def ill_predict(model,testsamplesID,groundtruth_dic,method):
ground_truths, precitions = CWCCUU227_predict(model, testsamplesID,groundtruth_dic)
precitions = np.squeeze(precitions)
return ground_truths,precitions
def CWCCUU227_predict(model, testsamplesID,groundtruth_dic):
predictions = []
ground_truths = []
for ID in tqdm(testsamplesID):
img = cv2.resize(cv2.imread(ID,-1),(227,227))
img = (img * 1.0/ 65535.0 ).astype('float32')
#img =adjust_gamma_training(img)
img = np.expand_dims(img, axis=0)
img = [np.expand_dims(img[:,:,:,2],axis=-1),np.expand_dims(img[:,:,:,1],axis=-1),np.expand_dims(img[:,:,:,0],axis=-1)]
#img =preprocess_input(img)
#img_patches = preprocess_input(np.float32(img[..., ::-1]*255.0))
patches_prediction = model.predict(img)
patches_prediction = patches_prediction[:, 0:3]
patches_prediction= np.reshape(patches_prediction,(-1,3))
# patches_prediction = np.clip(patches_prediction[0], 10**(-6), np.max(patches_prediction[0]) )
patches_prediction /= np.linalg.norm(patches_prediction,2)
predictions.append(patches_prediction)
ground_truths.append(groundtruth_dic[ID])
return np.array(ground_truths,dtype = 'float32' ),np.squeeze(np.array(predictions))
def CWCCUU2271_predict(model, testsamplesID,groundtruth_dic):
predictions = []
ground_truths = []
for ID in tqdm(testsamplesID):
img = cv2.resize(cv2.imread(ID,-1),(227,227))
img = (img * 1.0/ 65535.0 ).astype('float32')
#img =adjust_gamma_training(img)
img = np.expand_dims(img, axis=0)
img = [np.expand_dims(img[:,:,:,2],axis=-1),np.expand_dims(img[:,:,:,1],axis=-1),np.expand_dims(img[:,:,:,0],axis=-1)]
#img =preprocess_input(img)
#img_patches = preprocess_input(np.float32(img[..., ::-1]*255.0))
patches_prediction = model.predict(img)
# patches_prediction = patches_prediction[:, 0:3]
patches_prediction= np.reshape(patches_prediction,(-1,6))
# patches_prediction = np.clip(patches_prediction[0], 10**(-6), np.max(patches_prediction[0]) )
# patches_prediction /= np.linalg.norm(patches_prediction,2)
predictions.append(patches_prediction)
ground_truths.append(groundtruth_dic[ID])
return np.array(ground_truths,dtype = 'float32' ),np.squeeze(np.array(predictions))
def angular_error_reproduction(ground_truth, prediction):
"""
calculate angular error(s) between the ground truth RGB triplet(s) and the predicted one(s)
:param ground_truth: N*3 or 1*3 Numpy array, each row for one ground truth triplet
:param prediction: N*3 Numpy array, each row for one predicted triplet
:return: angular error(s) in degree as Numpy array
"""
res = np.divide(ground_truth,prediction)
res_norm = res / np.linalg.norm(res, ord=2, axis=-1, keepdims=True)
u = np.ones(np.shape(res)) / np.sqrt(3)
u_norm = u / np.linalg.norm(u, ord=2, axis=-1, keepdims=True)
return 180 * np.arccos(np.clip(np.sum(res_norm * u_norm, axis=-1),.0,1.0)) / np.pi
def angular_error_recovery(ground_truth, prediction):
"""
calculate angular error(s) between the ground truth RGB triplet(s) and the predicted one(s)
:param ground_truth: N*3 or 1*3 Numpy array, each row for one ground truth triplet
:param prediction: N*3 Numpy array, each row for one predicted triplet
:return: angular error(s) in degree as Numpy array
"""
ground_truth_norm = ground_truth / np.linalg.norm(ground_truth, ord=2, axis=-1, keepdims=True)
prediction_norm = prediction / np.linalg.norm(prediction, ord=2, axis=-1, keepdims=True)
return 180 * np.arccos(np.sum(ground_truth_norm * prediction_norm, axis=-1)) / np.pi
def summary_angular_errors(errors):
errors = sorted(errors)
def g(f):
return np.percentile(errors, f * 100)
median = g(0.5)
mean = np.mean(errors)
max = np.max(errors)
trimean = 0.25 * (g(0.25) + 2 * g(0.5) + g(0.75))
results = {
'25': np.mean(errors[:int(0.25 * len(errors))]),
'75': np.mean(errors[int(0.75 * len(errors)):]),
'95': g(0.95),
'tri': trimean,
'med': median,
'mean': mean,
'max': max
}
return results
def just_print_angular_errors(results):
print("25: %5.3f," % results['25'], end=' ')
print("med: %5.3f" % results['med'], end=' ')
print("tri: %5.3f" % results['tri'], end=' ')
print("avg: %5.3f" % results['mean'], end=' ')
print("75: %5.3f" % results['75'], end=' ')
print("95: %5.3f" % results['95'], end=' ')
print("max: %5.3f" % results['max'])
def print_angular_errors(errors):
print("%d images tested. Results:" % len(errors))
results = summary_angular_errors(errors)
just_print_angular_errors(results)
def save_errors(errors,path):
import csv
print("%d images tested. Results:" % len(errors))
results = summary_angular_errors(errors)
w = csv.writer(open(path+ '.csv', "w"))
for key, val in results.items():
w.writerow([key, val])
just_print_angular_errors(results)