forked from merlresearch/InSeGAN-ICCV2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_iou.py
48 lines (40 loc) · 1.49 KB
/
compute_iou.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
#!/usr/bin/env python3
# Copyright (c) 2021,2022 Mitsubishi Electric Research Laboratories (MERL)
#
# SPDX-License-Identifier: AGPL-3.0-or-later
import torch
def compute_iou(X, Y):
"""
computes the best segmentation alignment between the predictions and the GT,
and uses the best match to compute the IOU.
"""
def compute_inst_tensor(insts):
uq = torch.unique(insts)
inst_map = []
for t in range(len(uq)):
if uq[t] == 0:
continue
inst_map.append((insts == uq[t]).unsqueeze(0))
inst_map = torch.cat(inst_map, dim=0)
return inst_map
miou = 0.
num_test = len(X)
for t in range(num_test):
im1 = X[t][0]
im2 = Y[t][0]
im2[im1 == 0] = 0 # remove points that are not in the ground truth, as depth will be -1 for those.
try:
gt = compute_inst_tensor(im1)
pred = compute_inst_tensor(im2)
except:
print('error in compute_iou! ')
continue
# compute a matrix of best IoUs for every predicted instance against every gt instance
# and use the best match, and average the IoU scores on all instances and all images.
iou = torch.zeros(len(gt), len(pred))
for i in range(len(gt)):
for j in range(len(pred)):
iou[i,j] = ((gt[i]*pred[j])>0).sum().float()/(((gt[i]+pred[j])>0).sum().float() + 1e-5)
miou += iou.max(dim=1)[0].mean()
miou /= num_test
return miou