-
Notifications
You must be signed in to change notification settings - Fork 5
/
slot_attn.py
157 lines (130 loc) · 6.84 KB
/
slot_attn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
''' Based on SLATE and BOQSA libraries:
https://github.com/singhgautam/slate/blob/master/slot_attn.py
https://github.com/YuLiu-LY/BO-QSA/blob/main/models/slot_attn.py
'''
from utils_spot import *
from timm.models.layers import DropPath
class SlotAttention(nn.Module):
def __init__(
self,
num_iter,
input_size,
slot_size,
mlp_size,
truncate,
heads,
epsilon=1e-8,
drop_path=0,
):
super().__init__()
self.num_iter = num_iter
self.input_size = input_size
self.slot_size = slot_size
self.epsilon = epsilon
self.truncate = truncate
self.num_heads = heads
self.norm_inputs = nn.LayerNorm(input_size)
self.norm_slots = nn.LayerNorm(slot_size)
self.norm_mlp = nn.LayerNorm(slot_size)
self.project_q = linear(slot_size, slot_size, bias=False)
self.project_k = linear(input_size, slot_size, bias=False)
self.project_v = linear(input_size, slot_size, bias=False)
self.gru = gru_cell(slot_size, slot_size)
self.mlp = nn.Sequential(
linear(slot_size, mlp_size, weight_init='kaiming'),
nn.ReLU(),
linear(mlp_size, slot_size),
)
self.drop_path = DropPath(drop_path) if drop_path > 0 else nn.Identity()
print(self.truncate)
assert self.truncate in ['bi-level', 'fixed-point', 'none']
def forward(self, inputs, slots_init):
# `inputs` has shape [batch_size, num_inputs, input_size].
# `slots` has shape [batch_size, num_slots, slot_size].
slots = slots_init
B, N_kv, D_inp = inputs.size()
B, N_q, D_slot = slots.size()
inputs = self.norm_inputs(inputs)
k = self.project_k(inputs).view(B, N_kv, self.num_heads, -1).transpose(1, 2) # Shape: [batch_size, num_heads, num_inputs, slot_size // num_heads].
v = self.project_v(inputs).view(B, N_kv, self.num_heads, -1).transpose(1, 2) # Shape: [batch_size, num_heads, num_inputs, slot_size // num_heads].
k = ((self.slot_size // self.num_heads) ** (-0.5)) * k
# Multiple rounds of attention.
for i in range(self.num_iter):
if i == self.num_iter - 1:
if self.truncate == 'bi-level':
slots = slots.detach() + slots_init - slots_init.detach()
elif self.truncate == 'fixed-point':
slots = slots.detach()
slots_prev = slots
slots = self.norm_slots(slots)
# Attention.
q = self.project_q(slots).view(B, N_q, self.num_heads, -1).transpose(1, 2) # Shape: [batch_size, num_heads, num_slots, slot_size // num_heads].
attn_logits = torch.matmul(k, q.transpose(-1, -2)) # Shape: [batch_size, num_heads, num_inputs, num_slots].
attn = F.softmax(
attn_logits.transpose(1, 2).reshape(B, N_kv, self.num_heads * N_q)
, dim=-1).view(B, N_kv, self.num_heads, N_q).transpose(1, 2) # Shape: [batch_size, num_heads, num_inputs, num_slots].
attn_vis = attn.sum(1) # Shape: [batch_size, num_inputs, num_slots].
# Weighted mean.
attn = attn + self.epsilon
attn = attn / torch.sum(attn, dim=-2, keepdim=True)
updates = torch.matmul(attn.transpose(-1, -2), v) # Shape: [batch_size, num_heads, num_slots, slot_size // num_heads].
updates = updates.transpose(1, 2).reshape(B, N_q, -1) # Shape: [batch_size, num_slots, slot_size].
# Slot update.
slots = self.gru(updates.view(-1, self.slot_size),
slots_prev.view(-1, self.slot_size))
slots = slots.view(-1, N_q, self.slot_size)
slots = slots + self.mlp(self.norm_mlp(slots))
return slots, attn_vis, attn_logits
class SlotAttentionEncoder(nn.Module):
def __init__(self, num_iterations, num_slots,
input_channels, slot_size, mlp_hidden_size, pos_channels, truncate='bi-level', init_method='embedding', num_heads = 1, drop_path = 0.0):
super().__init__()
self.num_iterations = num_iterations
self.num_slots = num_slots
self.input_channels = input_channels
self.slot_size = slot_size
self.mlp_hidden_size = mlp_hidden_size
self.pos_channels = pos_channels
self.init_method = init_method
self.layer_norm = nn.LayerNorm(input_channels)
self.mlp = nn.Sequential(
linear(input_channels, input_channels, weight_init='kaiming'),
nn.ReLU(),
linear(input_channels, input_channels))
assert init_method in ['shared_gaussian', 'embedding']
if init_method == 'shared_gaussian':
# Parameters for Gaussian init (shared by all slots).
self.slot_mu = nn.Parameter(torch.zeros(1, 1, slot_size))
self.slot_log_sigma = nn.Parameter(torch.zeros(1, 1, slot_size))
nn.init.xavier_uniform_(self.slot_mu)
nn.init.xavier_uniform_(self.slot_log_sigma)
elif init_method == 'embedding':
self.slots_init = nn.Embedding(num_slots, slot_size)
nn.init.xavier_uniform_(self.slots_init.weight)
else:
raise NotImplementedError
self.slot_attention = SlotAttention(
num_iterations,
input_channels, slot_size, mlp_hidden_size, truncate, num_heads, drop_path=drop_path)
def forward(self, x):
# `image` has shape: [batch_size, img_channels, img_height, img_width].
# `encoder_grid` has shape: [batch_size, pos_channels, enc_height, enc_width].
B, *_ = x.size()
dtype = x.dtype
device = x.device
x = self.mlp(self.layer_norm(x))
# `x` has shape: [batch_size, enc_height * enc_width, cnn_hidden_size].
# Slot Attention module.
init_slots = self.slots_initialization(B, dtype, device)
slots, attn, attn_logits = self.slot_attention(x, init_slots)
# `slots` has shape: [batch_size, num_slots, slot_size].
# `attn` has shape: [batch_size, enc_height * enc_width, num_slots].
return slots, attn, init_slots, attn_logits
def slots_initialization(self, B, dtype, device):
# The first frame, initialize slots.
if self.init_method == 'shared_gaussian':
slots_init = torch.empty((B, self.num_slots, self.slot_size), dtype=dtype, device=device).normal_()
slots_init = self.slot_mu + torch.exp(self.slot_log_sigma) * slots_init
elif self.init_method == 'embedding':
slots_init = self.slots_init.weight.expand(B, -1, -1).contiguous()
return slots_init