fpn_test.py 4.17 KB
Newer Older
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
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
# Copyright 2022 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 FPN."""

# Import libraries
from absl.testing import parameterized
import tensorflow as tf

from official.vision.modeling.backbones import mobilenet
from official.vision.modeling.backbones import resnet
from official.vision.modeling.decoders import fpn


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

  @parameterized.parameters(
29
30
31
32
      (256, 3, 7, False, False, 'sum'),
      (256, 3, 7, False, True, 'sum'),
      (256, 3, 7, True, False, 'concat'),
      (256, 3, 7, True, True, 'concat'),
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
33
34
  )
  def test_network_creation(self, input_size, min_level, max_level,
35
                            use_separable_conv, use_keras_layer, fusion_type):
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
36
37
38
39
40
41
42
43
44
45
46
    """Test creation of FPN."""
    tf.keras.backend.set_image_data_format('channels_last')

    inputs = tf.keras.Input(shape=(input_size, input_size, 3), batch_size=1)

    backbone = resnet.ResNet(model_id=50)
    network = fpn.FPN(
        input_specs=backbone.output_specs,
        min_level=min_level,
        max_level=max_level,
        fusion_type=fusion_type,
47
48
        use_separable_conv=use_separable_conv,
        use_keras_layer=use_keras_layer)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
49
50
51
52
53
54
55
56
57
58
59

    endpoints = backbone(inputs)
    feats = network(endpoints)

    for level in range(min_level, max_level + 1):
      self.assertIn(str(level), feats)
      self.assertAllEqual(
          [1, input_size // 2**level, input_size // 2**level, 256],
          feats[str(level)].shape.as_list())

  @parameterized.parameters(
60
61
62
63
      (256, 3, 7, False, False),
      (256, 3, 7, False, True),
      (256, 3, 7, True, False),
      (256, 3, 7, True, True),
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
64
65
  )
  def test_network_creation_with_mobilenet(self, input_size, min_level,
66
67
                                           max_level, use_separable_conv,
                                           use_keras_layer):
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
68
69
70
71
72
73
74
75
76
77
    """Test creation of FPN with mobilenet backbone."""
    tf.keras.backend.set_image_data_format('channels_last')

    inputs = tf.keras.Input(shape=(input_size, input_size, 3), batch_size=1)

    backbone = mobilenet.MobileNet(model_id='MobileNetV2')
    network = fpn.FPN(
        input_specs=backbone.output_specs,
        min_level=min_level,
        max_level=max_level,
78
79
        use_separable_conv=use_separable_conv,
        use_keras_layer=use_keras_layer)
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

    endpoints = backbone(inputs)
    feats = network(endpoints)

    for level in range(min_level, max_level + 1):
      self.assertIn(str(level), feats)
      self.assertAllEqual(
          [1, input_size // 2**level, input_size // 2**level, 256],
          feats[str(level)].shape.as_list())

  def test_serialize_deserialize(self):
    # Create a network object that sets all of its config options.
    kwargs = dict(
        input_specs=resnet.ResNet(model_id=50).output_specs,
        min_level=3,
        max_level=7,
        num_filters=256,
        fusion_type='sum',
        use_separable_conv=False,
99
        use_keras_layer=False,
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
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
        use_sync_bn=False,
        activation='relu',
        norm_momentum=0.99,
        norm_epsilon=0.001,
        kernel_initializer='VarianceScaling',
        kernel_regularizer=None,
        bias_regularizer=None,
    )
    network = fpn.FPN(**kwargs)

    expected_config = dict(kwargs)
    self.assertEqual(network.get_config(), expected_config)

    # Create another network object from the first object's config.
    new_network = fpn.FPN.from_config(network.get_config())

    # Validate that the config can be forced to JSON.
    _ = new_network.to_json()

    # If the serialization was successful, the new config should match the old.
    self.assertAllEqual(network.get_config(), new_network.get_config())


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