-
Notifications
You must be signed in to change notification settings - Fork 26
/
encoder.py
executable file
·854 lines (742 loc) · 32.9 KB
/
encoder.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
# encoding: utf-8
#This code borrows Zhu et al.'s repository of the paper Toward Multimodal Image-to-Image Translation.
import torch
import torch.nn as nn
from torch.nn import init
from torch.autograd import Variable
import functools
from torch.optim import lr_scheduler
###############################################################################
# Functions
###############################################################################
def weights_init_normal(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
init.normal_(m.weight.data, 0.0, 0.02)
elif classname.find('Linear') != -1:
init.normal_(m.weight.data, 0.0, 0.02)
elif classname.find('BatchNorm2d') != -1:
init.normal_(m.weight.data, 1.0, 0.02)
init.constant_(m.bias.data, 0.0)
def weights_init_xavier(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
init.xavier_normal_(m.weight.data, gain=1.0)
elif classname.find('Linear') != -1:
init.xavier_normal_(m.weight.data, gain=1.0)
elif classname.find('BatchNorm2d') != -1:
init.normal_(m.weight.data, 1.0, 0.02)
init.constant_(m.bias.data, 0.0)
def weights_init_kaiming(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
init.kaiming_normal(m.weight.data, a=0, mode='fan_in')
elif classname.find('Linear') != -1:
init.kaiming_normal(m.weight.data, a=0, mode='fan_in')
elif classname.find('BatchNorm2d') != -1:
init.normal_(m.weight.data, 1.0, 0.02)
init.constant_(m.bias.data, 0.0)
def weights_init_orthogonal(m):
classname = m.__class__.__name__
print(classname)
if classname.find('Conv') != -1:
init.orthogonal(m.weight.data, gain=0.02)
elif classname.find('Linear') != -1:
init.orthogonal(m.weight.data, gain=0.02)
elif classname.find('BatchNorm2d') != -1:
init.normal_(m.weight.data, 1.0, 0.02)
init.constant_(m.bias.data, 0.0)
def init_weights(net, init_type='xavier'):
#print('initialization method [%s]' % init_type)
if init_type == 'normal':
net.apply(weights_init_normal)
elif init_type == 'xavier':
net.apply(weights_init_xavier)
elif init_type == 'kaiming':
net.apply(weights_init_kaiming)
elif init_type == 'orthogonal':
net.apply(weights_init_orthogonal)
else:
raise NotImplementedError(
'initialization method [%s] is not implemented' % init_type)
def get_scheduler(optimizer, opt):
if opt.lr_policy == 'lambda':
def lambda_rule(epoch):
lr_l = 1.0 - max(0, epoch - opt.niter) / float(opt.niter_decay + 1)
return lr_l
scheduler = lr_scheduler.LambdaLR(optimizer, lr_lambda=lambda_rule)
elif opt.lr_policy == 'step':
scheduler = lr_scheduler.StepLR(
optimizer, step_size=opt.lr_decay_iters, gamma=0.1)
elif opt.lr_policy == 'plateau':
scheduler = lr_scheduler.ReduceLROnPlateau(
optimizer, mode='min', factor=0.2, threshold=0.01, patience=5)
else:
return NotImplementedError('learning rate policy [%s] is not implemented', opt.lr_policy)
return scheduler
def get_norm_layer(layer_type='instance'):
if layer_type == 'batch':
norm_layer = functools.partial(nn.BatchNorm2d, affine=True)
elif layer_type == 'instance':
norm_layer = functools.partial(nn.InstanceNorm2d, affine=False)
elif layer_type == 'none':
norm_layer = None
else:
raise NotImplementedError(
'normalization layer [%s] is not found' % layer_type)
return norm_layer
def get_non_linearity(layer_type='relu'):
if layer_type == 'relu':
nl_layer = functools.partial(nn.ReLU, inplace=True)
elif layer_type == 'lrelu':
nl_layer = functools.partial(
nn.LeakyReLU, negative_slope=0.2, inplace=True)
elif layer_type == 'elu':
nl_layer = functools.partial(nn.ELU, inplace=True)
else:
raise NotImplementedError(
'nonlinearity activitation [%s] is not found' % layer_type)
return nl_layer
def define_G(input_nc, output_nc, nz, ngf,
which_model_netG='unet_128', norm='batch', nl='relu',
use_dropout=False, init_type='xavier', gpu_ids=[], where_add='input', upsample='bilinear'):
netG = None
use_gpu = len(gpu_ids) > 0
norm_layer = get_norm_layer(layer_type=norm)
nl_layer = get_non_linearity(layer_type=nl)
# upsample = 'bilinear'
if use_gpu:
assert(torch.cuda.is_available())
if nz == 0:
where_add = 'input'
if which_model_netG == 'unet_128' and where_add == 'input':
netG = G_Unet_add_input(input_nc, output_nc, nz, 7, ngf, norm_layer=norm_layer, nl_layer=nl_layer,
use_dropout=use_dropout, gpu_ids=gpu_ids, upsample=upsample)
elif which_model_netG == 'unet_256' and where_add == 'input':
netG = G_Unet_add_input(input_nc, output_nc, nz, 8, ngf, norm_layer=norm_layer, nl_layer=nl_layer,
use_dropout=use_dropout, gpu_ids=gpu_ids, upsample=upsample)
elif which_model_netG == 'unet_128' and where_add == 'all':
netG = G_Unet_add_all(input_nc, output_nc, nz, 7, ngf, norm_layer=norm_layer, nl_layer=nl_layer,
use_dropout=use_dropout, gpu_ids=gpu_ids, upsample=upsample)
elif which_model_netG == 'unet_256' and where_add == 'all':
netG = G_Unet_add_all(input_nc, output_nc, nz, 8, ngf, norm_layer=norm_layer, nl_layer=nl_layer,
use_dropout=use_dropout, gpu_ids=gpu_ids, upsample=upsample)
else:
raise NotImplementedError(
'Generator model name [%s] is not recognized' % which_model_netG)
if len(gpu_ids) > 0:
netG.cuda(gpu_ids[0])
init_weights(netG, init_type=init_type)
return netG
def define_D(input_nc, ndf, which_model_netD,
norm='batch', nl='lrelu',
use_sigmoid=False, init_type='xavier', num_Ds=1, gpu_ids=[]):
netD = None
use_gpu = len(gpu_ids) > 0
norm_layer = get_norm_layer(layer_type=norm)
nl = 'lrelu' # use leaky relu for D
nl_layer = get_non_linearity(layer_type=nl)
if use_gpu:
assert(torch.cuda.is_available())
if which_model_netD == 'basic_128':
netD = D_NLayers(input_nc, ndf, n_layers=2, norm_layer=norm_layer,
nl_layer=nl_layer, use_sigmoid=use_sigmoid, gpu_ids=gpu_ids)
elif which_model_netD == 'basic_256':
netD = D_NLayers(input_nc, ndf, n_layers=3, norm_layer=norm_layer,
nl_layer=nl_layer, use_sigmoid=use_sigmoid, gpu_ids=gpu_ids)
elif which_model_netD == 'basic_128_multi':
netD = D_NLayersMulti(input_nc=input_nc, ndf=ndf, n_layers=2, norm_layer=norm_layer,
use_sigmoid=use_sigmoid, gpu_ids=gpu_ids, num_D=num_Ds)
elif which_model_netD == 'basic_256_multi':
netD = D_NLayersMulti(input_nc=input_nc, ndf=ndf, n_layers=3, norm_layer=norm_layer,
use_sigmoid=use_sigmoid, gpu_ids=gpu_ids, num_D=num_Ds)
else:
raise NotImplementedError(
'Discriminator model name [%s] is not recognized' % which_model_netD)
if use_gpu:
netD.cuda(gpu_ids[0])
init_weights(netD, init_type=init_type)
return netD
def define_E(input_nc, output_nc, ndf, which_model_netE,
norm='batch', nl='lrelu',
init_type='xavier', gpu_ids=[], vaeLike=True):
netE = None
use_gpu = len(gpu_ids) > 0
norm_layer = get_norm_layer(layer_type=norm)
nl = 'lrelu' # use leaky relu for E
nl_layer = get_non_linearity(layer_type=nl)
if use_gpu:
assert(torch.cuda.is_available())
if which_model_netE == 'resnet_128':
netE = E_ResNet(input_nc, output_nc, ndf, n_blocks=4, norm_layer=norm_layer,
nl_layer=nl_layer, gpu_ids=gpu_ids, vaeLike=vaeLike)
elif which_model_netE == 'resnet_256':
netE = E_ResNet(input_nc, output_nc, ndf, n_blocks=5, norm_layer=norm_layer,
nl_layer=nl_layer, gpu_ids=gpu_ids, vaeLike=vaeLike)
elif which_model_netE == 'conv_128':
netE = E_NLayers(input_nc, output_nc, ndf, n_layers=4, norm_layer=norm_layer,
nl_layer=nl_layer, gpu_ids=gpu_ids, vaeLike=vaeLike)
elif which_model_netE == 'conv_256':
netE = E_NLayers(input_nc, output_nc, ndf, n_layers=5, norm_layer=norm_layer,
nl_layer=nl_layer, gpu_ids=gpu_ids, vaeLike=vaeLike)
else:
raise NotImplementedError(
'Encoder model name [%s] is not recognized' % which_model_netE)
if use_gpu:
netE.cuda(gpu_ids[0])
init_weights(netE, init_type=init_type)
return netE
class ListModule(object):
# should work with all kind of module
def __init__(self, module, prefix, *args):
self.module = module
self.prefix = prefix
self.num_module = 0
for new_module in args:
self.append(new_module)
def append(self, new_module):
if not isinstance(new_module, nn.Module):
raise ValueError('Not a Module')
else:
self.module.add_module(
self.prefix + str(self.num_module), new_module)
self.num_module += 1
def __len__(self):
return self.num_module
def __getitem__(self, i):
if i < 0 or i >= self.num_module:
raise IndexError('Out of bound')
return getattr(self.module, self.prefix + str(i))
class D_NLayersMulti(nn.Module):
def __init__(self, input_nc, ndf=64, n_layers=3,
norm_layer=nn.BatchNorm2d, use_sigmoid=False, gpu_ids=[], num_D=1):
super(D_NLayersMulti, self).__init__()
# st()
self.gpu_ids = gpu_ids
self.num_D = num_D
if num_D == 1:
layers = self.get_layers(
input_nc, ndf, n_layers, norm_layer, use_sigmoid)
self.model = nn.Sequential(*layers)
else:
self.model = ListModule(self, 'model')
layers = self.get_layers(
input_nc, ndf, n_layers, norm_layer, use_sigmoid)
self.model.append(nn.Sequential(*layers))
self.down = nn.AvgPool2d(3, stride=2, padding=[
1, 1], count_include_pad=False)
for i in range(num_D - 1):
ndf = int(round(ndf / (2**(i + 1))))
layers = self.get_layers(
input_nc, ndf, n_layers, norm_layer, use_sigmoid)
self.model.append(nn.Sequential(*layers))
def get_layers(self, input_nc, ndf=64, n_layers=3,
norm_layer=nn.BatchNorm2d, use_sigmoid=False):
kw = 4
padw = 1
sequence = [nn.Conv2d(input_nc, ndf, kernel_size=kw,
stride=2, padding=padw), nn.LeakyReLU(0.2, True)]
nf_mult = 1
nf_mult_prev = 1
for n in range(1, n_layers):
nf_mult_prev = nf_mult
nf_mult = min(2**n, 8)
sequence += [
nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult,
kernel_size=kw, stride=2, padding=padw),
norm_layer(ndf * nf_mult),
nn.LeakyReLU(0.2, True)
]
nf_mult_prev = nf_mult
nf_mult = min(2**n_layers, 8)
sequence += [
nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult,
kernel_size=kw, stride=1, padding=padw),
norm_layer(ndf * nf_mult),
nn.LeakyReLU(0.2, True)
]
sequence += [nn.Conv2d(ndf * nf_mult, 1,
kernel_size=kw, stride=1, padding=padw)]
if use_sigmoid:
sequence += [nn.Sigmoid()]
return sequence
def parallel_forward(self, model, input):
if self.gpu_ids and isinstance(input.data, torch.cuda.FloatTensor):
return nn.parallel.data_parallel(model, input, self.gpu_ids)
else:
return model(input)
def forward(self, input):
if self.num_D == 1:
return self.parallel_forward(self.model, input)
result = []
down = input
for i in range(self.num_D):
result.append(self.parallel_forward(self.model[i], down))
if i != self.num_D - 1:
down = self.parallel_forward(self.down, down)
return result
# Defines the conv discriminator with the specified arguments.
class G_NLayers(nn.Module):
def __init__(self, output_nc=3, nz=100, ngf=64, n_layers=3,
norm_layer=None, nl_layer=None, gpu_ids=[]):
super(G_NLayers, self).__init__()
self.gpu_ids = gpu_ids
kw, s, padw = 4, 2, 1
sequence = [nn.ConvTranspose2d(
nz, ngf * 4, kernel_size=kw, stride=1, padding=0, bias=True)]
if norm_layer is not None:
sequence += [norm_layer(ngf * 4)]
sequence += [nl_layer()]
nf_mult = 4
nf_mult_prev = 4
for n in range(n_layers, 0, -1):
nf_mult_prev = nf_mult
nf_mult = min(n, 4)
sequence += [nn.ConvTranspose2d(ngf * nf_mult_prev, ngf * nf_mult,
kernel_size=kw, stride=s, padding=padw, bias=True)]
if norm_layer is not None:
sequence += [norm_layer(ngf * nf_mult)]
sequence += [nl_layer()]
sequence += [nn.ConvTranspose2d(ngf, output_nc,
kernel_size=4, stride=s, padding=padw, bias=True)]
sequence += [nn.Tanh()]
self.model = nn.Sequential(*sequence)
def forward(self, input):
if len(self.gpu_ids) and isinstance(input.data, torch.cuda.FloatTensor):
return nn.parallel.data_parallel(self.model, input, self.gpu_ids)
else:
return self.model(input)
class D_NLayers(nn.Module):
def __init__(self, input_nc=3, ndf=64, n_layers=3,
norm_layer=None, nl_layer=None, use_sigmoid=False, gpu_ids=[]):
super(D_NLayers, self).__init__()
self.gpu_ids = gpu_ids
kw, padw, use_bias = 4, 1, True
# st()
sequence = [
nn.Conv2d(input_nc, ndf, kernel_size=kw,
stride=2, padding=padw, bias=use_bias),
nl_layer()
]
nf_mult = 1
nf_mult_prev = 1
for n in range(1, n_layers):
nf_mult_prev = nf_mult
nf_mult = min(2**n, 8)
sequence += [nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult,
kernel_size=kw, stride=2, padding=padw, bias=use_bias)]
if norm_layer is not None:
sequence += [norm_layer(ndf * nf_mult)]
sequence += [nl_layer()]
nf_mult_prev = nf_mult
nf_mult = min(2**n_layers, 8)
sequence += [
nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult,
kernel_size=kw, stride=1, padding=padw, bias=use_bias)]
if norm_layer is not None:
sequence += [norm_layer(ndf * nf_mult)]
sequence += [nl_layer()]
sequence += [nn.Conv2d(ndf * nf_mult, 1, kernel_size=4,
stride=1, padding=0, bias=use_bias)]
if use_sigmoid:
sequence += [nn.Sigmoid()]
self.model = nn.Sequential(*sequence)
def forward(self, input):
output = self.model(input)
return output
def print_network(net):
num_params = 0
for param in net.parameters():
num_params += param.numel()
print(net)
print('Total number of parameters: %d' % num_params)
##############################################################################
# Classes
##############################################################################
class RecLoss(nn.Module):
def __init__(self, use_L2=True):
super(RecLoss, self).__init__()
self.use_L2 = use_L2
def __call__(self, input, target, batch_mean=True):
if self.use_L2:
diff = (input - target) ** 2
else:
diff = torch.abs(input - target)
if batch_mean:
return torch.mean(diff)
else:
return torch.mean(torch.mean(torch.mean(diff, dim=1), dim=2), dim=3)
# Defines the GAN loss which uses either LSGAN or the regular GAN.
# When LSGAN is used, it is basically same as MSELoss,
# but it abstracts away the need to create the target label tensor
# that has the same size as the input
class GANLoss(nn.Module):
def __init__(self, mse_loss=True, target_real_label=1.0, target_fake_label=0.0,
tensor=torch.FloatTensor):
super(GANLoss, self).__init__()
self.real_label = target_real_label
self.fake_label = target_fake_label
self.real_label_var = None
self.fake_label_var = None
self.Tensor = tensor
if mse_loss:
self.loss = nn.MSELoss()
else:
self.loss = nn.BCELoss()
def get_target_tensor(self, input, target_is_real):
target_tensor = None
if target_is_real:
create_label = ((self.real_label_var is None) or
(self.real_label_var.numel() != input.numel()))
if create_label:
real_tensor = self.Tensor(input.size()).fill_(self.real_label)
self.real_label_var = Variable(
real_tensor, requires_grad=False)
target_tensor = self.real_label_var
else:
create_label = ((self.fake_label_var is None) or
(self.fake_label_var.numel() != input.numel()))
if create_label:
fake_tensor = self.Tensor(input.size()).fill_(self.fake_label)
self.fake_label_var = Variable(
fake_tensor, requires_grad=False)
target_tensor = self.fake_label_var
return target_tensor
def __call__(self, inputs, target_is_real):
# if input is a list
loss = 0.0
all_losses = []
for input in inputs:
target_tensor = self.get_target_tensor(input, target_is_real)
loss_input = self.loss(input, target_tensor)
loss = loss + loss_input
all_losses.append(loss_input)
# st()
return loss, all_losses
# Defines the Unet generator.
# |num_downs|: number of downsamplings in UNet. For example,
# if |num_downs| == 7, image of size 128x128 will become of size 1x1
# at the bottleneck
class G_Unet_add_input(nn.Module):
def __init__(self, input_nc, output_nc, nz, num_downs, ngf=64,
norm_layer=None, nl_layer=None, use_dropout=False,
gpu_ids=[], upsample='basic'):
super(G_Unet_add_input, self).__init__()
self.gpu_ids = gpu_ids
self.nz = nz
# currently support only input_nc == output_nc
# assert(input_nc == output_nc)
max_nchn = 8
# construct unet structure
unet_block = UnetBlock(ngf * max_nchn, ngf * max_nchn, ngf * max_nchn,
innermost=True, norm_layer=norm_layer, nl_layer=nl_layer, upsample=upsample)
for i in range(num_downs - 5):
unet_block = UnetBlock(ngf * max_nchn, ngf * max_nchn, ngf * max_nchn, unet_block,
norm_layer=norm_layer, nl_layer=nl_layer, use_dropout=use_dropout, upsample=upsample)
unet_block = UnetBlock(ngf * 4, ngf * 4, ngf * max_nchn, unet_block,
norm_layer=norm_layer, nl_layer=nl_layer, upsample=upsample)
unet_block = UnetBlock(ngf * 2, ngf * 2, ngf * 4, unet_block,
norm_layer=norm_layer, nl_layer=nl_layer, upsample=upsample)
unet_block = UnetBlock(ngf, ngf, ngf * 2, unet_block,
norm_layer=norm_layer, nl_layer=nl_layer, upsample=upsample)
unet_block = UnetBlock(input_nc + nz, output_nc, ngf, unet_block,
outermost=True, norm_layer=norm_layer, nl_layer=nl_layer, upsample=upsample)
self.model = unet_block
def forward(self, x, z=None):
if self.nz > 0:
z_img = z.view(z.size(0), z.size(1), 1, 1).expand(
z.size(0), z.size(1), x.size(2), x.size(3))
x_with_z = torch.cat([x, z_img], 1)
else:
x_with_z = x # no z
return self.model(x_with_z)
def upsampleLayer(inplanes, outplanes, upsample='basic', padding_type='zero'):
# padding_type = 'zero'
if upsample == 'basic':
upconv = [nn.ConvTranspose2d(
inplanes, outplanes, kernel_size=4, stride=2, padding=1)]
elif upsample == 'bilinear':
upconv = [nn.Upsample(scale_factor=2, mode='bilinear'),
nn.ReflectionPad2d(1),
nn.Conv2d(inplanes, outplanes, kernel_size=3, stride=1, padding=0)]
else:
raise NotImplementedError(
'upsample layer [%s] not implemented' % upsample)
return upconv
# Defines the submodule with skip connection.
# X -------------------identity---------------------- X
# |-- downsampling -- |submodule| -- upsampling --|
class UnetBlock(nn.Module):
def __init__(self, input_nc, outer_nc, inner_nc,
submodule=None, outermost=False, innermost=False,
norm_layer=None, nl_layer=None, use_dropout=False, upsample='basic', padding_type='zero'):
super(UnetBlock, self).__init__()
self.outermost = outermost
p = 0
downconv = []
if padding_type == 'reflect':
downconv += [nn.ReflectionPad2d(1)]
elif padding_type == 'replicate':
downconv += [nn.ReplicationPad2d(1)]
elif padding_type == 'zero':
p = 1
else:
raise NotImplementedError(
'padding [%s] is not implemented' % padding_type)
downconv += [nn.Conv2d(input_nc, inner_nc,
kernel_size=4, stride=2, padding=p)]
# downsample is different from upsample
downrelu = nn.LeakyReLU(0.2, True)
downnorm = norm_layer(inner_nc) if norm_layer is not None else None
uprelu = nl_layer()
upnorm = norm_layer(outer_nc) if norm_layer is not None else None
if outermost:
upconv = upsampleLayer(
inner_nc * 2, outer_nc, upsample=upsample, padding_type=padding_type)
down = downconv
up = [uprelu] + upconv + [nn.Tanh()]
model = down + [submodule] + up
elif innermost:
upconv = upsampleLayer(
inner_nc, outer_nc, upsample=upsample, padding_type=padding_type)
down = [downrelu] + downconv
up = [uprelu] + upconv
if upnorm is not None:
up += [upnorm]
model = down + up
else:
upconv = upsampleLayer(
inner_nc * 2, outer_nc, upsample=upsample, padding_type=padding_type)
down = [downrelu] + downconv
if downnorm is not None:
down += [downnorm]
up = [uprelu] + upconv
if upnorm is not None:
up += [upnorm]
if use_dropout:
model = down + [submodule] + up + [nn.Dropout(0.5)]
else:
model = down + [submodule] + up
self.model = nn.Sequential(*model)
def forward(self, x):
if self.outermost:
return self.model(x)
else:
return torch.cat([self.model(x), x], 1)
def conv3x3(in_planes, out_planes):
return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=1,
padding=1, bias=True)
# two usage cases, depend on kw and padw
def upsampleConv(inplanes, outplanes, kw, padw):
sequence = []
sequence += [nn.Upsample(scale_factor=2, mode='nearest')]
sequence += [nn.Conv2d(inplanes, outplanes, kernel_size=kw,
stride=1, padding=padw, bias=True)]
return nn.Sequential(*sequence)
def meanpoolConv(inplanes, outplanes):
sequence = []
sequence += [nn.AvgPool2d(kernel_size=2, stride=2)]
sequence += [nn.Conv2d(inplanes, outplanes,
kernel_size=1, stride=1, padding=0, bias=True)]
return nn.Sequential(*sequence)
def convMeanpool(inplanes, outplanes):
sequence = []
sequence += [conv3x3(inplanes, outplanes)]
sequence += [nn.AvgPool2d(kernel_size=2, stride=2)]
return nn.Sequential(*sequence)
class BasicBlockUp(nn.Module):
def __init__(self, inplanes, outplanes, norm_layer=None, nl_layer=None):
super(BasicBlockUp, self).__init__()
layers = []
if norm_layer is not None:
layers += [norm_layer(inplanes)]
layers += [nl_layer()]
layers += [upsampleConv(inplanes, outplanes, kw=3, padw=1)]
if norm_layer is not None:
layers += [norm_layer(outplanes)]
layers += [conv3x3(outplanes, outplanes)]
self.conv = nn.Sequential(*layers)
self.shortcut = upsampleConv(inplanes, outplanes, kw=1, padw=0)
def forward(self, x):
out = self.conv(x) + self.shortcut(x)
return out
class BasicBlock(nn.Module):
def __init__(self, inplanes, outplanes, norm_layer=None, nl_layer=None):
super(BasicBlock, self).__init__()
layers = []
if norm_layer is not None:
layers += [norm_layer(inplanes)]
layers += [nl_layer()]
layers += [conv3x3(inplanes, inplanes)]
if norm_layer is not None:
layers += [norm_layer(inplanes)]
layers += [nl_layer()]
layers += [convMeanpool(inplanes, outplanes)]
self.conv = nn.Sequential(*layers)
self.shortcut = meanpoolConv(inplanes, outplanes)
def forward(self, x):
out = self.conv(x) + self.shortcut(x)
return out
class E_ResNet(nn.Module):
def __init__(self, input_nc=3, output_nc=1, ndf=64, n_blocks=4,
norm_layer=None, nl_layer=None, gpu_ids=[], vaeLike=False):
super(E_ResNet, self).__init__()
self.gpu_ids = gpu_ids
self.vaeLike = vaeLike
max_ndf = 4
conv_layers = [
nn.Conv2d(input_nc, ndf, kernel_size=4, stride=2, padding=1, bias=True)]
for n in range(1, n_blocks):
input_ndf = ndf * min(max_ndf, n)
output_ndf = ndf * min(max_ndf, n + 1)
conv_layers += [BasicBlock(input_ndf,
output_ndf, norm_layer, nl_layer)]
conv_layers += [nl_layer(), nn.AvgPool2d(8)]
if vaeLike:
self.fc = nn.Sequential(*[nn.Linear(output_ndf, output_nc)])
self.fcVar = nn.Sequential(*[nn.Linear(output_ndf, output_nc)])
else:
self.fc = nn.Sequential(*[nn.Linear(output_ndf, output_nc)])
self.conv = nn.Sequential(*conv_layers)
def forward(self, x):
x_conv = self.conv(x)
conv_flat = x_conv.view(x.size(0), -1)
output = self.fc(conv_flat)
if self.vaeLike:
outputVar = self.fcVar(conv_flat)
return output, outputVar
else:
return output
return output
# Defines the Unet generator.
# |num_downs|: number of downsamplings in UNet. For example,
# if |num_downs| == 7, image of size 128x128 will become of size 1x1
# at the bottleneck
class G_Unet_add_all(nn.Module):
def __init__(self, input_nc, output_nc, nz, num_downs, ngf=64,
norm_layer=None, nl_layer=None, use_dropout=False, gpu_ids=[], upsample='basic'):
super(G_Unet_add_all, self).__init__()
self.gpu_ids = gpu_ids
self.nz = nz
# construct unet structure
unet_block = UnetBlock_with_z(ngf * 8, ngf * 8, ngf * 8, nz, None, innermost=True,
norm_layer=norm_layer, nl_layer=nl_layer, upsample=upsample)
unet_block = UnetBlock_with_z(ngf * 8, ngf * 8, ngf * 8, nz, unet_block,
norm_layer=norm_layer, nl_layer=nl_layer, use_dropout=use_dropout, upsample=upsample)
for i in range(num_downs - 6):
unet_block = UnetBlock_with_z(ngf * 8, ngf * 8, ngf * 8, nz, unet_block,
norm_layer=norm_layer, nl_layer=nl_layer, use_dropout=use_dropout, upsample=upsample)
unet_block = UnetBlock_with_z(ngf * 4, ngf * 4, ngf * 8, nz, unet_block,
norm_layer=norm_layer, nl_layer=nl_layer, upsample=upsample)
unet_block = UnetBlock_with_z(ngf * 2, ngf * 2, ngf * 4, nz, unet_block,
norm_layer=norm_layer, nl_layer=nl_layer, upsample=upsample)
unet_block = UnetBlock_with_z(
ngf, ngf, ngf * 2, nz, unet_block, norm_layer=norm_layer, nl_layer=nl_layer, upsample=upsample)
unet_block = UnetBlock_with_z(input_nc, output_nc, ngf, nz, unet_block,
outermost=True, norm_layer=norm_layer, nl_layer=nl_layer, upsample=upsample)
self.model = unet_block
def forward(self, x, z):
return self.model(x, z)
class UnetBlock_with_z(nn.Module):
def __init__(self, input_nc, outer_nc, inner_nc, nz=0,
submodule=None, outermost=False, innermost=False,
norm_layer=None, nl_layer=None, use_dropout=False, upsample='basic', padding_type='zero'):
super(UnetBlock_with_z, self).__init__()
p = 0
downconv = []
if padding_type == 'reflect':
downconv += [nn.ReflectionPad2d(1)]
elif padding_type == 'replicate':
downconv += [nn.ReplicationPad2d(1)]
elif padding_type == 'zero':
p = 1
else:
raise NotImplementedError(
'padding [%s] is not implemented' % padding_type)
self.outermost = outermost
self.innermost = innermost
self.nz = nz
input_nc = input_nc + nz
downconv += [nn.Conv2d(input_nc, inner_nc,
kernel_size=4, stride=2, padding=p)]
# downsample is different from upsample
downrelu = nn.LeakyReLU(0.2, True)
uprelu = nl_layer()
if outermost:
upconv = upsampleLayer(
inner_nc * 2, outer_nc, upsample=upsample, padding_type=padding_type)
down = downconv
up = [uprelu] + upconv + [nn.Tanh()]
elif innermost:
upconv = upsampleLayer(
inner_nc, outer_nc, upsample=upsample, padding_type=padding_type)
down = [downrelu] + downconv
up = [uprelu] + upconv
if norm_layer is not None:
up += [norm_layer(outer_nc)]
else:
upconv = upsampleLayer(
inner_nc * 2, outer_nc, upsample=upsample, padding_type=padding_type)
down = [downrelu] + downconv
if norm_layer is not None:
down += [norm_layer(inner_nc)]
up = [uprelu] + upconv
if norm_layer is not None:
up += [norm_layer(outer_nc)]
if use_dropout:
up += [nn.Dropout(0.5)]
self.down = nn.Sequential(*down)
self.submodule = submodule
self.up = nn.Sequential(*up)
def forward(self, x, z):
# print(x.size())
if self.nz > 0:
z_img = z.view(z.size(0), z.size(1), 1, 1).expand(
z.size(0), z.size(1), x.size(2), x.size(3))
x_and_z = torch.cat([x, z_img], 1)
else:
x_and_z = x
if self.outermost:
x1 = self.down(x_and_z)
x2 = self.submodule(x1, z)
return self.up(x2)
elif self.innermost:
x1 = self.up(self.down(x_and_z))
return torch.cat([x1, x], 1)
else:
x1 = self.down(x_and_z)
x2 = self.submodule(x1, z)
return torch.cat([self.up(x2), x], 1)
class E_NLayers(nn.Module):
def __init__(self, input_nc, output_nc=1, ndf=64, n_layers=3,
norm_layer=None, nl_layer=None, gpu_ids=[], vaeLike=False):
super(E_NLayers, self).__init__()
self.gpu_ids = gpu_ids
self.vaeLike = vaeLike
kw, padw = 4, 1
sequence = [nn.Conv2d(input_nc, ndf, kernel_size=kw,
stride=2, padding=padw), nl_layer()]
nf_mult = 1
nf_mult_prev = 1
for n in range(1, n_layers):
nf_mult_prev = nf_mult
nf_mult = min(2**n, 4)
sequence += [
nn.Conv2d(ndf * nf_mult_prev, ndf * nf_mult,
kernel_size=kw, stride=2, padding=padw)]
if norm_layer is not None:
sequence += [norm_layer(ndf * nf_mult)]
sequence += [nl_layer()]
sequence += [nn.AvgPool2d(8)]
self.conv = nn.Sequential(*sequence)
self.fc = nn.Sequential(*[nn.Linear(ndf * nf_mult, output_nc)])
if vaeLike:
self.fcVar = nn.Sequential(*[nn.Linear(ndf * nf_mult, output_nc)])
def forward(self, x):
x_conv = self.conv(x)
conv_flat = x_conv.view(x.size(0), -1)
output = self.fc(conv_flat)
if self.vaeLike:
outputVar = self.fcVar(conv_flat)
return output, outputVar
return output