| 72 | |
| 73 | |
| 74 | def gen_sineembed_for_position(pos_tensor): |
| 75 | # n_query, bs, _ = pos_tensor.size() |
| 76 | # sineembed_tensor = torch.zeros(n_query, bs, 256) |
| 77 | scale = 2 * math.pi |
| 78 | dim_t = torch.arange(128, dtype=torch.float32, device=pos_tensor.device) |
| 79 | dim_t = 10000 ** (2 * (dim_t // 2) / 128) |
| 80 | x_embed = pos_tensor[:, :, 0] * scale |
| 81 | y_embed = pos_tensor[:, :, 1] * scale |
| 82 | pos_x = x_embed[:, :, None] / dim_t |
| 83 | pos_y = y_embed[:, :, None] / dim_t |
| 84 | pos_x = torch.stack((pos_x[:, :, 0::2].sin(), pos_x[:, :, 1::2].cos()), dim=3).flatten(2) |
| 85 | pos_y = torch.stack((pos_y[:, :, 0::2].sin(), pos_y[:, :, 1::2].cos()), dim=3).flatten(2) |
| 86 | if pos_tensor.size(-1) == 2: |
| 87 | pos = torch.cat((pos_y, pos_x), dim=2) |
| 88 | elif pos_tensor.size(-1) == 4: |
| 89 | w_embed = pos_tensor[:, :, 2] * scale |
| 90 | pos_w = w_embed[:, :, None] / dim_t |
| 91 | pos_w = torch.stack((pos_w[:, :, 0::2].sin(), pos_w[:, :, 1::2].cos()), dim=3).flatten(2) |
| 92 | |
| 93 | h_embed = pos_tensor[:, :, 3] * scale |
| 94 | pos_h = h_embed[:, :, None] / dim_t |
| 95 | pos_h = torch.stack((pos_h[:, :, 0::2].sin(), pos_h[:, :, 1::2].cos()), dim=3).flatten(2) |
| 96 | |
| 97 | pos = torch.cat((pos_y, pos_x, pos_w, pos_h), dim=2) |
| 98 | else: |
| 99 | raise ValueError("Unknown pos_tensor shape(-1):{}".format(pos_tensor.size(-1))) |
| 100 | return pos |
| 101 | |
| 102 | |
| 103 | def _get_activation_fn(activation): |