-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
202 lines (156 loc) · 6.82 KB
/
utils.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
from matplotlib import pyplot as plt
from numpy import prod
from math import sqrt
import torch
from collections import OrderedDict
from torch.autograd import grad, Variable
from torch import nn
import math
def sv_img(img, savename, epoch = None, title=None,):
npimg = img[[2,1,0]].permute(1,2,0).numpy()
plt.imshow(npimg, interpolation='nearest')
if epoch is not None:
plt.title("Epoch {}".format(epoch))
if title is not None:
plt.title(title)
plt.axis('off')
plt.tight_layout()
plt.savefig(savename, dpi=300)
def get_gradient_stats(net):
"""Returns the maximum of the gradients in this network. Useful to set proper weight clipping"""
maximum = 0
for p in net.parameters():
maximum = max(maximum, p.grad.data.max())
return maximum
def KLfromSN(x):
"""Assumes the inputs are batched samples from a Gaussian dist. Calculates the KL of this dist. from a mean=0, var=1 dist.
"""
sig = torch.var(x, 0)
mu2 = torch.mean(x, 0) ** 2
log_sig = torch.log(sig + 1e-12)
mu2_m1 = mu2 - 1
out = .5 * torch.sum(sig + mu2_m1 - log_sig)
return out
def to_variable(intermediate_state_dict):
v_state_dict = OrderedDict()
for i, (layer, activations) in enumerate(intermediate_state_dict.items()):
v_state_dict[layer] = Variable(activations.detach(), requires_grad=True)
return v_state_dict
def get_detached_state_dict(state_dict):
detached_dict = {k: None if (v is None) else v.detach()
for k, v in state_dict.items()}
return detached_dict
def get_gradient_penalty(discriminator, state_dict, lamda = 1, p=1, ):
"""Calculate the gradient of the discriminator's output w/r/t inputs"""
state_dict = to_variable(state_dict)
d = discriminator(state_dict)
bs = d.size(0)
gradients = grad(outputs=d, inputs=[s for _,s in state_dict.items()],
grad_outputs=torch.ones(d.size()).to(d.device),
create_graph=True, retain_graph=True, only_inputs=True, allow_unused=True)
all_gs = torch.cat([g.view(bs, -1) for g in gradients if g is not None],dim=1)
gp = ((all_gs.norm(p, dim=1) - 1) ** p).mean()
return gp * lamda
def get_gradient_penalty_inputs(readout_discriminator, inference_net, input_data, lamda=10, p=2, ):
"""Calculate the gradient of the discriminator's output w/r/t inputs,
and ensure the encoder changes accordingly too"""
input_data = Variable(input_data.detach(), requires_grad=True)
inference_layer = inference_net(input_data, to_layer=4, update_states=False)
d = readout_discriminator(inference_layer)
bs = d.size(0)
gradients = grad(outputs=d, inputs=input_data,
grad_outputs=torch.ones(d.size()).to(d.device),
create_graph=True, retain_graph=True, only_inputs=True)[0]
gp = ((gradients.view(gradients.size()[0], -1).norm(p, dim=1) - 1) ** p).mean()
return gp * lamda
def stdDev(x):
r"""
Add a standard deviation channel to the current layer. Assumes a linear layer
In other words:
1) Compute the standard deviation of the features over the minibatch
2) Get its mean, over all channels
3) expand the layer and concatenate it
Args:
- x (tensor): previous layer
"""
size = x.size()
y = torch.var(x, 0)
y = torch.sqrt(y + 1e-8)
y = y.view(-1)
y = torch.mean(y)
y = torch.ones(size[0], 1).to(x.device) * y
return torch.cat([x, y], dim=1)
def stdDev_conv(x):
r"""
Add a standard deviation channel to the current layer.
In other words:
1) Compute the standard deviation of the feature map over the minibatch
2) concatenate it with the input
Args:
- x (tensor): previous layer
"""
size = x.size()
y = torch.var(x, 0)
y = torch.sqrt(y + 1e-8)
y = torch.mean(y, 0)
y = torch.ones(size[0], 1, size[2], size[3]).to(x.device) * y
return torch.cat([x, y], dim=1)
def gen_surprisal(inf_state_dict, generator, criterion, surprisal=None,
detach_inference=True, as_list = False):
ML_loss = 0 if not as_list else []
sig = nn.Sigmoid()
for i, G in enumerate(generator.listed_modules):
lower_h = inf_state_dict[generator.layer_names[i]]
upper_h = inf_state_dict[generator.layer_names[i + 1]]
if detach_inference:
lower_h = lower_h.detach()
upper_h = upper_h.detach()
x = G(upper_h)
x = generator.activations[generator.layer_names[i + 1]](x)
if surprisal is not None:
scale = 2 * sig(-surprisal.detach().view(-1))
loss = criterion(x, lower_h).view(lower_h.size(0), -1).mean(dim=1)
loss = (loss * scale).mean()
else:
loss = criterion(x, lower_h)
if as_list:
ML_loss.append(loss.detach())
else:
ML_loss = ML_loss + loss
return ML_loss
def get_pixelwise_channel_norms(inference, generator):
"""This can be used to implement a 'soft' divisive normalization (where the 'hard' version is
that which is implemented in Karras et al.). It can also be used for logging & debugging.
Logs the total value by layer as a tuple (inferred norm, generated norm)
Returns the pixel-wise distance from 0 (as a cost) to be minimized)"""
epsilon = 1e-8
mse = nn.MSELoss()
total_distance_from_1 = 0
for i, layer in enumerate(inference.layer_names):
inferred_state = inference.intermediate_state_dict[layer]
should_be = math.sqrt(list(inferred_state.size())[1])
inferred_state_pixel_norm = (((inferred_state ** 2).mean(dim=1) + epsilon).sqrt())
inferred_state_pixel_norm_dist_from_1 = mse(inferred_state_pixel_norm,
should_be * torch.ones_like(inferred_state_pixel_norm))
total_distance_from_1 = inferred_state_pixel_norm_dist_from_1 + total_distance_from_1
return total_distance_from_1
def weights_init(net):
for m in net.modules():
if isinstance(m, nn.Conv2d):
m.weight.data.normal_(0, 0.02)
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.ConvTranspose2d):
m.weight.data.normal_(0, 0.02)
if m.bias is not None:
m.bias.data.zero_()
elif isinstance(m, nn.Linear):
m.weight.data.normal_(0, 0.02)
if m.bias is not None:
m.bias.data.zero_()
def promote_attributes(dataparallel_mod):
dataparallel_mod.listed_modules = dataparallel_mod.module.listed_modules
dataparallel_mod.intermediate_state_dict = dataparallel_mod.module.intermediate_state_dict
dataparallel_mod.activations = dataparallel_mod.module.activations
dataparallel_mod.layer_names = dataparallel_mod.module.layer_names
dataparallel_mod.sigma2 = dataparallel_mod.module.sigma2