/
arch.py
134 lines (115 loc) · 4.94 KB
/
arch.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
import torch
import torch.nn as nn
class ResidualBlock(nn.Module):
"""
https://github.com/hhb072/IntroVAE
Difference: self.bn2 on output and not on (output + identity)
"""
def __init__(self, inc=64, outc=64, groups=1, scale=1.0):
super(ResidualBlock, self).__init__()
midc = int(outc * scale)
if inc is not outc:
self.conv_expand = nn.Conv2d(in_channels=inc, out_channels=outc, kernel_size=1, stride=1, padding=0,
groups=1, bias=False)
else:
self.conv_expand = None
self.conv1 = nn.Conv2d(in_channels=inc, out_channels=midc, kernel_size=3, stride=1, padding=1, groups=groups,
bias=False)
self.bn1 = nn.BatchNorm2d(midc)
self.relu1 = nn.LeakyReLU(0.2, inplace=True)
self.conv2 = nn.Conv2d(in_channels=midc, out_channels=outc, kernel_size=3, stride=1, padding=1, groups=groups,
bias=False)
self.bn2 = nn.BatchNorm2d(outc)
self.relu2 = nn.LeakyReLU(0.2, inplace=True)
def forward(self, x):
if self.conv_expand is not None:
identity_data = self.conv_expand(x)
else:
identity_data = x
output = self.relu1(self.bn1(self.conv1(x)))
output = self.conv2(output)
output = self.bn2(output)
output = self.relu2(torch.add(output, identity_data))
return output
class Encoder(nn.Module):
def __init__(self, cdim=3, zdim=512, channels=(64, 128, 256, 512, 512, 512), image_size=256, conditional=False,
cond_dim=10):
super(Encoder, self).__init__()
self.zdim = zdim
self.cdim = cdim
self.image_size = image_size
self.conditional = conditional
self.cond_dim = cond_dim
cc = channels[0]
self.main = nn.Sequential(
nn.Conv2d(cdim, cc, 5, 1, 2, bias=False),
nn.BatchNorm2d(cc),
nn.LeakyReLU(0.2),
nn.AvgPool2d(2),
)
sz = image_size // 2
for ch in channels[1:]:
self.main.add_module('res_in_{}'.format(sz), ResidualBlock(cc, ch, scale=1.0))
self.main.add_module('down_to_{}'.format(sz // 2), nn.AvgPool2d(2))
cc, sz = ch, sz // 2
self.main.add_module('res_in_{}'.format(sz), ResidualBlock(cc, cc, scale=1.0))
self.conv_output_size = self.calc_conv_output_size()
num_fc_features = torch.zeros(self.conv_output_size).view(-1).shape[0]
print("conv shape: ", self.conv_output_size)
print("num fc features: ", num_fc_features)
if self.conditional:
self.fc = nn.Linear(num_fc_features + self.cond_dim, 2 * zdim)
else:
self.fc = nn.Linear(num_fc_features, 2 * zdim)
def calc_conv_output_size(self):
dummy_input = torch.zeros(1, self.cdim, self.image_size, self.image_size)
dummy_input = self.main(dummy_input)
return dummy_input[0].shape
def forward(self, x, o_cond=None):
y = self.main(x).view(x.size(0), -1)
if self.conditional and o_cond is not None:
y = torch.cat([y, o_cond], dim=1)
y = self.fc(y)
mu, logvar = y.chunk(2, dim=1)
return mu, logvar
class Decoder(nn.Module):
def __init__(self, cdim=3, zdim=512, channels=(64, 128, 256, 512, 512, 512), image_size=256, conditional=False,
conv_input_size=None, cond_dim=10):
super(Decoder, self).__init__()
self.cdim = cdim
self.image_size = image_size
self.conditional = conditional
cc = channels[-1]
self.conv_input_size = conv_input_size
if conv_input_size is None:
num_fc_features = cc * 4 * 4
else:
num_fc_features = torch.zeros(self.conv_input_size).view(-1).shape[0]
self.cond_dim = cond_dim
if self.conditional:
self.fc = nn.Sequential(
nn.Linear(zdim + self.cond_dim, num_fc_features),
nn.ReLU(True),
)
else:
self.fc = nn.Sequential(
nn.Linear(zdim, num_fc_features),
nn.ReLU(True),
)
sz = 4
self.main = nn.Sequential()
for ch in channels[::-1]:
self.main.add_module('res_in_{}'.format(sz), ResidualBlock(cc, ch, scale=1.0))
self.main.add_module('up_to_{}'.format(sz * 2), nn.Upsample(scale_factor=2, mode='nearest'))
cc, sz = ch, sz * 2
self.main.add_module('res_in_{}'.format(sz), ResidualBlock(cc, cc, scale=1.0))
self.main.add_module('predict', nn.Conv2d(cc, cdim, 5, 1, 2))
def forward(self, z, y_cond=None):
z = z.view(z.size(0), -1)
if self.conditional and y_cond is not None:
y_cond = y_cond.view(y_cond.size(0), -1)
z = torch.cat([z, y_cond], dim=1)
y = self.fc(z)
y = y.view(z.size(0), *self.conv_input_size)
y = self.main(y)
return y