File size: 21,706 Bytes
9e15541
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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
126
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
import os
import pathlib
from typing import Dict, Tuple, List

import timm
import torch
import torch.nn as nn
import torch.nn.functional as F
from timm.models.vision_transformer import VisionTransformer
from torch import Tensor
from torch.fx import GraphModule
from torchvision.models.feature_extraction import create_feature_extractor

# from ups.utils import normalize

__all__: Tuple[str, ...] = (
    "dino_small",
    "dino_base",
    "dinov2_small",
    "dinov2_base",
    "dino_reg_small",
    "dino_reg_base",
    "i_jepa_huge",
    "mae_base",
    "self_patch_small",
    "synclr_base",
    "mocov3_base",
    "msn_base",
    "vmae_large",
)


def _disable_fused_attention(model: nn.Module) -> None:
    """Function disables the use of fused attention in Timm's ViT models. (Don't use anywhere else!)

    Args:
        model (nn.Module): Timm ViT model.
    """
    # Get ViT depth
    depth: int = len(model.blocks)  # type: ignore
    # Disable fused attention for last block
    for name, module in model.named_modules():
        if "Attention" in str(type(module)):
            if str(depth - 1) in name:
                module.fused_attn = False  # type: ignore


def _load_vit(name: str, image_size: Tuple[int, int] = (224, 224), depth: int = 12) -> nn.Module:
    """Function to load ViT models from Timm.

    Args:
        name (str): Timm name of the model.
        depth (int): Depth of the model.

    Returns:
        model (nn.Module): ViT model as a nn.Module.
    """
    # Load model
    model: nn.Module = timm.create_model(name, pretrained=True, img_size=image_size, num_classes=0)
    # Force not to use fused attention to access attention maps
    # _disable_fused_attention(model)
    return model


