-
Notifications
You must be signed in to change notification settings - Fork 0
/
NCSNv2.py
522 lines (423 loc) · 20.1 KB
/
NCSNv2.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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
import torch
import torch.nn as nn
import torchvision
import torch.optim as optim
import logging
import numpy as np
import ml_collections
from functools import partial
from einops import rearrange, reduce, repeat
from einops.layers.torch import Rearrange
import time
import math
from torch.utils.data import Dataset
import torch.nn.functional as F
########################### Below Code for NCSNv2 ###########################
def get_default_configs(datasetname, device):
assert datasetname in ("CelebA", "CelebA-A", "CelebA-S"), f"wrong datasetname {datasetname}"
if datasetname in ("CelebA", "CelebA-A", "CelebA-S"):
config = ml_collections.ConfigDict()
# training
config.training = training = ml_collections.ConfigDict()
config.training.batch_size = 128
training.n_epochs = 500000
training.n_iters = 210001
training.anneal_power = 2
# sampling
config.sampling = sampling = ml_collections.ConfigDict()
sampling.n_steps_each = 5
sampling.denoise = True
sampling.step_lr = 0.0000033
# evaluation
config.test = test = ml_collections.ConfigDict()
test.begin_ckpt = 5000
test.end_ckpt = 210000
test.batch_size = 100
# model
config.model = model = ml_collections.ConfigDict()
model.sigma_begin = 90.0
model.num_classes = 500
model.spec_norm = False
model.sigma_dist = 'geometric'
model.sigma_end = 0.01
model.nonlinearity = "elu"
model.normalization = "InstanceNorm++"
model.ngf = 128
# optimization
config.optim = optim = ml_collections.ConfigDict()
optim.weight_decay = 0
optim.optimizer = "Adam"
optim.lr = 0.0001
optim.beta1 = 0.9
optim.eps = 0.00000001
optim.amsgrad = False
config.seed = 47
config.device = device
print(f"config.device: {config.device}")
return config
def get_sigmas(config):
if config.model.sigma_dist == 'geometric':
sigmas = torch.tensor(
np.exp(np.linspace(np.log(config.model.sigma_begin), np.log(config.model.sigma_end),
config.model.num_classes))).float().to(config.device)
elif config.model.sigma_dist == 'uniform':
sigmas = torch.tensor(
np.linspace(config.model.sigma_begin, config.model.sigma_end, config.model.num_classes)
).float().to(config.device)
else:
raise NotImplementedError('sigma distribution not supported')
return sigmas
def get_act(config):
"""Get activation functions from the config file."""
if config.model.nonlinearity.lower() == "elu":
return nn.ELU()
elif config.model.nonlinearity.lower() == "relu":
return nn.ReLU()
elif config.model.nonlinearity.lower() == "lrelu":
return nn.LeakyReLU(negative_slope=0.2)
elif config.model.nonlinearity.lower() == "swish":
return nn.SiLU()
else:
raise NotImplementedError("activation function does not exist!")
class InstanceNorm2dPlus(nn.Module):
def __init__(self, num_features, bias=True):
super().__init__()
self.num_features = num_features
self.bias = bias
self.instance_norm = nn.InstanceNorm2d(
num_features, affine=False, track_running_stats=False
)
self.alpha = nn.Parameter(torch.zeros(num_features))
self.gamma = nn.Parameter(torch.zeros(num_features))
self.alpha.data.normal_(1, 0.02)
self.gamma.data.normal_(1, 0.02)
if bias:
self.beta = nn.Parameter(torch.zeros(num_features))
def forward(self, x):
means = torch.mean(x, dim=(2, 3))
m = torch.mean(means, dim=-1, keepdim=True)
v = torch.var(means, dim=-1, keepdim=True)
means = (means - m) / (torch.sqrt(v + 1e-5))
h = self.instance_norm(x)
if self.bias:
h = h + means[..., None, None] * self.alpha[..., None, None]
out = self.gamma.view(-1, self.num_features, 1, 1) * h + self.beta.view(
-1, self.num_features, 1, 1
)
else:
h = h + means[..., None, None] * self.alpha[..., None, None]
out = self.gamma.view(-1, self.num_features, 1, 1) * h
return out
def get_normalization(config):
norm = config.model.normalization
if norm == "InstanceNorm++":
return InstanceNorm2dPlus
else:
raise ValueError("Unknown normalization: %s" % norm)
def get_optimizer(config, parameters):
if config.optim.optimizer == "Adam":
return optim.Adam(
parameters,
lr=config.optim.lr,
weight_decay=config.optim.weight_decay,
betas=(config.optim.beta1, 0.999),
amsgrad=config.optim.amsgrad,
eps=config.optim.eps,
)
elif config.optim.optimizer == "RMSProp":
return optim.RMSprop(parameters, lr=config.optim.lr, weight_decay=config.optim.weight_decay)
elif config.optim.optimizer == "SGD":
return optim.SGD(parameters, lr=config.optim.lr, momentum=0.9)
else:
raise NotImplementedError("Optimizer {} not understood.".format(config.optim.optimizer))
def spectral_norm(layer, n_iters=1):
return torch.nn.utils.spectral_norm(layer, n_power_iterations=n_iters)
def conv1x1(in_planes, out_planes, stride=1, bias=True, spec_norm=False):
"1x1 convolution"
conv = nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride,
padding=0, bias=bias)
if spec_norm:
conv = spectral_norm(conv)
return conv
def conv3x3(in_planes, out_planes, stride=1, bias=True, spec_norm=False):
"3x3 convolution with padding"
conv = nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
padding=1, bias=bias)
if spec_norm:
conv = spectral_norm(conv)
return conv
def dilated_conv3x3(in_planes, out_planes, dilation, bias=True, spec_norm=False):
conv = nn.Conv2d(in_planes, out_planes, kernel_size=3, padding=dilation, dilation=dilation, bias=bias)
if spec_norm:
conv = spectral_norm(conv)
return conv
class CRPBlock(nn.Module):
def __init__(self, features, n_stages, act=nn.ReLU(), maxpool=True, spec_norm=False):
super().__init__()
self.convs = nn.ModuleList()
for i in range(n_stages):
self.convs.append(conv3x3(features, features, stride=1, bias=False, spec_norm=spec_norm))
self.n_stages = n_stages
if maxpool:
self.maxpool = nn.MaxPool2d(kernel_size=5, stride=1, padding=2)
else:
self.maxpool = nn.AvgPool2d(kernel_size=5, stride=1, padding=2)
self.act = act
def forward(self, x):
x = self.act(x)
path = x
for i in range(self.n_stages):
path = self.maxpool(path)
path = self.convs[i](path)
x = path + x
return x
class RCUBlock(nn.Module):
def __init__(self, features, n_blocks, n_stages, act=nn.ReLU(), spec_norm=False):
super().__init__()
for i in range(n_blocks):
for j in range(n_stages):
setattr(self, '{}_{}_conv'.format(i + 1, j + 1), conv3x3(features, features, stride=1, bias=False,
spec_norm=spec_norm))
self.stride = 1
self.n_blocks = n_blocks
self.n_stages = n_stages
self.act = act
def forward(self, x):
for i in range(self.n_blocks):
residual = x
for j in range(self.n_stages):
x = self.act(x)
x = getattr(self, '{}_{}_conv'.format(i + 1, j + 1))(x)
x += residual
return x
class MSFBlock(nn.Module):
def __init__(self, in_planes, features, spec_norm=False):
"""
:param in_planes: tuples of input planes
"""
super().__init__()
assert isinstance(in_planes, list) or isinstance(in_planes, tuple)
self.convs = nn.ModuleList()
self.features = features
for i in range(len(in_planes)):
self.convs.append(conv3x3(in_planes[i], features, stride=1, bias=True, spec_norm=spec_norm))
def forward(self, xs, shape):
sums = torch.zeros(xs[0].shape[0], self.features, *shape, device=xs[0].device)
for i in range(len(self.convs)):
h = self.convs[i](xs[i])
h = F.interpolate(h, size=shape, mode='bilinear', align_corners=True)
sums += h
return sums
class RefineBlock(nn.Module):
def __init__(self, in_planes, features, act=nn.ReLU(), start=False, end=False, maxpool=True, spec_norm=False):
super().__init__()
assert isinstance(in_planes, tuple) or isinstance(in_planes, list)
self.n_blocks = n_blocks = len(in_planes)
self.adapt_convs = nn.ModuleList()
for i in range(n_blocks):
self.adapt_convs.append(
RCUBlock(in_planes[i], 2, 2, act, spec_norm=spec_norm)
)
self.output_convs = RCUBlock(features, 3 if end else 1, 2, act, spec_norm=spec_norm)
if not start:
self.msf = MSFBlock(in_planes, features, spec_norm=spec_norm)
self.crp = CRPBlock(features, 2, act, maxpool=maxpool, spec_norm=spec_norm)
def forward(self, xs, output_shape):
assert isinstance(xs, tuple) or isinstance(xs, list)
hs = []
for i in range(len(xs)):
h = self.adapt_convs[i](xs[i])
hs.append(h)
if self.n_blocks > 1:
h = self.msf(hs, output_shape)
else:
h = hs[0]
h = self.crp(h)
h = self.output_convs(h)
return h
class ConvMeanPool(nn.Module):
def __init__(self, input_dim, output_dim, kernel_size=3, biases=True, adjust_padding=False, spec_norm=False):
super().__init__()
if not adjust_padding:
conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride=1, padding=kernel_size // 2, bias=biases)
if spec_norm:
conv = spectral_norm(conv)
self.conv = conv
else:
conv = nn.Conv2d(input_dim, output_dim, kernel_size, stride=1, padding=kernel_size // 2, bias=biases)
if spec_norm:
conv = spectral_norm(conv)
self.conv = nn.Sequential(
nn.ZeroPad2d((1, 0, 1, 0)),
conv
)
def forward(self, inputs):
output = self.conv(inputs)
output = sum([output[:, :, ::2, ::2], output[:, :, 1::2, ::2],
output[:, :, ::2, 1::2], output[:, :, 1::2, 1::2]]) / 4.
return output
class ResidualBlock(nn.Module):
def __init__(self, input_dim, output_dim, resample=None, act=nn.ELU(),
normalization=nn.BatchNorm2d, adjust_padding=False, dilation=None, spec_norm=False):
super().__init__()
self.non_linearity = act
self.input_dim = input_dim
self.output_dim = output_dim
self.resample = resample
self.normalization = normalization
if resample == 'down':
if dilation is not None:
self.conv1 = dilated_conv3x3(input_dim, input_dim, dilation=dilation, spec_norm=spec_norm)
self.normalize2 = normalization(input_dim)
self.conv2 = dilated_conv3x3(input_dim, output_dim, dilation=dilation, spec_norm=spec_norm)
conv_shortcut = partial(dilated_conv3x3, dilation=dilation, spec_norm=spec_norm)
else:
self.conv1 = conv3x3(input_dim, input_dim, spec_norm=spec_norm)
self.normalize2 = normalization(input_dim)
self.conv2 = ConvMeanPool(input_dim, output_dim, 3, adjust_padding=adjust_padding, spec_norm=spec_norm)
conv_shortcut = partial(ConvMeanPool, kernel_size=1, adjust_padding=adjust_padding, spec_norm=spec_norm)
elif resample is None:
if dilation is not None:
conv_shortcut = partial(dilated_conv3x3, dilation=dilation, spec_norm=spec_norm)
self.conv1 = dilated_conv3x3(input_dim, output_dim, dilation=dilation, spec_norm=spec_norm)
self.normalize2 = normalization(output_dim)
self.conv2 = dilated_conv3x3(output_dim, output_dim, dilation=dilation, spec_norm=spec_norm)
else:
# conv_shortcut = nn.Conv2d ### Something wierd here.
conv_shortcut = partial(conv1x1, spec_norm=spec_norm)
self.conv1 = conv3x3(input_dim, output_dim, spec_norm=spec_norm)
self.normalize2 = normalization(output_dim)
self.conv2 = conv3x3(output_dim, output_dim, spec_norm=spec_norm)
else:
raise Exception('invalid resample value')
if output_dim != input_dim or resample is not None:
self.shortcut = conv_shortcut(input_dim, output_dim)
self.normalize1 = normalization(input_dim)
def forward(self, x):
output = self.normalize1(x)
output = self.non_linearity(output)
output = self.conv1(output)
output = self.normalize2(output)
output = self.non_linearity(output)
output = self.conv2(output)
if self.output_dim == self.input_dim and self.resample is None:
shortcut = x
else:
shortcut = self.shortcut(x)
return shortcut + output
class NCSNv2(nn.Module):
def __init__(self, config, img_channels, img_size):
super().__init__()
self.norm = get_normalization(config)
self.ngf = ngf = config.model.ngf
self.num_classes = num_classes = config.model.num_classes
self.act = act = get_act(config)
self.register_buffer('sigmas', get_sigmas(config))
self.config = config
self.begin_conv = nn.Conv2d(img_channels, ngf, 3, stride=1, padding=1)
self.normalizer = self.norm(ngf, self.num_classes)
self.end_conv = nn.Conv2d(ngf, img_channels, 3, stride=1, padding=1)
self.res1 = nn.ModuleList([
ResidualBlock(self.ngf, self.ngf, resample=None, act=act,
normalization=self.norm),
ResidualBlock(self.ngf, self.ngf, resample=None, act=act,
normalization=self.norm)]
)
self.res2 = nn.ModuleList([
ResidualBlock(self.ngf, 2 * self.ngf, resample='down', act=act,
normalization=self.norm),
ResidualBlock(2 * self.ngf, 2 * self.ngf, resample=None, act=act,
normalization=self.norm)]
)
self.res3 = nn.ModuleList([
ResidualBlock(2 * self.ngf, 2 * self.ngf, resample='down', act=act,
normalization=self.norm, dilation=2),
ResidualBlock(2 * self.ngf, 2 * self.ngf, resample=None, act=act,
normalization=self.norm, dilation=2)]
)
if img_size == 28:
self.res4 = nn.ModuleList([
ResidualBlock(2 * self.ngf, 2 * self.ngf, resample='down', act=act,
normalization=self.norm, adjust_padding=True, dilation=4),
ResidualBlock(2 * self.ngf, 2 * self.ngf, resample=None, act=act,
normalization=self.norm, dilation=4)]
)
else:
self.res4 = nn.ModuleList([
ResidualBlock(2 * self.ngf, 2 * self.ngf, resample='down', act=act,
normalization=self.norm, adjust_padding=False, dilation=4),
ResidualBlock(2 * self.ngf, 2 * self.ngf, resample=None, act=act,
normalization=self.norm, dilation=4)]
)
self.refine1 = RefineBlock([2 * self.ngf], 2 * self.ngf, act=act, start=True)
self.refine2 = RefineBlock([2 * self.ngf, 2 * self.ngf], 2 * self.ngf, act=act)
self.refine3 = RefineBlock([2 * self.ngf, 2 * self.ngf], self.ngf, act=act)
self.refine4 = RefineBlock([self.ngf, self.ngf], self.ngf, act=act, end=True)
def _compute_cond_module(self, module, x):
for m in module:
x = m(x)
return x
def forward(self, x, y):
h = 2 * x - 1.
output = self.begin_conv(h)
layer1 = self._compute_cond_module(self.res1, output)
layer2 = self._compute_cond_module(self.res2, layer1)
layer3 = self._compute_cond_module(self.res3, layer2)
layer4 = self._compute_cond_module(self.res4, layer3)
ref1 = self.refine1([layer4], layer4.shape[2:])
ref2 = self.refine2([layer3, ref1], layer3.shape[2:])
ref3 = self.refine3([layer2, ref2], layer2.shape[2:])
output = self.refine4([layer1, ref3], layer1.shape[2:])
output = self.normalizer(output)
output = self.act(output)
output = self.end_conv(output)
used_sigmas = self.sigmas[y].view(x.shape[0], *([1] * len(x.shape[1:])))
output = output / used_sigmas
return output
def anneal_dsm_score_estimation(scorenet, samples, sigmas, labels=None, anneal_power=2.0):
if labels is None:
labels = torch.randint(0, len(sigmas), (samples.shape[0],), device=samples.device)
used_sigmas = sigmas[labels].view(samples.shape[0], *([1] * len(samples.shape[1:])))
noise = torch.randn_like(samples) * used_sigmas
perturbed_samples = samples + noise
target = -1 / (used_sigmas**2) * noise
scores = scorenet(perturbed_samples, labels)
target = target.view(target.shape[0], -1)
scores = scores.view(scores.shape[0], -1)
loss = 1 / 2.0 * ((scores - target) ** 2).sum(dim=-1) * used_sigmas.squeeze() ** anneal_power
return loss.mean(dim=0)
def anneal_Langevin_dynamics(x_mod, scorenet, sigmas, n_steps_each=200, step_lr=0.000008,
final_only=False, verbose=False, denoise=True):
images = []
with torch.no_grad():
for c, sigma in enumerate(sigmas):
# print("c=", c)
labels = torch.ones(x_mod.shape[0], device=x_mod.device) * c
labels = labels.long()
step_size = step_lr * (sigma / sigmas[-1]) ** 2
for s in range(n_steps_each):
# print("x_mod.dtype", x_mod.dtype)
grad = scorenet(x_mod, labels).float()
noise = torch.randn_like(x_mod)
# print("noise.dtype:", noise.dtype)
grad_norm = torch.norm(grad.view(grad.shape[0], -1), dim=-1).mean()
noise_norm = torch.norm(noise.view(noise.shape[0], -1), dim=-1).mean()
x_mod = x_mod + step_size * grad + noise * np.sqrt(step_size * 2)
# print("grad.dtype", grad.dtype)
image_norm = torch.norm(x_mod.view(x_mod.shape[0], -1), dim=-1).mean()
snr = np.sqrt(step_size / 2.) * grad_norm / noise_norm
grad_mean_norm = torch.norm(grad.mean(dim=0).view(-1)) ** 2 * sigma ** 2
if not final_only:
images.append(x_mod.to('cpu'))
if verbose:
print("level: {}, step_size: {}, grad_norm: {}, image_norm: {}, snr: {}, grad_mean_norm: {}".format(
c, step_size, grad_norm.item(), image_norm.item(), snr.item(), grad_mean_norm.item()))
if denoise:
last_noise = (len(sigmas) - 1) * torch.ones(x_mod.shape[0], device=x_mod.device)
last_noise = last_noise.long()
x_mod = x_mod + sigmas[-1] ** 2 * scorenet(x_mod, last_noise)
images.append(x_mod.to('cpu'))
if final_only:
return x_mod.to('cpu')
else:
return images