test_modeling_tf_vit.py 9.65 KB
Newer Older
Yih-Dar's avatar
Yih-Dar committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# coding=utf-8
# Copyright 2021 The HuggingFace Inc. team. 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.
""" Testing suite for the TensorFlow ViT model. """


Matt's avatar
Matt committed
18
19
from __future__ import annotations

Yih-Dar's avatar
Yih-Dar committed
20
21
22
23
import inspect
import unittest

from transformers import ViTConfig
NielsRogge's avatar
NielsRogge committed
24
from transformers.testing_utils import require_tf, require_vision, slow
25
from transformers.utils import cached_property, is_tf_available, is_vision_available
Yih-Dar's avatar
Yih-Dar committed
26

Yih-Dar's avatar
Yih-Dar committed
27
28
from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
29
from ...test_pipeline_mixin import PipelineTesterMixin
Yih-Dar's avatar
Yih-Dar committed
30
31
32
33
34
35


if is_tf_available():
    import tensorflow as tf

    from transformers import TFViTForImageClassification, TFViTModel
36
    from transformers.modeling_tf_utils import keras
Yih-Dar's avatar
Yih-Dar committed
37
38
39
40
41


if is_vision_available():
    from PIL import Image

42
    from transformers import ViTImageProcessor
Yih-Dar's avatar
Yih-Dar committed
43
44
45
46
47
48
49
50
51
52
53
54
55


class TFViTModelTester:
    def __init__(
        self,
        parent,
        batch_size=13,
        image_size=30,
        patch_size=2,
        num_channels=3,
        is_training=True,
        use_labels=True,
        hidden_size=32,
56
        num_hidden_layers=2,
Yih-Dar's avatar
Yih-Dar committed
57
58
59
60
61
62
63
64
65
        num_attention_heads=4,
        intermediate_size=37,
        hidden_act="gelu",
        hidden_dropout_prob=0.1,
        attention_probs_dropout_prob=0.1,
        type_sequence_label_size=10,
        initializer_range=0.02,
        num_labels=3,
        scope=None,
66
        attn_implementation="eager",
Yih-Dar's avatar
Yih-Dar committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    ):
        self.parent = parent
        self.batch_size = batch_size
        self.image_size = image_size
        self.patch_size = patch_size
        self.num_channels = num_channels
        self.is_training = is_training
        self.use_labels = use_labels
        self.hidden_size = hidden_size
        self.num_hidden_layers = num_hidden_layers
        self.num_attention_heads = num_attention_heads
        self.intermediate_size = intermediate_size
        self.hidden_act = hidden_act
        self.hidden_dropout_prob = hidden_dropout_prob
        self.attention_probs_dropout_prob = attention_probs_dropout_prob
        self.type_sequence_label_size = type_sequence_label_size
        self.initializer_range = initializer_range
        self.scope = scope
85
        self.attn_implementation = attn_implementation
Yih-Dar's avatar
Yih-Dar committed
86

NielsRogge's avatar
NielsRogge committed
87
        # in ViT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
88
        num_patches = (image_size // patch_size) ** 2
NielsRogge's avatar
NielsRogge committed
89
        self.seq_length = num_patches + 1
90

Yih-Dar's avatar
Yih-Dar committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
    def prepare_config_and_inputs(self):
        pixel_values = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

        labels = None
        if self.use_labels:
            labels = ids_tensor([self.batch_size], self.type_sequence_label_size)

        config = self.get_config()

        return config, pixel_values, labels

    def get_config(self):
        return ViTConfig(
            image_size=self.image_size,
            patch_size=self.patch_size,
            num_channels=self.num_channels,
            hidden_size=self.hidden_size,
            num_hidden_layers=self.num_hidden_layers,
            num_attention_heads=self.num_attention_heads,
            intermediate_size=self.intermediate_size,
            hidden_act=self.hidden_act,
            hidden_dropout_prob=self.hidden_dropout_prob,
            attention_probs_dropout_prob=self.attention_probs_dropout_prob,
            is_decoder=False,
            initializer_range=self.initializer_range,
116
            attn_implementation=self.attn_implementation,
Yih-Dar's avatar
Yih-Dar committed
117
118
119
120
121
        )

    def create_and_check_model(self, config, pixel_values, labels):
        model = TFViTModel(config=config)
        result = model(pixel_values, training=False)
NielsRogge's avatar
NielsRogge committed
122
        self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size))
Yih-Dar's avatar
Yih-Dar committed
123
124
125
126
127

        # Test with an image with different size than the one specified in config.
        image_size = self.image_size // 2
        pixel_values = pixel_values[:, :, :image_size, :image_size]
        result = model(pixel_values, interpolate_pos_encoding=True, training=False)
NielsRogge's avatar
NielsRogge committed
128
129
        seq_length = (image_size // self.patch_size) ** 2 + 1
        self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, seq_length, self.hidden_size))
Yih-Dar's avatar
Yih-Dar committed
130
131
132
133
134
135
136
137
138
139
140
141
142

    def create_and_check_for_image_classification(self, config, pixel_values, labels):
        config.num_labels = self.type_sequence_label_size
        model = TFViTForImageClassification(config)
        result = model(pixel_values, labels=labels, training=False)
        self.parent.assertEqual(result.logits.shape, (self.batch_size, self.type_sequence_label_size))

        # Test with an image with different size than the one specified in config.
        image_size = self.image_size // 2
        pixel_values = pixel_values[:, :, :image_size, :image_size]
        result = model(pixel_values, interpolate_pos_encoding=True, training=False)
        self.parent.assertEqual(result.logits.shape, (self.batch_size, self.type_sequence_label_size))

