"tests/vilt/test_feature_extraction_vilt.py" did not exist on "1fc6817a30ea583a02bffb77215f213429d5b5bc"
test_modeling_tvp.py 13.3 KB
Newer Older
jiqing-feng's avatar
jiqing-feng committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
# coding=utf-8
# Copyright 2023 The Intel Team Authors, 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.
Arthur's avatar
Arthur committed
15
"""Testing suite for the PyTorch TVP model."""
jiqing-feng's avatar
jiqing-feng committed
16
17
18

import unittest

19
20
from transformers import ResNetConfig, TimmBackboneConfig, TvpConfig
from transformers.testing_utils import require_timm, require_torch, require_vision, torch_device
jiqing-feng's avatar
jiqing-feng committed
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
78
79
80
81
82
83
84
85
86
87
88
89
90
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
116
117
118
119
120
121
122
123
124
125
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_modeling_common import (
    ModelTesterMixin,
    _config_zero_init,
    floats_tensor,
    ids_tensor,
    random_attention_mask,
)
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import TvpForVideoGrounding, TvpModel

if is_vision_available():
    from PIL import Image

    from transformers import TvpImageProcessor


# Copied from test.models.videomae.test_modeling_videomae.VideoMAEModelTester with VideoMAE->TVP
class TVPModelTester:
    def __init__(
        self,
        parent,
        batch_size=1,
        seq_length=2,
        alpha=1.0,
        beta=0.1,
        visual_prompter_type="framepad",
        visual_prompter_apply="replace",
        num_frames=2,
        max_img_size=448,
        visual_prompt_size=96,
        vocab_size=100,
        hidden_size=32,
        intermediate_size=32,
        num_hidden_layers=2,
        num_attention_heads=4,
        max_position_embeddings=30,
        max_grid_col_position_embeddings=30,
        max_grid_row_position_embeddings=30,
        hidden_dropout_prob=0.1,
        hidden_act="gelu",
        layer_norm_eps=1e-12,
        initializer_range=0.02,
        pad_token_id=0,
        type_vocab_size=2,
        attention_probs_dropout_prob=0.1,
    ):
        self.parent = parent
        self.batch_size = batch_size
        self.input_id_length = seq_length
        self.seq_length = seq_length + 10 + 784  # include text prompt length and visual input length
        self.alpha = alpha
        self.beta = beta
        self.visual_prompter_type = visual_prompter_type
        self.visual_prompter_apply = visual_prompter_apply
        self.num_frames = num_frames
        self.max_img_size = max_img_size
        self.visual_prompt_size = visual_prompt_size
        self.vocab_size = vocab_size
        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.max_position_embeddings = max_position_embeddings
        self.max_grid_col_position_embeddings = max_grid_col_position_embeddings
        self.max_grid_row_position_embeddings = max_grid_row_position_embeddings
        self.layer_norm_eps = layer_norm_eps
        self.initializer_range = initializer_range
        self.pad_token_id = pad_token_id
        self.type_vocab_size = type_vocab_size
        self.is_training = False
        self.num_channels = 3

    def prepare_config_and_inputs(self):
        input_ids = ids_tensor([self.batch_size, self.input_id_length], self.vocab_size)
        attention_mask = random_attention_mask([self.batch_size, self.input_id_length])
        pixel_values = floats_tensor(
            [self.batch_size, self.num_frames, self.num_channels, self.max_img_size, self.max_img_size]
        )

        config = self.get_config()

        return (config, input_ids, pixel_values, attention_mask)

    def get_config(self):
        resnet_config = ResNetConfig(
            num_channels=3,
            embeddings_size=64,
            hidden_sizes=[64, 128],
            depths=[2, 2],
            hidden_act="relu",
            out_features=["stage2"],
            out_indices=[2],
        )
        return TvpConfig(
            backbone_config=resnet_config,
126
            backbone=None,
jiqing-feng's avatar
jiqing-feng committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
            alpha=self.alpha,
            beta=self.beta,
            visual_prompter_type=self.visual_prompter_type,
            visual_prompter_apply=self.visual_prompter_apply,
            num_frames=self.num_frames,
            max_img_size=self.max_img_size,
            visual_prompt_size=self.visual_prompt_size,
            vocab_size=self.vocab_size,
            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,
            max_position_embeddings=self.max_position_embeddings,
            max_grid_col_position_embeddings=self.max_grid_col_position_embeddings,
            max_grid_row_position_embeddings=self.max_grid_row_position_embeddings,
            layer_norm_eps=self.layer_norm_eps,
            initializer_range=self.initializer_range,
            pad_token_id=self.pad_token_id,
            type_vocab_size=self.type_vocab_size,
        )

    def create_and_check_model(self, config, input_ids, pixel_values, attention_mask):
        model = TvpModel(config=config)
        model.to(torch_device)
        model.eval()
        result = model(input_ids, pixel_values, attention_mask)
        self.parent.assertEqual(result.last_hidden_state.shape, (self.batch_size, self.seq_length, self.hidden_size))

    def prepare_config_and_inputs_for_common(self):
        config_and_inputs = self.prepare_config_and_inputs()
        config, input_ids, pixel_values, attention_mask = config_and_inputs
        inputs_dict = {"input_ids": input_ids, "pixel_values": pixel_values, "attention_mask": attention_mask}
        return config, inputs_dict


