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

Function get_2d_sincos_pos_embed

PATH/core/models/backbones/vit.py:699–715  ·  view source on GitHub ↗

grid_size: int of the grid height and width return: pos_embed: [grid_size*grid_size, embed_dim] or [1+grid_size*grid_size, embed_dim] (w/ or w/o cls_token)

(embed_dim, grid_size, cls_token=False)

Source from the content-addressed store, hash-verified

697# MoCo v3: https://github.com/facebookresearch/moco-v3
698# --------------------------------------------------------
699def get_2d_sincos_pos_embed(embed_dim, grid_size, cls_token=False):
700 """
701 grid_size: int of the grid height and width
702 return:
703 pos_embed: [grid_size*grid_size, embed_dim] or [1+grid_size*grid_size, embed_dim] (w/ or w/o cls_token)
704 """
705 grid_size = to_2tuple(grid_size)
706 grid_h = np.arange(grid_size[0], dtype=np.float32)
707 grid_w = np.arange(grid_size[1], dtype=np.float32)
708 grid = np.meshgrid(grid_w, grid_h) # here w goes first
709 grid = np.stack(grid, axis=0)
710
711 grid = grid.reshape([2, 1, grid_size[0], grid_size[1]])
712 pos_embed = get_2d_sincos_pos_embed_from_grid(embed_dim, grid)
713 if cls_token:
714 pos_embed = np.concatenate([np.zeros([1, embed_dim]), pos_embed], axis=0)
715 return pos_embed
716
717
718def get_2d_sincos_pos_embed_from_grid(embed_dim, grid):

Callers 1

_reset_parametersMethod · 0.50

Calls 2

stackMethod · 0.80

Tested by

no test coverage detected