-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmodel.py
209 lines (172 loc) · 7.78 KB
/
model.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import numpy as np
from torch import nn
import torch
import torch.nn.functional as F
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
class SlotAttention(nn.Module):
def __init__(self, num_slots, dim, iters = 3, eps = 1e-8, hidden_dim = 128):
super().__init__()
self.num_slots = num_slots
self.iters = iters
self.eps = eps
self.scale = dim ** -0.5
self.slots_mu = nn.Parameter(torch.randn(1, 1, dim))
self.slots_sigma = nn.Parameter(torch.rand(1, 1, dim))
self.to_q = nn.Linear(dim, dim)
self.to_k = nn.Linear(dim, dim)
self.to_v = nn.Linear(dim, dim)
self.gru = nn.GRUCell(dim, dim)
hidden_dim = max(dim, hidden_dim)
self.fc1 = nn.Linear(dim, hidden_dim)
self.fc2 = nn.Linear(hidden_dim, dim)
self.norm_input = nn.LayerNorm(dim)
self.norm_slots = nn.LayerNorm(dim)
self.norm_pre_ff = nn.LayerNorm(dim)
def forward(self, inputs, num_slots = None):
b, n, d = inputs.shape
n_s = num_slots if num_slots is not None else self.num_slots
mu = self.slots_mu.expand(b, n_s, -1)
sigma = self.slots_sigma.expand(b, n_s, -1)
slots = torch.normal(mu, sigma)
inputs = self.norm_input(inputs)
k, v = self.to_k(inputs), self.to_v(inputs)
for _ in range(self.iters):
slots_prev = slots
slots = self.norm_slots(slots)
q = self.to_q(slots)
dots = torch.einsum('bid,bjd->bij', q, k) * self.scale
attn = dots.softmax(dim=1) + self.eps
attn = attn / attn.sum(dim=-1, keepdim=True)
updates = torch.einsum('bjd,bij->bid', v, attn)
slots = self.gru(
updates.reshape(-1, d),
slots_prev.reshape(-1, d)
)
slots = slots.reshape(b, -1, d)
slots = slots + self.fc2(F.relu(self.fc1(self.norm_pre_ff(slots))))
return slots
def build_grid(resolution):
ranges = [np.linspace(0., 1., num=res) for res in resolution]
grid = np.meshgrid(*ranges, sparse=False, indexing="ij")
grid = np.stack(grid, axis=-1)
grid = np.reshape(grid, [resolution[0], resolution[1], -1])
grid = np.expand_dims(grid, axis=0)
grid = grid.astype(np.float32)
return torch.from_numpy(np.concatenate([grid, 1.0 - grid], axis=-1)).to(device)
"""Adds soft positional embedding with learnable projection."""
class SoftPositionEmbed(nn.Module):
def __init__(self, hidden_size, resolution):
"""Builds the soft position embedding layer.
Args:
hidden_size: Size of input feature dimension.
resolution: Tuple of integers specifying width and height of grid.
"""
super().__init__()
self.embedding = nn.Linear(4, hidden_size, bias=True)
self.grid = build_grid(resolution)
def forward(self, inputs):
grid = self.embedding(self.grid)
return inputs + grid
class Encoder(nn.Module):
def __init__(self, resolution, hid_dim):
super().__init__()
self.conv1 = nn.Conv2d(3, hid_dim, 5, padding = 2)
self.conv2 = nn.Conv2d(hid_dim, hid_dim, 5, padding = 2)
self.conv3 = nn.Conv2d(hid_dim, hid_dim, 5, padding = 2)
self.conv4 = nn.Conv2d(hid_dim, hid_dim, 5, padding = 2)
self.encoder_pos = SoftPositionEmbed(hid_dim, resolution)
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = self.conv3(x)
x = F.relu(x)
x = self.conv4(x)
x = F.relu(x)
x = x.permute(0,2,3,1)
x = self.encoder_pos(x)
x = torch.flatten(x, 1, 2)
return x
class Decoder(nn.Module):
def __init__(self, hid_dim, resolution):
super().__init__()
self.conv1 = nn.ConvTranspose2d(hid_dim, hid_dim, 5, stride=(2, 2), padding=2, output_padding=1).to(device)
self.conv2 = nn.ConvTranspose2d(hid_dim, hid_dim, 5, stride=(2, 2), padding=2, output_padding=1).to(device)
self.conv3 = nn.ConvTranspose2d(hid_dim, hid_dim, 5, stride=(2, 2), padding=2, output_padding=1).to(device)
self.conv4 = nn.ConvTranspose2d(hid_dim, hid_dim, 5, stride=(2, 2), padding=2, output_padding=1).to(device)
self.conv5 = nn.ConvTranspose2d(hid_dim, hid_dim, 5, stride=(1, 1), padding=2).to(device)
self.conv6 = nn.ConvTranspose2d(hid_dim, 4, 3, stride=(1, 1), padding=1)
self.decoder_initial_size = (8, 8)
self.decoder_pos = SoftPositionEmbed(hid_dim, self.decoder_initial_size)
self.resolution = resolution
def forward(self, x):
x = self.decoder_pos(x)
x = x.permute(0,3,1,2)
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
# x = F.pad(x, (4,4,4,4)) # no longer needed
x = self.conv3(x)
x = F.relu(x)
x = self.conv4(x)
x = F.relu(x)
x = self.conv5(x)
x = F.relu(x)
x = self.conv6(x)
x = x[:,:,:self.resolution[0], :self.resolution[1]]
x = x.permute(0,2,3,1)
return x
"""Slot Attention-based auto-encoder for object discovery."""
class SlotAttentionAutoEncoder(nn.Module):
def __init__(self, resolution, num_slots, num_iterations, hid_dim):
"""Builds the Slot Attention-based auto-encoder.
Args:
resolution: Tuple of integers specifying width and height of input image.
num_slots: Number of slots in Slot Attention.
num_iterations: Number of iterations in Slot Attention.
"""
super().__init__()
self.hid_dim = hid_dim
self.resolution = resolution
self.num_slots = num_slots
self.num_iterations = num_iterations
self.encoder_cnn = Encoder(self.resolution, self.hid_dim)
self.decoder_cnn = Decoder(self.hid_dim, self.resolution)
self.fc1 = nn.Linear(hid_dim, hid_dim)
self.fc2 = nn.Linear(hid_dim, hid_dim)
self.slot_attention = SlotAttention(
num_slots=self.num_slots,
dim=hid_dim,
iters = self.num_iterations,
eps = 1e-8,
hidden_dim = 128)
def forward(self, image):
# `image` has shape: [batch_size, num_channels, width, height].
# Convolutional encoder with position embedding.
x = self.encoder_cnn(image) # CNN Backbone.
x = nn.LayerNorm(x.shape[1:]).to(device)(x)
x = self.fc1(x)
x = F.relu(x)
x = self.fc2(x) # Feedforward network on set.
# `x` has shape: [batch_size, width*height, input_size].
# Slot Attention module.
slots = self.slot_attention(x)
# `slots` has shape: [batch_size, num_slots, slot_size].
# """Broadcast slot features to a 2D grid and collapse slot dimension.""".
slots = slots.reshape((-1, slots.shape[-1])).unsqueeze(1).unsqueeze(2)
slots = slots.repeat((1, 8, 8, 1))
# `slots` has shape: [batch_size*num_slots, width_init, height_init, slot_size].
x = self.decoder_cnn(slots)
# `x` has shape: [batch_size*num_slots, width, height, num_channels+1].
# Undo combination of slot and batch dimension; split alpha masks.
recons, masks = x.reshape(image.shape[0], -1, x.shape[1], x.shape[2], x.shape[3]).split([3,1], dim=-1)
# `recons` has shape: [batch_size, num_slots, width, height, num_channels].
# `masks` has shape: [batch_size, num_slots, width, height, 1].
# Normalize alpha masks over slots.
masks = nn.Softmax(dim=1)(masks)
recon_combined = torch.sum(recons * masks, dim=1) # Recombine image.
recon_combined = recon_combined.permute(0,3,1,2)
# `recon_combined` has shape: [batch_size, width, height, num_channels].
return recon_combined, recons, masks, slots