forked from ygCoconut/mAP_3Dvolume
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvol3d_util_custom.py
404 lines (342 loc) · 14.9 KB
/
vol3d_util_custom.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
import sys
import numpy as np
import h5py
####
# list of utility functions
# 0. I/O util
# 1. binary pred -> instance seg
# 2. instance seg + pred heatmap -> instance score
# 3. instance seg -> bbox
# 4. instance seg + gt seg + instance score -> sorted match result
# 0. I/O
def seg2im(seg): # seg -> 3-channel image
if seg.max()>255:
return np.stack([seg//65536, seg//256, seg%256],axis=2).astype(np.uint8)
else:
return seg.astype(np.uint8)
def im2seg(im): # image -> seg
if im.ndim==2:
return im
else:
return im[:,:,0].astype(np.uint32)*65536+im[:,:,1].astype(np.uint32)*256+im[:,:,2].astype(np.uint32)
def heatmap_by_channel(im, channel=-1): # image to heatmap
if channel != -1:
heatmap = im[channel]
else:
heatmap = im.mean(axis=0)
return heatmap
def readh5(path, vol=''):
# do the first key
fid = h5py.File(path, 'r')
if vol == '':
if sys.version[0] == '3':
vol = list(fid)[0]
else: # python 2
vol = fid.keys()[0]
return np.array(fid[vol]).squeeze()
# 1. binary pred -> instance seg
def seg_bbox2d(seg,do_count=False, uid=None):
sz = seg.shape
assert len(sz) == 2
if uid is None:
uid = np.unique(seg)
uid = uid[uid > 0]
um = uid.max()
out = np.zeros((1+int(um), 5+do_count), dtype=np.uint32)
out[:, 0] = np.arange(out.shape[0])
out[:, 1] = sz[0]
out[:, 3] = sz[1]
# for each row
rids = np.where((seg > 0).sum(axis=1) > 0)[0]
for rid in rids:
sid = np.unique(seg[rid])
sid = sid[(sid > 0)*(sid <= um)]
out[sid, 1] = np.minimum(out[sid, 1], rid)
out[sid, 2] = np.maximum(out[sid, 2], rid)
cids = np.where((seg > 0).sum(axis=0) > 0)[0]
for cid in cids:
sid = np.unique(seg[:, cid])
sid = sid[(sid > 0)*(sid <= um)]
out[sid, 3] = np.minimum(out[sid, 3], cid)
out[sid, 4] = np.maximum(out[sid, 4], cid)
if do_count:
ui, uc = np.unique(seg, return_counts=True)
out[ui, -1] = uc
return out[uid]
def getSegType(mid):
m_type = np.uint64
if mid < 2**8:
m_type = np.uint8
elif mid < 2**16:
m_type = np.uint16
elif mid < 2**32:
m_type = np.uint32
return m_type
def readh5_handle(path, vol=''):
# do the first key
fid = h5py.File(path, 'r')
if vol == '':
if sys.version[0] == '3':
vol = list(fid)[0]
else: # python 2
vol = fid.keys()[0]
return fid[vol]
def getQueryCount(ui, uc, qid):
# memory efficient
ui_r = [ui[ui > 0].min(), max(ui.max(), qid.max())]
rl = np.zeros(1 + int(ui_r[1] - ui_r[0]), uc.dtype)
rl[ui[ui > 0] - ui_r[0]] = uc[ui > 0]
cc = np.zeros(qid.shape, uc.dtype)
gid = np.logical_and(qid >= ui_r[0], qid <= ui_r[1])
cc[gid] = rl[qid[gid] - ui_r[0]]
return cc
def unique_chunk(seg, slices, chunk_size=50, do_count=True):
# load unique segment ids and segment sizes (in voxels) chunk by chunk
num_z = slices[1] - slices[0]
num_chunk = (num_z + chunk_size - 1) // chunk_size
uc_arr = None
ui = []
for cid in range(num_chunk):
# compute max index, modulo takes care of slices[1] = -1
max_idx = min([(cid + 1) * chunk_size + slices[0], slices[1]])
chunk = np.array(seg[cid * chunk_size + slices[0]: max_idx])
if do_count:
ui_c, uc_c = np.unique(chunk, return_counts=True)
if uc_arr is None:
uc_arr = np.zeros(ui_c.max() + 1, int)
uc_arr[ui_c] = uc_c
uc_len = len(uc_arr)
else:
if uc_len <= ui_c.max():
# at least double the length
uc_arr = np.hstack([uc_arr, np.zeros(max(ui_c.max() - uc_len, uc_len) + 1,
int)]) # max + 1 for edge case (uc_len = ui_c.max())
uc_len = len(uc_arr)
uc_arr[ui_c] += uc_c
else:
ui = np.unique(np.hstack([ui, np.unique(chunk)]))
if do_count:
ui = np.where(uc_arr > 0)[0]
return ui, uc_arr[ui]
else:
return ui
def unique_chunks_bbox(seg1, seg2, seg2_val, bbox, chunk_size=50, do_count=True):
# load unique segment ids and segment sizes (in voxels) chunk by chunk
num_z = bbox[1] - bbox[0]
num_chunk = (num_z + chunk_size - 1) // chunk_size
uc_arr = None
ui = []
for cid in range(num_chunk):
# compute max index, modulo takes care of slices[1] = -1
max_idx = min([(cid + 1) * chunk_size + bbox[0], bbox[1]])
chunk = np.array(seg1[cid * chunk_size + bbox[0]:max_idx, bbox[2]:bbox[3], bbox[4]:bbox[5]])
chunk = chunk * (
np.array(seg2[cid * chunk_size + bbox[0]:max_idx, bbox[2]:bbox[3], bbox[4]:bbox[5]]) == seg2_val)
if do_count:
ui_c, uc_c = np.unique(chunk, return_counts=True)
if uc_arr is None:
uc_arr = np.zeros(ui_c.max() + 1, int)
uc_arr[ui_c] = uc_c
uc_len = len(uc_arr)
else:
if uc_len <= ui_c.max():
# at least double the length
uc_arr = np.hstack([uc_arr, np.zeros(max(ui_c.max() - uc_len, uc_len) + 1,
int)]) # max + 1 for edge case (uc_len = ui_c.max())
uc_len = len(uc_arr)
uc_arr[ui_c] += uc_c
else:
ui = np.unique(np.hstack([ui, np.unique(chunk)]))
if do_count:
ui = np.where(uc_arr > 0)[0]
return ui, uc_arr[ui]
else:
return ui
# 3. instance seg -> bbox
def seg_bbox3d(seg, slices, uid=None, chunk_size=50):
"""returns bounding box of segments"""
sz = seg.shape
assert len(sz) == 3
uic = None
if uid is None:
uid, uic = unique_chunk(seg, slices, chunk_size)
uic = uic[uid > 0]
uid = uid[uid > 0]
um = int(uid.max())
out = np.zeros((1 + um, 7), dtype=np.uint32)
out[:, 0] = np.arange(out.shape[0])
out[:, 1], out[:, 3], out[:, 5] = sz[0], sz[1], sz[2]
num_z = slices[1] - slices[0]
num_chunk = (num_z + chunk_size - 1) // chunk_size
for chunk_id in range(num_chunk):
z0 = chunk_id * chunk_size + slices[0]
# compute max index, modulo takes care of slices[1] = -1
max_idx = min([z0 + chunk_size, slices[1]])
seg_c = np.array(seg[z0: max_idx])
# for each slice
for zid in np.where((seg_c > 0).sum(axis=1).sum(axis=1) > 0)[0]:
sid = np.unique(seg_c[zid])
sid = sid[(sid > 0) * (sid <= um)]
out[sid, 1] = np.minimum(out[sid, 1], z0 + zid)
out[sid, 2] = np.maximum(out[sid, 2], z0 + zid)
# for each row
for rid in np.where((seg_c > 0).sum(axis=0).sum(axis=1) > 0)[0]:
sid = np.unique(seg_c[:, rid])
sid = sid[(sid > 0) * (sid <= um)]
out[sid, 3] = np.minimum(out[sid, 3], rid)
out[sid, 4] = np.maximum(out[sid, 4], rid)
# for each col
for cid in np.where((seg_c > 0).sum(axis=0).sum(axis=0) > 0)[0]:
sid = np.unique(seg_c[:, :, cid])
sid = sid[(sid > 0) * (sid <= um)]
out[sid, 5] = np.minimum(out[sid, 5], cid)
out[sid, 6] = np.maximum(out[sid, 6], cid)
# max + 1
out[:, 2::2] += 1
return out[uid]
def seg_iou3d(pred, gt, slices, areaRng=np.array([]), todo_id=None, chunk_size=100, crumb_size=-1):
# returns the matching pairs of ground truth IDs and prediction IDs, as well as the IoU of each pair.
# (pred,gt)
# return: id_1,id_2,size_1,size_2,iou
pred_id, pred_sz = unique_chunk(pred, slices, chunk_size)
if todo_id.max() > pred_id.max():
raise ValueError(
'The predict-score has bigger id (%d) than the prediction (%d)' % (todo_id.max(), pred_id.max()))
pred_sz = pred_sz[pred_id > 0]
pred_id = pred_id[pred_id > 0]
predict_sz_rl = np.zeros(int(pred_id.max()) + 1, int)
predict_sz_rl[pred_id] = pred_sz
gt_id, gt_sz = unique_chunk(gt, slices, chunk_size)
gt_sz = gt_sz[gt_id > 0]
gt_id = gt_id[gt_id > 0]
rl_gt = None
if crumb_size > -1:
gt_id = gt_id[gt_sz >= crumb_size]
gt_sz = gt_sz[gt_sz >= crumb_size]
if todo_id is None:
todo_id = pred_id
todo_sz = pred_sz
else:
todo_sz = predict_sz_rl[todo_id]
bbs = seg_bbox3d(pred, slices, uid=todo_id, chunk_size=chunk_size)[:, 1:]
result_p = np.zeros((len(todo_id), 2 + 3 * areaRng.shape[0]), float)
result_p[:, 0] = todo_id
result_p[:, 1] = todo_sz
gt_matched_id = np.zeros(1 + gt_id.max(), int)
gt_matched_iou = np.zeros(1 + gt_id.max(), float)
for j, i in enumerate(todo_id):
# Find intersection of pred and gt instance inside bbox, call intersection match_id
bb = bbs[j]
# can be big memory
# match_id, match_sz=np.unique(np.array(gt[bb[0]:bb[1], bb[2]:bb[3], bb[4]:bb[5]])*(np.array(pred[bb[0]:bb[1],bb[2]:bb[3], bb[4]:bb[5]])==i),return_counts=True)
match_id, match_sz = unique_chunks_bbox(gt, pred, i, bb, chunk_size)
match_id_g = np.isin(match_id, gt_id)
match_sz = match_sz[match_id_g] # get intersection counts
match_id = match_id[match_id_g] # get intersection ids
if len(match_id) > 0:
# get count of all preds inside bbox (assume gt_id,match_id are of ascending order)
gt_sz_match = getQueryCount(gt_id, gt_sz, match_id)
ious = match_sz.astype(float) / (
todo_sz[j] + gt_sz_match - match_sz) # all possible iou combinations of bbox ids are contained
for r in range(areaRng.shape[0]): # fill up all, then s, m, l
gid = (gt_sz_match > areaRng[r, 0]) * (gt_sz_match <= areaRng[r, 1])
if sum(gid) > 0:
idx_iou_max = np.argmax(ious * gid)
result_p[j, 2 + r * 3:2 + r * 3 + 3] = [match_id[idx_iou_max], gt_sz_match[idx_iou_max],
ious[idx_iou_max]]
# update set2
gt_todo = gt_matched_iou[match_id] < ious
gt_matched_iou[match_id[gt_todo]] = ious[gt_todo]
gt_matched_id[match_id[gt_todo]] = i
# get the rest: false negative + dup
fn_gid = gt_id[np.isin(gt_id, result_p[:, 2], assume_unique=False, invert=True)]
fn_gic = gt_sz[np.isin(gt_id, fn_gid)]
fn_iou = gt_matched_iou[fn_gid]
fn_pid = gt_matched_id[fn_gid]
fn_pic = predict_sz_rl[fn_pid]
# add back duplicate
# instead of bookkeeping in the previous step, faster to redo them
result_fn = np.vstack([fn_pid, fn_pic, fn_gid, fn_gic, fn_iou]).T
return result_p, result_fn
def seg_iou3d_sorted(pred, gt, score, slices, areaRng=[0, 1e10], chunk_size=250, crumb_size=-1):
# pred_score: Nx2 [id, score]
# 1. sort prediction by confidence score
relabel = np.zeros(int(np.max(score[:, 0]) + 1), float)
relabel[score[:, 0].astype(int)] = score[:, 1]
# 1. sort the prediction by confidence
pred_id = np.unique(score[:, 0])
pred_id = pred_id[pred_id > 0]
pred_id_sorted = np.argsort(-relabel[pred_id])
result_p, result_fn = seg_iou3d(pred, gt, slices, areaRng, pred_id[pred_id_sorted], chunk_size, crumb_size)
# format: pid,pc,p_score, gid,gc,iou
pred_score_sorted = relabel[pred_id_sorted].reshape(-1, 1)
return result_p, result_fn, pred_score_sorted
def seg_iou2d(pred, gt, areaRng=np.array([]), todo_id=None):
# returns the matching pairs of ground truth IDs and prediction IDs, as well as the IoU of each pair.
# (pred,gt)
# return: id_1,id_2,size_1,size_2,iou
pred_id, pred_sz = np.unique(pred, return_counts=True)
pred_sz = pred_sz[pred_id > 0]
pred_id = pred_id[pred_id > 0]
predict_sz_rl = np.zeros(int(pred_id.max())+1, int)
predict_sz_rl[pred_id] = pred_sz
gt_id, gt_sz = np.unique(gt, return_counts=True)
gt_sz = gt_sz[gt_id > 0]; gt_id = gt_id[gt_id > 0]
if todo_id is None:
todo_id = pred_id
todo_sz = pred_sz
else:
todo_sz = predict_sz_rl[todo_id]
#print('\t compute bounding boxes')
bbs = seg_bbox2d(pred, uid=todo_id)[:, 1:]
result_p = np.zeros((len(todo_id), 2+3*areaRng.shape[0]), float)
result_p[:, 0] = todo_id
result_p[:, 1] = todo_sz
gt_matched_id = np.zeros(1+gt_id.max(), int)
gt_matched_iou = np.zeros(1+gt_id.max(), float)
#print('\t compute iou matching')
for j, i in enumerate(todo_id):
# Find intersection of pred and gt instance inside bbox, call intersection match_id
bb = bbs[j]
match_id, match_sz = np.unique(gt[bb[0]:bb[1]+1, bb[2]:bb[3]+1]*(pred[bb[0]:bb[1]+1, bb[2]:bb[3]+1] == i), return_counts=True)
match_sz = match_sz[match_id > 0] # get intersection counts
match_id = match_id[match_id > 0] # get intersection ids
if len(match_id) > 0:
# get count of all preds inside bbox (assume gt_id,match_id are of ascending order)
gt_sz_match = gt_sz[np.isin(gt_id, match_id)]
ious = match_sz.astype(float)/(todo_sz[j] + gt_sz_match - match_sz) #all possible iou combinations of bbox ids are contained
for r in range(areaRng.shape[0]): # fill up all, then s, m, l
gid = (gt_sz_match > areaRng[r, 0])*(gt_sz_match <= areaRng[r, 1])
if sum(gid) > 0:
idx_iou_max = np.argmax(ious*gid)
result_p[j, 2+r*3:2+r*3+3] = [match_id[idx_iou_max], gt_sz_match[idx_iou_max], ious[idx_iou_max]]
# update set2
gt_todo = gt_matched_iou[match_id] < ious
gt_matched_iou[match_id[gt_todo]] = ious[gt_todo]
gt_matched_id[match_id[gt_todo]] = i
# get the rest: false negative + dup
fn_gid = gt_id[np.isin(gt_id, result_p[:, 2], assume_unique=False, invert=True)]
fn_gic = gt_sz[np.isin(gt_id, fn_gid)]
fn_iou = gt_matched_iou[fn_gid]
fn_pid = gt_matched_id[fn_gid]
fn_pic = predict_sz_rl[fn_pid]
# add back duplicate
# instead of bookkeeping in the previous step, faster to redo them
result_fn = np.vstack([fn_pid, fn_pic, fn_gid, fn_gic, fn_iou]).T
return result_p, result_fn
def seg_iou2d_sorted(pred, gt, score, areaRng=[0, 1e10]):
# pred_score: Nx2 [id, score]
# 1. sort prediction by confidence score
try:
relabel = np.zeros(int(np.max(score[:, 0])+1), float)
except:
print("\n\nMake sure your data has no error !\n\n")
relabel[score[:,0].astype(int)] = score[:, 1]
# 1. sort the prediction by confidence
pred_id = np.unique(pred)
pred_id = pred_id[pred_id > 0]
pred_id_sorted = np.argsort(-relabel[pred_id])
result_p, result_fn = seg_iou2d(pred, gt, areaRng, todo_id=pred_id[pred_id_sorted])
# format: pid,pc,p_score, gid,gc,iou
pred_score_sorted = relabel[pred_id_sorted].reshape(-1, 1)
return result_p, result_fn, pred_score_sorted