NielsRogge's avatar
NielsRogge committed
143
144
145
146
147
148
149
        # test greyscale images
        config.num_channels = 1
        model = TFViTForImageClassification(config)
        pixel_values = floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
        result = model(pixel_values)
        self.parent.assertEqual(result.logits.shape, (self.batch_size, self.type_sequence_label_size))

Yih-Dar's avatar
Yih-Dar committed
150
151
152
153
154
155
156
157
    def prepare_config_and_inputs_for_common(self):
        config_and_inputs = self.prepare_config_and_inputs()
        config, pixel_values, labels = config_and_inputs
        inputs_dict = {"pixel_values": pixel_values}
        return config, inputs_dict


@require_tf
158
class TFViTModelTest(TFModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
Yih-Dar's avatar
Yih-Dar committed
159
160
161
162
163
164
    """
    Here we also overwrite some of the tests of test_modeling_tf_common.py, as ViT does not use input_ids, inputs_embeds,
    attention_mask and seq_length.
    """

    all_model_classes = (TFViTModel, TFViTForImageClassification) if is_tf_available() else ()
165
166
167
168
169
    pipeline_model_mapping = (
        {"feature-extraction": TFViTModel, "image-classification": TFViTForImageClassification}
        if is_tf_available()
        else {}
    )
Yih-Dar's avatar
Yih-Dar committed
170
171
172
173
174
175
176
177
178
179
180
181

    test_resize_embeddings = False
    test_head_masking = False
    test_onnx = False

    def setUp(self):
        self.model_tester = TFViTModelTester(self)
        self.config_tester = ConfigTester(self, config_class=ViTConfig, has_text_modality=False, hidden_size=37)

    def test_config(self):
        self.config_tester.run_common_tests()

NielsRogge's avatar
NielsRogge committed
182
    @unittest.skip(reason="ViT does not use inputs_embeds")
Yih-Dar's avatar
Yih-Dar committed
183
184
185
    def test_inputs_embeds(self):
        pass

NielsRogge's avatar
NielsRogge committed
186
    @unittest.skip(reason="ViT does not use inputs_embeds")
Yih-Dar's avatar
Yih-Dar committed
187
188
189
190
191
192
193
194
    def test_graph_mode_with_inputs_embeds(self):
        pass

    def test_model_common_attributes(self):
        config, _ = self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
            model = model_class(config)
195
            self.assertIsInstance(model.get_input_embeddings(), (keras.layers.Layer))
Yih-Dar's avatar
Yih-Dar committed
196
            x = model.get_output_embeddings()
197
            self.assertTrue(x is None or isinstance(x, keras.layers.Layer))
Yih-Dar's avatar
Yih-Dar committed
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220

    def test_forward_signature(self):
        config, _ = self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
            model = model_class(config)
            signature = inspect.signature(model.call)
            # signature.parameters is an OrderedDict => so arg_names order is deterministic
            arg_names = [*signature.parameters.keys()]

            expected_arg_names = ["pixel_values"]
            self.assertListEqual(arg_names[:1], expected_arg_names)

    def test_model(self):
        config_and_inputs = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*config_and_inputs)

    def test_for_image_classification(self):
        config_and_inputs = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*config_and_inputs)

    @slow
    def test_model_from_pretrained(self):
221
        model = TFViTModel.from_pretrained("google/vit-base-patch16-224")
Yih-Dar's avatar
Yih-Dar committed
222
223
224
225
226
227
228
229
230
        self.assertIsNotNone(model)


# We will verify our results on an image of cute cats
def prepare_img():
    image = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
    return image


231
@require_tf
Yih-Dar's avatar
Yih-Dar committed
232
233
234
@require_vision
class TFViTModelIntegrationTest(unittest.TestCase):
    @cached_property
235
236
    def default_image_processor(self):
        return ViTImageProcessor.from_pretrained("google/vit-base-patch16-224") if is_vision_available() else None
Yih-Dar's avatar
Yih-Dar committed
237
238
239

    @slow
    def test_inference_image_classification_head(self):
NielsRogge's avatar
NielsRogge committed
240
        model = TFViTForImageClassification.from_pretrained("google/vit-base-patch16-224")
Yih-Dar's avatar
Yih-Dar committed
241

242
        image_processor = self.default_image_processor
Yih-Dar's avatar
Yih-Dar committed
243
        image = prepare_img()
244
        inputs = image_processor(images=image, return_tensors="tf")
Yih-Dar's avatar
Yih-Dar committed
245
246
247
248
249
250
251
252
253
254
255

        # forward pass
        outputs = model(**inputs)

        # verify the logits
        expected_shape = tf.TensorShape((1, 1000))
        self.assertEqual(outputs.logits.shape, expected_shape)

        expected_slice = tf.constant([-0.2744, 0.8215, -0.0836])

        tf.debugging.assert_near(outputs.logits[0, :3], expected_slice, atol=1e-4)