-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
194 lines (175 loc) · 7.94 KB
/
model.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
import numpy as np
import torch
from torch import nn
from torch.nn import functional as F
from discretized_mix_logistic import discretized_mix_logistic_loss, sample_from_discretized_mix_logistic
from helpers import draw_gaussian_diag_samples, gaussian_analytical_kl, get_conv, get_3x3, get_1x1, pad_channels, get_width_settings, parse_layer_string
class HModule(nn.Module):
def __init__(self, H):
super().__init__()
self.H = H
self.build()
class DmolNet(nn.Module):
def __init__(self, H):
super().__init__()
self.H = H
self.width = H.width
self.out_conv = get_conv(H.width, H.num_mixtures * 10, kernel_size=1, stride=1, padding=0)
def nll(self, px_z, x):
return discretized_mix_logistic_loss(x=x, l=self.forward(px_z), low_bit=self.H.dataset in ['ffhq_256'])
def forward(self, px_z):
xhat = self.out_conv(px_z)
return xhat.permute(0, 2, 3, 1)
def sample(self, px_z):
im = sample_from_discretized_mix_logistic(self.forward(px_z), self.H.num_mixtures)
xhat = (im + 1.0) * 127.5
xhat = xhat.detach().cpu().numpy()
xhat = np.minimum(np.maximum(0.0, xhat), 255.0).astype(np.uint8)
return xhat
class Block(nn.Module):
def __init__(self, in_width, middle_width, out_width, down_rate=None, residual=False, use_3x3=True, zero_last=False):
super().__init__()
self.down_rate = down_rate
self.residual = residual
self.c1 = get_1x1(in_width, 2*middle_width)
self.c2 = get_3x3(2*middle_width, 2*middle_width) if use_3x3 else get_1x1(2*middle_width, 2*middle_width)
self.c3 = get_3x3(2*middle_width, 2*middle_width) if use_3x3 else get_1x1(2*middle_width, 2*middle_width)
self.c4 = get_1x1(2*middle_width, out_width, zero_weights=zero_last)
def forward(self, x):
xhat = self.c1(F.gelu(x))
xhat = self.c2(F.gelu(xhat))
xhat = self.c3(F.gelu(xhat))
xhat = self.c4(F.gelu(xhat))
out = x + xhat if self.residual else xhat
if self.down_rate is not None:
out = F.avg_pool2d(out, kernel_size=self.down_rate, stride=self.down_rate)
return out
class Encoder(HModule):
def build(self):
H = self.H
self.in_conv = get_3x3(H.image_channels, H.width)
self.widths = get_width_settings(H.width, H.custom_width_str)
enc_blocks = []
blockstr = parse_layer_string(H.enc_blocks)
for res, down_rate in blockstr:
use_3x3 = res > 2 # Don't use 3x3s for 1x1, 2x2 patches
enc_blocks.append(Block(self.widths[res], int(self.widths[res] * H.bottleneck_multiple), self.widths[res], down_rate=down_rate, residual=True, use_3x3=use_3x3))
n_blocks = len(blockstr)
for b in enc_blocks:
b.c4.weight.data *= np.sqrt(1 / n_blocks)
self.enc_blocks = nn.ModuleList(enc_blocks)
def forward(self, x):
x = x.permute(0, 3, 1, 2).contiguous()
x = self.in_conv(x)
activations = {}
activations[x.shape[2]] = x
for block in self.enc_blocks:
x = block(x)
res = x.shape[2]
x = x if x.shape[1] == self.widths[res] else pad_channels(x, self.widths[res])
activations[res] = x
return activations
class DecBlock(nn.Module):
def __init__(self, H, res, mixin, n_blocks):
super().__init__()
self.base = res
self.mixin = mixin
self.H = H
self.widths = get_width_settings(H.width, H.custom_width_str)
width = self.widths[res]
use_3x3 = res > 2
cond_width = int(width * H.bottleneck_multiple)
self.zdim = H.zdim
self.enc = Block(width * 2, cond_width, H.zdim * 2, residual=False, use_3x3=use_3x3)
self.prior = Block(width, cond_width, H.zdim * 2 + width, residual=False, use_3x3=use_3x3, zero_last=True)
self.z_proj = get_1x1(H.zdim, width)
self.z_proj.weight.data *= np.sqrt(1 / n_blocks)
self.resnet = Block(width, cond_width, width, residual=True, use_3x3=use_3x3)
self.resnet.c4.weight.data *= np.sqrt(1 / n_blocks)
self.z_fn = lambda x: self.z_proj(x)
def sample(self, x, acts):
qm, qv = self.enc(torch.cat([x, acts], dim=1)).chunk(2, dim=1)
feats = self.prior(x)
pm, pv, xpp = feats[:, :self.zdim, ...], feats[:, self.zdim:self.zdim * 2, ...], feats[:, self.zdim * 2:, ...]
x = x + xpp
z = draw_gaussian_diag_samples(qm, qv)
kl = gaussian_analytical_kl(qm, pm, qv, pv)
return z, x, kl
def sample_prior(self, x):
feats = self.prior(x)
pm, pv, xpp = feats[:, :self.zdim, ...], feats[:, self.zdim:self.zdim * 2, ...], feats[:, self.zdim * 2:, ...]
x = x + xpp
pv = pv + torch.ones_like(pv) * np.log(0.85)
z = draw_gaussian_diag_samples(pm, pv)
return z, x, None
def get_inputs(self, xs, activations):
acts = activations[self.base]
try:
x = xs[self.base]
except KeyError:
x = torch.zeros_like(acts)
if acts.shape[0] != x.shape[0]:
x = x.repeat(acts.shape[0], 1, 1, 1)
return x, acts
def forward(self, xs, activations, sample_prior, get_latents=False):
x, acts = self.get_inputs(xs, activations)
if self.mixin is not None:
x = x + F.interpolate(xs[self.mixin][:, :x.shape[1], ...], scale_factor=self.base // self.mixin)
if sample_prior:
z, x, kl = self.sample_prior(x)
else:
z, x, kl = self.sample(x, acts)
x = x + self.z_fn(z)
x = self.resnet(x)
xs[self.base] = x
if get_latents:
return xs, dict(z=z.detach(), kl=kl)
return xs, dict(kl=kl)
class Decoder(HModule):
def build(self):
H = self.H
resos = set()
dec_blocks = []
self.widths = get_width_settings(H.width, H.custom_width_str)
blocks = parse_layer_string(H.dec_blocks)
for idx, (res, mixin) in enumerate(blocks):
dec_blocks.append(DecBlock(H, res, mixin, n_blocks=len(blocks)))
resos.add(res)
self.resolutions = sorted(resos)
self.dec_blocks = nn.ModuleList(dec_blocks)
self.bias_xs = nn.ParameterList([nn.Parameter(torch.zeros(1, self.widths[res], res, res)) for res in self.resolutions if res <= H.no_bias_above])
self.out_net = DmolNet(H)
self.gain = nn.Parameter(torch.ones(1, H.width, 1, 1))
self.bias = nn.Parameter(torch.zeros(1, H.width, 1, 1))
self.final_fn = lambda x: x * self.gain + self.bias
self.label_embedding = nn.Embedding(10, 1*1*H.width)
self.k = H.k
def forward(self, y, activations, k=None, get_latents=False):
stats = []
xs = {1: self.label_embedding(y).reshape(-1, self.H.width, 1, 1)}
for i, block in enumerate(self.dec_blocks):
sample_prior = False if k is None else i >= k
xs, block_stats = block(xs, activations, sample_prior, get_latents=get_latents)
stats.append(block_stats)
xs[self.H.image_size] = self.final_fn(xs[self.H.image_size])
return xs[self.H.image_size], stats
class VAE(HModule):
def build(self):
self.encoder = Encoder(self.H)
self.decoder = Decoder(self.H)
def forward(self, x, y):
activations = self.encoder.forward(x)
px_z, stats = self.decoder.forward(y, activations)
rate_per_pixel = torch.zeros(y.shape)
ndims = np.prod(x.shape[1:])
for statdict in stats:
if statdict["kl"] is None: continue
rate_per_pixel += statdict["kl"].sum(dim=(1,2,3))
rate_per_pixel /= ndims
distortion_per_pixel = self.decoder.out_net.nll(px_z, x)
return distortion_per_pixel.mean(), rate_per_pixel.mean()
def reconstruct(self, x, y, k):
activations = self.encoder.forward(x)
px_z, _ = self.decoder.forward(y, activations, k)
recs = self.decoder.out_net.sample(px_z)
return recs