-
Notifications
You must be signed in to change notification settings - Fork 3
/
conv_modules.py
executable file
·917 lines (763 loc) · 32.5 KB
/
conv_modules.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
import torch
import util
from torch import nn
import numpy as np
import torchvision
from torchvision.models.resnet import BasicBlock, Bottleneck, conv1x1
import torch.nn.functional as F
import functools
from pdb import set_trace as pdb #debugging
def normalize_imagenet(x):
''' Normalize input images according to ImageNet standards.
Args:
x (tensor): input images
'''
x = x.clone()
x[:, 0] = (x[:, 0] - 0.485) / 0.229
x[:, 1] = (x[:, 1] - 0.456) / 0.224
x[:, 2] = (x[:, 2] - 0.406) / 0.225
return x
class FeaturePyramidEncoder(nn.Module):
'''
Taken from Alex Yu's PixelNerf
Similar functionality to U-Net but uses 1d conv instead of concatenation
'''
def __init__(
self,
backbone="resnet34",
pretrained=True,
num_layers=4,
index_interp="bilinear",
index_padding="border",
upsample_interp="bilinear",
feature_scale=1.0,
use_first_pool=True,
norm_type="batch",
):
"""
:param backbone Backbone network. Either custom, in which case
model.custom_encoder.ConvEncoder is used OR resnet18/resnet34, in which case the relevant
model from torchvision is used
:param num_layers number of resnet layers to use, 1-5
:param pretrained Whether to use model weights pretrained on ImageNet
:param index_interp Interpolation to use for indexing
:param index_padding Padding mode to use for indexing, border | zeros | reflection
:param upsample_interp Interpolation to use for upscaling latent code
:param feature_scale factor to scale all latent by. Useful (<1) if image
is extremely large, to fit in memory.
:param use_first_pool if false, skips first maxpool layer to avoid downscaling image
features too much (ResNet only)
"""
super().__init__()
if norm_type != "batch":
assert not pretrained
self.use_custom_resnet = backbone == "custom"
self.feature_scale = feature_scale
self.use_first_pool = use_first_pool
norm_layer = functools.partial(nn.BatchNorm2d, affine=True,)
print("Using torchvision", backbone, "encoder")
self.model = getattr(torchvision.models, backbone)(pretrained=pretrained)
# Following 2 lines need to be uncommented for older configs
self.model.fc = nn.Sequential()
self.model.avgpool = nn.Sequential()
self.latent_size = [0, 64, 128, 256, 512, 1024][num_layers]
self.num_layers = num_layers
self.index_interp = index_interp
self.index_padding = index_padding
self.upsample_interp = upsample_interp
self.register_buffer("latent", torch.empty(1, 1, 1, 1), persistent=False)
self.register_buffer(
"latent_scaling", torch.empty(2, dtype=torch.float32), persistent=False
)
# self.latent (B, L, H, W)
def forward(self, x):
"""
For extracting ResNet's features.
:param x image (B, C, H, W)
:return latent (B, latent_size, H, W)
"""
input_res = x.shape[-2:]
x = normalize_imagenet(x)
if self.feature_scale != 1.0:
x = F.interpolate(
x,
scale_factor=self.feature_scale,
mode="bilinear" if self.feature_scale > 1.0 else "area",
align_corners=True if self.feature_scale > 1.0 else None,
recompute_scale_factor=True,
)
x = x.to(device=self.latent.device)
x = self.model.conv1(x)
x = self.model.bn1(x)
x = self.model.relu(x)
latents = [x]
if self.num_layers > 1:
if self.use_first_pool:
x = self.model.maxpool(x)
x = self.model.layer1(x)
latents.append(x)
if self.num_layers > 2:
x = self.model.layer2(x)
latents.append(x)
if self.num_layers > 3:
x = self.model.layer3(x)
latents.append(x)
if self.num_layers > 4:
x = self.model.layer4(x)
latents.append(x)
self.latents = latents
align_corners = None if self.index_interp == "nearest " else True
latent_sz = latents[0].shape[-2:]
for i in range(len(latents)):
latents[i] = F.interpolate(
latents[i],
latent_sz,#input_res,
mode=self.upsample_interp,
align_corners=align_corners,
)
self.latent = torch.cat(latents, dim=1)
self.latent_scaling[0] = self.latent.shape[-1]
self.latent_scaling[1] = self.latent.shape[-2]
self.latent_scaling = self.latent_scaling / (self.latent_scaling - 1) * 2.0
return self.latent
class PixelNerfEncoder(nn.Module):
"""
Global image encoder
"""
def __init__(self, backbone="resnet18", pretrained=True, latent_size=512):
"""
:param backbone Backbone network. Assumes it is resnet*
e.g. resnet34 | resnet50
:param num_layers number of resnet layers to use, 1-5
:param pretrained Whether to use model pretrained on ImageNet
"""
super().__init__()
self.model = getattr(torchvision.models, backbone)(pretrained=pretrained)
self.model.fc = nn.Sequential()
self.register_buffer("latent", torch.empty(1, 1), persistent=False)
self.latent_size = latent_size
# self.fc = nn.Linear(2048, latent_size)
def forward(self, x):
"""
For extracting ResNet's features.
:param x image (B, C, H, W)
:return latent (B, latent_size)
"""
x = (x + 1) / 2
x = normalize_imagenet(x)
x = x.to(device=self.latent.device)
x = self.model.conv1(x)
x = self.model.bn1(x)
x = self.model.relu(x)
x = self.model.maxpool(x)
x = self.model.layer1(x)
x = self.model.layer2(x)
x = self.model.layer3(x)
x = self.model.layer4(x)
x = self.model.avgpool(x)
x = torch.flatten(x, 1)
# x = self.fc(x)
self.latent = x # (B, latent_size)
return self.latent
class ProgressiveDiscriminator(nn.Module):
def __init__(self, channel, in_sidelength):
super().__init__()
self.channel = channel
self.sl = in_sidelength
# conv_theta is input convolution
self.net = []
self.net.extend(
[Conv2dSame(channel, 128, 1),
nn.LeakyReLU(0.2, inplace=True),
Conv2dSame(128, 128, 3),
nn.LeakyReLU(0.2, inplace=True),
nn.AvgPool2d(2)]
)
num_down_convs = int(np.log2(in_sidelength))
for i in range(num_down_convs-1):
in_ch = min(128*(i+1), 400)
out_ch = min(128*(i+2), 400)
self.net.extend(
[Conv2dSame(in_ch, out_ch, 3),
nn.LeakyReLU(0.2, inplace=True),
Conv2dSame(out_ch, out_ch, 3),
nn.LeakyReLU(0.2, inplace=True),
nn.AvgPool2d(2)]
)
self.net = nn.Sequential(*self.net)
self.final_ch = min(128*(i+1), 400)
net = [nn.Linear(self.final_ch, 1)]
self.fc = nn.Sequential(*net)
def forward(self, I, detach=False):
I = util.flatten_first_two(I)
img = util.lin2img(I)
if detach:
img = img.detach()
o = self.net(img)
# o = self.fc(o.view(o.shape[0], self.final_ch, -1).squeeze(-1))
o = torch.sigmoid(self.fc(o.view(o.shape[0], self.final_ch, -1).squeeze(-1)))
return o
class ConvDiscriminator(nn.Module):
def __init__(self, channel, in_sidelength, out_features=256):
super().__init__()
self.channel = channel
self.out_features = out_features
self.sl = in_sidelength
# conv_theta is input convolution
self.net = []
self.net.extend(
[Conv2dSame(channel, 256, 3),
nn.LeakyReLU(inplace=True)]
)
num_down_convs = int(np.log2(in_sidelength))
for i in range(num_down_convs):
self.net.extend(
[Conv2dSame(256, 256, 3),
nn.LeakyReLU(0.2, inplace=True),
nn.AvgPool2d(2)]
)
self.net = nn.Sequential(*self.net)
net = [nn.Linear(out_features, 1)]
self.fc = nn.Sequential(*net)
def forward(self, I, detach=False):
# b, num_views, num_pix, ch = I.shape
# I = torch.split(I, num_views, dim=1).squeeze(1)
# I = torch.cat(I, dim=-1)
I = util.flatten_first_two(I)
img = util.lin2img(I)
if detach:
img = img.detach()
o = self.net(img)
o = torch.sigmoid(self.fc(o.view(o.shape[0], self.out_features, -1).squeeze(-1)))
return o
class Resnet18(nn.Module):
r''' ResNet-18 encoder network for image input.
Args:
c_dim (int): output dimension of the latent embedding
normalize (bool): whether the input images should be normalized
use_linear (bool): whether a final linear layer should be used
'''
def __init__(self, c_dim, normalize=True, use_linear=True):
super().__init__()
self.normalize = normalize
self.use_linear = use_linear
self.features = torchvision.models.resnet18(pretrained=True)
self.features.fc = nn.Sequential()
if use_linear:
self.fc = nn.Linear(512, c_dim)
self.fc.apply(init_weights_normal)
elif c_dim == 512:
self.fc = nn.Sequential()
else:
raise ValueError('c_dim must be 512 if use_linear is False')
def forward(self, input):
x = (input + 1) / 2
if self.normalize:
x = normalize_imagenet(x)
net = self.features(x)
out = self.fc(net)
return out
class ResNet(nn.Module):
def __init__(self, in_features, layers, out_features, zero_init_residual=False, block=BasicBlock,
groups=1, width_per_group=64, replace_stride_with_dilation=None,
norm_layer=None, pretrained=False):
super(ResNet, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
self._norm_layer = norm_layer
self.inplanes = 64
self.dilation = 1
if replace_stride_with_dilation is None:
# each element in the tuple indicates if we should replace
# the 2x2 stride with a dilated convolution instead
replace_stride_with_dilation = [False, False, False]
if len(replace_stride_with_dilation) != 3:
raise ValueError("replace_stride_with_dilation should be None "
"or a 3-element tuple, got {}".format(replace_stride_with_dilation))
self.groups = groups
self.base_width = width_per_group
self.conv1 = nn.Conv2d(in_features, self.inplanes, kernel_size=7, stride=2, padding=3,
bias=False)
self.bn1 = norm_layer(self.inplanes)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=in_features, stride=2, padding=1)
self.layer1 = self._make_layer(block, 64, layers[0])
self.layer2 = self._make_layer(block, 128, layers[1], stride=2,
dilate=replace_stride_with_dilation[0])
self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
dilate=replace_stride_with_dilation[1])
self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
dilate=replace_stride_with_dilation[2])
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512 * block.expansion, out_features)
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)
# Zero-initialize the last BN in each residual branch,
# so that the residual branch starts with zeros, and each residual block behaves like an identity.
# This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
if zero_init_residual:
for m in self.modules():
if isinstance(m, Bottleneck):
nn.init.constant_(m.bn3.weight, 0)
elif isinstance(m, BasicBlock):
nn.init.constant_(m.bn2.weight, 0)
if pretrained:
state_dict = torch.hub.load_state_dict_from_url('https://download.pytorch.org/models/resnet18-5c106cde.pth', progress=True)
del state_dict['conv1.weight']
del state_dict['fc.weight']
del state_dict['fc.bias']
self.load_state_dict(state_dict, strict=False)
def _make_layer(self, block, planes, blocks, stride=1, dilate=False):
norm_layer = self._norm_layer
downsample = None
previous_dilation = self.dilation
if dilate:
self.dilation *= stride
stride = 1
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
conv1x1(self.inplanes, planes * block.expansion, stride),
norm_layer(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample, self.groups,
self.base_width, previous_dilation, norm_layer))
self.inplanes = planes * block.expansion
for _ in range(1, blocks):
layers.append(block(self.inplanes, planes, groups=self.groups,
base_width=self.base_width, dilation=self.dilation,
norm_layer=norm_layer))
return nn.Sequential(*layers)
def _forward_impl(self, x):
# See note [TorchScript super()]
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
x = self.fc(x)
return x
def forward(self, x):
return self._forward_impl((x+1)/2.)
class ConvImgEncoder(nn.Module):
def __init__(self, channel, in_sidelength, out_features=256, outermost_linear=False):
super().__init__()
self.channel = channel
self.out_features = out_features
# conv_theta is input convolution
self.net = []
self.net.extend(
[Conv2dSame(channel, 128, 3),
nn.ReLU(inplace=True)]
)
num_down_convs = int(np.log2(in_sidelength))
for i in range(num_down_convs):
if not i:
in_feats = 128
else:
in_feats = 256
if i != num_down_convs - 1:
out_feats = 256
else:
out_feats = out_features
self.net.append(
BasicDownBlock(in_feats, out_feats)
)
self.net = nn.Sequential(*self.net)
net = [nn.Linear(out_features, out_features)]
if not outermost_linear:
net += [nn.ReLU(inplace=True)]
self.fc = nn.Sequential(*net)
self.net.apply(init_weights_normal)
self.fc.apply(init_weights_normal)
def forward(self, I):
o = self.net(I)
o = self.fc(o.view(o.shape[0], self.out_features, -1).squeeze(-1))
return o
class Conv2dResBlock(nn.Module):
'''Aadapted from https://github.com/makora9143/pytorch-convcnp/blob/master/convcnp/modules/resblock.py'''
def __init__(self, in_channel, out_channel=128):
super().__init__()
self.convs = nn.Sequential(
Conv2dSame(in_channel, out_channel, 5),
nn.ReLU(inplace=True),
Conv2dSame(in_channel, out_channel, 5),
nn.ReLU(inplace=True)
)
self.final_relu = nn.ReLU(inplace=True)
def forward(self, x):
shortcut = x
output = self.convs(x)
output = self.final_relu(output + shortcut)
return output
def channel_last(x):
return x.transpose(1, 2).transpose(2, 3)
class PatchDiscriminator(nn.Module):
def __init__(self, input_nc, ndf=28, n_layers=3):
super().__init__()
sequence = [
nn.ReflectionPad2d(1),
nn.Conv2d(input_nc, ndf, kernel_size=4, stride=2, padding=0),
nn.LeakyReLU(0.2, True)
]
nf_mult = 1
for n in range(n_layers):
nf_mult_prev = nf_mult
nf_mult = min(2**(n+1), 8)
stride = 1 if n == n_layers - 1 else 2
sequence += [
nn.ReflectionPad2d(1),
nn.Conv2d(ndf * nf_mult_prev,
ndf * nf_mult,
kernel_size=4,
stride=stride,
padding=0),
nn.BatchNorm2d(ndf*nf_mult),
nn.Dropout2d(0.5),
nn.LeakyReLU(0.2, True)
]
sequence += [
nn.ReflectionPad2d(1),
nn.Conv2d(ndf * nf_mult,
1,
kernel_size=4,
stride=1,
padding=0),
nn.Sigmoid()
]
self.model = nn.Sequential(*sequence)
def forward(self, input, detach=False, gradient=False):
images = input['rgb']
if detach:
disc_input = images.detach()[:,0]
else:
disc_input = images[:, 0]
if gradient:
disc_input = disc_input.requires_grad_(True)
images = util.lin2img(disc_input)
out = self.model(images)
gradient = torch.autograd.grad(out.sum(), [disc_input], create_graph=True)[0]
return out, gradient
else:
images = util.lin2img(disc_input)
out = self.model(images)
return out
#######################
# UNet Parts
class UnetSkipConnectionBlock(nn.Module):
'''Helper class for building a 2D unet.
'''
def __init__(self,
outer_nc,
inner_nc,
upsampling_mode,
norm=nn.BatchNorm2d,
submodule=None):
super().__init__()
if submodule is None:
model = [DownBlock(outer_nc, inner_nc, norm=norm),
UpBlock(inner_nc, outer_nc, norm=norm,
upsampling_mode=upsampling_mode)]
else:
model = [DownBlock(outer_nc, inner_nc, norm=norm),
submodule,
UpBlock(2 * inner_nc, outer_nc, norm=norm,
upsampling_mode=upsampling_mode)]
self.model = nn.Sequential(*model)
def forward(self, x):
forward_passed = self.model(x)
return torch.cat([x, forward_passed], 1)
class Unet(nn.Module):
'''A 2d-Unet implementation with sane defaults.
'''
def __init__(self,
in_channels,
out_channels,
nf0,
num_down,
max_channels,
upsampling_mode,
norm=nn.BatchNorm2d,
outermost_linear=False):
'''
:param in_channels: Number of input channels
:param out_channels: Number of output channels
:param nf0: Number of features at highest level of U-Net
:param num_down: Number of downsampling stages.
:param max_channels: Maximum number of channels (channels multiply by 2 with every downsampling stage)
:param use_dropout: Whether to use dropout or no.
:param dropout_prob: Dropout probability if use_dropout=True.
:param upsampling_mode: Which type of upsampling should be used. See "UpBlock" for documentation.
:param norm: Which norm to use. If None, no norm is used. Default is Batchnorm with affinity.
:param outermost_linear: Whether the output layer should be a linear layer or a nonlinear one.
'''
super().__init__()
assert (num_down > 0), "Need at least one downsampling layer in UNet."
# Define the in block
self.in_layer = [Conv2dSame(in_channels, nf0, kernel_size=3, bias=False)]
if norm is not None:
self.in_layer += [norm(nf0, affine=True)]
self.in_layer += [nn.LeakyReLU(0.2, True)]
self.in_layer = nn.Sequential(*self.in_layer)
# Define the center UNet block
self.unet_block = UnetSkipConnectionBlock(min(2 ** num_down * nf0, max_channels),
min(2 ** num_down * nf0, max_channels),
norm=None,
upsampling_mode=upsampling_mode)
for i in list(range(0, num_down - 1))[::-1]:
self.unet_block = UnetSkipConnectionBlock(min(2 ** i * nf0, max_channels),
min(2 ** (i + 1) * nf0, max_channels),
submodule=self.unet_block,
norm=norm,
upsampling_mode=upsampling_mode)
# Define the out layer. Each unet block concatenates its inputs with its outputs - so the output layer
# automatically receives the output of the in_layer and the output of the last unet layer.
self.out_layer = [Conv2dSame(2 * nf0,
out_channels,
kernel_size=3,
bias=outermost_linear)]
if not outermost_linear:
if norm is not None:
self.out_layer += [norm(out_channels, affine=True)]
self.out_layer += [nn.ReLU(True)]
self.out_layer = nn.Sequential(*self.out_layer)
self.out_layer_weight = self.out_layer[0].weight
def forward(self, x):
in_layer = self.in_layer(x)
unet = self.unet_block(in_layer)
out_layer = self.out_layer(unet)
return out_layer
class Conv2dSame(torch.nn.Module):
'''2D convolution that pads to keep spatial dimensions equal.
Cannot deal with stride. Only quadratic kernels (=scalar kernel_size).
'''
def __init__(self, in_channels, out_channels, kernel_size, bias=True, padding_layer=nn.ReflectionPad2d):
'''
:param in_channels: Number of input channels
:param out_channels: Number of output channels
:param kernel_size: Scalar. Spatial dimensions of kernel (only quadratic kernels supported).
:param bias: Whether or not to use bias.
:param padding_layer: Which padding to use. Default is reflection padding.
'''
super().__init__()
ka = kernel_size // 2
kb = ka - 1 if kernel_size % 2 == 0 else ka
self.net = nn.Sequential(
padding_layer((ka, kb, ka, kb)),
nn.Conv2d(in_channels, out_channels, kernel_size, bias=bias, stride=1)
)
self.weight = self.net[1].weight
self.bias = self.net[1].bias
def forward(self, x):
return self.net(x)
class UpBlock(nn.Module):
'''A 2d-conv upsampling block with a variety of options for upsampling, and following best practices / with
reasonable defaults. (LeakyReLU, kernel size multiple of stride)
'''
def __init__(self,
in_channels,
out_channels,
post_conv=True,
norm=nn.BatchNorm2d,
upsampling_mode='transpose'):
'''
:param in_channels: Number of input channels
:param out_channels: Number of output channels
:param post_conv: Whether to have another convolutional layer after the upsampling layer.
:param use_dropout: bool. Whether to use dropout or not.
:param dropout_prob: Float. The dropout probability (if use_dropout is True)
:param norm: Which norm to use. If None, no norm is used. Default is Batchnorm with affinity.
:param upsampling_mode: Which upsampling mode:
transpose: Upsampling with stride-2, kernel size 4 transpose convolutions.
bilinear: Feature map is upsampled with bilinear upsampling, then a conv layer.
nearest: Feature map is upsampled with nearest neighbor upsampling, then a conv layer.
shuffle: Feature map is upsampled with pixel shuffling, then a conv layer.
'''
super().__init__()
net = list()
if upsampling_mode == 'transpose':
net += [nn.ConvTranspose2d(in_channels,
out_channels,
kernel_size=4,
stride=2,
padding=1,
bias=True if norm is None else False)]
elif upsampling_mode == 'bilinear':
net += [nn.UpsamplingBilinear2d(scale_factor=2)]
net += [
Conv2dSame(in_channels, out_channels, kernel_size=3, bias=True if norm is None else False)]
elif upsampling_mode == 'nearest':
net += [nn.UpsamplingNearest2d(scale_factor=2)]
net += [
Conv2dSame(in_channels, out_channels, kernel_size=3, bias=True if norm is None else False)]
elif upsampling_mode == 'shuffle':
net += [nn.PixelShuffle(upscale_factor=2)]
net += [
Conv2dSame(in_channels // 4, out_channels, kernel_size=3,
bias=True if norm is None else False)]
else:
raise ValueError("Unknown upsampling mode!")
if norm is not None:
net += [norm(out_channels, affine=True)]
net += [nn.ReLU(True)]
if post_conv:
net += [Conv2dSame(out_channels,
out_channels,
kernel_size=3,
bias=True if norm is None else False)]
if norm is not None:
net += [norm(out_channels, affine=True)]
net += [nn.ReLU(True)]
self.net = nn.Sequential(*net)
def forward(self, x, skipped=None):
if skipped is not None:
input = torch.cat([skipped, x], dim=1)
else:
input = x
return self.net(input)
class DownBlock(nn.Module):
'''A 2D-conv downsampling block following best practices / with reasonable defaults
(LeakyReLU, kernel size multiple of stride)
'''
def __init__(self,
in_channels,
out_channels,
prep_conv=True,
middle_channels=None,
norm=nn.BatchNorm2d):
'''
:param in_channels: Number of input channels
:param out_channels: Number of output channels
:param prep_conv: Whether to have another convolutional layer before the downsampling layer.
:param middle_channels: If prep_conv is true, this sets the number of channels between the prep and downsampling
convs.
:param use_dropout: bool. Whether to use dropout or not.
:param dropout_prob: Float. The dropout probability (if use_dropout is True)
:param norm: Which norm to use. If None, no norm is used. Default is Batchnorm with affinity.
'''
super().__init__()
if middle_channels is None:
middle_channels = in_channels
net = list()
if prep_conv:
net += [nn.ReflectionPad2d(1),
nn.Conv2d(in_channels,
middle_channels,
kernel_size=3,
padding=0,
stride=1,
bias=True if norm is None else False)]
if norm is not None:
net += [norm(middle_channels, affine=True)]
net += [nn.LeakyReLU(0.2, True)]
net += [nn.ReflectionPad2d(1),
nn.Conv2d(middle_channels,
out_channels,
kernel_size=4,
padding=0,
stride=2,
bias=True if norm is None else False)]
if norm is not None:
net += [norm(out_channels, affine=True)]
net += [nn.LeakyReLU(0.2, True)]
self.net = nn.Sequential(*net)
def forward(self, x):
return self.net(x)
class BasicDownBlock(nn.Module):
'''A 2D-conv downsampling block following best practices / with reasonable defaults
(LeakyReLU, kernel size multiple of stride)
'''
def __init__(self,
in_channels,
out_channels,
prep_conv=True,
middle_channels=None):
super().__init__()
if middle_channels is None:
middle_channels = in_channels
net = list()
if prep_conv:
net += [nn.ReflectionPad2d(1),
nn.Conv2d(in_channels,
middle_channels,
kernel_size=3,
padding=0,
stride=1,
bias=True)]
net += [nn.ReLU(True)]
net += [nn.ReflectionPad2d(1),
nn.Conv2d(middle_channels,
out_channels,
kernel_size=4,
padding=0,
stride=2,
bias=True)]
net += [nn.ReLU(True)]
self.net = nn.Sequential(*net)
def forward(self, x):
return self.net(x)
def init_weights_normal(m):
if hasattr(m, 'weight'):
nn.init.kaiming_normal_(m.weight, a=0.0, nonlinearity='relu', mode='fan_in')
# Unet taken from Koven
class UnetEncoder(nn.Module):
def __init__(self, input_nc=3, z_dim=64, bottom=False):
super().__init__()
self.bottom = bottom
if self.bottom:
self.enc_down_0 = nn.Sequential(nn.Conv2d(input_nc + 4, z_dim, 3, stride=1, padding=1),
nn.ReLU(True))
self.enc_down_1 = nn.Sequential(nn.Conv2d(z_dim if bottom else input_nc+4,
#z_dim, 3, stride=2 if bottom else 1, padding=1),
z_dim, 3, stride=1, padding=1), # mod
nn.ReLU(True))
self.enc_down_2 = nn.Sequential(nn.Conv2d(z_dim, z_dim, 3, stride=2, padding=1),
nn.ReLU(True))
self.enc_down_3 = nn.Sequential(nn.Conv2d(z_dim, z_dim, 3, stride=2, padding=1),
nn.ReLU(True))
self.enc_up_3 = nn.Sequential(nn.Conv2d(z_dim, z_dim, 3, stride=1, padding=1),
nn.ReLU(True),
nn.Upsample(scale_factor=2, mode='bilinear',
align_corners=False))
self.enc_up_2 = nn.Sequential(nn.Conv2d(z_dim*2, z_dim, 3, stride=1, padding=1),
nn.ReLU(True),
nn.Upsample(scale_factor=2, mode='bilinear',
align_corners=False))
self.enc_up_1 = nn.Sequential(nn.Conv2d(z_dim * 2, z_dim, 3, stride=1, padding=1),
nn.ReLU(True))
def forward(self, x):
"""
input:
x: input image, Bx3xHxW
output:
feature_map: BxCxHxW
"""
W, H = x.shape[3], x.shape[2]
X = torch.linspace(-1, 1, W)
Y = torch.linspace(-1, 1, H)
y1_m, x1_m = torch.meshgrid([Y, X])
x2_m, y2_m = 2 - x1_m, 2 - y1_m # Normalized distance in the four direction
# ask koven if i'm correct in expanding here for B>1
pixel_emb = torch.stack([x1_m, x2_m, y1_m, y2_m]).to(x.device
).unsqueeze(0).expand(x.size(0),-1,-1,-1) # 1x4xHxW
x_ = torch.cat([x, pixel_emb], dim=1)
if self.bottom:
x_down_0 = self.enc_down_0(x_)
x_down_1 = self.enc_down_1(x_down_0)
else:
x_down_1 = self.enc_down_1(x_)
x_down_2 = self.enc_down_2(x_down_1)
x_down_3 = self.enc_down_3(x_down_2)
x_up_3 = self.enc_up_3(x_down_3)
x_up_2 = self.enc_up_2(torch.cat([x_up_3, x_down_2], dim=1))
feature_map = self.enc_up_1(torch.cat([x_up_2, x_down_1], dim=1)) # BxCxHxW
return feature_map