-
Notifications
You must be signed in to change notification settings - Fork 70
/
utils.py
221 lines (176 loc) · 7.22 KB
/
utils.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import numpy as np
import torchvision.utils
import torch
from torch.utils.tensorboard import SummaryWriter
from typing import Any, Callable, Union, Dict
def print_args(args: Any) -> None:
"""Utilities to print arguments
Args:
args: arguments to print out
"""
print("################################ args ################################")
for k, v in args.__dict__.items():
print("{0: <10}\t{1: <30}\t{2: <20}".format(k, str(v), str(type(v))))
print("########################################################################")
def make_nograd_func(func: Callable) -> Callable:
"""Utilities to make function no gradient
Args:
func: input function
Returns:
no gradient function wrapper for input function
"""
def wrapper(*f_args, **f_kwargs):
with torch.no_grad():
ret = func(*f_args, **f_kwargs)
return ret
return wrapper
def make_recursive_func(func: Callable) -> Callable:
"""Convert a function into recursive style to handle nested dict/list/tuple variables
Args:
func: input function
Returns:
recursive style function
"""
def wrapper(args):
if isinstance(args, list):
return [wrapper(x) for x in args]
elif isinstance(args, tuple):
return tuple([wrapper(x) for x in args])
elif isinstance(args, dict):
return {k: wrapper(v) for k, v in args.items()}
else:
return func(args)
return wrapper
@make_recursive_func
def tensor2float(args: Any) -> float:
"""Convert tensor to float"""
if isinstance(args, float):
return args
elif isinstance(args, torch.Tensor):
return args.data.item()
else:
raise NotImplementedError("invalid input type {} for tensor2float".format(type(args)))
@make_recursive_func
def tensor2numpy(args: Any) -> np.ndarray:
"""Convert tensor to numpy array"""
if isinstance(args, np.ndarray):
return args
elif isinstance(args, torch.Tensor):
return args.detach().cpu().numpy().copy()
else:
raise NotImplementedError("invalid input type {} for tensor2numpy".format(type(args)))
@make_recursive_func
def to_cuda(args: Any) -> Union[str, torch.Tensor]:
"""Convert tensor to tensor on GPU"""
if isinstance(args, torch.Tensor):
return args.cuda()
elif isinstance(args, str):
return args
else:
raise NotImplementedError("invalid input type {} for to_cuda".format(type(args)))
def save_scalars(logger: SummaryWriter, mode: str, scalar_dict: Dict[str, Any], global_step: int) -> None:
"""Log values stored in the scalar dictionary
Args:
logger: tensorboard summary writer
mode: mode name used in writing summaries
scalar_dict: python dictionary stores the key and value pairs to be recorded
global_step: step index where the logger should write
"""
scalar_dict = tensor2float(scalar_dict)
for key, value in scalar_dict.items():
if not isinstance(value, (list, tuple)):
name = "{}/{}".format(mode, key)
logger.add_scalar(name, value, global_step)
else:
for idx in range(len(value)):
name = "{}/{}_{}".format(mode, key, idx)
logger.add_scalar(name, value[idx], global_step)
def save_images(logger: SummaryWriter, mode: str, images: Dict[str, np.ndarray], global_step: int) -> None:
"""Log images stored in the image dictionary
Args:
logger: tensorboard summary writer
mode: mode name used in writing summaries
images: python dictionary stores the key and image pairs to be recorded
global_step: step index where the logger should write
"""
def preprocess(image_name, image):
if not (len(image.shape) == 3 or len(image.shape) == 4):
raise NotImplementedError("invalid img shape {}:{} in save_images".format(image_name, image.shape))
if len(image.shape) == 3:
image = image[:, np.newaxis, :, :]
image = torch.from_numpy(image[:1])
return torchvision.utils.make_grid(image, padding=0, nrow=1, normalize=True, scale_each=True)
for key, value in images.items():
if not isinstance(value, (list, tuple)):
name = "{}/{}".format(mode, key)
logger.add_image(name, preprocess(name, value), global_step)
else:
for idx in range(len(value)):
name = "{}/{}_{}".format(mode, key, idx)
logger.add_image(name, preprocess(name, value[idx]), global_step)
class DictAverageMeter:
"""Wrapper class for dictionary variables that require the average value"""
def __init__(self) -> None:
"""Initialization method"""
self.data: Dict[Any, float] = {}
self.count = 0
def update(self, new_input: Dict[Any, float]) -> None:
"""Update the stored dictionary with new input data
Args:
new_input: new data to update self.data
"""
self.count += 1
if len(self.data) == 0:
for k, v in new_input.items():
if not isinstance(v, float):
raise NotImplementedError("invalid data {}: {}".format(k, type(v)))
self.data[k] = v
else:
for k, v in new_input.items():
if not isinstance(v, float):
raise NotImplementedError("invalid data {}: {}".format(k, type(v)))
self.data[k] += v
def mean(self) -> Any:
"""Return the average value of values stored in self.data"""
return {k: v / self.count for k, v in self.data.items()}
def compute_metrics_for_each_image(metric_func: Callable) -> Callable:
"""A wrapper to compute metrics for each image individually"""
def wrapper(depth_est, depth_gt, mask, *args):
batch_size = depth_gt.shape[0]
results = []
# compute result one by one
for idx in range(batch_size):
ret = metric_func(depth_est[idx], depth_gt[idx], mask[idx], *args)
results.append(ret)
return torch.stack(results).mean()
return wrapper
@make_nograd_func
@compute_metrics_for_each_image
def threshold_metrics(
depth_est: torch.Tensor, depth_gt: torch.Tensor, mask: torch.Tensor, threshold: float
) -> torch.Tensor:
"""Return error rate for where absolute error is larger than threshold.
Args:
depth_est: estimated depth map
depth_gt: ground truth depth map
mask: mask
threshold: threshold
Returns:
error rate: error rate of the depth map
"""
depth_est, depth_gt = depth_est[mask], depth_gt[mask]
errors = torch.abs(depth_est - depth_gt).float()
err_mask = errors > threshold
return torch.mean(err_mask.float())
# NOTE: please do not use this to build up training loss
@make_nograd_func
@compute_metrics_for_each_image
def absolute_depth_error_metrics(depth_est: torch.Tensor, depth_gt: torch.Tensor, mask: torch.Tensor) -> torch.Tensor:
"""Calculate average absolute depth error
Args:
depth_est: estimated depth map
depth_gt: ground truth depth map
mask: mask
"""
depth_est, depth_gt = depth_est[mask], depth_gt[mask]
return torch.mean((depth_est - depth_gt).abs())