Calculate absolute positional embeddings. If needed, resize embeddings and remove cls_token dimension for the original embeddings. Args: abs_pos (Tensor): absolute positional embeddings with (1, num_position, C). has_cls_token (bool): If true, has 1 embedding in abs_
(abs_pos, has_cls_token, hw)
| 863 | |
| 864 | |
| 865 | def get_abs_pos(abs_pos, has_cls_token, hw): |
| 866 | """ |
| 867 | Calculate absolute positional embeddings. If needed, resize embeddings and remove cls_token |
| 868 | dimension for the original embeddings. |
| 869 | Args: |
| 870 | abs_pos (Tensor): absolute positional embeddings with (1, num_position, C). |
| 871 | has_cls_token (bool): If true, has 1 embedding in abs_pos for cls token. |
| 872 | hw (Tuple): size of input image tokens. |
| 873 | Returns: |
| 874 | Absolute positional embeddings after processing with shape (1, H, W, C) |
| 875 | """ |
| 876 | h, w = hw |
| 877 | if has_cls_token: |
| 878 | cls_pos = abs_pos[:, 0].reshape(abs_pos.shape[0], 1, abs_pos.shape[-1]) |
| 879 | abs_pos = abs_pos[:, 1:] |
| 880 | xy_num = abs_pos.shape[1] |
| 881 | size = int(math.sqrt(xy_num)) |
| 882 | assert size * size == xy_num |
| 883 | |
| 884 | if size != h or size != w: |
| 885 | new_abs_pos = F.interpolate( |
| 886 | abs_pos.reshape(1, size, size, -1).permute(0, 3, 1, 2), |
| 887 | size=(h, w), |
| 888 | mode="bicubic", |
| 889 | align_corners=False, |
| 890 | ) |
| 891 | |
| 892 | if has_cls_token: |
| 893 | return torch.cat([cls_pos, new_abs_pos.permute(0, 2, 3, 1).reshape(1, h*w, -1)], dim=1) |
| 894 | else: |
| 895 | return new_abs_pos.permute(0, 2, 3, 1).reshape(1, h*w, -1) |
| 896 | else: |
| 897 | if has_cls_token: |
| 898 | return torch.cat([cls_pos, abs_pos.reshape(1, h*w, -1)], dim=1) |
| 899 | else: |
| 900 | return abs_pos.reshape(1, h*w, -1) |
no test coverage detected