vit_test.py 2.51 KB
Newer Older
A. Unique TensorFlower's avatar
A. Unique TensorFlower committed
1
# Copyright 2022 The TensorFlow Authors. All Rights Reserved.
Xianzhi Du's avatar
Xianzhi Du committed
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#
# 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 VIT."""

from absl.testing import parameterized
import tensorflow as tf

Xianzhi Du's avatar
Xianzhi Du committed
20
from official.projects.vit.modeling import vit
Xianzhi Du's avatar
Xianzhi Du committed
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39


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

  @parameterized.parameters(
      (224, 85798656),
      (256, 85844736),
  )
  def test_network_creation(self, input_size, params_count):
    """Test creation of VisionTransformer family models."""
    tf.keras.backend.set_image_data_format('channels_last')
    input_specs = tf.keras.layers.InputSpec(
        shape=[2, input_size, input_size, 3])
    network = vit.VisionTransformer(input_specs=input_specs)

    inputs = tf.keras.Input(shape=(input_size, input_size, 3), batch_size=1)
    _ = network(inputs)
    self.assertEqual(network.count_params(), params_count)

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  def test_network_none_pooler(self):
    tf.keras.backend.set_image_data_format('channels_last')
    input_size = 256
    input_specs = tf.keras.layers.InputSpec(
        shape=[2, input_size, input_size, 3])
    network = vit.VisionTransformer(
        input_specs=input_specs,
        patch_size=16,
        pooler='none',
        representation_size=128,
        pos_embed_shape=(14, 14))  # (224 // 16)

    inputs = tf.keras.Input(shape=(input_size, input_size, 3), batch_size=1)
    output = network(inputs)['encoded_tokens']
    self.assertEqual(output.shape, [1, 256, 128])

56
57
58
59
60
61
62
63
  def test_posembedding_interpolation(self):
    tf.keras.backend.set_image_data_format('channels_last')
    input_size = 256
    input_specs = tf.keras.layers.InputSpec(
        shape=[2, input_size, input_size, 3])
    network = vit.VisionTransformer(
        input_specs=input_specs,
        patch_size=16,
64
        pooler='gap',
65
66
67
68
69
70
        pos_embed_shape=(14, 14))  # (224 // 16)

    inputs = tf.keras.Input(shape=(input_size, input_size, 3), batch_size=1)
    output = network(inputs)['pre_logits']
    self.assertEqual(output.shape, [1, 1, 1, 768])

Xianzhi Du's avatar
Xianzhi Du committed
71
72
73

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