"vscode:/vscode.git/clone" did not exist on "7ae93d70dc6be0883f5515297095c7396b9698c6"
iou.py 1.94 KB
Newer Older
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
1
2
3
# Copyright 2016-present, Facebook, Inc.
# All rights reserved.
#
Benjamin Graham's avatar
Benjamin Graham committed
4
# This source code is licensed under the BSD-style license found in the
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
5
6
7
8
# LICENSE file in the root directory of this source tree.

import torch, numpy as np

Benjamin Thomas Graham's avatar
iou  
Benjamin Thomas Graham committed
9
10
#VALID_CLASS_IDS = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 16, 24, 28, 33, 34, 36, 39])

Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
11
12
13
14
15
16
#Classes relabelled {-100,0,1,...,19}.
#Predictions will all be in the set {0,1,...,19}


CLASS_LABELS = ['wall', 'floor', 'cabinet', 'bed', 'chair', 'sofa', 'table', 'door', 'window', 'bookshelf', 'picture', 'counter', 'desk', 'curtain', 'refrigerator', 'shower curtain', 'toilet', 'sink', 'bathtub', 'otherfurniture']
UNKNOWN_ID = -100
Benjamin Thomas Graham's avatar
iou  
Benjamin Thomas Graham committed
17
N_CLASSES = len(CLASS_LABELS)
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
18
19
20
21
22
23
24
25
26
27

def confusion_matrix(pred_ids, gt_ids):
    assert pred_ids.shape == gt_ids.shape, (pred_ids.shape, gt_ids.shape)
    idxs= gt_ids>=0
    return np.bincount(pred_ids[idxs]*20+gt_ids[idxs],minlength=400).reshape((20,20)).astype(np.ulonglong)

def get_iou(label_id, confusion):
    # true positives
    tp = np.longlong(confusion[label_id, label_id])
    # false positives
Benjamin Thomas Graham's avatar
iou  
Benjamin Thomas Graham committed
28
29
30
    fp = np.longlong(confusion[label_id, :].sum()) - tp
    # false negatives
    fn = np.longlong(confusion[:, label_id].sum())
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
31
32
33
34
35
36
37
38
39
40
41

    denom = (tp + fp + fn)
    if denom == 0:
        return float('nan')
    return (float(tp) / denom, tp, denom)

def evaluate(pred_ids,gt_ids):
    print('evaluating', gt_ids.size, 'points...')
    confusion=confusion_matrix(pred_ids,gt_ids)
    class_ious = {}
    mean_iou = 0
Benjamin Thomas Graham's avatar
iou  
Benjamin Thomas Graham committed
42
    for i in range(N_CLASSES):
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
43
        label_name = CLASS_LABELS[i]
Benjamin Thomas Graham's avatar
iou  
Benjamin Thomas Graham committed
44
        class_ious[label_name] = get_iou(i, confusion)
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
45
46
47
48
        mean_iou+=class_ious[label_name][0]/20

    print('classes          IoU')
    print('----------------------------')
Benjamin Thomas Graham's avatar
iou  
Benjamin Thomas Graham committed
49
    for i in range(N_CLASSES):
Benjamin Thomas Graham's avatar
Benjamin Thomas Graham committed
50
51
52
        label_name = CLASS_LABELS[i]
        print('{0:<14s}: {1:>5.3f}   ({2:>6d}/{3:<6d})'.format(label_name, class_ious[label_name][0], class_ious[label_name][1], class_ious[label_name][2]))
    print('mean IOU', mean_iou)