-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
597 lines (477 loc) · 22.7 KB
/
models.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
import torch
import torch.nn as nn
from torch.nn.utils import spectral_norm
import torch.nn.functional as F
import numpy as np
import random
seq = nn.Sequential
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
try:
m.weight.data.normal_(0.0, 0.02)
except:
pass
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
def conv2d(*args, **kwargs):
return spectral_norm(nn.Conv2d(*args, **kwargs))
def convTranspose2d(*args, **kwargs):
return spectral_norm(nn.ConvTranspose2d(*args, **kwargs))
def batchNorm2d(*args, **kwargs):
return nn.BatchNorm2d(*args, **kwargs)
def linear(*args, **kwargs):
return spectral_norm(nn.Linear(*args, **kwargs))
def get_wav(in_channels, pool=True):
"""wavelet decomposition using conv2d"""
harr_wav_L = 1 / np.sqrt(2) * np.ones((1, 2))
harr_wav_H = 1 / np.sqrt(2) * np.ones((1, 2))
harr_wav_H[0, 0] = -1 * harr_wav_H[0, 0]
harr_wav_LL = np.transpose(harr_wav_L) * harr_wav_L
harr_wav_LH = np.transpose(harr_wav_L) * harr_wav_H
harr_wav_HL = np.transpose(harr_wav_H) * harr_wav_L
harr_wav_HH = np.transpose(harr_wav_H) * harr_wav_H
filter_LL = torch.from_numpy(harr_wav_LL).unsqueeze(0)
# print(filter_LL.size())
filter_LH = torch.from_numpy(harr_wav_LH).unsqueeze(0)
filter_HL = torch.from_numpy(harr_wav_HL).unsqueeze(0)
filter_HH = torch.from_numpy(harr_wav_HH).unsqueeze(0)
if pool:
net = nn.Conv2d
else:
net = nn.ConvTranspose2d
LL = net(in_channels, in_channels*2,
kernel_size=2, stride=2, padding=0, bias=False,
groups=in_channels)
LH = net(in_channels, in_channels*2,
kernel_size=2, stride=2, padding=0, bias=False,
groups=in_channels)
HL = net(in_channels, in_channels*2,
kernel_size=2, stride=2, padding=0, bias=False,
groups=in_channels)
HH = net(in_channels, in_channels*2,
kernel_size=2, stride=2, padding=0, bias=False,
groups=in_channels)
LL.weight.requires_grad = False
LH.weight.requires_grad = False
HL.weight.requires_grad = False
HH.weight.requires_grad = False
LL.weight.data = filter_LL.float().unsqueeze(0).expand(in_channels*2, -1, -1, -1)
LH.weight.data = filter_LH.float().unsqueeze(0).expand(in_channels*2, -1, -1, -1)
HL.weight.data = filter_HL.float().unsqueeze(0).expand(in_channels*2, -1, -1, -1)
HH.weight.data = filter_HH.float().unsqueeze(0).expand(in_channels*2, -1, -1, -1)
return LL, LH, HL, HH
def get_wav_two(in_channels, pool=True):
"""wavelet decomposition using conv2d"""
harr_wav_L = 1 / np.sqrt(2) * np.ones((1, 2))
harr_wav_H = 1 / np.sqrt(2) * np.ones((1, 2))
harr_wav_H[0, 0] = -1 * harr_wav_H[0, 0]
harr_wav_LL = np.transpose(harr_wav_L) * harr_wav_L
harr_wav_LH = np.transpose(harr_wav_L) * harr_wav_H
harr_wav_HL = np.transpose(harr_wav_H) * harr_wav_L
harr_wav_HH = np.transpose(harr_wav_H) * harr_wav_H
filter_LL = torch.from_numpy(harr_wav_LL).unsqueeze(0)
# print(filter_LL.size())
filter_LH = torch.from_numpy(harr_wav_LH).unsqueeze(0)
filter_HL = torch.from_numpy(harr_wav_HL).unsqueeze(0)
filter_HH = torch.from_numpy(harr_wav_HH).unsqueeze(0)
if pool:
net = nn.Conv2d
else:
net = nn.ConvTranspose2d
LL = net(in_channels, in_channels,
kernel_size=2, stride=2, padding=0, bias=False,
groups=in_channels)
LH = net(in_channels, in_channels,
kernel_size=2, stride=2, padding=0, bias=False,
groups=in_channels)
HL = net(in_channels, in_channels,
kernel_size=2, stride=2, padding=0, bias=False,
groups=in_channels)
HH = net(in_channels, in_channels,
kernel_size=2, stride=2, padding=0, bias=False,
groups=in_channels)
LL.weight.requires_grad = False
LH.weight.requires_grad = False
HL.weight.requires_grad = False
HH.weight.requires_grad = False
LL.weight.data = filter_LL.float().unsqueeze(0).expand(in_channels, -1, -1, -1)
LH.weight.data = filter_LH.float().unsqueeze(0).expand(in_channels, -1, -1, -1)
HL.weight.data = filter_HL.float().unsqueeze(0).expand(in_channels, -1, -1, -1)
HH.weight.data = filter_HH.float().unsqueeze(0).expand(in_channels, -1, -1, -1)
return LL, LH, HL, HH
class WavePool(nn.Module):
def __init__(self, in_channels):
super(WavePool, self).__init__()
self.LL, self.LH, self.HL, self.HH = get_wav(in_channels)
def forward(self, x):
return self.LL(x), self.LH(x), self.HL(x), self.HH(x)
class WaveUnpool(nn.Module):
def __init__(self, in_channels, option_unpool='cat5'):
super(WaveUnpool, self).__init__()
self.in_channels = in_channels
self.option_unpool = option_unpool
self.LL, self.LH, self.HL, self.HH = get_wav_two(self.in_channels, pool=False)
def forward(self, LL, LH, HL, HH, original=None):
if self.option_unpool == 'sum':
return self.LL(LL) + self.LH(LH) + self.HL(HL) + self.HH(HH)
elif self.option_unpool == 'cat5' and original is not None:
return torch.cat([self.LL(LL), self.LH(LH), self.HL(HL), self.HH(HH), original], dim=1)
else:
raise NotImplementedError
class PixelNorm(nn.Module):
def forward(self, input):
return input * torch.rsqrt(torch.mean(input ** 2, dim=1, keepdim=True) + 1e-8)
class Reshape(nn.Module):
def __init__(self, shape):
super().__init__()
self.target_shape = shape
def forward(self, feat):
batch = feat.shape[0]
return feat.view(batch, *self.target_shape)
class GLU(nn.Module):
def forward(self, x):
nc = x.size(1)
assert nc % 2 == 0, 'channels dont divide 2!'
nc = int(nc/2)
return x[:, :nc] * torch.sigmoid(x[:, nc:])
class NoiseInjection(nn.Module):
def __init__(self):
super().__init__()
self.weight = nn.Parameter(torch.zeros(1), requires_grad=True)
def forward(self, feat, noise=None):
if noise is None:
batch, _, height, width = feat.shape
noise = torch.randn(batch, 1, height, width).to(feat.device)
return feat + self.weight * noise
class Swish(nn.Module):
def forward(self, feat):
return feat * torch.sigmoid(feat)
class SEBlock(nn.Module):
def __init__(self, ch_in, ch_out):
super().__init__()
self.main = nn.Sequential( nn.AdaptiveAvgPool2d(4),
conv2d(ch_in, ch_out, 4, 1, 0, bias=False), Swish(),
conv2d(ch_out, ch_out, 1, 1, 0, bias=False), nn.Sigmoid() )
def forward(self, feat_small, feat_big):
return feat_big * self.main(feat_small)
class InitLayer(nn.Module):
def __init__(self, nz, channel):
super().__init__()
self.init = nn.Sequential(
convTranspose2d(nz, channel*2, 4, 1, 0, bias=False),
batchNorm2d(channel*2), GLU() )
def forward(self, noise):
noise = noise.view(noise.shape[0], -1, 1, 1)
return self.init(noise)
def UpBlock(in_planes, out_planes):
block = nn.Sequential(
nn.Upsample(scale_factor=2, mode='nearest'),
conv2d(in_planes, out_planes*2, 3, 1, 1, bias=False),
#convTranspose2d(in_planes, out_planes*2, 4, 2, 1, bias=False),
batchNorm2d(out_planes*2), GLU())
return block
def UpBlockComp(in_planes, out_planes):
block = nn.Sequential(
nn.Upsample(scale_factor=2, mode='nearest'),
conv2d(in_planes, out_planes*2, 3, 1, 1, bias=False),
#convTranspose2d(in_planes, out_planes*2, 4, 2, 1, bias=False),
NoiseInjection(),
batchNorm2d(out_planes*2), GLU(),
conv2d(out_planes, out_planes*2, 3, 1, 1, bias=False),
NoiseInjection(),
batchNorm2d(out_planes*2), GLU()
)
return block
class Generator(nn.Module):
def __init__(self, ngf=64, nz=100, nc=3, im_size=1024):
super(Generator, self).__init__()
nfc_multi = {4:16, 8:8, 16:4, 32:2, 64:2, 128:1, 256:0.5, 512:0.25, 1024:0.125}
nfc = {}
for k, v in nfc_multi.items():
nfc[k] = int(v*ngf)
self.im_size = im_size
self.init = InitLayer(nz, channel=nfc[4])
self.feat_8 = UpBlockComp(nfc[4], nfc[8])
self.feat_16 = UpBlock(nfc[8], nfc[16])
self.feat_32 = UpBlockComp(nfc[16], nfc[32])
self.feat_64 = UpBlock(nfc[32], nfc[64])
self.feat_128 = UpBlockComp(nfc[64], nfc[128])
self.feat_256 = UpBlock(nfc[128], nfc[256])
self.se_64 = SEBlock(nfc[4], nfc[64])
self.se_128 = SEBlock(nfc[8], nfc[128])
self.se_256 = SEBlock(nfc[16], nfc[256])
self.feat_256_to_128 = UpBlock(256, 128)
self.feat_256_to_64 = UpBlock(256, 64)
self.feat_512_to_128 = UpBlock(512, 128)
self.feat_1024_to_256 = UpBlock(1024, 256)
self.feat_512_to_512 = UpBlock(512, 512)
self.to_128 = conv2d(nfc[128], nc, 1, 1, 0, bias=False)
self.to_big = conv2d(nfc[im_size], nc, 3, 1, 1, bias=False)
if im_size > 256:
self.feat_512 = UpBlockComp(nfc[256], nfc[512])
self.se_512 = SEBlock(nfc[32], nfc[512])
if im_size > 512:
self.feat_1024 = UpBlock(nfc[512], nfc[1024])
# WaveUnpool
self.recon_block1 = WaveUnpool(128, "sum")
self.recon_block2 = WaveUnpool(256, "sum")
self.recon_block3 = WaveUnpool(512, "sum")
self.recon_block4 = WaveUnpool(1024, "sum")
self.conv_f1 = conv2d(1024, 1024, 7, 1, 1, bias=False)
self.conv_f2 = conv2d(512, 512, 11, 1, 1, bias=False)
self.conv_f3 = conv2d(256, 256, 19, 1, 1, bias=False)
# WavePool
self.pool64 = WavePool(64).cuda()
self.pool128 = WavePool(128).cuda()
self.pool256 = WavePool(256).cuda()
self.pool512 = WavePool(512).cuda()
def forward(self, input, skips):
if skips:
feat_4 = self.init(input)
feat_8 = self.feat_8(feat_4)
LL_8, LH_8, HL_8, HH_8 = self.pool512(feat_8)
original_8 = self.recon_block4(LL_8, LH_8, HL_8, HH_8)
original_8 = self.feat_1024_to_256(original_8)
fres_8 = LH_8 + HL_8 + HH_8
feat_16 = self.feat_16(feat_8)
LL_16, LH_16, HL_16, HH_16 = self.pool256(feat_16)
original_16 = self.recon_block3(LL_16, LH_16, HL_16, HH_16)
original_16 = self.feat_512_to_128(original_16)
fres_16 = LH_16 + HL_16 + HH_16
feat_32 = self.feat_32(feat_16)
LL_32, LH_32, HL_32, HH_32 = self.pool128(feat_32)
original_32 = self.recon_block2(LL_32, LH_32, HL_32, HH_32)
original_32 = self.feat_256_to_128(original_32)
fres_32 = LH_32 + HL_32 + HH_32
feat_64 = self.se_64(feat_4, self.feat_64(feat_32))
LL_64, LH_64, HL_64, HH_64 = self.pool128(feat_64)
original_64 = self.recon_block2(LL_64, LH_64, HL_64, HH_64)
original_64 = self.feat_256_to_64(original_64)
feat_128 = self.se_128(feat_8, self.feat_128(feat_64))
feat_256 = self.se_256(feat_16, self.feat_256(feat_128))
if self.im_size == 256:
return [self.to_big(feat_256), self.to_128(feat_128)]
feat_512 = self.se_512(feat_32, self.feat_512(feat_256))
if self.im_size == 512:
return [self.to_big(feat_512), self.to_128(feat_128)]
feat_1024 = self.feat_1024(feat_512)
im_128 = torch.tanh(self.to_128(feat_128))
im_1024 = torch.tanh(self.to_big(feat_1024))
return [im_1024, im_128]
else:
feat_4 = self.init(input)
feat_8 = self.feat_8(feat_4)
LL_8, LH_8, HL_8, HH_8 = self.pool512(feat_8)
original_8 = self.recon_block4(LL_8, LH_8, HL_8, HH_8)
original_8 = self.feat_1024_to_256(original_8)
fres_8 = LH_8 + HL_8 + HH_8
feat_16 = self.feat_16(feat_8)
LL_16, LH_16, HL_16, HH_16 = self.pool256(feat_16)
original_16 = self.recon_block3(LL_16, LH_16, HL_16, HH_16)
original_16 = self.feat_512_to_128(original_16)
fres_16 = LH_16 + HL_16 + HH_16
feat_32 = self.feat_32(feat_16 + original_8)
LL_32, LH_32, HL_32, HH_32 = self.pool128(feat_32)
original_32 = self.recon_block2(LL_32, LH_32, HL_32, HH_32)
original_32 = self.feat_256_to_128(original_32)
fres_32 = LH_32 + HL_32 + HH_32
feat_64 = self.se_64(feat_4, self.feat_64(feat_32 + original_16))
LL_64, LH_64, HL_64, HH_64 = self.pool128(feat_64)
original_64 = self.recon_block2(LL_64, LH_64, HL_64, HH_64)
# original_64 = self.feat_256_to_64(original_64)
feat_128 = self.se_128(feat_8, self.feat_128(feat_64 + original_32))
feat_256 = self.se_256(feat_16, self.feat_256(feat_128))
if self.im_size == 256:
return [self.to_big(feat_256), self.to_128(feat_128)], fres_8, fres_16, fres_32
feat_512 = self.se_512(feat_32, self.feat_512(feat_256))
if self.im_size == 512:
return [self.to_big(feat_512), self.to_128(feat_128)], fres_8, fres_16, fres_32
feat_1024 = self.feat_1024(feat_512)
im_128 = torch.tanh(self.to_128(feat_128))
im_1024 = torch.tanh(self.to_big(feat_1024))
return [im_1024, im_128], fres_8, fres_16, fres_32
class DownBlock(nn.Module):
def __init__(self, in_planes, out_planes):
super(DownBlock, self).__init__()
self.main = nn.Sequential(
conv2d(in_planes, out_planes, 4, 2, 1, bias=False),
batchNorm2d(out_planes), nn.LeakyReLU(0.2, inplace=True),
)
def forward(self, feat):
return self.main(feat)
class DownBlockComp(nn.Module):
def __init__(self, in_planes, out_planes):
super(DownBlockComp, self).__init__()
self.main = nn.Sequential(
conv2d(in_planes, out_planes, 4, 2, 1, bias=False),
batchNorm2d(out_planes), nn.LeakyReLU(0.2, inplace=True),
conv2d(out_planes, out_planes, 3, 1, 1, bias=False),
batchNorm2d(out_planes), nn.LeakyReLU(0.2)
)
self.direct = nn.Sequential(
nn.AvgPool2d(2, 2),
conv2d(in_planes, out_planes, 1, 1, 0, bias=False),
batchNorm2d(out_planes), nn.LeakyReLU(0.2))
def forward(self, feat):
return (self.main(feat) + self.direct(feat)) / 2
class Discriminator(nn.Module):
def __init__(self, ndf=64, nc=3, im_size=512):
super(Discriminator, self).__init__()
self.ndf = ndf
self.im_size = im_size
nfc_multi = {4:16, 8:16, 16:8, 32:4, 64:2, 128:1, 256:0.5, 512:0.25, 1024:0.125}
nfc = {}
for k, v in nfc_multi.items():
nfc[k] = int(v*ndf)
if im_size == 1024:
self.down_from_big = nn.Sequential(
conv2d(nc, nfc[1024], 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
conv2d(nfc[1024], nfc[512], 4, 2, 1, bias=False),
batchNorm2d(nfc[512]),
nn.LeakyReLU(0.2, inplace=True))
elif im_size == 512:
self.down_from_big = nn.Sequential(
conv2d(nc, nfc[512], 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True) )
elif im_size == 256:
self.down_from_big = nn.Sequential(
conv2d(nc, nfc[512], 3, 1, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True) )
self.down_4 = DownBlockComp(nfc[512], nfc[256])
self.down_8 = DownBlockComp(nfc[256], nfc[128])
self.down_16 = DownBlockComp(nfc[128], nfc[64])
self.down_32 = DownBlockComp(nfc[64], nfc[32])
self.down_64 = DownBlockComp(nfc[32], nfc[16])
self.rf_big = nn.Sequential(
conv2d(nfc[16] , nfc[8], 1, 1, 0, bias=False),
batchNorm2d(nfc[8]), nn.LeakyReLU(0.2, inplace=True),
conv2d(nfc[8], 1, 4, 1, 0, bias=False))
self.se_2_16 = SEBlock(nfc[512], nfc[64])
self.se_4_32 = SEBlock(nfc[256], nfc[32])
self.se_8_64 = SEBlock(nfc[128], nfc[16])
self.down_from_small = nn.Sequential(
conv2d(nc, nfc[256], 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
DownBlock(nfc[256], nfc[128]),
DownBlock(nfc[128], nfc[64]),
DownBlock(nfc[64], nfc[32]), )
self.rf_small = conv2d(nfc[32], 1, 4, 1, 0, bias=False)
self.decoder_big = SimpleDecoder(nfc[16], nc)
self.decoder_part = SimpleDecoder(nfc[32], nc)
self.decoder_small = SimpleDecoder(nfc[32], nc)
# WavePool
self.pool64 = WavePool(64).cuda()
self.pool128 = WavePool(128).cuda()
self.pool256 = WavePool(256).cuda()
self.pool512 = WavePool(512).cuda()
def forward(self, imgs, label, part=None):
if type(imgs) is not list:
imgs = [F.interpolate(imgs, size=self.im_size), F.interpolate(imgs, size=128)]
skips = {}
feat_2 = self.down_from_big(imgs[0])
feat_4 = self.down_4(feat_2)
feat_8 = self.down_8(feat_4)
LL_64, LH_64, HL_64, HH_64 = self.pool64(feat_8)
HF_64 = LH_64 + HL_64 + HH_64
skips['conv1_1'] = LH_64 + HL_64 + HH_64
HF_64 = self.down_32(HF_64)
HF_64 = self.down_64(HF_64)
Drf_HF_64 = self.rf_big(HF_64).view(-1)
feat_16 = self.down_16(feat_8)
feat_16 = self.se_2_16(feat_2, feat_16)
LL_128, LH_128, HL_128, HH_128 = self.pool128(feat_16)
HF_128 = LH_128 + HL_128 + HH_128
skips['conv2_1'] = LH_128 + HL_128 + HH_128
HF_128 = self.down_64(HF_128)
Drf_HF_128 = self.rf_big(HF_128).view(-1)
feat_32 = self.down_32(feat_16)
feat_32 = self.se_4_32(feat_4, feat_32)
LL_256, LH_256, HL_256, HH_256 = self.pool256(feat_32)
HF_256 = LH_256 + HL_256 + HH_256
skips['conv3_1'] = LH_256 + HL_256 + HH_256
Drf_HF_256 = self.rf_big(HF_256).view(-1)
feat_last = self.down_64(feat_32)
feat_last = self.se_8_64(feat_8, feat_last)
skips['pool4'] = feat_last.detach()
LL_512, LH_512, HL_512, HH_512 = self.pool512(feat_last)
skips['conv4_1'] = LH_512 + HL_512 + HH_512
#rf_0 = torch.cat([self.rf_big_1(feat_last).view(-1),self.rf_big_2(feat_last).view(-1)])
#rff_big = torch.sigmoid(self.rf_factor_big)
rf_0 = self.rf_big(feat_last).view(-1)
feat_small = self.down_from_small(imgs[1])
#rf_1 = torch.cat([self.rf_small_1(feat_small).view(-1),self.rf_small_2(feat_small).view(-1)])
rf_1 = self.rf_small(feat_small).view(-1)
if label=='real':
rec_img_big = self.decoder_big(feat_last)
rec_img_small = self.decoder_small(feat_small)
assert part is not None
rec_img_part = None
if part==0:
rec_img_part = self.decoder_part(feat_32[:,:,:8,:8])
if part==1:
rec_img_part = self.decoder_part(feat_32[:,:,:8,8:])
if part==2:
rec_img_part = self.decoder_part(feat_32[:,:,8:,:8])
if part==3:
rec_img_part = self.decoder_part(feat_32[:,:,8:,8:])
return torch.cat([rf_0, rf_1, Drf_HF_64, Drf_HF_128, Drf_HF_256]) , [rec_img_big, rec_img_small, rec_img_part], skips
return torch.cat([rf_0, rf_1, Drf_HF_64, Drf_HF_128, Drf_HF_256])
class SimpleDecoder(nn.Module):
"""docstring for CAN_SimpleDecoder"""
def __init__(self, nfc_in=64, nc=3):
super(SimpleDecoder, self).__init__()
nfc_multi = {4:16, 8:8, 16:4, 32:2, 64:2, 128:1, 256:0.5, 512:0.25, 1024:0.125}
nfc = {}
for k, v in nfc_multi.items():
nfc[k] = int(v*32)
def upBlock(in_planes, out_planes):
block = nn.Sequential(
nn.Upsample(scale_factor=2, mode='nearest'),
conv2d(in_planes, out_planes*2, 3, 1, 1, bias=False),
batchNorm2d(out_planes*2), GLU())
return block
self.main = nn.Sequential( nn.AdaptiveAvgPool2d(8),
upBlock(nfc_in, nfc[16]) ,
upBlock(nfc[16], nfc[32]),
upBlock(nfc[32], nfc[64]),
upBlock(nfc[64], nfc[128]),
conv2d(nfc[128], nc, 3, 1, 1, bias=False),
nn.Tanh() )
def forward(self, input):
# input shape: c x 4 x 4
return self.main(input)
from random import randint
def random_crop(image, size):
h, w = image.shape[2:]
ch = randint(0, h-size-1)
cw = randint(0, w-size-1)
return image[:,:,ch:ch+size,cw:cw+size]
class TextureDiscriminator(nn.Module):
def __init__(self, ndf=64, nc=3, im_size=512):
super(TextureDiscriminator, self).__init__()
self.ndf = ndf
self.im_size = im_size
nfc_multi = {4:16, 8:8, 16:8, 32:4, 64:2, 128:1, 256:0.5, 512:0.25, 1024:0.125}
nfc = {}
for k, v in nfc_multi.items():
nfc[k] = int(v*ndf)
self.down_from_small = nn.Sequential(
conv2d(nc, nfc[256], 4, 2, 1, bias=False),
nn.LeakyReLU(0.2, inplace=True),
DownBlock(nfc[256], nfc[128]),
DownBlock(nfc[128], nfc[64]),
DownBlock(nfc[64], nfc[32]), )
self.rf_small = nn.Sequential(
conv2d(nfc[16], 1, 4, 1, 0, bias=False))
self.decoder_small = SimpleDecoder(nfc[32], nc)
def forward(self, img, label):
img = random_crop(img, size=128)
feat_small = self.down_from_small(img)
rf = self.rf_small(feat_small).view(-1)
if label=='real':
rec_img_small = self.decoder_small(feat_small)
return rf, rec_img_small, img
return rf