MCPcopy Create free account
hub / github.com/OpenGVLab/HumanBench / interpolate_pos_embed

Function interpolate_pos_embed

PATH/core/models/backbones/vit.py:679–690  ·  view source on GitHub ↗
(pos_embed_checkpoint, patch_shape, num_extra_tokens)

Source from the content-addressed store, hash-verified

677
678
679def interpolate_pos_embed(pos_embed_checkpoint, patch_shape, num_extra_tokens):
680 embedding_size = pos_embed_checkpoint.shape[-1]
681 orig_size = to_2tuple(int((pos_embed_checkpoint.shape[-2] - num_extra_tokens) ** 0.5))
682 # class_token and dist_token are kept unchanged
683 if orig_size != patch_shape:
684 print(f"[rank {dist.get_rank()}] Position interpolate from {orig_size} to {patch_shape}")
685 # only the position tokens are interpolated
686 pos_tokens = pos_embed_checkpoint[:, num_extra_tokens:]
687 pos_tokens = pos_tokens.reshape(-1, orig_size[0], orig_size[1], embedding_size).permute(0, 3, 1, 2)
688 pos_tokens = torch.nn.functional.interpolate(pos_tokens, size=patch_shape, mode='bicubic', align_corners=False)
689 new_pos_embed = pos_tokens.permute(0, 2, 3, 1).flatten(1, 2) # (b, h*w, c)
690 return new_pos_embed
691
692
693# --------------------------------------------------------

Callers 1

load_checkpointFunction · 0.70

Calls 1

permuteMethod · 0.80

Tested by

no test coverage detected