-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.py
231 lines (183 loc) · 7.28 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
import torch
import json
import os
import config
import matplotlib.patches as patches
import torchvision.transforms as T
from PIL import ImageDraw, ImageFont
from matplotlib import pyplot as plt
def get_iou(p, a):
p_tl, p_br = bbox_to_coords(p) # (batch, S, S, B, 2)
a_tl, a_br = bbox_to_coords(a)
# Largest top-left corner and smallest bottom-right corner give the intersection
coords_join_size = (-1, -1, -1, config.B, config.B, 2)
tl = torch.max(
p_tl.unsqueeze(4).expand(coords_join_size), # (batch, S, S, B, 1, 2) -> (batch, S, S, B, B, 2)
a_tl.unsqueeze(3).expand(coords_join_size) # (batch, S, S, 1, B, 2) -> (batch, S, S, B, B, 2)
)
br = torch.min(
p_br.unsqueeze(4).expand(coords_join_size),
a_br.unsqueeze(3).expand(coords_join_size)
)
intersection_sides = torch.clamp(br - tl, min=0.0)
intersection = intersection_sides[..., 0] \
* intersection_sides[..., 1] # (batch, S, S, B, B)
p_area = bbox_attr(p, 2) * bbox_attr(p, 3) # (batch, S, S, B)
p_area = p_area.unsqueeze(4).expand_as(intersection) # (batch, S, S, B, 1) -> (batch, S, S, B, B)
a_area = bbox_attr(a, 2) * bbox_attr(a, 3) # (batch, S, S, B)
a_area = a_area.unsqueeze(3).expand_as(intersection) # (batch, S, S, 1, B) -> (batch, S, S, B, B)
union = p_area + a_area - intersection
# Catch division-by-zero
zero_unions = (union == 0.0)
union[zero_unions] = config.EPSILON
intersection[zero_unions] = 0.0
return intersection / union
def bbox_to_coords(t):
"""Changes format of bounding boxes from [x, y, width, height] to ([x1, y1], [x2, y2])."""
width = bbox_attr(t, 2)
x = bbox_attr(t, 0)
x1 = x - width / 2.0
x2 = x + width / 2.0
height = bbox_attr(t, 3)
y = bbox_attr(t, 1)
y1 = y - height / 2.0
y2 = y + height / 2.0
return torch.stack((x1, y1), dim=4), torch.stack((x2, y2), dim=4)
def scheduler_lambda(epoch):
if epoch < config.WARMUP_EPOCHS + 75:
return 1
elif epoch < config.WARMUP_EPOCHS + 105:
return 0.1
else:
return 0.01
def load_class_dict():
if os.path.exists(config.CLASSES_PATH):
with open(config.CLASSES_PATH, 'r') as file:
return json.load(file)
new_dict = {}
save_class_dict(new_dict)
return new_dict
def load_class_array():
classes = load_class_dict()
result = [None for _ in range(len(classes))]
for c, i in classes.items():
result[i] = c
return result
def save_class_dict(obj):
folder = os.path.dirname(config.CLASSES_PATH)
if not os.path.exists(folder):
os.makedirs(folder)
with open(config.CLASSES_PATH, 'w') as file:
json.dump(obj, file, indent=2)
def get_dimensions(label):
size = label['annotation']['size']
return int(size['width']), int(size['height'])
def get_bounding_boxes(label):
width, height = get_dimensions(label)
x_scale = config.IMAGE_SIZE[0] / width
y_scale = config.IMAGE_SIZE[1] / height
boxes = []
objects = label['annotation']['object']
for obj in objects:
box = obj['bndbox']
coords = (
int(int(box['xmin']) * x_scale),
int(int(box['xmax']) * x_scale),
int(int(box['ymin']) * y_scale),
int(int(box['ymax']) * y_scale)
)
name = obj['name']
boxes.append((name, coords))
return boxes
def bbox_attr(data, i):
"""Returns the Ith attribute of each bounding box in data."""
attr_start = config.C + i
return data[..., attr_start::5]
def scale_bbox_coord(coord, center, scale):
return ((coord - center) * scale) + center
def get_overlap(a, b):
"""Returns proportion overlap between two boxes in the form (tl, width, height, confidence, class)."""
a_tl, a_width, a_height, _, _ = a
b_tl, b_width, b_height, _, _ = b
i_tl = (
max(a_tl[0], b_tl[0]),
max(a_tl[1], b_tl[1])
)
i_br = (
min(a_tl[0] + a_width, b_tl[0] + b_width),
min(a_tl[1] + a_height, b_tl[1] + b_height),
)
intersection = max(0, i_br[0] - i_tl[0]) \
* max(0, i_br[1] - i_tl[1])
a_area = a_width * a_height
b_area = b_width * b_height
a_intersection = b_intersection = intersection
if a_area == 0:
a_intersection = 0
a_area = config.EPSILON
if b_area == 0:
b_intersection = 0
b_area = config.EPSILON
return torch.max(
a_intersection / a_area,
b_intersection / b_area
).item()
def plot_boxes(data, labels, classes, color='orange', min_confidence=0.2, max_overlap=0.5, file=None):
"""Plots bounding boxes on the given image."""
grid_size_x = data.size(dim=2) / config.S
grid_size_y = data.size(dim=1) / config.S
m = labels.size(dim=0)
n = labels.size(dim=1)
bboxes = []
for i in range(m):
for j in range(n):
for k in range((labels.size(dim=2) - config.C) // 5):
bbox_start = 5 * k + config.C
bbox_end = 5 * (k + 1) + config.C
bbox = labels[i, j, bbox_start:bbox_end]
class_index = torch.argmax(labels[i, j, :config.C]).item()
confidence = labels[i, j, class_index].item() * bbox[4].item() # pr(c) * IOU
if confidence > min_confidence:
width = bbox[2] * config.IMAGE_SIZE[0]
height = bbox[3] * config.IMAGE_SIZE[1]
tl = (
bbox[0] * config.IMAGE_SIZE[0] + j * grid_size_x - width / 2,
bbox[1] * config.IMAGE_SIZE[1] + i * grid_size_y - height / 2
)
bboxes.append([tl, width, height, confidence, class_index])
# Sort by highest to lowest confidence
bboxes = sorted(bboxes, key=lambda x: x[3], reverse=True)
# Calculate IOUs between each pair of boxes
num_boxes = len(bboxes)
iou = [[0 for _ in range(num_boxes)] for _ in range(num_boxes)]
for i in range(num_boxes):
for j in range(num_boxes):
iou[i][j] = get_overlap(bboxes[i], bboxes[j])
# Non-maximum suppression and render image
image = T.ToPILImage()(data)
draw = ImageDraw.Draw(image)
discarded = set()
for i in range(num_boxes):
if i not in discarded:
tl, width, height, confidence, class_index = bboxes[i]
# Decrease confidence of other conflicting bboxes
for j in range(num_boxes):
other_class = bboxes[j][4]
if j != i and other_class == class_index and iou[i][j] > max_overlap:
discarded.add(j)
# Annotate image
draw.rectangle((tl, (tl[0] + width, tl[1] + height)), outline='orange')
text_pos = (max(0, tl[0]), max(0, tl[1] - 11))
text = f'{classes[class_index]} {round(confidence * 100, 1)}%'
text_bbox = draw.textbbox(text_pos, text)
draw.rectangle(text_bbox, fill='orange')
draw.text(text_pos, text)
if file is None:
image.show()
else:
output_dir = os.path.dirname(file)
if not os.path.exists(output_dir):
os.makedirs(output_dir)
if not file.endswith('.png'):
file += '.png'
image.save(file)