/
test.py
68 lines (56 loc) · 2.24 KB
/
test.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 torch
import time
import os
from loss import CustomLoss
from data import get_data_loader
from model import LDOPC
from utils import get_model_name, load_config, plot_bev, plot_label_map
from evaluate import non_max_suppression
import sys
import cv2
def inference():
config_name='config.json'
config, _, _, _ = load_config(config_name)
if torch.cuda.is_available():
device = 'cuda'
net = LDOPC(config['use_bn']).cuda()
else:
device = 'cpu'
net = LDOPC(config['use_bn']).cpu()
net.load_state_dict(torch.load(get_model_name(config['name']), map_location=device))
net.set_decode(True)
loader, _ = get_data_loader(batch_size=1, use_npy=config['use_npy'], frame_range=config['frame_range'])
net.eval()
image_id = 25
threshold = config['cls_threshold']
with torch.no_grad():
pc_feature, label_map = loader.dataset[image_id]
pc_feature = pc_feature.to(device)
label_map = label_map.to(device)
label_map_unnorm, label_list = loader.dataset.get_label(image_id)
t_start = time.time()
pred = net(pc_feature.unsqueeze(0)).squeeze_(0)
print("Forward pass time", time.time() - t_start)
cls_pred = pred[..., 0]
activation = cls_pred > threshold
num_boxes = int(activation.sum())
if num_boxes == 0:
print("No bounding box found")
return
corners = torch.zeros((num_boxes, 8))
for i in range(1, 9):
corners[:, i - 1] = torch.masked_select(pred[..., i], activation)
corners = corners.view(-1, 4, 2).numpy()
scores = (torch.masked_select(pred[..., 0], activation)).cpu().numpy()
t_start = time.time()
selected_ids = non_max_suppression(corners, scores, config['nms_iou_threshold'])
corners = corners[selected_ids]
scores = scores[selected_ids]
print("Non max suppression time:", time.time() - t_start)
pc_feature = pc_feature.cpu().numpy()
plot_bev(pc_feature, label_list, window_name='GT')
plot_bev(pc_feature, corners, window_name='Prediction')
plot_label_map(cls_pred.cpu().numpy())
cv2.waitKey (0)
if __name__=='__main__':
inference()