def _interpolate_positional_embeddings(
    positional_embeddings: Tensor,
    original_image_size: Tuple[int, int],
    target_image_size: Tuple[int, int],
    patch_size: int,
    num_additional_tokens: int = 1,
) -> Tensor:
    """Function interpolates positional embeddings to a different image size.

    Args:
        positional_embeddings (Tensor): Positional embeddings of the sape [1, N, C].
        original_image_size (Tuple[int, int]): Original image size as a tuple.
        target_image_size (Tuple[int, int]): Target image size as a tuple.
        patch_size (int): Utilize patch size.
        num_additional_tokens (int): Number of additional tokens used. Default 1 (class token).

    Returns:
        positional_embeddings_interpolated (Tensor): Interpolated positional embeddings [1, N_new, C].
    """
    # Get positional embeddings for image
    if num_additional_tokens > 0:
        positional_embeddings_add_tokens: Tensor = positional_embeddings[:, :num_additional_tokens]
        positional_embeddings_image: Tensor = positional_embeddings[:, num_additional_tokens:]
    else:
        positional_embeddings_image = positional_embeddings
    # Reshape embeddings to 2D
    positional_embeddings_image = positional_embeddings_image.view(
        1, original_image_size[0] // patch_size, original_image_size[1] // patch_size, -1
    )
    # Interpolate positional embeddings
    positional_embeddings_image = F.interpolate(
        positional_embeddings_image.permute(0, 3, 1, 2),
        size=(target_image_size[0] // patch_size, target_image_size[1] // patch_size),
        mode="bicubic",
        align_corners=False,
        antialias=False,
    ).permute(0, 2, 3, 1)
    # Stack positional embeddings again
    if num_additional_tokens > 0:
        positional_embeddings_interpolated: Tensor = torch.cat(
            (positional_embeddings_add_tokens, positional_embeddings_image.flatten(1, 2)), dim=1
        )
    else:
        positional_embeddings_interpolated = positional_embeddings_image.flatten(1, 2)
    return positional_embeddings_interpolated


class _ViT(nn.Module):
    """This class wraps Timm's ViT's and always ensures eval mode."""

    def __init__(
        self,
        vit: nn.Module,
        patch_size: int,
        registers: bool = False,
        class_token: bool = False,
        intermediate_features: List[int] = None,
    ) -> None:
        """Constructor method.

        Args:
            vit (nn.Module): Timm ViT model.
            patch_size (int): Patch size utilized.
            registers (bool): Set to true if registers are use. Default False.
            class_token (bool): Set true if class token is use. Default False.
        """
        # Call super constructor
        super(_ViT, self).__init__()
        # Save parameter
        self.patch_size: int = patch_size
        self.registers: bool = registers
        self.class_token: bool = class_token
        # Get ViT depth
        depth: int = len(vit.blocks)  # type: ignore

        return_nodes = {
            #f"blocks.{depth - 1}.attn.softmax": "attention_maps",
            "norm": "features_normalized",
            f"blocks.{depth - 1}.attn.getitem_4": "key_features",
        }
        if intermediate_features is not None:
            for idx, feat in enumerate(intermediate_features):
                return_nodes[f"blocks.{feat}"] = f"intermediate_features.{idx}"

        # Make FX graph module for feature extraction
        self.vit: GraphModule = create_feature_extractor(vit, return_nodes)

    def forward(self, images: Tensor) -> Dict[str, Tensor]:
        """Forward pass.

        Notes:
            attention_maps have the shape of [B, num heads, N, N].
            features have the shape of [B, N, C]
            Class token and register tokens omitted!

        Args:
            images (Tensor): Images (w/ pix. range of [0, 1]) of the shape [B, 3, H, W].

        Returns:
            output_dict (Dict[str, Tensor]): Dict of features ("attention_maps" and "features").
        """
        # Ensure model is in eval mode
        self.vit.eval()
        # Normalize images
        images_normalized: Tensor = images  # normalize(images)
        # Perform forward pass
        output_dict: Dict[str, Tensor] = self.vit(images_normalized)
        # Omit class token (and registers) from attention maps and features
        if self.registers:
            output_dict["features_normalized"] = output_dict["features_normalized"][:, 5:]
            #output_dict["attention_maps"] = output_dict["attention_maps"][..., 5:, 5:]
            output_dict["key_features"] = output_dict["key_features"][:, :, 5:]
            for output_key in output_dict:
                if output_key.startswith('intermediate_features'):
                    output_dict[output_key] = output_dict[output_key][:, 5:]
        elif self.class_token:
            output_dict["features_normalized"] = output_dict["features_normalized"][:, 1:]
            #output_dict["attention_maps"] = output_dict["attention_maps"][..., 1:, 1:]
            output_dict["key_features"] = output_dict["key_features"][:, :, 1:]
            for output_key in output_dict:
                if output_key.startswith('intermediate_features'):
                    output_dict[output_key] = output_dict[output_key][:, 1:]
        # Normalize features
        output_dict["features_normalized"] = F.normalize(output_dict["features_normalized"], p=2, dim=2)  # [B, N, C]
        return output_dict


def mae_base(image_size: Tuple[int, int] = (224, 224)) -> nn.Module:
    """Builds the pre-trained MAE base model (patch size is 16 x 16).

    Args:
        image_size (Tuple[int, int]): Image size to be used. Default is (224, 224).

    Returns:
        model (nn.Module): ViT MAE model as a nn.Module.
    """
    return _ViT(
        _load_vit(name="vit_base_patch16_224.mae", image_size=image_size),
        patch_size=16,
        class_token=True,
    )


def vmae_large(image_size: Tuple[int, int] = (224, 224)) -> nn.Module:
    """Builds the pre-trained video MAE large model (patch size is 16 x 16).

    Args:
        image_size (Tuple[int, int]): Image size to be used. Default is (224, 224).

    Returns:
        model (nn.Module): ViT MAE model as a nn.Module.
    """
    # Init model
    model = VisionTransformer(
        img_size=image_size, patch_size=16, num_classes=0, qkv_bias=True, embed_dim=1024, depth=24, num_heads=16
    )
    # Force not to use fused attention to access attention maps
    _disable_fused_attention(model)
    # Load and adapt checkpoint
    checkpoint: Dict[str, Tensor] = torch.load(
        os.path.join(pathlib.Path(__file__).parent.resolve(), "checkpoints/mae_pretrain_vit_large_k700.pth"),
        map_location="cpu",
    )["model_state"]
    checkpoint["pos_embed"] = checkpoint["pos_embed_spatial"] + checkpoint["pos_embed_temporal"].mean(
        dim=1, keepdim=True
    )
    checkpoint["pos_embed"] = torch.cat((checkpoint["pos_embed_class"], checkpoint["pos_embed"]), dim=1)
    checkpoint["patch_embed.proj.weight"] = checkpoint["patch_embed.proj.weight"][:, :, 0]
    for layer in range(24):
        checkpoint[f"blocks.{layer}.attn.qkv.weight"] = torch.cat(
            (
                checkpoint[f"blocks.{layer}.attn.q.weight"],
                checkpoint[f"blocks.{layer}.attn.k.weight"],
                checkpoint[f"blocks.{layer}.attn.v.weight"],
            ),
            dim=0,
        )
        checkpoint[f"blocks.{layer}.attn.qkv.bias"] = torch.cat(
            (
                checkpoint[f"blocks.{layer}.attn.q.bias"],
                checkpoint[f"blocks.{layer}.attn.k.bias"],
                checkpoint[f"blocks.{layer}.attn.v.bias"],
            ),
            dim=0,
        )
    checkpoint = {key: value for key, value in checkpoint.items() if key in model.state_dict().keys()}
    # Interpolated positional embeddings
    checkpoint["pos_embed"] = _interpolate_positional_embeddings(
        checkpoint["pos_embed"],
        original_image_size=(224, 224),
        target_image_size=image_size,
        patch_size=16,
        num_additional_tokens=1,
    )
    # Load checkpoint
    model.load_state_dict(checkpoint)
    return _ViT(model, patch_size=16, class_token=True)


def dino_small(image_size: Tuple[int, int] = (224, 224), intermediate_features: List[int] = None) -> nn.Module:
    """Builds the pre-trained ViT DINO small model (patch size is 16 x 16).

    Args:
        image_size (Tuple[int, int]): Image size to be used. Default is (224, 224).

    Returns:
        model (nn.Module): ViT Dino model as a nn.Module.
    """
    return _ViT(
        _load_vit(name="vit_small_patch16_224.dino", image_size=image_size),
        patch_size=16,
        class_token=True,
        intermediate_features=intermediate_features,
    )


def dino_small8(image_size: Tuple[int, int] = (224, 224), intermediate_features: List[int] = None) -> nn.Module:
    """Builds the pre-trained ViT DINO small model (patch size is 8 x 8).

    Args:
        image_size (Tuple[int, int]): Image size to be used. Default is (224, 224).

    Returns:
        model (nn.Module): ViT Dino model as a nn.Module.
    """
    return _ViT(
        _load_vit(name="vit_small_patch8_224.dino", image_size=image_size),
        patch_size=8,
        class_token=True,
        intermediate_features=intermediate_features,
    )


def dino_base(image_size: Tuple[int, int] = (224, 224), intermediate_features: List[int] = None) -> nn.Module:
    """Builds the pre-trained ViT DINO base model (patch size is 16 x 16).

    Args:
        image_size (Tuple[int, int]): Image size to be used. Default is (224, 224).

    Returns:
        model (nn.Module): ViT Dino model as a nn.Module.
    """
    return _ViT(
        _load_vit(name="vit_base_patch16_224.dino", image_size=image_size),
        patch_size=16,
        class_token=True,
        intermediate_features=intermediate_features,
    )


def dino_base8(image_size: Tuple[int, int] = (224, 224), intermediate_features: List[int] = None) -> nn.Module:
    """Builds the pre-trained ViT DINO base model (patch size is 16 x 16).

    Args:
        image_size (Tuple[int, int]): Image size to be used. Default is (224, 224).

    Returns:
        model (nn.Module): ViT Dino model as a nn.Module.
    """
    return _ViT(
        _load_vit(name="vit_base_patch8_224.dino", image_size=image_size),
        patch_size=8,
        class_token=True,
        intermediate_features=intermediate_features,
    )


def dinov2_small(image_size: Tuple[int, int] = (224, 224), intermediate_features: List[int] = None) -> nn.Module:
    """Builds the pre-trained ViT DINO V2 small model (patch size is 14 x 14).

    Args:
        image_size (Tuple[int, int]): Image size to be used. Default is (224, 224).
        intermediate_features (List[int]): Index of intermediate layer features to return.

    Returns:
        model (nn.Module): ViT Dino model as a nn.Module.
    """
    return _ViT(
        _load_vit(name="vit_small_patch14_dinov2.lvd142m", image_size=image_size),
        patch_size=14,
        class_token=True,
        intermediate_features=intermediate_features,
    )


def dinov2_base(image_size: Tuple[int, int] = (224, 224), intermediate_features: List[int] = None) -> nn.Module:
    """Builds the pre-trained ViT DINO V2 base model (patch size is 14 x 14).

    Args:
        image_size (Tuple[int, int]): Image size to be used. Default is (224, 224).
        intermediate_features (List[int]): Index of intermediate layer features to return.

    Returns:
        model (nn.Module): ViT Dino model as a nn.Module.
    """
    return _ViT(
        _load_vit(name="vit_base_patch14_dinov2.lvd142m", image_size=image_size),
        patch_size=14,
        class_token=True,
        intermediate_features=intermediate_features,
    )


def dino_reg_small(image_size: Tuple[int, int] = (224, 224), intermediate_features: List[int] = None) -> nn.Module:
    """Builds the pre-trained ViT DINO (w/ registers) small model (patch size is 14 x 14).

    Args:
        image_size (Tuple[int, int]): Image size to be used. Default is (224, 224).
        intermediate_features (List[int]): Index of intermediate layer features to return.

    Returns:
        model (nn.Module): ViT Dino model as a nn.Module.
    """
    return _ViT(
        _load_vit(name="vit_small_patch14_reg4_dinov2.lvd142m", image_size=image_size),
        patch_size=14,
        registers=True,
        intermediate_features=intermediate_features,
    )


def dino_reg_base(image_size: Tuple[int, int] = (224, 224), intermediate_features: List[int] = None) -> nn.Module:
    """Builds the pre-trained ViT DINO (w/ registers) base model (patch size is 14 x 14).

    Args:
        image_size (Tuple[int, int]): Image size to be used. Default is (224, 224).
        intermediate_features (List[int]): Index of intermediate layer features to return.

    Returns:
        model (nn.Module): ViT Dino model as a nn.Module.
    """
    return _ViT(
        _load_vit(name="vit_base_patch14_reg4_dinov2.lvd142m", image_size=image_size),
        patch_size=14,
        registers=True,
        intermediate_features=intermediate_features,

    )


def synclr_base(image_size: Tuple[int, int] = (224, 224)) -> nn.Module:
    """Builds the pre-trained SynCLR ViT base model (patch size is 16 x 16).

    Args:
        image_size (Tuple[int, int]): Image size to be used. Default is (224, 224).

    Returns:
        model (nn.Module): SynCLR ViT model as a nn.Module
    """
    # Init model
    model = _load_vit(name="vit_base_patch16_224", image_size=image_size)
    # Load and adapt checkpoint
    checkpoint: Dict[str, Tensor] = torch.load(
        os.path.join(pathlib.Path(__file__).parent.resolve(), "checkpoints/synclr_vit_b_16.pth")
    )["model"]
    checkpoint = {key.replace("module.visual.", ""): value for key, value in checkpoint.items()}
    # Interpolated positional embeddings
    if image_size != (224, 224):
        checkpoint["pos_embed"] = _interpolate_positional_embeddings(
            checkpoint["pos_embed"],
            original_image_size=(224, 224),
            target_image_size=image_size,
            patch_size=16,
            num_additional_tokens=1,
        )
    # Load checkpoint
    model.load_state_dict(checkpoint)
    return _ViT(model, patch_size=16, class_token=True)


def mocov3_base(image_size: Tuple[int, int] = (224, 224)) -> nn.Module:
    """Builds the pre-trained MoCo-V3 ViT base model (patch size is 16 x 16).

    Args:
        image_size (Tuple[int, int]): Image size to be used. Default is (224, 224).

    Returns:
        model (nn.Module): MoCo-V3 ViT model as a nn.Module
    """
    # Init model
    model = _load_vit(name="vit_base_patch16_224", image_size=image_size)
    # Load and adapt checkpoint
    checkpoint: Dict[str, Tensor] = torch.load(
        os.path.join(pathlib.Path(__file__).parent.resolve(), "checkpoints/vit-b-300ep.pth.tar")
    )["state_dict"]
    checkpoint = {
        key.replace("module.momentum_encoder.", ""): value
        for key, value in checkpoint.items()
        if ("module.momentum_encoder." in key) and ("head." not in key)
    }
    # Interpolated positional embeddings
    if image_size != (224, 224):
        checkpoint["pos_embed"] = _interpolate_positional_embeddings(
            checkpoint["pos_embed"],
            original_image_size=(224, 224),
            target_image_size=image_size,
            patch_size=16,
            num_additional_tokens=1,
        )
    # Load checkpoint
    model.load_state_dict(checkpoint)
    return _ViT(model, patch_size=16, class_token=True)


def msn_base(image_size: Tuple[int, int] = (224, 224)) -> nn.Module:
    """Builds the pre-trained MSN ViT base model (patch size is 16 x 16).

    Args:
        image_size (Tuple[int, int]): Image size to be used. Default is (224, 224).

    Returns:
        model (nn.Module): MSN ViT model as a nn.Module
    """
    # Init model
    model = _load_vit(name="vit_base_patch16_224", image_size=image_size)
    # Load and adapt checkpoint
    checkpoint: Dict[str, Tensor] = torch.load(
        os.path.join(pathlib.Path(__file__).parent.resolve(), "checkpoints/vitb16_600ep.pth.tar")
    )["target_encoder"]
    checkpoint = {
        key.replace("module.", ""): value
        for key, value in checkpoint.items()
        if key.replace("module.", "") in model.state_dict().keys()
    }
    # Interpolated positional embeddings
    if image_size != (224, 224):
        checkpoint["pos_embed"] = _interpolate_positional_embeddings(
            checkpoint["pos_embed"],
            original_image_size=(224, 224),
            target_image_size=image_size,
            patch_size=16,
            num_additional_tokens=1,
        )
    # Load checkpoint
    model.load_state_dict(checkpoint)
    return _ViT(model, patch_size=16, class_token=True)


def self_patch_small(image_size: Tuple[int, int] = (224, 224)) -> nn.Module:
    """Builds the pre-trained Self-Patch ViT small model (patch size is 16 x 16).

    Args:
        image_size (Tuple[int, int]): Image size to be used. Default is (224, 224).

    Returns:
        model (nn.Module): Self-Patch ViT model as a nn.Module
    """
    # Init model
    model = VisionTransformer(
        img_size=image_size,
        patch_size=16,
        num_classes=0,
        class_token=False,
        qkv_bias=True,
        global_pool="avg",
        embed_dim=384,
        depth=12,
        num_heads=6,
    )
    # Force not to use fused attention to access attention maps
    _disable_fused_attention(model)
    # Load and adapt checkpoint
    checkpoint: Dict[str, Tensor] = torch.load(
        os.path.join(pathlib.Path(__file__).parent.resolve(), "checkpoints/dino_selfpatch.pth")
    )
    checkpoint = {
        key.replace("module.", ""): value for key, value in checkpoint.items() if key in model.state_dict().keys()
    }
    # Interpolated positional embeddings
    if image_size != (224, 224):
        checkpoint["pos_embed"] = _interpolate_positional_embeddings(
            checkpoint["pos_embed"],
            original_image_size=(224, 224),
            target_image_size=image_size,
            patch_size=16,
            num_additional_tokens=0,
        )
    # Load checkpoint
    model.load_state_dict(checkpoint, strict=False)
    return _ViT(model, patch_size=16, class_token=False, registers=False)


def i_jepa_huge(image_size: Tuple[int, int] = (224, 224)) -> nn.Module:
    """Builds the pre-trained I-JEPA ViT huge model (patch size is 14 x 14).

    Notes:
        ViT huge is very large...

    Args:
        image_size (Tuple[int, int]): Image size to be used. Default is (224, 224).

    Returns:
        model (nn.Module): SynCLR ViT model as a nn.Module
    """
    # Init model
    model = VisionTransformer(
        img_size=image_size,
        patch_size=14,
        num_classes=0,
        class_token=False,
        global_pool="avg",
        qkv_bias=True,
        embed_dim=1280,
        depth=32,
        num_heads=16,
    )
    # Force not to use fused attention to access attention maps
    _disable_fused_attention(model)
    # Load and adapt checkpoint
    checkpoint: Dict[str, Tensor] = torch.load(
        os.path.join(pathlib.Path(__file__).parent.resolve(), "checkpoints/IN22k-vit.h.14-900e.pth.tar"),
        map_location="cpu",
    )["encoder"]
    checkpoint = {key.replace("module.", ""): value for key, value in checkpoint.items()}
    # Interpolated positional embeddings
    if image_size != (224, 224):
        checkpoint["pos_embed"] = _interpolate_positional_embeddings(
            checkpoint["pos_embed"],
            original_image_size=(224, 224),
            target_image_size=image_size,
            patch_size=14,
            num_additional_tokens=0,
        )
    # Load checkpoint
    model.load_state_dict(checkpoint, strict=False)
    return _ViT(model, patch_size=14, class_token=False, registers=False)