-
Notifications
You must be signed in to change notification settings - Fork 37
/
utils.py
253 lines (211 loc) · 7.3 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import numpy as np
import re
import functools
import torch as th
import cv2
from numba import jit
def graph2line(junctions, adj_mtx, threshold=0.5):
assert len(junctions) == len(adj_mtx)
# assert np.allclose(adj_mtx, adj_mtx.transpose((0, 2, 1)), rtol=1e-2, atol=1e-2), f"{adj_mtx}"
bs = len(junctions)
lines = []
scores = []
for b in range(bs):
junc = junctions[b]
mtx = adj_mtx[b]
num_junc = np.sum(junc.sum(axis=1) > 0)
line = []
score = []
for i in range(num_junc):
for j in range(i, num_junc):
if mtx[i, j] > threshold:
line.append(np.hstack((junc[i], junc[j])))
score.append(mtx[i, j])
scores.append(np.array(score))
lines.append(np.array(line))
return lines, scores
def draw_lines(imgs, lines, scores=None, width=2):
assert len(imgs) == len(lines)
imgs = np.uint8(imgs)
bs = len(imgs)
if scores is not None:
assert len(scores) == bs
res = []
for b in range(bs):
img = imgs[b].transpose((1, 2, 0))
line = lines[b]
if scores is None:
score = np.zeros(len(line))
else:
score = scores[b]
img = img.copy()
for (x1, y1, x2, y2), c in zip(line, score):
pt1, pt2 = (x1, y1), (x2, y2)
c = tuple(cv2.applyColorMap(np.array(c * 255, dtype=np.uint8), cv2.COLORMAP_JET).flatten().tolist())
img = cv2.line(img, pt1, pt2, c, width)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
res.append(th.from_numpy(img.transpose((2, 0, 1))))
return res
def draw_jucntions(hms, junctions):
assert len(hms) == len(junctions)
if hms.ndim == 3:
imgs = np.uint8(hms * 255)
else:
imgs = np.uint8(hms)
bs = len(imgs)
res = []
for b in range(bs):
if hms.ndim == 3:
img = cv2.cvtColor(imgs[b], cv2.COLOR_GRAY2BGR)
else:
img = np.array(imgs[b].transpose((1, 2, 0)))
junc = junctions[b]
junc = junc[junc.sum(axis=1) > 0.1]
if hms.ndim == 3:
score = hms[b][np.int32(junc[:, 1]), np.int32(junc[:, 0])]
else:
score = [1.] * len(junc)
img = img.copy()
for (x, y), c in zip(junc, score):
c = tuple(cv2.applyColorMap(np.array(c * 255, dtype=np.uint8), cv2.COLORMAP_JET).flatten().tolist())
cv2.circle(img, (x, y), 5, c, thickness=2)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
res.append(th.from_numpy(img.transpose((2, 0, 1))))
return res
class AverageMeter(object):
"""Computes and stores the average and current value"""
def __init__(self):
self.initialized = False
self.val = None
self.avg = None
self.sum = None
self.count = None
def initialize(self, val, weight):
self.val = val
self.avg = val
self.sum = val * weight
self.count = weight
self.initialized = True
def update(self, val, weight=1):
if not self.initialized:
self.initialize(val, weight)
else:
self.add(val, weight)
def add(self, val, weight):
self.val = val
self.sum += val * weight
self.count += weight
self.avg = self.sum / self.count
def value(self):
return self.val
def average(self):
return self.avg
def unique(ar, return_index=False, return_inverse=False, return_counts=False):
ar = np.asanyarray(ar).flatten()
optional_indices = return_index or return_inverse
optional_returns = optional_indices or return_counts
if ar.size == 0:
if not optional_returns:
ret = ar
else:
ret = (ar,)
if return_index:
ret += (np.empty(0, np.bool),)
if return_inverse:
ret += (np.empty(0, np.bool),)
if return_counts:
ret += (np.empty(0, np.intp),)
return ret
if optional_indices:
perm = ar.argsort(kind='mergesort' if return_index else 'quicksort')
aux = ar[perm]
else:
ar.sort()
aux = ar
flag = np.concatenate(([True], aux[1:] != aux[:-1]))
if not optional_returns:
ret = aux[flag]
else:
ret = (aux[flag],)
if return_index:
ret += (perm[flag],)
if return_inverse:
iflag = np.cumsum(flag) - 1
inv_idx = np.empty(ar.shape, dtype=np.intp)
inv_idx[perm] = iflag
ret += (inv_idx,)
if return_counts:
idx = np.concatenate(np.nonzero(flag) + ([ar.size],))
ret += (np.diff(idx),)
return ret
def colorEncode(labelmap, colors, mode='BGR'):
labelmap = labelmap.astype('int')
labelmap_rgb = np.zeros((labelmap.shape[0], labelmap.shape[1], 3),
dtype=np.uint8)
for label in unique(labelmap):
if label < 0:
continue
labelmap_rgb += (labelmap == label)[:, :, np.newaxis] * \
np.tile(colors[label],
(labelmap.shape[0], labelmap.shape[1], 1))
if mode == 'BGR':
return labelmap_rgb[:, :, ::-1]
else:
return labelmap_rgb
def accuracy(preds, label):
valid = (label >= 0)
acc_sum = (valid * (preds == label)).sum()
valid_sum = valid.sum()
acc = float(acc_sum) / (valid_sum + 1e-10)
return acc, valid_sum
def intersectionAndUnion(imPred, imLab, numClass):
imPred = np.asarray(imPred).copy()
imLab = np.asarray(imLab).copy()
imPred += 1
imLab += 1
# Remove classes from unlabeled pixels in gt image.
# We should not penalize detections in unlabeled portions of the image.
imPred = imPred * (imLab > 0)
# Compute area intersection:
intersection = imPred * (imPred == imLab)
(area_intersection, _) = np.histogram(
intersection, bins=numClass, range=(1, numClass))
# Compute area union:
(area_pred, _) = np.histogram(imPred, bins=numClass, range=(1, numClass))
(area_lab, _) = np.histogram(imLab, bins=numClass, range=(1, numClass))
area_union = area_pred + area_lab - area_intersection
return (area_intersection, area_union)
class NotSupportedCliException(Exception):
pass
def process_range(xpu, inp):
start, end = map(int, inp)
if start > end:
end, start = start, end
return map(lambda x: '{}{}'.format(xpu, x), range(start, end + 1))
REGEX = [
(re.compile(r'^gpu(\d+)$'), lambda x: ['gpu%s' % x[0]]),
(re.compile(r'^(\d+)$'), lambda x: ['gpu%s' % x[0]]),
(re.compile(r'^gpu(\d+)-(?:gpu)?(\d+)$'),
functools.partial(process_range, 'gpu')),
(re.compile(r'^(\d+)-(\d+)$'),
functools.partial(process_range, 'gpu')),
]
def parse_devices(input_devices):
"""Parse user's devices input str to standard format.
e.g. [gpu0, gpu1, ...]
"""
ret = []
for d in input_devices.split(','):
for regex, func in REGEX:
m = regex.match(d.lower().strip())
if m:
tmp = func(m.groups())
# prevent duplicate
for x in tmp:
if x not in ret:
ret.append(x)
break
else:
raise NotSupportedCliException(
'Can not recognize device: "%s"' % d)
return ret