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

Function interpolate_pos_embed

PATH/core/models/backbones/vitdet.py:783–793  ·  view source on GitHub ↗
(pos_embed_checkpoint, patch_shape, num_extra_tokens)

Source from the content-addressed store, hash-verified

781
782
783def interpolate_pos_embed(pos_embed_checkpoint, patch_shape, num_extra_tokens):
784 embedding_size = pos_embed_checkpoint.shape[-1]
785 orig_size = to_2tuple(int((pos_embed_checkpoint.shape[-2] - num_extra_tokens) ** 0.5))
786 # class_token and dist_token are kept unchanged
787 print(f"[rank {dist.get_rank()}] Position interpolate from {orig_size} to {patch_shape}")
788 # only the position tokens are interpolated
789 pos_tokens = pos_embed_checkpoint[:, num_extra_tokens:] if pos_embed_checkpoint.size(0) == 1 else pos_embed_checkpoint[num_extra_tokens:]
790 pos_tokens = pos_tokens.reshape(-1, orig_size[0], orig_size[1], embedding_size).permute(0, 3, 1, 2)
791 pos_tokens = torch.nn.functional.interpolate(pos_tokens, size=patch_shape, mode='bicubic', align_corners=False)
792 new_pos_embed = pos_tokens.permute(0, 2, 3, 1).flatten(1, 2) # (b, h*w, c)
793 return new_pos_embed
794
795
796def interpolate_pos_embed_with_cls_token(pos_embed_checkpoint, patch_shape, num_extra_tokens):

Callers 1

load_checkpointFunction · 0.70

Calls 2

sizeMethod · 0.80
permuteMethod · 0.80

Tested by

no test coverage detected