-
Notifications
You must be signed in to change notification settings - Fork 3
/
valid.py
68 lines (53 loc) · 2.19 KB
/
valid.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
import sys
import gc
import traceback
import torch.nn.parallel
import torch.optim
import torch.utils.data
from tqdm import tqdm
from common.helper import update_summary, Lss, Err
def validate(loader, model, criterion, args, summary, it):
lss = Lss(criterion.loss_name)
err = Err(args['dataset'])
model.eval()
with torch.no_grad():
description = '[i] Valid iter {}'.format(it)
for i, (pcd, img, calib, A, gt, fname) in \
enumerate(tqdm(loader, desc=description, unit='batches')):
try:
# Convert data type
pcd = pcd.to(args['DEVICE']).float()
img = img.to(args['DEVICE']).float()
calib = calib.to(args['DEVICE']).float()
A = A.to(args['DEVICE']).float()
# run model
pred = model(pcd, img, calib, A)
# compute loss
losses, gt = criterion.compute_loss(pcd, img, calib, A, gt, pred)
lss.update(losses, pcd.size(0))
err.update(gt, pred)
# del pcd, img, gt
# torch.cuda.empty_cache()
except RuntimeError as ex:
print("in VAL, RuntimeError " + repr(ex))
# traceback.print_tb(ex.__traceback__, file=logger.out_fd)
traceback.print_tb(ex.__traceback__)
if "CUDA out of memory" in str(ex) or "cuda runtime error" in str(ex):
print("out of memory, continue")
del pcd, img, gt
torch.cuda.empty_cache()
gc.collect()
print('remained objects after OOM crash')
else:
sys.exit(1)
update_summary(summary, 'valid', it, lss.dict, err.dict, pcd, img, calib, A, gt, pred, args['raw_cam_img_size'], args['lidar_fov_rad'])
print('[i] Valid iter {}; '.format(it))
print('Loss; ', end=" ")
for k in list(lss.keys):
print(k + ' {:.2f}'.format(lss.dict[k].avg), end=" ")
print()
print('Error; ', end=" ")
for k in list(err.dict.keys()):
print(k + ' {:.4f}'.format(err.dict[k]), end=" ")
print()
return lss.dict