movinet_test.py 6.19 KB
Newer Older
Dan Kondratyuk's avatar
Dan Kondratyuk committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 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.

# Lint as: python3
"""Tests for movinet.py."""

from absl.testing import parameterized
import tensorflow as tf

Dan Kondratyuk's avatar
Dan Kondratyuk committed
21
from official.projects.movinet.modeling import movinet
Dan Kondratyuk's avatar
Dan Kondratyuk committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37


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

  def test_network_creation(self):
    """Test creation of MoViNet family models."""
    tf.keras.backend.set_image_data_format('channels_last')

    network = movinet.Movinet(
        model_id='a0',
        causal=True,
    )
    inputs = tf.keras.Input(shape=(8, 128, 128, 3), batch_size=1)
    endpoints, states = network(inputs)

    self.assertAllEqual(endpoints['stem'].shape, [1, 8, 64, 64, 8])
38
39
40
41
42
    self.assertAllEqual(endpoints['block0_layer0'].shape, [1, 8, 32, 32, 8])
    self.assertAllEqual(endpoints['block1_layer0'].shape, [1, 8, 16, 16, 32])
    self.assertAllEqual(endpoints['block2_layer0'].shape, [1, 8, 8, 8, 56])
    self.assertAllEqual(endpoints['block3_layer0'].shape, [1, 8, 8, 8, 56])
    self.assertAllEqual(endpoints['block4_layer0'].shape, [1, 8, 4, 4, 104])
Dan Kondratyuk's avatar
Dan Kondratyuk committed
43
44
45
46
47
48
49
50
    self.assertAllEqual(endpoints['head'].shape, [1, 1, 1, 1, 480])

    self.assertNotEmpty(states)

  def test_network_with_states(self):
    """Test creation of MoViNet family models with states."""
    tf.keras.backend.set_image_data_format('channels_last')

51
    backbone = movinet.Movinet(
Dan Kondratyuk's avatar
Dan Kondratyuk committed
52
53
        model_id='a0',
        causal=True,
54
        use_external_states=True,
Dan Kondratyuk's avatar
Dan Kondratyuk committed
55
56
57
    )
    inputs = tf.ones([1, 8, 128, 128, 3])

58
59
    init_states = backbone.init_states(tf.shape(inputs))
    endpoints, new_states = backbone({**init_states, 'image': inputs})
Dan Kondratyuk's avatar
Dan Kondratyuk committed
60
61

    self.assertAllEqual(endpoints['stem'].shape, [1, 8, 64, 64, 8])
62
63
64
65
66
    self.assertAllEqual(endpoints['block0_layer0'].shape, [1, 8, 32, 32, 8])
    self.assertAllEqual(endpoints['block1_layer0'].shape, [1, 8, 16, 16, 32])
    self.assertAllEqual(endpoints['block2_layer0'].shape, [1, 8, 8, 8, 56])
    self.assertAllEqual(endpoints['block3_layer0'].shape, [1, 8, 8, 8, 56])
    self.assertAllEqual(endpoints['block4_layer0'].shape, [1, 8, 4, 4, 104])
Dan Kondratyuk's avatar
Dan Kondratyuk committed
67
68
    self.assertAllEqual(endpoints['head'].shape, [1, 1, 1, 1, 480])

69
    self.assertNotEmpty(init_states)
Dan Kondratyuk's avatar
Dan Kondratyuk committed
70
71
72
    self.assertNotEmpty(new_states)

  def test_movinet_stream(self):
73
    """Test if the backbone can be run in streaming mode."""
Dan Kondratyuk's avatar
Dan Kondratyuk committed
74
75
    tf.keras.backend.set_image_data_format('channels_last')

76
    backbone = movinet.Movinet(
Dan Kondratyuk's avatar
Dan Kondratyuk committed
77
78
        model_id='a0',
        causal=True,
79
        use_external_states=True,
Dan Kondratyuk's avatar
Dan Kondratyuk committed
80
81
82
    )
    inputs = tf.ones([1, 5, 128, 128, 3])

