"tests/data/test_simple_sphere_light_phong.png" did not exist on "15c72be444fab0d1ba6879d097399b12f6a2a8b0"
metrics_test.py 2.14 KB
Newer Older
Gunho Park's avatar
Gunho Park committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# Copyright 2021 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.

15
"""Tests for metrics.py."""
Gunho Park's avatar
Gunho Park committed
16
17
18
from absl.testing import parameterized
import tensorflow as tf

19
from official.projects.basnet.evaluation import metrics
Gunho Park's avatar
Gunho Park committed
20
21
22
23
24
25


class BASNetMetricTest(parameterized.TestCase, tf.test.TestCase):

  def test_mae(self):
    input_size = 224
26

Gunho Park's avatar
Gunho Park committed
27
28
    inputs = (tf.random.uniform([2, input_size, input_size, 1]),)
    labels = (tf.random.uniform([2, input_size, input_size, 1]),)
29

30
    mae_obj = metrics.MAE()
Gunho Park's avatar
Gunho Park committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
    mae_obj.reset_states()
    mae_obj.update_state(labels, inputs)
    output = mae_obj.result()

    mae_tf = tf.keras.metrics.MeanAbsoluteError()
    mae_tf.reset_state()
    mae_tf.update_state(labels[0], inputs[0])
    compare = mae_tf.result().numpy()

    self.assertAlmostEqual(output, compare, places=4)

  def test_max_f(self):
    input_size = 224
    beta = 0.3

    inputs = (tf.random.uniform([2, input_size, input_size, 1]),)
    labels = (tf.random.uniform([2, input_size, input_size, 1]),)
48
49

    max_f_obj = metrics.MaxFscore()
Gunho Park's avatar
Gunho Park committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
    max_f_obj.reset_states()
    max_f_obj.update_state(labels, inputs)
    output = max_f_obj.result()

    pre_tf = tf.keras.metrics.Precision(thresholds=0.78)
    rec_tf = tf.keras.metrics.Recall(thresholds=0.78)
    pre_tf.reset_state()
    rec_tf.reset_state()
    pre_tf.update_state(labels[0], inputs[0])
    rec_tf.update_state(labels[0], inputs[0])
    pre_out_tf = pre_tf.result().numpy()
    rec_out_tf = rec_tf.result().numpy()
    compare = (1+beta)*pre_out_tf*rec_out_tf/(beta*pre_out_tf+rec_out_tf+1e-8)

    self.assertAlmostEqual(output, compare, places=1)


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