-
Notifications
You must be signed in to change notification settings - Fork 18
/
vitgan.py
357 lines (297 loc) · 11.9 KB
/
vitgan.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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import numpy as np
from einops import rearrange, repeat
import torch
import torch.nn as nn
class SLN(nn.Module):
"""
Self-modulated LayerNorm
"""
def __init__(self, num_features):
super(SLN, self).__init__()
self.ln = nn.LayerNorm(num_features)
# self.gamma = nn.Parameter(torch.FloatTensor(1, 1, 1))
# self.beta = nn.Parameter(torch.FloatTensor(1, 1, 1))
self.gamma = nn.Parameter(torch.randn(1, 1, 1)) #.to("cuda")
self.beta = nn.Parameter(torch.randn(1, 1, 1)) #.to("cuda")
def forward(self, hl, w):
return self.gamma * w * self.ln(hl) + self.beta * w
class MLP(nn.Module):
def __init__(self, in_feat, hid_feat = None, out_feat = None, dropout = 0.):
super().__init__()
if not hid_feat:
hid_feat = in_feat
if not out_feat:
out_feat = in_feat
self.linear1 = nn.Linear(in_feat, hid_feat)
self.activation = nn.GELU()
self.linear2 = nn.Linear(hid_feat, out_feat)
self.dropout = nn.Dropout(dropout)
def forward(self, x):
x = self.linear1(x)
x = self.activation(x)
x = self.dropout(x)
x = self.linear2(x)
return self.dropout(x)
class Attention(nn.Module):
"""
Implement multi head self attention layer using the "Einstein summation convention".
Parameters
----------
dim:
Token's dimension, EX: word embedding vector size
num_heads:
The number of distinct representations to learn
dim_head:
The dimension of the each head
discriminator:
Used in discriminator or not.
"""
def __init__(self, dim, num_heads = 4, dim_head = None, discriminator = False):
super(Attention, self).__init__()
self.num_heads = num_heads
self.dim_head = int(dim / num_heads) if dim_head is None else dim_head
self.weight_dim = self.num_heads * self.dim_head
self.to_qkv = nn.Linear(dim, self.weight_dim * 3, bias = False)
self.scale_factor = dim ** -0.5
self.discriminator = discriminator
self.w_out = nn.Linear(self.weight_dim, dim, bias = True)
if discriminator:
u, s, v = torch.svd(self.to_qkv.weight)
self.init_spect_norm = torch.max(s)
def forward(self, x):
assert x.dim() == 3
if self.discriminator:
u, s, v = torch.svd(self.to_qkv.weight)
self.to_qkv.weight = torch.nn.Parameter(self.to_qkv.weight * self.init_spect_norm / torch.max(s))
# Generate the q, k, v vectors
qkv = self.to_qkv(x)
q, k, v = tuple(rearrange(qkv, 'b t (d k h) -> k b h t d', k = 3, h = self.num_heads))
# Enforcing Lipschitzness of Transformer Discriminator
# Due to Lipschitz constant of standard dot product self-attention
# layer can be unbounded, so adopt the l2 attention replace the dot product.
if self.discriminator:
attn = torch.cdist(q, k, p = 2)
else:
attn = torch.einsum("... i d, ... j d -> ... i j", q, k)
scale_attn = attn * self.scale_factor
scale_attn_score = torch.softmax(scale_attn, dim = -1)
result = torch.einsum("... i j, ... j d -> ... i d", scale_attn_score, v)
# re-compose
result = rearrange(result, "b h t d -> b t (h d)")
return self.w_out(result)
class DEncoderBlock(nn.Module):
def __init__(self, dim, num_heads = 4, dim_head = None,
dropout = 0., mlp_ratio = 4):
super(DEncoderBlock, self).__init__()
self.attn = Attention(dim, num_heads, dim_head, discriminator = True)
self.dropout = nn.Dropout(dropout)
self.norm1 = nn.LayerNorm(dim)
self.norm2 = nn.LayerNorm(dim)
self.mlp = MLP(dim, dim * mlp_ratio, dropout = dropout)
def forward(self, x):
x1 = self.norm1(x)
x = x + self.dropout(self.attn(x1))
x2 = self.norm2(x)
x = x + self.mlp(x2)
return x
class GEncoderBlock(nn.Module):
def __init__(self, dim, num_heads = 4, dim_head = None,
dropout = 0., mlp_ratio = 4):
super(GEncoderBlock, self).__init__()
self.attn = Attention(dim, num_heads, dim_head)
self.dropout = nn.Dropout(dropout)
self.norm1 = SLN(dim)
self.norm2 = SLN(dim)
self.mlp = MLP(dim, dim * mlp_ratio, dropout = dropout)
def forward(self, hl, x):
hl_temp = self.dropout(self.attn(self.norm1(hl, x))) + hl
hl_final = self.mlp(self.norm2(hl_temp, x)) + hl_temp
return x, hl_final
class GTransformerEncoder(nn.Module):
def __init__(self,
dim,
blocks = 6,
num_heads = 8,
dim_head = None,
dropout = 0
):
super(GTransformerEncoder, self).__init__()
self.blocks = self._make_layers(dim, blocks, num_heads, dim_head, dropout)
def _make_layers(self,
dim,
blocks = 6,
num_heads = 8,
dim_head = None,
dropout = 0
):
layers = []
for _ in range(blocks):
layers.append(GEncoderBlock(dim, num_heads, dim_head, dropout))
return nn.Sequential(*layers)
def forward(self, hl, x):
for block in self.blocks:
x, hl = block(hl, x)
return x, hl
class DTransformerEncoder(nn.Module):
def __init__(self,
dim,
blocks = 6,
num_heads = 8,
dim_head = None,
dropout = 0
):
super(DTransformerEncoder, self).__init__()
self.blocks = self._make_layers(dim, blocks, num_heads, dim_head, dropout)
def _make_layers(self,
dim,
blocks = 6,
num_heads = 8,
dim_head = None,
dropout = 0
):
layers = []
for _ in range(blocks):
layers.append(DEncoderBlock(dim, num_heads, dim_head, dropout))
return nn.Sequential(*layers)
def forward(self, x):
for block in self.blocks:
x = block(x)
return x
class SineLayer(nn.Module):
"""
Paper: Implicit Neural Representation with Periodic Activ ation Function (SIREN)
"""
def __init__(self, in_features, out_features, bias = True,is_first = False, omega_0 = 30):
super().__init__()
self.omega_0 = omega_0
self.is_first = is_first
self.in_features = in_features
self.linear = nn.Linear(in_features, out_features, bias=bias)
self.init_weights()
def init_weights(self):
with torch.no_grad():
if self.is_first:
self.linear.weight.uniform_(-1 / self.in_features, 1 / self.in_features)
else:
self.linear.weight.uniform_(-np.sqrt(6 / self.in_features) / self.omega_0, np.sqrt(6 / self.in_features) / self.omega_0)
def forward(self, input):
return torch.sin(self.omega_0 * self.linear(input))
class Generator(nn.Module):
def __init__(self,
initialize_size = 8,
dim = 384,
blocks = 6,
num_heads = 6,
dim_head = None,
dropout = 0,
out_channels = 3,
input_dim=1024,
):
super(Generator, self).__init__()
self.initialize_size = initialize_size
self.dim = dim
self.blocks = blocks
self.num_heads = num_heads
self.dim_head = dim_head
self.dropout = dropout
self.out_channels = out_channels
self.pos_emb1D = nn.Parameter(torch.randn(self.initialize_size * 8, dim))
self.mlp = nn.Linear(input_dim, (self.initialize_size * 8) * self.dim)
self.Transformer_Encoder = GTransformerEncoder(dim, blocks, num_heads, dim_head, dropout)
# Implicit Neural Representation
self.w_out = nn.Sequential(
# SineLayer(dim, dim * 2, is_first = True, omega_0 = 30.),
# SineLayer(dim * 2, self.initialize_size * 8 * self.out_channels, is_first = False, omega_0 = 30)
nn.Linear(dim, self.initialize_size * 8 * self.out_channels),
)
self.sln_norm = SLN(self.dim)
def forward(self, noise):
x = self.mlp(noise).view(-1, self.initialize_size * 8, self.dim)
x, hl = self.Transformer_Encoder(self.pos_emb1D, x)
x = self.sln_norm(hl, x)
x = self.w_out(x) # Replace to siren
result = x.view(x.shape[0], self.out_channels, self.initialize_size * 8, self.initialize_size * 8)
return result
class SimpleGenerator(nn.Module):
def __init__(self,
size = 8,
in_channels=256,
dim = 384,
blocks = 6,
num_heads = 6,
dim_head = None,
dropout = 0,
out_channels = 3,
input_dim=1024,
):
super().__init__()
self.size = size
self.dim = dim
self.blocks = blocks
self.num_heads = num_heads
self.dim_head = dim_head
self.dropout = dropout
self.out_channels = out_channels
self.pos_emb1D = nn.Parameter(torch.randn(self.size*self.size, dim))
self.mlp = nn.Linear(input_dim, (self.size*self.size) * self.dim)
self.inp = nn.Linear(input_dim, (self.size*self.size) * self.dim)
self.Transformer_Encoder = GTransformerEncoder(dim, blocks, num_heads, dim_head, dropout)
# Implicit Neural Representation
self.w_out = nn.Sequential(
# SineLayer(dim, dim * 2, is_first = True, omega_0 = 30.),
# SineLayer(dim * 2, self.initialize_size * 8 * self.out_channels, is_first = False, omega_0 = 30)
nn.Linear(dim, self.out_channels),
)
self.sln_norm = SLN(self.dim)
def forward(self, noise):
inp = self.inp(noise)
x = self.mlp(noise).view(-1, self.size*self.size, self.dim)
inp_emb = inp.view(inp.shape[0], self.dim, self.size*self.size).permute(0,2,1).contiguous()
x, hl = self.Transformer_Encoder(inp_emb+self.pos_emb1D, x)
x = self.sln_norm(hl, x)
x = self.w_out(x) # Replace to siren
result = x.view(x.shape[0], self.size, self.size, self.out_channels,).permute(0,3,1,2).contiguous()
return result
class Discriminator(nn.Module):
def __init__(self,
in_channels = 3,
patch_size = 8,
extend_size = 2,
dim = 384,
blocks = 6,
num_heads = 6,
dim_head = None,
dropout = 0
):
super(Discriminator, self).__init__()
self.patch_size = patch_size + 2 * extend_size
self.token_dim = in_channels * (self.patch_size ** 2)
self.project_patches = nn.Linear(self.token_dim, dim)
self.emb_dropout = nn.Dropout(dropout)
self.cls_token = nn.Parameter(torch.randn(1, 1, dim))
self.pos_emb1D = nn.Parameter(torch.randn(self.token_dim + 1, dim))
self.mlp_head = nn.Sequential(
nn.LayerNorm(dim),
nn.Linear(dim, 1)
)
self.Transformer_Encoder = DTransformerEncoder(dim, blocks, num_heads, dim_head, dropout)
def forward(self, img):
# Generate overlappimg image patches
stride_h = (img.shape[2] - self.patch_size) // 8 + 1
stride_w = (img.shape[3] - self.patch_size) // 8 + 1
img_patches = img.unfold(2, self.patch_size, stride_h).unfold(3, self.patch_size, stride_w)
img_patches = img_patches.contiguous().view(
img_patches.shape[0], img_patches.shape[2] * img_patches.shape[3], img_patches.shape[1] * img_patches.shape[4] * img_patches.shape[5]
)
img_patches = self.project_patches(img_patches)
batch_size, tokens, _ = img_patches.shape
# Prepend the classifier token
cls_token = repeat(self.cls_token, '() n d -> b n d', b = batch_size)
img_patches = torch.cat((cls_token, img_patches), dim = 1)
# Plus the positional embedding
img_patches = img_patches + self.pos_emb1D[: tokens + 1, :]
img_patches = self.emb_dropout(img_patches)
result = self.Transformer_Encoder(img_patches)
logits = self.mlp_head(result[:, 0, :])
logits = nn.Sigmoid()(logits)
return logits