@require_torch
class TVPModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
    """
    Here we also overwrite some of the tests of test_modeling_common.py, as TVP does not use, inputs_embeds.
    The seq_length in TVP contain textual and visual inputs, and prompt.
    """

    all_model_classes = (TvpModel, TvpForVideoGrounding) if is_torch_available() else ()
    pipeline_model_mapping = (
        {"feature-extraction": TvpModel, "temporal-video-grounding": TvpForVideoGrounding}
        if is_torch_available()
        else {}
    )

Yih-Dar's avatar
Yih-Dar committed
179
180
181
    # TODO: Enable this once this model gets more usage
    test_torchscript = False

jiqing-feng's avatar
jiqing-feng committed
182
183
184
185
186
187
188
189
190
191
192
193
    def setUp(self):
        self.model_tester = TVPModelTester(self)

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

    @unittest.skip(reason="TVP does not use inputs_embeds")
    def test_inputs_embeds(self):
        pass

    @unittest.skip(reason="TVPModel does not have input/output embeddings")
194
    def test_model_get_set_embeddings(self):
jiqing-feng's avatar
jiqing-feng committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
        pass

    # override as the `logit_scale` parameter initilization is different for TVP
    def test_initialization(self):
        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()

        configs_no_init = _config_zero_init(config)
        for model_class in self.all_model_classes:
            model = model_class(config=configs_no_init)
            for name, param in model.named_parameters():
                if param.requires_grad:
                    # params are randomly initialized.
                    self.assertAlmostEqual(
                        param.data.mean().item(),
                        0.0,
                        delta=1.0,
                        msg=f"Parameter {name} of model {model_class} seems not properly initialized",
                    )

214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    @require_timm
    def test_backbone_selection(self):
        def _validate_backbone_init():
            for model_class in self.all_model_classes:
                model = model_class(config)
                model.to(torch_device)
                model.eval()

                # Confirm out_indices propogated to backbone
                if model.__class__.__name__ == "TvpModel":
                    self.assertEqual(len(model.vision_model.backbone.out_indices), 2)
                elif model.__class__.__name__ == "TvpForVideoGrounding":
                    self.assertEqual(len(model.model.vision_model.backbone.out_indices), 2)

        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
        # Force load_backbone path
        config.is_hybrid = False

        # We load through configs, as the modeling file assumes config.backbone_config is always set
        config.use_pretrained_backbone = False
        config.backbone_kwargs = None

        # Load a timm backbone
        # We hack adding hidden_sizes to the config to test the backbone loading
        backbone_config = TimmBackboneConfig("resnet18", out_indices=[-2, -1], hidden_sizes=[64, 128])
        config.backbone_config = backbone_config
        _validate_backbone_init()

        # Load a HF backbone
        backbone_config = ResNetConfig.from_pretrained("facebook/dinov2-small", out_indices=[-2, -1])
        config.backbone_config = backbone_config
        _validate_backbone_init()

jiqing-feng's avatar
jiqing-feng committed
247
248
249
250
251
252
253
254
255
256
257
258

