-
Notifications
You must be signed in to change notification settings - Fork 43
/
model.py
executable file
·402 lines (295 loc) · 10.4 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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
import math
from typing import List
import torch
from torch import nn
from torch.nn import functional as F
from pydantic import StrictInt, StrictFloat, StrictBool
# def swish(input):
# return input * torch.sigmoid(input)
swish = F.silu
@torch.no_grad()
def variance_scaling_init_(tensor, scale=1, mode="fan_avg", distribution="uniform"):
fan_in, fan_out = nn.init._calculate_fan_in_and_fan_out(tensor)
if mode == "fan_in":
scale /= fan_in
elif mode == "fan_out":
scale /= fan_out
else:
scale /= (fan_in + fan_out) / 2
if distribution == "normal":
std = math.sqrt(scale)
return tensor.normal_(0, std)
else:
bound = math.sqrt(3 * scale)
return tensor.uniform_(-bound, bound)
def conv2d(
in_channel,
out_channel,
kernel_size,
stride=1,
padding=0,
bias=True,
scale=1,
mode="fan_avg",
):
conv = nn.Conv2d(
in_channel, out_channel, kernel_size, stride=stride, padding=padding, bias=bias
)
variance_scaling_init_(conv.weight, scale, mode=mode)
if bias:
nn.init.zeros_(conv.bias)
return conv
def linear(in_channel, out_channel, scale=1, mode="fan_avg"):
lin = nn.Linear(in_channel, out_channel)
variance_scaling_init_(lin.weight, scale, mode=mode)
nn.init.zeros_(lin.bias)
return lin
class Swish(nn.Module):
def __init__(self):
super().__init__()
def forward(self, input):
return swish(input)
class Upsample(nn.Sequential):
def __init__(self, channel):
layers = [
nn.Upsample(scale_factor=2, mode="nearest"),
conv2d(channel, channel, 3, padding=1),
]
super().__init__(*layers)
class Downsample(nn.Sequential):
def __init__(self, channel):
layers = [conv2d(channel, channel, 3, stride=2, padding=1)]
super().__init__(*layers)
class ResBlock(nn.Module):
def __init__(
self, in_channel, out_channel, time_dim, use_affine_time=False, dropout=0
):
super().__init__()
self.use_affine_time = use_affine_time
time_out_dim = out_channel
time_scale = 1
norm_affine = True
if self.use_affine_time:
time_out_dim *= 2
time_scale = 1e-10
norm_affine = False
self.norm1 = nn.GroupNorm(32, in_channel)
self.activation1 = Swish()
self.conv1 = conv2d(in_channel, out_channel, 3, padding=1)
self.time = nn.Sequential(
Swish(), linear(time_dim, time_out_dim, scale=time_scale)
)
self.norm2 = nn.GroupNorm(32, out_channel, affine=norm_affine)
self.activation2 = Swish()
self.dropout = nn.Dropout(dropout)
self.conv2 = conv2d(out_channel, out_channel, 3, padding=1, scale=1e-10)
if in_channel != out_channel:
self.skip = conv2d(in_channel, out_channel, 1)
else:
self.skip = None
def forward(self, input, time):
batch = input.shape[0]
out = self.conv1(self.activation1(self.norm1(input)))
if self.use_affine_time:
gamma, beta = self.time(time).view(batch, -1, 1, 1).chunk(2, dim=1)
out = (1 + gamma) * self.norm2(out) + beta
else:
out = out + self.time(time).view(batch, -1, 1, 1)
out = self.norm2(out)
out = self.conv2(self.dropout(self.activation2(out)))
if self.skip is not None:
input = self.skip(input)
return out + input
class SelfAttention(nn.Module):
def __init__(self, in_channel, n_head=1):
super().__init__()
self.n_head = n_head
self.norm = nn.GroupNorm(32, in_channel)
self.qkv = conv2d(in_channel, in_channel * 3, 1)
self.out = conv2d(in_channel, in_channel, 1, scale=1e-10)
def forward(self, input):
batch, channel, height, width = input.shape
n_head = self.n_head
head_dim = channel // n_head
norm = self.norm(input)
qkv = self.qkv(norm).view(batch, n_head, head_dim * 3, height, width)
query, key, value = qkv.chunk(3, dim=2) # bhdyx
attn = torch.einsum(
"bnchw, bncyx -> bnhwyx", query, key
).contiguous() / math.sqrt(channel)
attn = attn.view(batch, n_head, height, width, -1)
attn = torch.softmax(attn, -1)
attn = attn.view(batch, n_head, height, width, height, width)
out = torch.einsum("bnhwyx, bncyx -> bnchw", attn, value).contiguous()
out = self.out(out.view(batch, channel, height, width))
return out + input
class TimeEmbedding(nn.Module):
def __init__(self, dim):
super().__init__()
self.dim = dim
inv_freq = torch.exp(
torch.arange(0, dim, 2, dtype=torch.float32) * (-math.log(10000) / dim)
)
self.register_buffer("inv_freq", inv_freq)
def forward(self, input):
shape = input.shape
sinusoid_in = torch.ger(input.view(-1).float(), self.inv_freq)
pos_emb = torch.cat([sinusoid_in.sin(), sinusoid_in.cos()], dim=-1)
pos_emb = pos_emb.view(*shape, self.dim)
return pos_emb
class ResBlockWithAttention(nn.Module):
def __init__(
self,
in_channel,
out_channel,
time_dim,
dropout,
use_attention=False,
attention_head=1,
use_affine_time=False,
):
super().__init__()
self.resblocks = ResBlock(
in_channel, out_channel, time_dim, use_affine_time, dropout
)
if use_attention:
self.attention = SelfAttention(out_channel, n_head=attention_head)
else:
self.attention = None
def forward(self, input, time):
out = self.resblocks(input, time)
if self.attention is not None:
out = self.attention(out)
return out
def spatial_fold(input, fold):
if fold == 1:
return input
batch, channel, height, width = input.shape
h_fold = height // fold
w_fold = width // fold
return (
input.view(batch, channel, h_fold, fold, w_fold, fold)
.permute(0, 1, 3, 5, 2, 4)
.reshape(batch, -1, h_fold, w_fold)
)
def spatial_unfold(input, unfold):
if unfold == 1:
return input
batch, channel, height, width = input.shape
h_unfold = height * unfold
w_unfold = width * unfold
return (
input.view(batch, -1, unfold, unfold, height, width)
.permute(0, 1, 4, 2, 5, 3)
.reshape(batch, -1, h_unfold, w_unfold)
)
class UNet(nn.Module):
def __init__(
self,
in_channel: StrictInt,
channel: StrictInt,
channel_multiplier: List[StrictInt],
n_res_blocks: StrictInt,
attn_strides: List[StrictInt],
attn_heads: StrictInt = 1,
use_affine_time: StrictBool = False,
dropout: StrictFloat = 0,
fold: StrictInt = 1,
):
super().__init__()
self.fold = fold
time_dim = channel * 4
n_block = len(channel_multiplier)
self.time = nn.Sequential(
TimeEmbedding(channel),
linear(channel, time_dim),
Swish(),
linear(time_dim, time_dim),
)
down_layers = [conv2d(in_channel * (fold ** 2), channel, 3, padding=1)]
feat_channels = [channel]
in_channel = channel
for i in range(n_block):
for _ in range(n_res_blocks):
channel_mult = channel * channel_multiplier[i]
down_layers.append(
ResBlockWithAttention(
in_channel,
channel_mult,
time_dim,
dropout,
use_attention=2 ** i in attn_strides,
attention_head=attn_heads,
use_affine_time=use_affine_time,
)
)
feat_channels.append(channel_mult)
in_channel = channel_mult
if i != n_block - 1:
down_layers.append(Downsample(in_channel))
feat_channels.append(in_channel)
self.down = nn.ModuleList(down_layers)
self.mid = nn.ModuleList(
[
ResBlockWithAttention(
in_channel,
in_channel,
time_dim,
dropout=dropout,
use_attention=True,
attention_head=attn_heads,
use_affine_time=use_affine_time,
),
ResBlockWithAttention(
in_channel,
in_channel,
time_dim,
dropout=dropout,
use_affine_time=use_affine_time,
),
]
)
up_layers = []
for i in reversed(range(n_block)):
for _ in range(n_res_blocks + 1):
channel_mult = channel * channel_multiplier[i]
up_layers.append(
ResBlockWithAttention(
in_channel + feat_channels.pop(),
channel_mult,
time_dim,
dropout=dropout,
use_attention=2 ** i in attn_strides,
attention_head=attn_heads,
use_affine_time=use_affine_time,
)
)
in_channel = channel_mult
if i != 0:
up_layers.append(Upsample(in_channel))
self.up = nn.ModuleList(up_layers)
self.out = nn.Sequential(
nn.GroupNorm(32, in_channel),
Swish(),
conv2d(in_channel, 3 * (fold ** 2), 3, padding=1, scale=1e-10),
)
def forward(self, input, time):
time_embed = self.time(time)
feats = []
out = spatial_fold(input, self.fold)
for layer in self.down:
if isinstance(layer, ResBlockWithAttention):
out = layer(out, time_embed)
else:
out = layer(out)
feats.append(out)
for layer in self.mid:
out = layer(out, time_embed)
for layer in self.up:
if isinstance(layer, ResBlockWithAttention):
out = layer(torch.cat((out, feats.pop()), 1), time_embed)
else:
out = layer(out)
out = self.out(out)
out = spatial_unfold(out, self.fold)
return out