-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
87 lines (61 loc) · 2.94 KB
/
helper.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
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchvision
from shapely.geometry import Polygon
def convert_map_to_lane_map(ego_map, binary_lane):
mask = (ego_map[0,:,:] == ego_map[1,:,:]) * (ego_map[1,:,:] == ego_map[2,:,:]) + (ego_map[0,:,:] == 250 / 255)
if binary_lane:
return (~ mask)
return ego_map * (~ mask.view(1, ego_map.shape[1], ego_map.shape[2]))
def convert_map_to_road_map(ego_map):
mask = (ego_map[0,:,:] == 1) * (ego_map[1,:,:] == 1) * (ego_map[2,:,:] == 1)
return (~mask)
def collate_fn(batch):
return tuple(zip(*batch))
def draw_box(ax, corners, color):
point_squence = torch.stack([corners[:, 0], corners[:, 1], corners[:, 3], corners[:, 2], corners[:, 0]])
# the corners are in meter and time 10 will convert them in pixels
# Add 400, since the center of the image is at pixel (400, 400)
# The negative sign is because the y axis is reversed for matplotlib
ax.plot(point_squence.T[0] * 10 + 400, -point_squence.T[1] * 10 + 400, color=color)
def compute_ats_bounding_boxes(boxes1, boxes2):
num_boxes1 = boxes1.size(0)
num_boxes2 = boxes2.size(0)
boxes1_max_x = boxes1[:, 0].max(dim=1)[0]
boxes1_min_x = boxes1[:, 0].min(dim=1)[0]
boxes1_max_y = boxes1[:, 1].max(dim=1)[0]
boxes1_min_y = boxes1[:, 1].min(dim=1)[0]
boxes2_max_x = boxes2[:, 0].max(dim=1)[0]
boxes2_min_x = boxes2[:, 0].min(dim=1)[0]
boxes2_max_y = boxes2[:, 1].max(dim=1)[0]
boxes2_min_y = boxes2[:, 1].min(dim=1)[0]
condition1_matrix = (boxes1_max_x.unsqueeze(1) > boxes2_min_x.unsqueeze(0))
condition2_matrix = (boxes1_min_x.unsqueeze(1) < boxes2_max_x.unsqueeze(0))
condition3_matrix = (boxes1_max_y.unsqueeze(1) > boxes2_min_y.unsqueeze(0))
condition4_matrix = (boxes1_min_y.unsqueeze(1) < boxes2_max_y.unsqueeze(0))
condition_matrix = condition1_matrix * condition2_matrix * condition3_matrix * condition4_matrix
iou_matrix = torch.zeros(num_boxes1, num_boxes2)
for i in range(num_boxes1):
for j in range(num_boxes2):
if condition_matrix[i][j]:
iou_matrix[i][j] = compute_iou(boxes1[i], boxes2[j])
iou_max = iou_matrix.max(dim=0)[0]
iou_thresholds = [0.5, 0.6, 0.7, 0.8, 0.9]
total_threat_score = 0
total_weight = 0
for threshold in iou_thresholds:
tp = (iou_max > threshold).sum()
threat_score = tp * 1.0 / (num_boxes1 + num_boxes2 - tp)
total_threat_score += 1.0 / threshold * threat_score
total_weight += 1.0 / threshold
average_threat_score = total_threat_score / total_weight
return average_threat_score
def compute_ts_road_map(road_map1, road_map2):
tp = (road_map1 * road_map2).sum()
return tp * 1.0 / (road_map1.sum() + road_map2.sum() - tp)
def compute_iou(box1, box2):
a = Polygon(torch.t(box1)).convex_hull
b = Polygon(torch.t(box2)).convex_hull
return a.intersection(b).area / a.union(b).area