83
84
    init_states = backbone.init_states(tf.shape(inputs))
    expected_endpoints, _ = backbone({**init_states, 'image': inputs})
Dan Kondratyuk's avatar
Dan Kondratyuk committed
85
86
87

    frames = tf.split(inputs, inputs.shape[1], axis=1)

88
    states = init_states
Dan Kondratyuk's avatar
Dan Kondratyuk committed
89
    for frame in frames:
90
      output, states = backbone({**states, 'image': frame})
Dan Kondratyuk's avatar
Dan Kondratyuk committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    predicted_endpoints = output

    predicted = predicted_endpoints['head']

    # The expected final output is simply the mean across frames
    expected = expected_endpoints['head']
    expected = tf.reduce_mean(expected, 1, keepdims=True)

    self.assertEqual(predicted.shape, expected.shape)
    self.assertAllClose(predicted, expected, 1e-5, 1e-5)

  def test_movinet_2plus1d_stream(self):
    tf.keras.backend.set_image_data_format('channels_last')

105
    backbone = movinet.Movinet(
Dan Kondratyuk's avatar
Dan Kondratyuk committed
106
107
108
        model_id='a0',
        causal=True,
        conv_type='2plus1d',
109
        use_external_states=True,
Dan Kondratyuk's avatar
Dan Kondratyuk committed
110
111
112
    )
    inputs = tf.ones([1, 5, 128, 128, 3])

113
114
    init_states = backbone.init_states(tf.shape(inputs))
    expected_endpoints, _ = backbone({**init_states, 'image': inputs})
Dan Kondratyuk's avatar
Dan Kondratyuk committed
115
116
117

    frames = tf.split(inputs, inputs.shape[1], axis=1)

118
    states = init_states
Dan Kondratyuk's avatar
Dan Kondratyuk committed
119
    for frame in frames:
120
      output, states = backbone({**states, 'image': frame})
Dan Kondratyuk's avatar
Dan Kondratyuk committed
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    predicted_endpoints = output

    predicted = predicted_endpoints['head']

    # The expected final output is simply the mean across frames
    expected = expected_endpoints['head']
    expected = tf.reduce_mean(expected, 1, keepdims=True)

    self.assertEqual(predicted.shape, expected.shape)
    self.assertAllClose(predicted, expected, 1e-5, 1e-5)

  def test_movinet_3d_2plus1d_stream(self):
    tf.keras.backend.set_image_data_format('channels_last')

135
    backbone = movinet.Movinet(
Dan Kondratyuk's avatar
Dan Kondratyuk committed
136
137
138
        model_id='a0',
        causal=True,
        conv_type='3d_2plus1d',
139
        use_external_states=True,
Dan Kondratyuk's avatar
Dan Kondratyuk committed
140
141
142
    )
    inputs = tf.ones([1, 5, 128, 128, 3])

143
144
    init_states = backbone.init_states(tf.shape(inputs))
    expected_endpoints, _ = backbone({**init_states, 'image': inputs})
Dan Kondratyuk's avatar
Dan Kondratyuk committed
145
146
147

    frames = tf.split(inputs, inputs.shape[1], axis=1)

148
    states = init_states
Dan Kondratyuk's avatar
Dan Kondratyuk committed
149
    for frame in frames:
150
      output, states = backbone({**states, 'image': frame})
Dan Kondratyuk's avatar
Dan Kondratyuk committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
    predicted_endpoints = output

    predicted = predicted_endpoints['head']

    # The expected final output is simply the mean across frames
    expected = expected_endpoints['head']
    expected = tf.reduce_mean(expected, 1, keepdims=True)

    self.assertEqual(predicted.shape, expected.shape)
    self.assertAllClose(predicted, expected, 1e-5, 1e-5)

  def test_serialize_deserialize(self):
    # Create a network object that sets all of its config options.
    kwargs = dict(
        model_id='a0',
        causal=True,
        use_positional_encoding=True,
168
        use_external_states=True,
Dan Kondratyuk's avatar
Dan Kondratyuk committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
    )
    network = movinet.Movinet(**kwargs)

    # Create another network object from the first object's config.
    new_network = movinet.Movinet.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()