Spaces:
Running
Running
Upload models/position_encoding.py
Browse files- models/position_encoding.py +86 -0
models/position_encoding.py
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved
|
2 |
+
"""
|
3 |
+
Various positional encodings for the transformer.
|
4 |
+
"""
|
5 |
+
import math
|
6 |
+
import torch
|
7 |
+
from torch import nn
|
8 |
+
|
9 |
+
|
10 |
+
class PositionEmbeddingSine(nn.Module):
|
11 |
+
"""
|
12 |
+
This is a more standard version of the position embedding, very similar to the one
|
13 |
+
used by the Attention is all you need paper, generalized to work on images.
|
14 |
+
"""
|
15 |
+
def __init__(self, num_pos_feats=64, temperature=10000, normalize=False, scale=None):
|
16 |
+
super().__init__()
|
17 |
+
self.num_pos_feats = num_pos_feats
|
18 |
+
self.temperature = temperature
|
19 |
+
self.normalize = normalize
|
20 |
+
if scale is not None and normalize is False:
|
21 |
+
raise ValueError("normalize should be True if scale is passed")
|
22 |
+
if scale is None:
|
23 |
+
scale = 2 * math.pi
|
24 |
+
self.scale = scale
|
25 |
+
|
26 |
+
def forward(self, token_tensors):
|
27 |
+
## input: (B,C,H,W)
|
28 |
+
x = token_tensors
|
29 |
+
h, w = x.shape[-2:]
|
30 |
+
identity_map= torch.ones((h,w), device=x.device)
|
31 |
+
y_embed = identity_map.cumsum(0, dtype=torch.float32)
|
32 |
+
x_embed = identity_map.cumsum(1, dtype=torch.float32)
|
33 |
+
if self.normalize:
|
34 |
+
eps = 1e-6
|
35 |
+
y_embed = y_embed / (y_embed[-1:, :] + eps) * self.scale
|
36 |
+
x_embed = x_embed / (x_embed[:, -1:] + eps) * self.scale
|
37 |
+
|
38 |
+
dim_t = torch.arange(self.num_pos_feats, dtype=torch.float32, device=x.device)
|
39 |
+
dim_t = self.temperature ** (2 * (dim_t // 2) / self.num_pos_feats)
|
40 |
+
|
41 |
+
pos_x = x_embed[:, :, None] / dim_t
|
42 |
+
pos_y = y_embed[:, :, None] / dim_t
|
43 |
+
pos_x = torch.stack((pos_x[:, :, 0::2].sin(), pos_x[:, :, 1::2].cos()), dim=3).flatten(2)
|
44 |
+
pos_y = torch.stack((pos_y[:, :, 0::2].sin(), pos_y[:, :, 1::2].cos()), dim=3).flatten(2)
|
45 |
+
pos = torch.cat((pos_y, pos_x), dim=2).permute(2, 0, 1)
|
46 |
+
batch_pos = pos.unsqueeze(0).repeat(x.shape[0], 1, 1, 1)
|
47 |
+
return batch_pos
|
48 |
+
|
49 |
+
|
50 |
+
class PositionEmbeddingLearned(nn.Module):
|
51 |
+
"""
|
52 |
+
Absolute pos embedding, learned.
|
53 |
+
"""
|
54 |
+
def __init__(self, n_pos_x=16, n_pos_y=16, num_pos_feats=64):
|
55 |
+
super().__init__()
|
56 |
+
self.row_embed = nn.Embedding(n_pos_y, num_pos_feats)
|
57 |
+
self.col_embed = nn.Embedding(n_pos_x, num_pos_feats)
|
58 |
+
self.reset_parameters()
|
59 |
+
|
60 |
+
def reset_parameters(self):
|
61 |
+
nn.init.uniform_(self.row_embed.weight)
|
62 |
+
nn.init.uniform_(self.col_embed.weight)
|
63 |
+
|
64 |
+
def forward(self, token_tensors):
|
65 |
+
## input: (B,C,H,W)
|
66 |
+
x = token_tensors
|
67 |
+
h, w = x.shape[-2:]
|
68 |
+
i = torch.arange(w, device=x.device)
|
69 |
+
j = torch.arange(h, device=x.device)
|
70 |
+
x_emb = self.col_embed(i)
|
71 |
+
y_emb = self.row_embed(j)
|
72 |
+
pos = torch.cat([
|
73 |
+
x_emb.unsqueeze(0).repeat(h, 1, 1),
|
74 |
+
y_emb.unsqueeze(1).repeat(1, w, 1),
|
75 |
+
], dim=-1).permute(2, 0, 1)
|
76 |
+
batch_pos = pos.unsqueeze(0).repeat(x.shape[0], 1, 1, 1)
|
77 |
+
return batch_pos
|
78 |
+
|
79 |
+
|
80 |
+
def build_position_encoding(num_pos_feats=64, n_pos_x=16, n_pos_y=16, is_learned=False):
|
81 |
+
if is_learned:
|
82 |
+
position_embedding = PositionEmbeddingLearned(n_pos_x, n_pos_y, num_pos_feats)
|
83 |
+
else:
|
84 |
+
position_embedding = PositionEmbeddingSine(num_pos_feats, normalize=True)
|
85 |
+
|
86 |
+
return position_embedding
|