-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtilities.py
189 lines (145 loc) · 6.5 KB
/
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
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
import math
import matplotlib
import matplotlib.cm
import matplotlib.pyplot as plt
import numpy as np
import torch
from PIL import Image
cmap = plt.cm.viridis
def DepthNorm(depth):
depth = depth[:, :, ::2, ::2]
return depth
def log10(x):
"""Convert a new tensor with the base-10 logarithm of the elements of x. """
return torch.log(x) / math.log(10)
class Result(object):
def __init__(self):
self.irmse, self.imae = 0, 0
self.mse, self.rmse, self.mae = 0, 0, 0
self.absrel, self.lg10 = 0, 0
self.delta1, self.delta2, self.delta3 = 0, 0, 0
self.data_time, self.gpu_time = 0, 0
def set_to_worst(self):
self.irmse, self.imae = np.inf, np.inf
self.mse, self.rmse, self.mae = np.inf, np.inf, np.inf
self.absrel, self.lg10 = np.inf, np.inf
self.delta1, self.delta2, self.delta3 = 0, 0, 0
self.data_time, self.gpu_time = 0, 0
def update(self, irmse, imae, mse, rmse, mae, absrel, lg10, delta1, delta2, delta3, gpu_time, data_time):
self.irmse, self.imae = irmse, imae
self.mse, self.rmse, self.mae = mse, rmse, mae
self.absrel, self.lg10 = absrel, lg10
self.delta1, self.delta2, self.delta3 = delta1, delta2, delta3
self.data_time, self.gpu_time = data_time, gpu_time
def evaluate(self, output, target):
valid_mask = ((target > 0) + (output > 0)) > 0
output = 1e3 * output[valid_mask]
target = 1e3 * target[valid_mask]
abs_diff = (output - target).abs()
# print((target-output).abs())
self.mse = float((torch.pow(abs_diff, 2)).mean())
self.rmse = math.sqrt(self.mse)
self.mae = float(abs_diff.mean())
self.lg10 = float((log10(output) - log10(target)).abs().mean())
self.absrel = float((abs_diff / target).mean())
maxRatio = torch.max(output / target, target / output)
self.delta1 = float((maxRatio < 1.25).float().mean())
self.delta2 = float((maxRatio < 1.25 ** 2).float().mean())
self.delta3 = float((maxRatio < 1.25 ** 3).float().mean())
self.data_time = 0
self.gpu_time = 0
inv_output = 1 / output
inv_target = 1 / target
abs_inv_diff = (inv_output - inv_target).abs()
self.irmse = math.sqrt((torch.pow(abs_inv_diff, 2)).mean())
self.imae = float(abs_inv_diff.mean())
class AverageMeter(object):
def __init__(self):
self.reset()
def reset(self):
self.count = 0.0
self.sum_irmse, self.sum_imae = 0, 0
self.sum_mse, self.sum_rmse, self.sum_mae = 0, 0, 0
self.sum_absrel, self.sum_lg10 = 0, 0
self.sum_delta1, self.sum_delta2, self.sum_delta3 = 0, 0, 0
self.sum_data_time, self.sum_gpu_time = 0, 0
def update(self, result, gpu_time, data_time, n=1):
self.count += n
self.sum_irmse += n * result.irmse
self.sum_imae += n * result.imae
self.sum_mse += n * result.mse
self.sum_rmse += n * result.rmse
self.sum_mae += n * result.mae
self.sum_absrel += n * result.absrel
self.sum_lg10 += n * result.lg10
self.sum_delta1 += n * result.delta1
self.sum_delta2 += n * result.delta2
self.sum_delta3 += n * result.delta3
self.sum_data_time += n * data_time
self.sum_gpu_time += n * gpu_time
def average(self):
avg = Result()
avg.update(
self.sum_irmse / self.count, self.sum_imae / self.count,
self.sum_mse / self.count, self.sum_rmse / self.count, self.sum_mae / self.count,
self.sum_absrel / self.count, self.sum_lg10 / self.count,
self.sum_delta1 / self.count, self.sum_delta2 / self.count, self.sum_delta3 / self.count,
self.sum_gpu_time / self.count, self.sum_data_time / self.count)
return avg
def colorize(value, vmin=10, vmax=1000, cmap='plasma'):
value = value.cpu().numpy()[0, :, :]
# normalize
vmin = value.min() if vmin is None else vmin
vmax = value.max() if vmax is None else vmax
if vmin != vmax:
value = (value - vmin) / (vmax - vmin) # vmin..vmax
else:
# Avoid 0-division
value = value * 0.
# squeeze last dim if it exists
# value = value.squeeze(axis=0)
cmapper = matplotlib.cm.get_cmap(cmap)
value = cmapper(value, bytes=True) # (nxmx4)
img = value[:, :, :3]
return img.transpose((2, 0, 1))
def logEpoch(num_epoch, loss, file_name):
f = open(file_name, "a+")
s = "Epoch [{:}]\t loss value is: {:}\n".format(num_epoch, loss)
f.write("******************************************\n")
f.write(s)
f.close()
return
def colored_depthmap(depth, d_min=None, d_max=None):
if d_min is None:
d_min = np.min(depth)
if d_max is None:
d_max = np.max(depth)
depth_relative = (depth - d_min) / (d_max - d_min)
return 255 * cmap(depth_relative)[:, :, :3] # H, W, C
def merge_into_row(input, depth_target, depth_pred):
rgb = 255 * np.transpose(np.squeeze(input.cpu().numpy()), (1, 2, 0)) # H, W, C
depth_target_cpu = np.squeeze(depth_target.cpu().numpy())
depth_pred_cpu = np.squeeze(depth_pred.data.cpu().numpy())
d_min = min(np.min(depth_target_cpu), np.min(depth_pred_cpu))
d_max = max(np.max(depth_target_cpu), np.max(depth_pred_cpu))
depth_target_col = colored_depthmap(depth_target_cpu, d_min, d_max)
depth_pred_col = colored_depthmap(depth_pred_cpu, d_min, d_max)
img_merge = np.hstack([rgb, depth_target_col, depth_pred_col])
return img_merge
def merge_into_row_with_gt(input, depth_input, depth_target, depth_pred):
rgb = 255 * np.transpose(np.squeeze(input.cpu().numpy()), (1, 2, 0)) # H, W, C
depth_input_cpu = np.squeeze(depth_input.cpu().numpy())
depth_target_cpu = np.squeeze(depth_target.cpu().numpy())
depth_pred_cpu = np.squeeze(depth_pred.data.cpu().numpy())
d_min = min(np.min(depth_input_cpu), np.min(depth_target_cpu), np.min(depth_pred_cpu))
d_max = max(np.max(depth_input_cpu), np.max(depth_target_cpu), np.max(depth_pred_cpu))
depth_input_col = colored_depthmap(depth_input_cpu, d_min, d_max)
depth_target_col = colored_depthmap(depth_target_cpu, d_min, d_max)
depth_pred_col = colored_depthmap(depth_pred_cpu, d_min, d_max)
img_merge = np.hstack([rgb, depth_input_col, depth_target_col, depth_pred_col])
return img_merge
def add_row(img_merge, row):
return np.vstack([img_merge, row])
def save_image(img_merge, filename):
img_merge = Image.fromarray(img_merge.astype('uint8'))
img_merge.save(filename)