-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdcgan_models.py
395 lines (310 loc) · 16.3 KB
/
dcgan_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
import torch
import torch.nn as nn
from collections import OrderedDict
from torch.autograd import Variable, grad
import math
from numpy import prod
from utils import stdDev, weights_init
class Generator(nn.Module):
""" The feedback edges of the cortex. Generates images with the DCGAN architecture.
Parameters
noise_dim
n_filters
n_img_channels
noise_type: What sort of noise is applied after each convolutional layer? Gaussian, but of what variance?
'fixed' = Noise is always Gaussian with variance 0.01
'none' = No noise
'learned_by_layer' = The variance is learned and different for each layer
'learned_by_channel' = The variance is learned and different for each channel and each layer
'learned_filter' = Tariance that is the result of a learned filter
on the previous layer. Like the `reparameterization trick` of
variational autoencoders.
'poisson' = variance is equal to value
backprop_to_start
image_size
batchnorm
normalize
he_init
"""
def __init__(self, noise_dim, n_filters, n_img_channels, image_size = 32,hard_norm = False):
super(Generator, self).__init__()
self.noise_dim = noise_dim
self.n_filters = n_filters
self.n_img_channels = n_img_channels
self.generative_5to4_conv = nn.ConvTranspose2d(noise_dim, n_filters * 8, image_size//16, 1, 0 )
self.generative_4to3_conv = nn.ConvTranspose2d(n_filters * 8, n_filters * 4, 4, 2, 1 )
self.generative_3to2_conv = nn.ConvTranspose2d(n_filters * 4, n_filters * 2, 4, 2, 1 )
self.generative_2to1_conv = nn.ConvTranspose2d(n_filters * 2, n_filters, 4, 2, 1 )
self.generative_1to0_conv = nn.ConvTranspose2d(n_filters, n_img_channels, 4, 2, 1 )
self.normalizer = NormalizationLayer() if hard_norm else null()
# list modules bottom to top. Probably a more general way exists
self.listed_modules = [self.generative_1to0_conv,
self.generative_2to1_conv,
self.generative_3to2_conv,
self.generative_4to3_conv,
self.generative_5to4_conv]
self.activations = OrderedDict([('Layer1', nn.Tanh()),
('Layer2', nn.ReLU()),
('Layer3', nn.ReLU()),
('Layer4', nn.ReLU()),
('Layer5', nn.ReLU())])
self.intermediate_state_dict = OrderedDict([('Input', None),
('Layer1', None),
('Layer2', None),
('Layer3', None),
('Layer4', None),
('Layer5', None)])
self.layer_names = list(self.intermediate_state_dict.keys())
weights_init(self)
# noise applied
self.sigma2 = 0.01
def forward(self, x, from_layer=5, update_states=True):
x = self.normalizer(x)
# iterate through layers and pass the noise downwards
for i, (G, layer_name) in enumerate(zip(self.listed_modules[::-1],
self.layer_names[:0:-1])):
if from_layer < 5 - i:
continue
if update_states:
self.intermediate_state_dict[layer_name] = x
x = G(x)
if layer_name != "Layer1":
x = self.normalizer(x)
if self.training:
noise = torch.empty_like(x).normal_() * self.sigma2
x += noise
x = self.activations[layer_name](x)
if update_states:
self.intermediate_state_dict["Input"] = x
return x
class Inference(nn.Module):
def __init__(self, n_latents, n_filters, n_img_channels, image_size = 32, bn = True,
noise_before=False, hard_norm=False, spec_norm=True, derelu = True):
super(Inference, self).__init__()
self.n_latents = n_latents
self.n_filters = n_filters
self.n_img_channels = n_img_channels
p1=1 if noise_before else 0
self.noise_before = noise_before
self.inference_5from4_conv = BasicBlock(n_filters * 8 + p1, n_latents, image_size//16, 1, 0,
spec_norm = spec_norm, bn=bn, derelu = derelu)
self.inference_4from3_conv = BasicBlock(n_filters * 4 + p1, n_filters * 8, 4, 2, 1,
spec_norm = spec_norm, bn=bn, derelu = derelu)
self.inference_3from2_conv = BasicBlock(n_filters * 2 + p1, n_filters * 4, 4, 2, 1,
spec_norm = spec_norm, bn=bn, derelu = derelu)
self.inference_2from1_conv = BasicBlock(n_filters + p1, n_filters * 2, 4, 2, 1,
spec_norm = spec_norm, bn=bn, derelu = derelu)
self.inference_1from0_conv = BasicBlock(n_img_channels + p1, n_filters , 4, 2, 1, derelu=False,
bn=bn, spec_norm = spec_norm )
self.normalizer = NormalizationLayer() if hard_norm else null()
self.listed_modules = [self.inference_1from0_conv,
self.inference_2from1_conv,
self.inference_3from2_conv,
self.inference_4from3_conv,
self.inference_5from4_conv]
self.intermediate_state_dict = OrderedDict([('Input', None),
('Layer1', None),
('Layer2', None),
('Layer3', None),
('Layer4', None),
('Layer5', None)])
self.activations = OrderedDict([('Layer1', nn.ReLU()),
('Layer2', nn.ReLU()),
('Layer3', nn.ReLU()),
('Layer4', nn.ReLU()),
('Layer5', null())])
self.layer_names = list(self.intermediate_state_dict.keys())
weights_init(self)
#noise applied after each conv
self.sigma2 = 0.01
def forward(self, x, to_layer=5, update_states=True):
self.intermediate_state_dict['Input'] = x
# iterate through layers and pass the input upwards
for i, (F, layer_name) in enumerate(zip(self.listed_modules,
self.layer_names[1:])):
if i >= to_layer:
continue
if self.noise_before:
if self.training:
noise = torch.empty(x.size(0), 1, x.size(2), x.size(3)).normal_().to(x.device)
else:
noise = torch.zeros(x.size(0), 1, x.size(2), x.size(3)).to(x.device)
x = torch.cat([x, noise], dim=1)
x = F(x, self.sigma2)
x = self.activations[layer_name](x)
x = self.normalizer(x)
if update_states:
self.intermediate_state_dict[layer_name] = x
return x
class BasicBlock(nn.Module):
def __init__(self, inplanes, planes, kernel, stride, padding, derelu=True, spec_norm = False, bn = True):
super(BasicBlock, self).__init__()
norm_layer = nn.BatchNorm2d
self.planes = planes
if spec_norm:
self.conv1 = nn.utils.spectral_norm(nn.Conv2d(inplanes, planes, kernel, stride, padding, bias=False))
else:
self.conv1 = nn.Conv2d(inplanes, planes, kernel, stride, padding, bias=False)
self.bn1 = norm_layer(planes) if bn else null()
self.derelu = DeReLU(inplanes) if derelu else null()
def forward(self, out, sigma2 = 0.01):
out = self.derelu(out)
out = self.conv1(out)
out = self.bn1(out)
noise = torch.empty_like(out).normal_() * sigma2
out = out + noise
return out
class DeReLU(nn.Module):
"""The stochastic "inverse" of a ReLU.
Puts random negative noise where there used to be zeros. """
def __init__(self, input_channels):
super(DeReLU, self).__init__()
self.scale = nn.Parameter(.5*torch.ones(1,input_channels,1,1), requires_grad = False)
def forward(self,x):
#exponentially distributed negative noise
noise = torch.empty_like(x).uniform_(1e-8, 1).log_() * self.scale
# place where it's zero
out = torch.where(x>0, x, noise)
return out
class null(nn.Module):
"Pickleable nothing"
def __init__(self):
super(null, self).__init__()
def forward(self, x):
return x
class Discriminator(nn.Module):
"""Takes both the input and latent state."""
def __init__(self, n_latents, n_filters, n_img_channels, image_size=32, hidden_dim=100, hard_norm=False,
dropout = 0):
super(Discriminator, self).__init__()
self.linear01 = (nn.Linear(n_img_channels * image_size ** 2 + n_filters * (image_size//2) ** 2 + 2, hidden_dim))
self.linear12 = (nn.Linear(n_filters * (image_size//2) ** 2 + n_filters * 2 * (image_size//4) ** 2 + 2, hidden_dim))
self.linear23 = (nn.Linear(n_filters * 2 * (image_size//4) ** 2 + n_filters * 4 * (image_size//8) ** 2 + 2, hidden_dim))
self.linear34 = (nn.Linear(n_filters * 4 * (image_size//8) ** 2 + n_filters * 8 * (image_size//16) ** 2 + 2, hidden_dim))
self.linear45 = (nn.Linear(n_filters * 8 * (image_size//16) ** 2 + n_latents + 2, hidden_dim))
self.linear2 = (nn.Linear(hidden_dim * 5, 1))
self.relu = nn.LeakyReLU()
self.normalizer = NormalizationLayer() if hard_norm else null()
self.dropout = nn.Dropout(dropout)
self.sigma2 = 0.01
weights_init(self)
def forward(self, state):
ins = [stdDev(s.view(s.size(0), -1)) for _, s in state.items()]
x = torch.cat([self.linear01(torch.cat([ins[0], ins[1]], dim=1)),
self.linear12(torch.cat([ins[1], ins[2]], dim=1)),
self.linear23(torch.cat([ins[2], ins[3]], dim=1)),
self.linear34(torch.cat([ins[3], ins[4]], dim=1)),
self.linear45(torch.cat([ins[4], ins[5]], dim=1))], dim=1)
noise = torch.empty_like(x).normal_() * self.sigma2
x = x + noise
x = self.relu(x)
x = self.dropout(x)
x = self.normalizer(x)
x = self.linear2(x)
return x
class ReadoutDiscriminator(nn.Module):
def __init__(self, n_filters, image_size,
spec_norm=True):
super(ReadoutDiscriminator, self).__init__()
# number of features in the top level (before latents)
self.n_features = n_filters * 8 * (image_size // 16) ** 2
if spec_norm:
self.readout = nn.utils.spectral_norm(nn.Linear(self.n_features + 1, 1))
else:
self.readout = nn.Linear(self.n_features + 1, 1)
weights_init(self)
def forward(self, inference_layer):
x = self.readout(stdDev(inference_layer.view(-1, self.n_features)))
return x
class NoiseChannel(nn.Module):
"""Adds some additional channels that are pure uniform noise"""
def __init__(self,n_channels = 3):
super(NoiseChannel, self).__init__()
self.n_channels = n_channels
def forward(self, x):
if self.training:
noise = torch.empty_like(x)[:,:self.n_channels,:,:].uniform_(1e-8,1).reciprocal_().log_()*0.02
else:
noise = torch.zeros_like(x)[:, :self.n_channels, :, :]
x = torch.cat([x,noise],dim=1)
return x
class AddNoise(nn.Module):
"""
Adds some noise with a certain variance. During evaluation no noise is applied.
noise_type: What sort of noise is applied after each convolutional layer? Gaussian, but of what variance?
'fixed' = Noise is always Gaussian with variance 0.01
'none' = No noise
'learned_by_layer' = The variance is learned and different for each layer
'learned_by_channel' = The variance is learned and different for each channel and each layer.
Requires n_channels be set.
'learned_filter' = Variance that is the result of a learned filter
on the previous layer. Like the `reparameterization trick` of
variational autoencoders.
'poisson' = variance is equal to value, divided by 10
Note: if `learned_filter` is used, the inputs of the previous layer are interpreted so that the first half
of channels are the mean and the second half of channels of the variance of the distribution that is the
outputs of the layer. In this case this module results in outputs that are not of the same shape
as the inputs but rather of half the number of channel dimensions.
For alo other modes, the output is of the same shape as the inputs.
"""
def __init__(self, noise_type, n_channels = None, fixed_variance = 0.02):
super(AddNoise, self).__init__()
self.noise_type = noise_type
self.fixed_variance = fixed_variance
if self.noise_type == 'learned_by_layer':
self.log_sigma = nn.Parameter(torch.ones(1) * -2)
elif self.noise_type == 'learned_by_channel':
self.log_sigma = nn.Parameter(torch.ones(n_channels) * -2)
elif self.noise_type == 'poisson' or self.noise_type == 'exponential':
self.relu = nn.ReLU()
self.decay = 1.
def forward(self, x):
if self.training:
if self.noise_type == 'none':
out = x
elif self.noise_type == 'fixed':
noise = torch.empty_like(x).normal_()
out = x + noise * self.fixed_variance
elif self.noise_type == 'learned_by_layer':
noise = torch.empty_like(x).normal_()
out = x + noise * torch.exp(self.log_sigma)
elif self.noise_type == 'learned_by_channel':
noise = torch.empty_like(x).normal_()
out = x + noise * torch.exp(self.log_sigma)[None,:,None,None]
elif self.noise_type == 'learned_filter':
n_channels = x.size()[1]
assert n_channels % 2 == 0
mu = x[:, :n_channels//2,:,:]
log_sigma = x[:, n_channels//2:,:,:]
#rescale log_sigma and shrink. This is just to make the initialization the right scale
log_sigma = log_sigma * .01 - 2
noise = torch.empty_like(mu).normal_()
out = mu + noise * torch.exp(log_sigma)
elif self.noise_type == 'poisson':
noise = torch.empty_like(x).normal_()
out = x + noise * self.relu(x) / 10
elif self.noise_type == 'exponential':
noise = torch.empty_like(x).uniform_(1e-8, 1).reciprocal_().log_()
# noise = self.relu(noise - 1)
out = x + self.decay * noise
elif self.noise_type == 'laplace':
noise = torch.empty_like(x).uniform_(1e-8, 1).reciprocal_().log_()
noise *= (torch.empty_like(x).bernoulli_() * 2) - 1
out = x + self.decay * noise * self.fixed_variance
else:
raise AssertionError("noise_type not in "
"['none', 'fixed', 'learned_by_layer', 'learned_by_channel', "
"'poisson', 'learned_filter']")
elif self.noise_type == 'learned_filter':
n_channels = x.size()[1]
assert n_channels % 2 == 0
out = x[:, :n_channels // 2, :, :]
else:
out = x
# self.decay *= .9999
return out
class NormalizationLayer(nn.Module):
def __init__(self):
super(NormalizationLayer, self).__init__()
def forward(self, x, epsilon=1e-8):
return x * (((x**2).mean(dim=1, keepdim=True) + epsilon).rsqrt())