# 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


@require_vision
@require_torch
class TvpModelIntegrationTests(unittest.TestCase):
    @cached_property
    def default_image_processor(self):
259
        return TvpImageProcessor.from_pretrained("Jiqing/tiny-random-tvp")
jiqing-feng's avatar
jiqing-feng committed
260
261
262
263
264
265

    def test_inference_no_head(self):
        model = TvpModel.from_pretrained("Jiqing/tiny-random-tvp").to(torch_device)

        image_processor = self.default_image_processor
        image = prepare_img()
266
        encoding = image_processor(images=image, return_tensors="pt")
jiqing-feng's avatar
jiqing-feng committed
267
268
269
        input_ids = torch.tensor([[1, 2]])
        attention_mask = torch.tensor([[1, 1]])
        encoding.update({"input_ids": input_ids, "attention_mask": attention_mask})
270
        encoding.to(torch_device)
jiqing-feng's avatar
jiqing-feng committed
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286

        with torch.no_grad():
            outputs = model(**encoding)

        expected_shape = torch.Size((1, 796, 128))
        assert outputs.last_hidden_state.shape == expected_shape
        expected_slice = torch.tensor(
            [[-0.4902, -0.4121, -1.7872], [-0.2184, 2.1211, -0.9371], [0.1180, 0.5003, -0.1727]]
        ).to(torch_device)
        self.assertTrue(torch.allclose(outputs.last_hidden_state[0, :3, :3], expected_slice, atol=1e-4))

    def test_inference_with_head(self):
        model = TvpForVideoGrounding.from_pretrained("Jiqing/tiny-random-tvp").to(torch_device)

        image_processor = self.default_image_processor
        image = prepare_img()
287
        encoding = image_processor(images=image, return_tensors="pt")
jiqing-feng's avatar
jiqing-feng committed
288
289
290
        input_ids = torch.tensor([[1, 2]])
        attention_mask = torch.tensor([[1, 1]])
        encoding.update({"input_ids": input_ids, "attention_mask": attention_mask})
291
        encoding.to(torch_device)
jiqing-feng's avatar
jiqing-feng committed
292
293
294
295
296
297
298
299

        with torch.no_grad():
            outputs = model(**encoding)

        expected_shape = torch.Size((1, 2))
        assert outputs.logits.shape == expected_shape
        expected_slice = torch.tensor([[0.5061, 0.4988]]).to(torch_device)
        self.assertTrue(torch.allclose(outputs.logits, expected_slice, atol=1e-4))
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337

    def test_interpolate_inference_no_head(self):
        model = TvpModel.from_pretrained("Jiqing/tiny-random-tvp").to(torch_device)

        image_processor = self.default_image_processor
        image = prepare_img()  # 480X640
        encoding = image_processor(
            images=image, return_tensors="pt", do_resize=False, do_pad=False, do_center_crop=False
        )
        input_ids = torch.tensor([[1, 2]])
        attention_mask = torch.tensor([[1, 1]])
        encoding.update({"input_ids": input_ids, "attention_mask": attention_mask})
        encoding.to(torch_device)

        with torch.no_grad():
            outputs = model(**encoding, interpolate_pos_encoding=True)

        expected_shape = torch.Size((1, 1212, 128))
        assert outputs.last_hidden_state.shape == expected_shape

    def test_interpolate_inference_with_head(self):
        model = TvpForVideoGrounding.from_pretrained("Jiqing/tiny-random-tvp").to(torch_device)

        image_processor = self.default_image_processor
        image = prepare_img()  # 480X640
        encoding = image_processor(
            images=image, return_tensors="pt", do_resize=False, do_pad=False, do_center_crop=False
        )
        input_ids = torch.tensor([[1, 2]])
        attention_mask = torch.tensor([[1, 1]])
        encoding.update({"input_ids": input_ids, "attention_mask": attention_mask})
        encoding.to(torch_device)

        with torch.no_grad():
            outputs = model(**encoding, interpolate_pos_encoding=True, output_hidden_states=True)

        expected_shape = torch.Size((1, 1212, 128))
        assert outputs.hidden_states[-1].shape == expected_shape