-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathutils.py
364 lines (302 loc) · 12.7 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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
import json
import logging
import os
import errno
import time
import numpy as np
import torch
from lib.pc_utils import colorize_pointcloud, save_point_cloud
def load_state_with_same_shape(model, weights):
model_state = model.state_dict()
filtered_weights = {
k: v for k, v in weights.items() if k in model_state and v.size() == model_state[k].size()
}
logging.info("Loading weights:" + ', '.join(filtered_weights.keys()))
return filtered_weights
def checkpoint(model, optimizer, epoch, iteration, config, best_val=None, best_val_iter=None, postfix=None):
mkdir_p(config.log_dir)
if config.overwrite_weights:
if postfix is not None:
filename = f"checkpoint_{config.wrapper_type}{config.model}{postfix}.pth"
else:
filename = f"checkpoint_{config.wrapper_type}{config.model}.pth"
else:
filename = f"checkpoint_{config.wrapper_type}{config.model}_iter_{iteration}.pth"
checkpoint_file = config.log_dir + '/' + filename
state = {
'iteration': iteration,
'epoch': epoch,
'arch': config.model,
'state_dict': model.state_dict(),
'optimizer': optimizer.state_dict()
}
if best_val is not None:
state['best_val'] = best_val
state['best_val_iter'] = best_val_iter
json.dump(vars(config), open(config.log_dir + '/config.json', 'w'), indent=4)
torch.save(state, checkpoint_file)
logging.info(f"Checkpoint saved to {checkpoint_file}")
# Delete symlink if it exists
if os.path.exists(f'{config.log_dir}/weights.pth'):
os.remove(f'{config.log_dir}/weights.pth')
# Create symlink
os.system(f'cd {config.log_dir}; ln -s {filename} weights.pth')
def precision_at_one(pred, target, ignore_label=255):
"""Computes the precision@k for the specified values of k"""
# batch_size = target.size(0) * target.size(1) * target.size(2)
pred = pred.view(1, -1)
target = target.view(1, -1)
correct = pred.eq(target)
correct = correct[target != ignore_label]
correct = correct.view(-1)
if correct.nelement():
return correct.float().sum(0).mul(100.0 / correct.size(0)).item()
else:
return float('nan')
def fast_hist(pred, label, n):
k = (label >= 0) & (label < n)
return np.bincount(n * label[k].astype(int) + pred[k], minlength=n**2).reshape(n, n)
def per_class_iu(hist):
with np.errstate(divide='ignore', invalid='ignore'):
return np.diag(hist) / (hist.sum(1) + hist.sum(0) - np.diag(hist))
class WithTimer(object):
"""Timer for with statement."""
def __init__(self, name=None):
self.name = name
def __enter__(self):
self.tstart = time.time()
def __exit__(self, type, value, traceback):
out_str = 'Elapsed: %s' % (time.time() - self.tstart)
if self.name:
logging.info('[{self.name}]')
logging.info(out_str)
class Timer(object):
"""A simple timer."""
def __init__(self):
self.total_time = 0.
self.calls = 0
self.start_time = 0.
self.diff = 0.
self.average_time = 0.
def reset(self):
self.total_time = 0
self.calls = 0
self.start_time = 0
self.diff = 0
self.averate_time = 0
def tic(self):
# using time.time instead of time.clock because time time.clock
# does not normalize for multithreading
self.start_time = time.time()
def toc(self, average=True):
self.diff = time.time() - self.start_time
self.total_time += self.diff
self.calls += 1
self.average_time = self.total_time / self.calls
if average:
return self.average_time
else:
return self.diff
class ExpTimer(Timer):
""" Exponential Moving Average Timer """
def __init__(self, alpha=0.5):
super(ExpTimer, self).__init__()
self.alpha = alpha
def toc(self):
self.diff = time.time() - self.start_time
self.average_time = self.alpha * self.diff + \
(1 - self.alpha) * self.average_time
return self.average_time
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.reset()
def reset(self):
self.val = 0
self.avg = 0
self.sum = 0
self.count = 0
def update(self, val, n=1):
self.val = val
self.sum += val * n
self.count += n
self.avg = self.sum / self.count
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir(path):
pass
else:
raise
def read_txt(path):
"""Read txt file into lines.
"""
with open(path) as f:
lines = f.readlines()
lines = [x.strip() for x in lines]
return lines
def debug_on():
import sys
import pdb
import functools
import traceback
def decorator(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
try:
return f(*args, **kwargs)
except Exception:
info = sys.exc_info()
traceback.print_exception(*info)
pdb.post_mortem(info[2])
return wrapper
return decorator
def get_prediction(dataset, output, target):
return output.max(1)[1]
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def get_torch_device(is_cuda):
return torch.device('cuda' if is_cuda else 'cpu')
class HashTimeBatch(object):
def __init__(self, prime=5279):
self.prime = prime
def __call__(self, time, batch):
return self.hash(time, batch)
def hash(self, time, batch):
return self.prime * batch + time
def dehash(self, key):
time = key % self.prime
batch = key / self.prime
return time, batch
def save_rotation_pred(iteration, pred, dataset, save_pred_dir):
"""Save prediction results in original pointcloud scale."""
decode_label_map = {}
for k, v in dataset.label_map.items():
decode_label_map[v] = k
pred = np.array([decode_label_map[x] for x in pred], dtype=np.int)
out_rotation_txt = dataset.get_output_id(iteration) + '.txt'
out_rotation_path = save_pred_dir + '/' + out_rotation_txt
np.savetxt(out_rotation_path, pred, fmt='%i')
def save_predictions(coords, upsampled_pred, transformation, dataset, config, iteration,
save_pred_dir):
"""Save prediction results in original pointcloud scale."""
from lib.dataset import OnlineVoxelizationDatasetBase
if dataset.IS_ONLINE_VOXELIZATION:
assert transformation is not None, 'Need transformation matrix.'
iter_size = coords[:, -1].max() + 1 # Normally batch_size, may be smaller at the end.
if dataset.IS_TEMPORAL: # Iterate over temporal dilation.
iter_size *= config.temporal_numseq
for i in range(iter_size):
# Get current pointcloud filtering mask.
if dataset.IS_TEMPORAL:
j = i % config.temporal_numseq
i = i // config.temporal_numseq
batch_mask = coords[:, -1].numpy() == i
if dataset.IS_TEMPORAL:
batch_mask = np.logical_and(batch_mask, coords[:, -2].numpy() == j)
# Calculate original coordinates.
coords_original = coords[:, :3].numpy()[batch_mask] + 0.5
if dataset.IS_ONLINE_VOXELIZATION:
# Undo voxelizer transformation.
curr_transformation = transformation[i, :16].numpy().reshape(4, 4)
xyz = np.hstack((coords_original, np.ones((batch_mask.sum(), 1))))
orig_coords = (np.linalg.inv(curr_transformation) @ xyz.T).T
else:
orig_coords = coords_original
orig_pred = upsampled_pred[batch_mask]
# Undo ignore label masking to fit original dataset label.
if dataset.IGNORE_LABELS:
if isinstance(dataset, OnlineVoxelizationDatasetBase):
label2masked = dataset.label2masked
maskedmax = label2masked[label2masked < 255].max() + 1
masked2label = [label2masked.tolist().index(i) for i in range(maskedmax)]
orig_pred = np.take(masked2label, orig_pred)
else:
decode_label_map = {}
for k, v in dataset.label_map.items():
decode_label_map[v] = k
orig_pred = np.array([decode_label_map[x] for x in orig_pred], dtype=np.int)
# Determine full path of the destination.
full_pred = np.hstack((orig_coords[:, :3], np.expand_dims(orig_pred, 1)))
filename = 'pred_%04d_%02d.npy' % (iteration, i)
if dataset.IS_TEMPORAL:
filename = 'pred_%04d_%02d_%02d.npy' % (iteration, i, j)
# Save final prediction as npy format.
np.save(os.path.join(save_pred_dir, filename), full_pred)
def visualize_results(coords, input, target, upsampled_pred, config, iteration):
# Get filter for valid predictions in the first batch.
target_batch = coords[:, 3].numpy() == 0
input_xyz = coords[:, :3].numpy()
target_valid = target.numpy() != 255
target_pred = np.logical_and(target_batch, target_valid)
target_nonpred = np.logical_and(target_batch, ~target_valid)
ptc_nonpred = np.hstack((input_xyz[target_nonpred], np.zeros((np.sum(target_nonpred), 3))))
# Unwrap file index if tested with rotation.
file_iter = iteration
if config.test_rotation >= 1:
file_iter = iteration // config.test_rotation
# Create directory to save visualization results.
os.makedirs(config.visualize_path, exist_ok=True)
# Label visualization in RGB.
xyzlabel = colorize_pointcloud(input_xyz[target_pred], upsampled_pred[target_pred])
xyzlabel = np.vstack((xyzlabel, ptc_nonpred))
filename = '_'.join([config.dataset, config.model, 'pred', '%04d.ply' % file_iter])
save_point_cloud(xyzlabel, os.path.join(config.visualize_path, filename), verbose=False)
# RGB input values visualization.
xyzrgb = np.hstack((input_xyz[target_batch], input[:, :3].cpu().numpy()[target_batch]))
filename = '_'.join([config.dataset, config.model, 'rgb', '%04d.ply' % file_iter])
save_point_cloud(xyzrgb, os.path.join(config.visualize_path, filename), verbose=False)
# Ground-truth visualization in RGB.
xyzgt = colorize_pointcloud(input_xyz[target_pred], target.numpy()[target_pred])
xyzgt = np.vstack((xyzgt, ptc_nonpred))
filename = '_'.join([config.dataset, config.model, 'gt', '%04d.ply' % file_iter])
save_point_cloud(xyzgt, os.path.join(config.visualize_path, filename), verbose=False)
def permute_pointcloud(input_coords, pointcloud, transformation, label_map,
voxel_output, voxel_pred):
"""Get permutation from pointcloud to input voxel coords."""
def _hash_coords(coords, coords_min, coords_dim):
return np.ravel_multi_index((coords - coords_min).T, coords_dim)
# Validate input.
input_batch_size = input_coords[:, -1].max().item()
pointcloud_batch_size = pointcloud[:, -1].max().int().item()
transformation_batch_size = transformation[:, -1].max().int().item()
assert input_batch_size == pointcloud_batch_size == transformation_batch_size
pointcloud_permutation, pointcloud_target = [], []
# Process each batch.
for i in range(input_batch_size + 1):
# Filter batch from the data.
input_coords_mask_b = input_coords[:, -1] == i
input_coords_b = (input_coords[input_coords_mask_b])[:, :-1].numpy()
pointcloud_b = pointcloud[pointcloud[:, -1] == i, :-1].numpy()
transformation_b = transformation[i, :-1].reshape(4, 4).numpy()
# Transform original pointcloud to voxel space.
original_coords1 = np.hstack((pointcloud_b[:, :3], np.ones((pointcloud_b.shape[0], 1))))
original_vcoords = np.floor(original_coords1 @ transformation_b.T)[:, :3].astype(int)
# Hash input and voxel coordinates to flat coordinate.
vcoords_all = np.vstack((input_coords_b, original_vcoords))
vcoords_min = vcoords_all.min(0)
vcoords_dims = vcoords_all.max(0) - vcoords_all.min(0) + 1
input_coords_key = _hash_coords(input_coords_b, vcoords_min, vcoords_dims)
original_vcoords_key = _hash_coords(original_vcoords, vcoords_min, vcoords_dims)
# Query voxel predictions from original pointcloud.
key_to_idx = dict(zip(input_coords_key, range(len(input_coords_key))))
pointcloud_permutation.append(
np.array([key_to_idx.get(i, -1) for i in original_vcoords_key]))
pointcloud_target.append(pointcloud_b[:, -1].astype(int))
pointcloud_permutation = np.concatenate(pointcloud_permutation)
# Prepare pointcloud permutation array.
pointcloud_permutation = torch.from_numpy(pointcloud_permutation)
permutation_mask = pointcloud_permutation >= 0
permutation_valid = pointcloud_permutation[permutation_mask]
# Permuate voxel output to pointcloud.
pointcloud_output = torch.zeros(pointcloud.shape[0], voxel_output.shape[1]).to(voxel_output)
pointcloud_output[permutation_mask] = voxel_output[permutation_valid]
# Permuate voxel prediction to pointcloud.
# NOTE: Invalid points (points found in pointcloud but not in the voxel) are mapped to 0.
pointcloud_pred = torch.ones(pointcloud.shape[0]).int().to(voxel_pred) * 0
pointcloud_pred[permutation_mask] = voxel_pred[permutation_valid]
# Map pointcloud target to respect dataset IGNORE_LABELS
pointcloud_target = torch.from_numpy(
np.array([label_map[i] for i in np.concatenate(pointcloud_target)])).int()
return pointcloud_output, pointcloud_pred, pointcloud_target