MCPcopy Create free account
hub / github.com/OpenDriveLab/ReSim / get_3d_sincos_pos_embed

Function get_3d_sincos_pos_embed

sat/dit_video_concat.py:100–140  ·  view source on GitHub ↗

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

(
    embed_dim,
    grid_height,
    grid_width,
    t_size,
    cls_token=False,
    height_interpolation=1.0,
    width_interpolation=1.0,
    time_interpolation=1.0,
)

Source from the content-addressed store, hash-verified

98
99
100def get_3d_sincos_pos_embed(
101 embed_dim,
102 grid_height,
103 grid_width,
104 t_size,
105 cls_token=False,
106 height_interpolation=1.0,
107 width_interpolation=1.0,
108 time_interpolation=1.0,
109):
110 """
111 grid_size: int of the grid height and width
112 t_size: int of the temporal size
113 return:
114 pos_embed: [t_size*grid_size*grid_size, embed_dim] or [1+t_size*grid_size*grid_size, embed_dim] (w/ or w/o cls_token)
115 """
116 assert embed_dim % 4 == 0
117 embed_dim_spatial = embed_dim // 4 * 3
118 embed_dim_temporal = embed_dim // 4
119
120 # spatial
121 grid_h = np.arange(grid_height, dtype=np.float32) / height_interpolation
122 grid_w = np.arange(grid_width, dtype=np.float32) / width_interpolation
123 grid = np.meshgrid(grid_w, grid_h) # here w goes first
124 grid = np.stack(grid, axis=0)
125
126 grid = grid.reshape([2, 1, grid_height, grid_width])
127 pos_embed_spatial = get_2d_sincos_pos_embed_from_grid(embed_dim_spatial, grid)
128
129 grid_t = np.arange(t_size, dtype=np.float32) / time_interpolation
130 pos_embed_temporal = get_1d_sincos_pos_embed_from_grid(embed_dim_temporal, grid_t)
131
132 # concate: [T, H, W] order
133 pos_embed_temporal = pos_embed_temporal[:, np.newaxis, :]
134 pos_embed_temporal = np.repeat(pos_embed_temporal, grid_height * grid_width, axis=1) # [T, H*W, D // 4]
135 pos_embed_spatial = pos_embed_spatial[np.newaxis, :, :]
136 pos_embed_spatial = np.repeat(pos_embed_spatial, t_size, axis=0) # [T, H*W, D // 4 * 3]
137
138 pos_embed = np.concatenate([pos_embed_temporal, pos_embed_spatial], axis=-1)
139
140 return pos_embed # [T, H*W, D]
141
142
143def get_2d_sincos_pos_embed(embed_dim, grid_height, grid_width, cls_token=False, extra_tokens=0):

Callers 1

reinitMethod · 0.85

Tested by

no test coverage detected