File size: 6,042 Bytes
9a393e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2017 The TensorFlow 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.
# ==============================================================================
"""Tests for object_detection.metrics."""

import numpy as np
import tensorflow as tf

from object_detection.utils import metrics


class MetricsTest(tf.test.TestCase):

  def test_compute_cor_loc(self):
    num_gt_imgs_per_class = np.array([100, 1, 5, 1, 1], dtype=int)
    num_images_correctly_detected_per_class = np.array(
        [10, 0, 1, 0, 0], dtype=int)
    corloc = metrics.compute_cor_loc(num_gt_imgs_per_class,
                                     num_images_correctly_detected_per_class)
    expected_corloc = np.array([0.1, 0, 0.2, 0, 0], dtype=float)
    self.assertTrue(np.allclose(corloc, expected_corloc))

  def test_compute_cor_loc_nans(self):
    num_gt_imgs_per_class = np.array([100, 0, 0, 1, 1], dtype=int)
    num_images_correctly_detected_per_class = np.array(
        [10, 0, 1, 0, 0], dtype=int)
    corloc = metrics.compute_cor_loc(num_gt_imgs_per_class,
                                     num_images_correctly_detected_per_class)
    expected_corloc = np.array([0.1, np.nan, np.nan, 0, 0], dtype=float)
    self.assertAllClose(corloc, expected_corloc)

  def test_compute_precision_recall(self):
    num_gt = 10
    scores = np.array([0.4, 0.3, 0.6, 0.2, 0.7, 0.1], dtype=float)
    labels = np.array([0, 1, 1, 0, 0, 1], dtype=bool)
    labels_float_type = np.array([0, 1, 1, 0, 0, 1], dtype=float)
    accumulated_tp_count = np.array([0, 1, 1, 2, 2, 3], dtype=float)
    expected_precision = accumulated_tp_count / np.array([1, 2, 3, 4, 5, 6])
    expected_recall = accumulated_tp_count / num_gt

    precision, recall = metrics.compute_precision_recall(scores, labels, num_gt)
    precision_float_type, recall_float_type = metrics.compute_precision_recall(
        scores, labels_float_type, num_gt)

    self.assertAllClose(precision, expected_precision)
    self.assertAllClose(recall, expected_recall)
    self.assertAllClose(precision_float_type, expected_precision)
    self.assertAllClose(recall_float_type, expected_recall)

  def test_compute_precision_recall_float(self):
    num_gt = 10
    scores = np.array([0.4, 0.3, 0.6, 0.2, 0.7, 0.1], dtype=float)
    labels_float = np.array([0, 1, 1, 0.5, 0, 1], dtype=float)
    expected_precision = np.array(
        [0., 0.5, 0.33333333, 0.5, 0.55555556, 0.63636364], dtype=float)
    expected_recall = np.array([0., 0.1, 0.1, 0.2, 0.25, 0.35], dtype=float)
    precision, recall = metrics.compute_precision_recall(
        scores, labels_float, num_gt)
    self.assertAllClose(precision, expected_precision)
    self.assertAllClose(recall, expected_recall)

  def test_compute_average_precision(self):
    precision = np.array([0.8, 0.76, 0.9, 0.65, 0.7, 0.5, 0.55, 0], dtype=float)
    recall = np.array([0.3, 0.3, 0.4, 0.4, 0.45, 0.45, 0.5, 0.5], dtype=float)
    processed_precision = np.array(
        [0.9, 0.9, 0.9, 0.7, 0.7, 0.55, 0.55, 0], dtype=float)
    recall_interval = np.array([0.3, 0, 0.1, 0, 0.05, 0, 0.05, 0], dtype=float)
    expected_mean_ap = np.sum(recall_interval * processed_precision)
    mean_ap = metrics.compute_average_precision(precision, recall)
    self.assertAlmostEqual(expected_mean_ap, mean_ap)

  def test_compute_precision_recall_and_ap_no_groundtruth(self):
    num_gt = 0
    scores = np.array([0.4, 0.3, 0.6, 0.2, 0.7, 0.1], dtype=float)
    labels = np.array([0, 0, 0, 0, 0, 0], dtype=bool)
    expected_precision = None
    expected_recall = None
    precision, recall = metrics.compute_precision_recall(scores, labels, num_gt)
    self.assertEquals(precision, expected_precision)
    self.assertEquals(recall, expected_recall)
    ap = metrics.compute_average_precision(precision, recall)
    self.assertTrue(np.isnan(ap))

  def test_compute_recall_at_k(self):
    num_gt = 4
    tp_fp = [
        np.array([1, 0, 0], dtype=float),
        np.array([0, 1], dtype=float),
        np.array([0, 0, 0, 0, 0], dtype=float)
    ]
    tp_fp_bool = [
        np.array([True, False, False], dtype=bool),
        np.array([False, True], dtype=float),
        np.array([False, False, False, False, False], dtype=float)
    ]

    recall_1 = metrics.compute_recall_at_k(tp_fp, num_gt, 1)
    recall_3 = metrics.compute_recall_at_k(tp_fp, num_gt, 3)
    recall_5 = metrics.compute_recall_at_k(tp_fp, num_gt, 5)

    recall_3_bool = metrics.compute_recall_at_k(tp_fp_bool, num_gt, 3)

    self.assertAlmostEqual(recall_1, 0.25)
    self.assertAlmostEqual(recall_3, 0.5)
    self.assertAlmostEqual(recall_3_bool, 0.5)
    self.assertAlmostEqual(recall_5, 0.5)

  def test_compute_median_rank_at_k(self):
    tp_fp = [
        np.array([1, 0, 0], dtype=float),
        np.array([0, 0.1], dtype=float),
        np.array([0, 0, 0, 0, 0], dtype=float)
    ]
    tp_fp_bool = [
        np.array([True, False, False], dtype=bool),
        np.array([False, True], dtype=float),
        np.array([False, False, False, False, False], dtype=float)
    ]

    median_ranks_1 = metrics.compute_median_rank_at_k(tp_fp, 1)
    median_ranks_3 = metrics.compute_median_rank_at_k(tp_fp, 3)
    median_ranks_5 = metrics.compute_median_rank_at_k(tp_fp, 5)
    median_ranks_3_bool = metrics.compute_median_rank_at_k(tp_fp_bool, 3)

    self.assertEquals(median_ranks_1, 0)
    self.assertEquals(median_ranks_3, 0.5)
    self.assertEquals(median_ranks_3_bool, 0.5)
    self.assertEquals(median_ranks_5, 0.5)


if __name__ == '__main__':
  tf.test.main()