forked from jonathan-hellwig/robot_detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
196 lines (166 loc) · 7.11 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
import itertools
import torch.nn.functional as F
import torch
NUM_BOX_PARAMETERS: int = 4
def xywh_to_tlbr(xywh_bounding_boxes: torch.Tensor) -> torch.Tensor:
"""
Convert bounding boxes from (center_x, center_y, width, height) format to (top_left_x, top_left_y, bottom_right_x, bottom_right_y) format.
Prameters:
- xywh_bounding_boxes: (batch_size, 4)
"""
tlbr_bounding_boxes = torch.zeros_like(xywh_bounding_boxes)
tlbr_bounding_boxes[:, 0:2] = (
xywh_bounding_boxes[:, 0:2] - xywh_bounding_boxes[:, 2:4] / 2
)
tlbr_bounding_boxes[:, 2:4] = (
xywh_bounding_boxes[:, 0:2] + xywh_bounding_boxes[:, 2:4] / 2
)
return tlbr_bounding_boxes
class Encoder:
def __init__(
self,
default_box_scalings: torch.Tensor,
classes: list[str],
feature_map_size: tuple[int, int] = (10, 8),
iou_threshold: float = 0.5,
) -> None:
self.default_scalings = default_box_scalings
self.feature_map_width, self.feature_map_height = feature_map_size
self.classes = classes
self.num_classes = len(classes)
self.threshold = iou_threshold
self.default_boxes_tl_br = self._default_boxes("tlbr")
self.default_boxes_xy_wh = self._default_boxes("xywh")
def apply(
self, target_boxes: torch.Tensor, target_classes: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
if target_boxes.size(0) == 0:
num_default_boxes = self.default_boxes_xy_wh.size(0)
return (
torch.zeros((num_default_boxes, NUM_BOX_PARAMETERS)),
torch.zeros((num_default_boxes,), dtype=torch.bool),
torch.zeros(num_default_boxes, dtype=torch.long),
)
target_boxes_tl_br = xywh_to_tlbr(target_boxes)
# Select the default box with the highest IoU and with IoU higher than the threshold value
ious = intersection_over_union(target_boxes_tl_br, self.default_boxes_tl_br)
_, best_dbox_idx = ious.max(dim=1)
masked_ious = (
torch.logical_or(
(
best_dbox_idx
== torch.arange(0, self.default_boxes_tl_br.size(0)).reshape(-1, 1)
).T,
ious > self.threshold,
)
* ious
)
# Select the target box with the highest IoU for each default box
best_value, best_idx = masked_ious.max(dim=0)
is_object = best_value > 0
selected_target_boxes = target_boxes[best_idx[is_object]]
selected_default_boxes = self.default_boxes_xy_wh[is_object]
# Encode target boxes with relative offsets to default box
encoded_target_boxes = self.encode_bounding_boxes(
selected_target_boxes, selected_default_boxes, is_object
)
encoded_target_classes = torch.zeros(
self.default_boxes_xy_wh.size(0), dtype=torch.long
)
encoded_target_classes[is_object] = target_classes[
best_idx[is_object]
].flatten()
return encoded_target_boxes, is_object, encoded_target_classes
def encode_bounding_boxes(
self, selected_target_boxes, selected_default_boxes, is_object
):
encoded_target_boxes = torch.zeros(
(self.default_boxes_xy_wh.size(0), NUM_BOX_PARAMETERS)
)
selected_default_boxes = selected_default_boxes.type(torch.DoubleTensor)
encoded_target_boxes = encoded_target_boxes.type(torch.DoubleTensor)
selected_target_boxes = selected_target_boxes.type(torch.DoubleTensor)
encoded_target_boxes[is_object, 0:2] = (
selected_target_boxes[:, 0:2] - selected_default_boxes[:, 0:2]
) / selected_default_boxes[:, 2:4]
encoded_target_boxes[is_object, 2:4] = torch.log(
selected_target_boxes[:, 2:4] / selected_default_boxes[:, 2:4]
)
return encoded_target_boxes
def decode_model_output(
self, predicted_boxes: torch.Tensor, encoded_predicted_classes: torch.Tensor
) -> torch.Tensor:
"""
Decode model output using the encoder. The decoded boxes are in (cx, cy, w, h) format.
Prameters:
- predicted_boxes: raw model output with shape (batch_size, feature_map_width * feature_map_height, 4)
"""
assert predicted_boxes.dim() == 3
prediction_is_object = encoded_predicted_classes > 0
decoded_boxes = torch.zeros_like(predicted_boxes)
decoded_boxes[:, :, 0:2] = (
self.default_boxes_xy_wh[:, 2:4] * (predicted_boxes[:, :, 0:2])
+ self.default_boxes_xy_wh[:, 0:2]
)
decoded_boxes[:, :, 2:4] = self.default_boxes_xy_wh[:, 2:4] * torch.exp(
predicted_boxes[:, :, 2:4]
)
decoded_boxes = decoded_boxes.reshape((-1, NUM_BOX_PARAMETERS))
return (decoded_boxes, encoded_predicted_classes, prediction_is_object)
def _default_boxes(self, type: str):
assert type in ["xywh", "tlbr"]
default_boxes = torch.zeros(
(
self.feature_map_width,
self.feature_map_height,
self.default_scalings.size(0),
NUM_BOX_PARAMETERS,
)
)
for i, j in itertools.product(
range(self.feature_map_width), range(self.feature_map_height)
):
center = torch.tensor(
[
(i + 0.5) / self.feature_map_width,
(j + 0.5) / self.feature_map_height,
]
)
if type == "xywh":
default_boxes[i, j, :, 0:2] = center
default_boxes[i, j, :, 2:4] = self.default_scalings
else:
default_boxes[i, j, :, 0:2] = center - self.default_scalings / 2
default_boxes[i, j, :, 2:4] = center + self.default_scalings / 2
return default_boxes.reshape((-1, NUM_BOX_PARAMETERS))
def intersection_over_union(
boxes_1: torch.Tensor, boxes_2: torch.Tensor
) -> torch.Tensor:
"""
Calculation of pairwise intersection over union metric based on two boxes tensor.
Reference: https://github.com/kuangliu/pytorch-src.
Paramters:
- boxes_1 with shape (N, 4)
- boxes_2 with shape (M, 4)
output:
IoU (N, M)
"""
N = boxes_1.size(0)
M = boxes_2.size(0)
be1 = boxes_1.unsqueeze(1).expand(-1, M, -1)
be2 = boxes_2.unsqueeze(0).expand(N, -1, -1)
# Left Top & Right Bottom
top_left = torch.max(be1[:, :, :2], be2[:, :, :2])
bottom_right = torch.min(be1[:, :, 2:], be2[:, :, 2:])
delta = bottom_right - top_left
delta[delta < 0] = 0
intersect = delta[:, :, 0] * delta[:, :, 1]
delta1 = be1[:, :, 2:] - be1[:, :, :2]
area1 = delta1[:, :, 0] * delta1[:, :, 1]
delta2 = be2[:, :, 2:] - be2[:, :, :2]
area2 = delta2[:, :, 0] * delta2[:, :, 1]
iou = intersect / (area1 + area2 - intersect)
return iou
def calculate_predicted_classes(predicted_class_logits: torch.Tensor) -> torch.Tensor:
class_probabilities = F.softmax(predicted_class_logits, dim=-1)
return torch.argmax(class_probabilities, dim=-1), class_probabilities