forked from lucidrains/lightweight-gan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlightweight_gan_.py
1370 lines (1058 loc) · 42.4 KB
/
lightweight_gan_.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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import json
import math
import multiprocessing
import os
from contextlib import ExitStack, contextmanager
from functools import partial
from math import floor, log2
from pathlib import Path
from random import random
from shutil import rmtree
import torch
import torch.nn.functional as F
import torchvision
from adabelief_pytorch import AdaBelief
from einops import rearrange, reduce
from gsa_pytorch import GSA
from kornia import filter2D
from PIL import Image
from torch import einsum, nn
from torch.autograd import grad as torch_grad
from torch.cuda.amp import GradScaler, autocast
from torch.nn.parallel import DistributedDataParallel as DDP
from torch.optim import Adam
from torch.utils.data import DataLoader, Dataset
from torch.utils.data.distributed import DistributedSampler
from torchvision import transforms
from tqdm import tqdm
from version import __version__
from bn import CategoricalConditionalBatchNorm2d
from diff_augment import DiffAugment
# asserts
assert torch.cuda.is_available(), 'You need to have an Nvidia GPU with CUDA installed.'
# constants
NUM_CORES = multiprocessing.cpu_count()
EXTS = ['jpg', 'jpeg', 'png']
# helpers
def exists(val):
return val is not None
@contextmanager
def null_context():
yield
def combine_contexts(contexts):
@contextmanager
def multi_contexts():
with ExitStack() as stack:
yield [stack.enter_context(ctx()) for ctx in contexts]
return multi_contexts
def is_power_of_two(val):
return log2(val).is_integer()
def default(val, d):
return val if exists(val) else d
def set_requires_grad(model, bool):
for p in model.parameters():
p.requires_grad = bool
def cycle(iterable):
while True:
for i in iterable:
yield i
def raise_if_nan(t):
if torch.isnan(t):
raise NanException
def gradient_accumulate_contexts(gradient_accumulate_every, is_ddp, ddps):
if is_ddp:
num_no_syncs = gradient_accumulate_every - 1
head = [combine_contexts(
map(lambda ddp: ddp.no_sync, ddps))] * num_no_syncs
tail = [null_context]
contexts = head + tail
else:
contexts = [null_context] * gradient_accumulate_every
for context in contexts:
with context():
yield
def hinge_loss(real, fake):
return (F.relu(1 + real) + F.relu(1 - fake)).mean()
def evaluate_in_chunks(max_batch_size, model, y=None, *args):
if y is not None: args = (*args, y)
split_args = list(
zip(*list(map(lambda x: x.split(max_batch_size, dim=0), args))))
chunked_outputs = [model(*i) for i in split_args]
chunked_classes = [x[1] for x in chunked_outputs] # TODO: return it?
chunked_outputs = [x[0] for x in chunked_outputs]
if len(chunked_outputs) == 1:
return chunked_outputs[0]
return torch.cat(chunked_outputs, dim=0)
def slerp(val, low, high):
low_norm = low / torch.norm(low, dim=1, keepdim=True)
high_norm = high / torch.norm(high, dim=1, keepdim=True)
omega = torch.acos((low_norm * high_norm).sum(1))
so = torch.sin(omega)
res = (torch.sin((1.0 - val) * omega) / so).unsqueeze(1) * \
low + (torch.sin(val * omega) / so).unsqueeze(1) * high
return res
def safe_div(n, d):
try:
res = n / d
except ZeroDivisionError:
prefix = '' if int(n >= 0) else '-'
res = float(f'{prefix}inf')
return res
# helper classes
class NanException(Exception):
pass
class EMA():
def __init__(self, beta):
super().__init__()
self.beta = beta
def update_average(self, old, new):
if not exists(old):
return new
return old * self.beta + (1 - self.beta) * new
class RandomApply(nn.Module):
def __init__(self, prob, fn, fn_else=lambda x: x):
super().__init__()
self.fn = fn
self.fn_else = fn_else
self.prob = prob
def forward(self, x):
fn = self.fn if random() < self.prob else self.fn_else
return fn(x)
class Rezero(nn.Module):
def __init__(self, fn):
super().__init__()
self.fn = fn
self.g = nn.Parameter(torch.tensor(1e-3))
def forward(self, x):
return self.g * self.fn(x)
class Residual(nn.Module):
def __init__(self, fn):
super().__init__()
self.fn = fn
def forward(self, x):
return self.fn(x) + x
class SumBranches(nn.Module):
def __init__(self, branches):
super().__init__()
self.branches = nn.ModuleList(branches)
def forward(self, x):
return sum(map(lambda fn: fn(x), self.branches))
class Blur(nn.Module):
def __init__(self):
super().__init__()
f = torch.Tensor([1, 2, 1])
self.register_buffer('f', f)
def forward(self, x):
f = self.f
f = f[None, None, :] * f[None, :, None]
return filter2D(x, f, normalized=True)
# dataset
def convert_image_to(img_type, image):
if image.mode != img_type:
return image.convert(img_type)
return image
class identity(object):
def __call__(self, tensor):
return tensor
# augmentations
def random_hflip(tensor, prob):
if prob > random():
return tensor
return torch.flip(tensor, dims=(3,))
class AugWrapper(nn.Module):
def __init__(self, D, image_size):
super().__init__()
self.D = D
def forward(self, images, y=None, prob=0., types=[], detach=False, **kwargs):
context = torch.no_grad if detach else null_context
with context():
if random() < prob:
images = random_hflip(images, prob=0.5)
images = DiffAugment(images, types=types)
return self.D(images, y, **kwargs)
# modifiable global variables
# TODO: make sure this is placeholder
norm_class = nn.BatchNorm2d
def upsample(scale_factor=2):
return nn.Upsample(scale_factor=scale_factor)
# squeeze excitation classes
# global context network
# https://arxiv.org/abs/2012.13375
# similar to squeeze-excite, but with a simplified attention pooling and a subsequent layer norm
class GlobalContext(nn.Module):
def __init__(
self,
*,
chan_in,
chan_out
):
super().__init__()
self.to_k = nn.Conv2d(chan_in, 1, 1)
chan_intermediate = max(3, chan_out // 2)
self.net = nn.Sequential(
nn.Conv2d(chan_in, chan_intermediate, 1),
nn.LeakyReLU(0.1),
nn.Conv2d(chan_intermediate, chan_out, 1),
nn.Sigmoid()
)
def forward(self, x):
context = self.to_k(x)
context = context.flatten(2).softmax(dim=-1)
out = einsum('b i n, b c n -> b c i', context, x.flatten(2))
out = out.unsqueeze(-1)
return self.net(out)
# frequency channel attention
# https://arxiv.org/abs/2012.11879
def get_1d_dct(i, freq, L):
result = math.cos(math.pi * freq * (i + 0.5) / L) / math.sqrt(L)
return result * (1 if freq == 0 else math.sqrt(2))
def get_dct_weights(width, channel, fidx_u, fidx_v):
dct_weights = torch.zeros(1, channel, width, width)
c_part = channel // len(fidx_u)
for i, (u_x, v_y) in enumerate(zip(fidx_u, fidx_v)):
for x in range(width):
for y in range(width):
coor_value = get_1d_dct(x, u_x, width) * \
get_1d_dct(y, v_y, width)
dct_weights[:, i * c_part: (i + 1) * c_part, x, y] = coor_value
return dct_weights
class FCANet(nn.Module):
def __init__(
self,
*,
chan_in,
chan_out,
reduction=4,
width
):
super().__init__()
# in paper, it seems 16 frequencies was ideal
freq_w, freq_h = ([0] * 8), list(range(8))
dct_weights = get_dct_weights(
width, chan_in, [*freq_w, *freq_h], [*freq_h, *freq_w])
self.register_buffer('dct_weights', dct_weights)
chan_intermediate = max(3, chan_out // reduction)
self.net = nn.Sequential(
nn.Conv2d(chan_in, chan_intermediate, 1),
nn.LeakyReLU(0.1),
nn.Conv2d(chan_intermediate, chan_out, 1),
nn.Sigmoid()
)
def forward(self, x):
x = reduce(x * self.dct_weights,
'b c (h h1) (w w1) -> b c h1 w1', 'sum', h1=1, w1=1)
return self.net(x)
# generative adversarial network
class GenSeq(nn.Module):
def __init__(self, chan_in, chan_out, num_classes=0):
super().__init__()
if num_classes > 0:
self.norm = CategoricalConditionalBatchNorm2d(
num_classes, chan_out*2)
else:
self.norm = norm_class(chan_out * 2)
self.prenorm = nn.Sequential(upsample(), Blur(), nn.Conv2d(
chan_in, chan_out * 2, 3, padding=1))
self.postnorm = nn.GLU(dim=1)
def forward(self, x, y=None):
x = self.prenorm(x)
x = self.norm(x) if y is None else self.norm(x, y)
return self.postnorm(x)
class Generator(nn.Module):
def __init__(
self,
*,
image_size,
latent_dim=256,
fmap_max=512,
fmap_inverse_coef=12,
num_chans=3,
attn_res_layers=[],
freq_chan_attn=False,
num_classes=0,
):
super().__init__()
resolution = log2(image_size)
assert is_power_of_two(image_size), 'image size must be a power of 2'
init_channel = num_chans
fmap_max = default(fmap_max, latent_dim)
self.init_conv = nn.Sequential(
nn.ConvTranspose2d(latent_dim, latent_dim * 2, 4),
norm_class(latent_dim * 2),
nn.GLU(dim=1)
)
self.num_classes = num_classes
num_layers = int(resolution) - 2
features = list(
map(lambda n: (n, 2 ** (fmap_inverse_coef - n)), range(2, num_layers + 2)))
features = list(map(lambda n: (n[0], min(n[1], fmap_max)), features))
features = list(map(lambda n: 3 if n[0] >= 8 else n[1], features)) # TODO: should it be num chans?
features = [latent_dim, *features]
in_out_features = list(zip(features[:-1], features[1:]))
self.res_layers = range(2, num_layers + 2)
self.layers = nn.ModuleList([])
self.res_to_feature_map = dict(zip(self.res_layers, in_out_features))
self.sle_map = ((3, 7), (4, 8), (5, 9), (6, 10))
self.sle_map = list(
filter(lambda t: t[0] <= resolution and t[1] <= resolution, self.sle_map))
self.sle_map = dict(self.sle_map)
self.num_layers_spatial_res = 1
for (res, (chan_in, chan_out)) in zip(self.res_layers, in_out_features):
image_width = 2 ** res
attn = None
if image_width in attn_res_layers:
attn = Rezero(GSA(dim=chan_in, norm_queries=True))
sle = None
if res in self.sle_map:
residual_layer = self.sle_map[res]
sle_chan_out = self.res_to_feature_map[residual_layer - 1][-1]
if freq_chan_attn:
sle = FCANet(
chan_in=chan_out,
chan_out=sle_chan_out,
width=2 ** (res + 1)
)
else:
sle = GlobalContext(
chan_in=chan_out,
chan_out=sle_chan_out
)
layer = nn.ModuleList([
GenSeq(chan_in=chan_in, chan_out=chan_out, num_classes=num_classes),
sle,
attn
])
self.layers.append(layer)
self.out_conv = nn.Conv2d(features[-1], init_channel, 3, padding=1)
def forward(self, x, y=None, start=0):
if start == 0:
x = rearrange(x, 'b c -> b c () ()')
x = self.init_conv(x) # HERE
x = F.normalize(x, dim=1)
residuals = dict()
if self.num_classes > 0 and y is None:
y = torch.randint(self.num_classes, x.shape[:1], device="cuda")
for (res, (up, sle, attn)) in zip(self.res_layers, self.layers):
if start > 1:
start -= 1
continue
if exists(attn):
x = attn(x) + x
x = up(x, y)
if exists(sle):
out_res = self.sle_map[res]
residual = sle(x)
residuals[out_res] = residual
next_res = res + 1
if next_res in residuals:
x = x * residuals[next_res]
return self.out_conv(x), y
class SimpleDecoder(nn.Module):
def __init__(
self,
*,
chan_in,
chan_out=3,
num_upsamples=4,
end_glu=True,
):
super().__init__()
self.layers = nn.ModuleList([])
final_chan = chan_out
chans = chan_in
for ind in range(num_upsamples):
last_layer = ind == (num_upsamples - 1)
chan_out = chans if (not last_layer or end_glu) else final_chan * 2
layer = nn.Sequential(
upsample(),
nn.Conv2d(chans, chan_out, 3, padding=1),
nn.GLU(dim=1)
)
self.layers.append(layer)
chans //= 2
if end_glu:
self.layers.append(nn.Conv2d(chans, final_chan, 3, padding=1))
def forward(self, x):
for layer in self.layers:
x = layer(x)
return x
class Discriminator(nn.Module):
def __init__(
self,
*,
image_size,
fmap_max=512,
fmap_inverse_coef=12,
num_chans=3,
disc_output_size=5,
attn_res_layers=[],
num_classes=0,
bn4decoder=False,
projection_loss_scale=1
):
super().__init__()
resolution = log2(image_size)
assert is_power_of_two(image_size), 'image size must be a power of 2'
assert disc_output_size in {
1, 5}, 'discriminator output dimensions can only be 5x5 or 1x1'
resolution = int(resolution)
init_channel = num_chans
num_non_residual_layers = max(0, int(resolution) - 8)
num_residual_layers = 8 - 3
non_residual_resolutions = range(min(8, resolution), 2, -1)
features = list(
map(lambda n: (n, 2 ** (fmap_inverse_coef - n)), non_residual_resolutions))
features = list(map(lambda n: (n[0], min(n[1], fmap_max)), features))
if num_non_residual_layers == 0:
res, _ = features[0]
features[0] = (res, init_channel)
chan_in_out = list(zip(features[:-1], features[1:]))
self.non_residual_layers = nn.ModuleList([])
for ind in range(num_non_residual_layers):
first_layer = ind == 0
last_layer = ind == (num_non_residual_layers - 1)
chan_out = features[0][-1] if last_layer else init_channel
self.non_residual_layers.append(nn.Sequential(
Blur(),
nn.Conv2d(init_channel, chan_out, 4, stride=2, padding=1),
nn.LeakyReLU(0.1)
))
self.residual_layers = nn.ModuleList([])
for (res, ((_, chan_in), (_, chan_out))) in zip(non_residual_resolutions, chan_in_out):
image_width = 2 ** res # res vs resolution
attn = None
if image_width in attn_res_layers:
attn = Rezero(
GSA(dim=chan_in, batch_norm=False, norm_queries=True))
self.residual_layers.append(nn.ModuleList([
SumBranches([
nn.Sequential(
Blur(),
nn.Conv2d(chan_in, chan_out, 4, stride=2, padding=1),
nn.LeakyReLU(0.1),
nn.Conv2d(chan_out, chan_out, 3, padding=1),
nn.LeakyReLU(0.1)
),
nn.Sequential(
Blur(),
nn.AvgPool2d(2),
nn.Conv2d(chan_in, chan_out, 1),
nn.LeakyReLU(0.1),
)
]),
attn
]))
last_chan = features[-1][-1]
if disc_output_size == 5:
raise NotImplementedError # though on projection
self.to_pre_logits = nn.Sequential(
nn.Conv2d(last_chan, last_chan, 1),
nn.LeakyReLU(0.1),
nn.Conv2d(last_chan, 1, 4)
)
elif disc_output_size == 1:
self.to_pre_logits = nn.Sequential(
Blur(),
nn.Conv2d(last_chan, last_chan, 3, stride=2, padding=1),
nn.LeakyReLU(0.1),
nn.Conv2d(last_chan, last_chan, 4),
nn.LeakyReLU(0.1),
)
self.out = nn.Linear(last_chan, 1)
self.to_shape_disc_out = nn.Sequential(
nn.Conv2d(init_channel, 64, 3, padding=1),
Residual(Rezero(GSA(dim=64, norm_queries=True, batch_norm=False))),
SumBranches([
nn.Sequential(
Blur(),
nn.Conv2d(64, 32, 4, stride=2, padding=1),
nn.LeakyReLU(0.1),
nn.Conv2d(32, 32, 3, padding=1),
nn.LeakyReLU(0.1)
),
nn.Sequential(
Blur(),
nn.AvgPool2d(2),
nn.Conv2d(64, 32, 1),
nn.LeakyReLU(0.1),
)
]),
Residual(Rezero(GSA(dim=32, norm_queries=True, batch_norm=False))),
nn.AdaptiveAvgPool2d((4, 4)),
nn.Conv2d(32, 1, 4)
)
self.decoder1 = SimpleDecoder(
chan_in=last_chan, chan_out=init_channel, end_glu=bn4decoder)
self.decoder2 = SimpleDecoder(
chan_in=features[-2][-1], chan_out=init_channel, end_glu=bn4decoder) if resolution >= 9 else None
if num_classes > 0:
self.l_y = nn.utils.spectral_norm(
nn.Embedding(num_classes, last_chan))
self._initialize()
self.projection_loss_scale = projection_loss_scale
def _initialize(self):
optional_l_y = getattr(self, 'l_y', None)
if optional_l_y is not None:
nn.init.xavier_uniform_(optional_l_y.weight.data)
def forward(self, x, y=None, calc_aux_loss=False):
orig_img = x
for layer in self.non_residual_layers:
x = layer(x)
layer_outputs = []
for (net, attn) in self.residual_layers:
if exists(attn):
x = attn(x) + x
x = net(x)
layer_outputs.append(x)
x = self.to_pre_logits(x).flatten(1)
out = self.out(x)
# HERE V
if y is not None:
out += torch.sum(self.l_y(y) * x, dim=1, keepdim=True) * self.projection_loss_scale
img_32x32 = F.interpolate(orig_img, size=(32, 32))
out_32x32 = self.to_shape_disc_out(img_32x32)
if not calc_aux_loss:
return out, out_32x32, None
# self-supervised auto-encoding loss
layer_8x8 = layer_outputs[-1]
layer_16x16 = layer_outputs[-2]
recon_img_8x8 = self.decoder1(layer_8x8)
aux_loss = F.mse_loss(
recon_img_8x8,
F.interpolate(orig_img,
size=recon_img_8x8.shape[2:])
)
if exists(self.decoder2):
def select_random_quadrant(rand_quadrant, img): return rearrange(
img, 'b c (m h) (n w) -> (m n) b c h w', m=2, n=2)[rand_quadrant]
crop_image_fn = partial(
select_random_quadrant, floor(random() * 4))
img_part, layer_16x16_part = map(
crop_image_fn, (orig_img, layer_16x16))
recon_img_16x16 = self.decoder2(layer_16x16_part)
aux_loss_16x16 = F.mse_loss(
recon_img_16x16,
F.interpolate(img_part,
size=recon_img_16x16.shape[2:])
)
aux_loss = aux_loss + aux_loss_16x16
# TODO: output vs aux loss? I think output, generator is bakcproped on it
return out, out_32x32, aux_loss
class LightweightGAN(nn.Module):
def __init__(
self,
*,
latent_dim,
image_size,
optimizer="adam",
fmap_max=512,
fmap_inverse_coef=12,
num_chans=3,
disc_output_size=5,
attn_res_layers=[],
freq_chan_attn=False,
ttur_mult=1.,
lr=2e-4,
rank=0,
ddp=False,
num_classes=0,
projection_loss_scale=1,
):
print(num_chans)
super().__init__()
self.latent_dim = latent_dim
self.image_size = image_size
G_kwargs = dict(
image_size=image_size,
latent_dim=latent_dim,
fmap_max=fmap_max,
fmap_inverse_coef=fmap_inverse_coef,
num_chans=num_chans,
attn_res_layers=attn_res_layers,
freq_chan_attn=freq_chan_attn,
num_classes=num_classes,
)
self.G = Generator(**G_kwargs)
self.D = Discriminator(
image_size=image_size,
fmap_max=fmap_max,
fmap_inverse_coef=fmap_inverse_coef,
num_chans=num_chans,
attn_res_layers=attn_res_layers,
disc_output_size=disc_output_size,
num_classes=num_classes,
projection_loss_scale=projection_loss_scale,
)
self.ema_updater = EMA(0.995)
self.GE = Generator(**G_kwargs)
set_requires_grad(self.GE, False)
if optimizer == "adam":
self.G_opt = Adam(self.G.parameters(), lr=lr, betas=(0.5, 0.9))
self.D_opt = Adam(self.D.parameters(), lr=lr *
ttur_mult, betas=(0.5, 0.9))
elif optimizer == "adabelief":
self.G_opt = AdaBelief(self.G.parameters(), print_change_log=False,
lr=lr, betas=(0.5, 0.9))
self.D_opt = AdaBelief(self.D.parameters(), print_change_log=False,
lr=lr * ttur_mult, betas=(0.5, 0.9))
else:
assert False, "No valid optimizer is given"
self.apply(self._init_weights)
self.reset_parameter_averaging()
self.cuda(rank)
self.D_aug = AugWrapper(self.D, image_size)
def _init_weights(self, m):
if type(m) in {nn.Conv2d, nn.Linear}:
nn.init.kaiming_normal_(
m.weight, a=0, mode='fan_in', nonlinearity='leaky_relu')
def EMA(self):
def update_moving_average(ma_model, current_model):
for current_params, ma_params in zip(current_model.parameters(), ma_model.parameters()):
old_weight, up_weight = ma_params.data, current_params.data
ma_params.data = self.ema_updater.update_average(
old_weight, up_weight)
for current_buffer, ma_buffer in zip(current_model.buffers(), ma_model.buffers()):
new_buffer_value = self.ema_updater.update_average(
ma_buffer, current_buffer)
ma_buffer.copy_(new_buffer_value)
update_moving_average(self.GE, self.G)
def reset_parameter_averaging(self):
self.GE.load_state_dict(self.G.state_dict())
def forward(self, latents, detach_gen=True, **aug_kwargs):
context = torch.no_grad if detach_gen else null_context
with context():
generated_images, y = self.G(latents)
return self.D_aug(
generated_images, y, detach=detach_gen, **aug_kwargs)
# trainer
class Trainer():
def __init__(
self,
name='default',
results_dir='results',
models_dir='models',
base_dir='./',
optimizer='adam',
num_workers=None,
latent_dim=256,
image_size=128,
num_image_tiles=8,
fmap_max=512,
num_chans=3,
batch_size=4,
gp_weight=10,
gradient_accumulate_every=1,
attn_res_layers=[],
freq_chan_attn=False,
disc_output_size=5,
antialias=False,
lr=2e-4,
lr_mlp=1.,
ttur_mult=1.,
save_every=1000,
evaluate_every=1000,
aug_prob=None,
aug_types=['translation', 'cutout'],
dataset_aug_prob=0.,
rank=0,
world_size=1,
multi_gpus=False,
num_classes=0,
aux_loss_multi=0.04,
projection_loss_scale=1,
shuffle=False,
*args,
**kwargs
):
self.GAN_params = [args, kwargs]
self.GAN = None
self.name = name
base_dir = Path(base_dir)
self.base_dir = base_dir
self.results_dir = base_dir / results_dir
self.models_dir = base_dir / models_dir
self.config_path = self.models_dir / name / '.config.json'
assert is_power_of_two(
image_size), 'image size must be a power of 2 (64, 128, 256, 512, 1024)'
assert all(map(is_power_of_two, attn_res_layers)
), 'resolution layers of attention must all be powers of 2 (16, 32, 64, 128, 256, 512)'
self.image_size = image_size
self.num_image_tiles = num_image_tiles
self.latent_dim = latent_dim
self.fmap_max = fmap_max
self.num_chans = num_chans
self.aug_prob = aug_prob
self.aug_types = aug_types
self.lr = lr
self.optimizer = optimizer
self.num_workers = num_workers
self.ttur_mult = ttur_mult
self.batch_size = batch_size
self.gradient_accumulate_every = gradient_accumulate_every
self.gp_weight = gp_weight
self.evaluate_every = evaluate_every
self.save_every = save_every
self.steps = 0
self.generator_top_k_gamma = 0.99
self.generator_top_k_frac = 0.5
self.attn_res_layers = attn_res_layers
self.freq_chan_attn = freq_chan_attn
self.disc_output_size = disc_output_size
self.antialias = antialias
self.d_loss = 0
self.g_loss = 0
self.last_gp_loss = None
self.last_recon_loss = None
self.init_folders()
self.loader = None
self.dataset_aug_prob = dataset_aug_prob
self.is_main = rank == 0
self.rank = rank
self.world_size = world_size
self.multi_gpus = multi_gpus
self.num_classes = num_classes
self.aux_loss_multi = aux_loss_multi
self.projection_loss_scale = projection_loss_scale
self.shuffle = shuffle
@property
def image_extension(self):
return 'jpg'
@property
def checkpoint_num(self):
return floor(self.steps // self.save_every)
def init_GAN(self):
args, kwargs = self.GAN_params
# set some global variables before instantiating GAN
global norm_class
global Blur
# TODO: KOKO
norm_class = nn.BatchNorm2d
Blur = nn.Identity if not self.antialias else Blur
# handle bugs when
# switching from multi-gpu back to single gpu
# instantiate GAN
self.GAN = LightweightGAN(
optimizer=self.optimizer,
lr=self.lr,
latent_dim=self.latent_dim,
attn_res_layers=self.attn_res_layers,
freq_chan_attn=self.freq_chan_attn,
image_size=self.image_size,
ttur_mult=self.ttur_mult,
fmap_max=self.fmap_max,
disc_output_size=self.disc_output_size,
rank=self.rank,
num_classes=self.num_classes,
projection_loss_scale=self.projection_loss_scale,
num_chans=self.num_chans,
*args,
**kwargs
)
self.parallel()
def write_config(self):
self.config_path.write_text(json.dumps(self.config()))
def load_config(self):
config = self.config() if not self.config_path.exists(
) else json.loads(self.config_path.read_text())
self.image_size = config['image_size']
self.disc_output_size = config['disc_output_size']
self.num_chans = config.pop('num_chans', False)
self.attn_res_layers = config.pop('attn_res_layers', [])
self.freq_chan_attn = config.pop('freq_chan_attn', False)
self.optimizer = config.pop('optimizer', 'adam')
self.fmap_max = config.pop('fmap_max', 512)
del self.GAN
self.init_GAN()
def config(self):
return {
'image_size': self.image_size,
'num_chans': self.num_chans,
'disc_output_size': self.disc_output_size,
'optimizer': self.optimizer,
'attn_res_layers': self.attn_res_layers,
'freq_chan_attn': self.freq_chan_attn
}
def set_dataset(self, dataset, num_workers):
self.dataset = dataset
dataloader = DataLoader(self.dataset, num_workers=num_workers, shuffle=self.shuffle,
batch_size=self.batch_size, drop_last=True, pin_memory=True)
self.loader = cycle(dataloader)
def parallel(self):
self.parallel_D_aug = nn.DataParallel(
self.GAN.D_aug) if self.multi_gpus else self.GAN.D_aug
self.parallel_GD = nn.DataParallel(