test_mean_iou.py 4.2 KB
Newer Older
W
whs 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
16

W
whs 已提交
17
import numpy as np
W
wanghuancoder 已提交
18
from eager_op_test import OpTest
W
whs 已提交
19 20


21 22 23
def compute_mean_iou(
    predictions, labels, num_classes, in_wrongs, in_corrects, in_mean_ious
):
W
whs 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    assert predictions.shape == labels.shape
    predictions = predictions.flatten()
    labels = labels.flatten()

    out_wrong = np.zeros([num_classes]).astype("int32")
    for _, wrong in in_wrongs:
        out_wrong += wrong
    out_correct = np.zeros([num_classes]).astype("int32")
    for _, correct in in_corrects:
        out_correct += correct

    for pred, label in zip(predictions, labels):
        if pred == label:
            out_correct[pred] += 1
        else:
            out_wrong[pred] += 1
            out_wrong[label] += 1

    denominator = out_wrong + out_correct
    valid_count = (denominator != 0).sum()
44 45 46
    denominator = np.where(
        denominator > 0, denominator, np.ones(denominator.shape)
    )
W
whs 已提交
47 48 49 50 51 52 53 54 55 56 57
    mean_iou = (out_correct / denominator).sum() / valid_count

    for _, in_mean_iou in in_mean_ious:
        mean_iou += in_mean_iou
    return mean_iou, out_wrong, out_correct


class TestMeanIOUOp(OpTest):
    def setUp(self):
        self.config()
        self.op_type = "mean_iou"
58 59 60 61 62 63
        predictions = np.random.randint(
            0, self.num_classes, self.image_size
        ).astype("int32")
        labels = np.random.randint(0, self.num_classes, self.image_size).astype(
            "int32"
        )
W
whs 已提交
64 65 66

        in_wrongs = []
        for i in range(self.in_wrong_num):
67
            in_wrongs.append(
68 69 70 71 72 73 74
                (
                    "in_wrong_%d" % i,
                    np.random.randint(0, 10, [self.num_classes]).astype(
                        "int32"
                    ),
                )
            )
W
whs 已提交
75 76 77

        in_corrects = []
        for i in range(self.in_correct_num):
78
            in_corrects.append(
79 80 81 82 83 84 85
                (
                    "in_correct_%d" % i,
                    np.random.randint(0, 10, [self.num_classes]).astype(
                        "int32"
                    ),
                )
            )
W
whs 已提交
86 87 88

        in_mean_ious = []
        for i in range(self.in_mean_iou_num):
89 90 91 92 93 94
            in_mean_ious.append(
                (
                    "in_mean_iou_%d" % i,
                    np.random.uniform(0, 1, [1]).astype("float32"),
                )
            )
W
whs 已提交
95 96 97 98 99 100

        self.inputs = {
            'Predictions': predictions,
            'Labels': labels,
            'InWrongs': in_wrongs,
            'InCorrects': in_corrects,
101
            'InMeanIou': in_mean_ious,
W
whs 已提交
102
        }
103
        self.attrs = {'num_classes': int(self.num_classes)}
W
whs 已提交
104
        mean_iou, out_wrong, out_correct = compute_mean_iou(
105 106 107 108 109 110 111
            predictions,
            labels,
            self.num_classes,
            in_wrongs,
            in_corrects,
            in_mean_ious,
        )
W
whs 已提交
112 113 114
        self.outputs = {
            'OutMeanIou': mean_iou,
            'OutWrong': out_wrong,
115
            'OutCorrect': out_correct,
W
whs 已提交
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
        }

    def config(self):
        self.num_classes = 10
        self.image_size = [128, 128]
        self.in_wrong_num = 0
        self.in_correct_num = 0
        self.in_mean_iou_num = 0

    def test_check_output(self):
        self.check_output()


class TestCase1(TestMeanIOUOp):
    def config(self):
        self.num_classes = 5
        self.image_size = [100, 128]
        self.in_wrong_num = 2
        self.in_correct_num = 2
        self.in_mean_iou_num = 2

137 138 139
    # NOTE(dev): Skip check_dygraph becuase Python API doesn't expose
    # in_wrong_num/in_correct_num/in_mean_iou_num argument
    def test_check_output(self):
W
wanghuancoder 已提交
140
        self.check_output(check_dygraph=False)
141

W
whs 已提交
142 143 144

if __name__ == '__main__':
    unittest.main()