File size: 2,767 Bytes
0924f30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# coding=utf-8
# Copyright 2021 The Deeplab2 Authors.
#
# 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.

"""Test for drop_path.py."""
import numpy as np
import tensorflow as tf

from deeplab2.model.layers import drop_path

# Set a fixed random seed.
tf.random.set_seed(1)


class DropPathTest(tf.test.TestCase):

  def test_drop_path_keep_prob_one(self):
    # Test drop_path_keep_prob = 1, where output should be equal to input.
    drop_path_keep_prob = 1.0
    input_tensor = tf.random.uniform(shape=(3, 65, 65, 32))
    layer_op = drop_path.DropPath(drop_path_keep_prob)
    output_tensor = layer_op(input_tensor, training=True)
    np.testing.assert_equal(input_tensor.numpy(), output_tensor.numpy())

  def test_not_training_mode(self):
    # Test not training mode, where output should be equal to input.
    drop_path_keep_prob = 0.8
    input_tensor = tf.random.uniform(shape=(3, 65, 65, 32))
    layer_op = drop_path.DropPath(drop_path_keep_prob)
    output_tensor = layer_op(input_tensor, training=False)
    np.testing.assert_equal(input_tensor.numpy(), output_tensor.numpy())

  def test_drop_path(self):
    drop_path_keep_prob = 0.8
    input_tensor = tf.random.uniform(shape=(3, 65, 65, 32))
    layer_op = drop_path.DropPath(drop_path_keep_prob)
    output_tensor = layer_op(input_tensor, training=True)
    self.assertFalse(np.array_equal(input_tensor.numpy(),
                                    output_tensor.numpy()))

  def test_constant_drop_path_schedule(self):
    keep_prob_for_last_stage = 0.8
    current_stage_keep_prob = drop_path.get_drop_path_keep_prob(
        keep_prob_for_last_stage,
        schedule='constant',
        current_stage=2,
        num_stages=5)
    self.assertEqual(current_stage_keep_prob, keep_prob_for_last_stage)

  def test_linear_drop_path_schedule(self):
    keep_prob_for_last_stage = 0.8
    current_stage_keep_prob = drop_path.get_drop_path_keep_prob(
        keep_prob_for_last_stage,
        schedule='linear',
        current_stage=1,
        num_stages=4)
    self.assertEqual(current_stage_keep_prob, 0.95)

  def test_unknown_drop_path_schedule(self):
    with self.assertRaises(ValueError):
      _ = drop_path.get_drop_path_keep_prob(0.8, 'unknown', 1, 4)


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