-
Notifications
You must be signed in to change notification settings - Fork 89
/
utils.py
144 lines (111 loc) · 5.4 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
import numpy as np
from torch import nn
from torch import autograd
import torch
from visualize import VisdomPlotter
import os
import pdb
class Concat_embed(nn.Module):
def __init__(self, embed_dim, projected_embed_dim):
super(Concat_embed, self).__init__()
self.projection = nn.Sequential(
nn.Linear(in_features=embed_dim, out_features=projected_embed_dim),
nn.BatchNorm1d(num_features=projected_embed_dim),
nn.LeakyReLU(negative_slope=0.2, inplace=True)
)
def forward(self, inp, embed):
projected_embed = self.projection(embed)
replicated_embed = projected_embed.repeat(4, 4, 1, 1).permute(2, 3, 0, 1)
hidden_concat = torch.cat([inp, replicated_embed], 1)
return hidden_concat
class minibatch_discriminator(nn.Module):
def __init__(self, num_channels, B_dim, C_dim):
super(minibatch_discriminator, self).__init__()
self.B_dim = B_dim
self.C_dim =C_dim
self.num_channels = num_channels
T_init = torch.randn(num_channels * 4 * 4, B_dim * C_dim) * 0.1
self.T_tensor = nn.Parameter(T_init, requires_grad=True)
def forward(self, inp):
inp = inp.view(-1, self.num_channels * 4 * 4)
M = inp.mm(self.T_tensor)
M = M.view(-1, self.B_dim, self.C_dim)
op1 = M.unsqueeze(3)
op2 = M.permute(1, 2, 0).unsqueeze(0)
output = torch.sum(torch.abs(op1 - op2), 2)
output = torch.sum(torch.exp(-output), 2)
output = output.view(M.size(0), -1)
output = torch.cat((inp, output), 1)
return output
class Utils(object):
@staticmethod
def smooth_label(tensor, offset):
return tensor + offset
@staticmethod
# based on: https://github.com/caogang/wgan-gp/blob/master/gan_cifar10.py
def compute_GP(netD, real_data, real_embed, fake_data, LAMBDA):
BATCH_SIZE = real_data.size(0)
alpha = torch.rand(BATCH_SIZE, 1)
alpha = alpha.expand(BATCH_SIZE, int(real_data.nelement() / BATCH_SIZE)).contiguous().view(BATCH_SIZE, 3, 64, 64)
alpha = alpha.cuda()
interpolates = alpha * real_data + ((1 - alpha) * fake_data)
interpolates = interpolates.cuda()
interpolates = autograd.Variable(interpolates, requires_grad=True)
disc_interpolates, _ = netD(interpolates, real_embed)
gradients = autograd.grad(outputs=disc_interpolates, inputs=interpolates,
grad_outputs=torch.ones(disc_interpolates.size()).cuda(),
create_graph=True, retain_graph=True, only_inputs=True)[0]
gradient_penalty = ((gradients.norm(2, dim=1) - 1) ** 2).mean() * LAMBDA
return gradient_penalty
@staticmethod
def save_checkpoint(netD, netG, dir_path, subdir_path, epoch):
path = os.path.join(dir_path, subdir_path)
if not os.path.exists(path):
os.makedirs(path)
torch.save(netD.state_dict(), '{0}/disc_{1}.pth'.format(path, epoch))
torch.save(netG.state_dict(), '{0}/gen_{1}.pth'.format(path, epoch))
@staticmethod
def weights_init(m):
classname = m.__class__.__name__
if classname.find('Conv') != -1:
m.weight.data.normal_(0.0, 0.02)
elif classname.find('BatchNorm') != -1:
m.weight.data.normal_(1.0, 0.02)
m.bias.data.fill_(0)
class Logger(object):
def __init__(self, vis_screen):
self.viz = VisdomPlotter(env_name=vis_screen)
self.hist_D = []
self.hist_G = []
self.hist_Dx = []
self.hist_DGx = []
def log_iteration_wgan(self, epoch, gen_iteration, d_loss, g_loss, real_loss, fake_loss):
print("Epoch: %d, Gen_iteration: %d, d_loss= %f, g_loss= %f, real_loss= %f, fake_loss = %f" %
(epoch, gen_iteration, d_loss.data.cpu().mean(), g_loss.data.cpu().mean(), real_loss, fake_loss))
self.hist_D.append(d_loss.data.cpu().mean())
self.hist_G.append(g_loss.data.cpu().mean())
def log_iteration_gan(self, epoch, d_loss, g_loss, real_score, fake_score):
print("Epoch: %d, d_loss= %f, g_loss= %f, D(X)= %f, D(G(X))= %f" % (
epoch, d_loss.data.cpu().mean(), g_loss.data.cpu().mean(), real_score.data.cpu().mean(),
fake_score.data.cpu().mean()))
self.hist_D.append(d_loss.data.cpu().mean())
self.hist_G.append(g_loss.data.cpu().mean())
self.hist_Dx.append(real_score.data.cpu().mean())
self.hist_DGx.append(fake_score.data.cpu().mean())
def plot_epoch(self, epoch):
self.viz.plot('Discriminator', 'train', epoch, np.array(self.hist_D).mean())
self.viz.plot('Generator', 'train', epoch, np.array(self.hist_G).mean())
self.hist_D = []
self.hist_G = []
def plot_epoch_w_scores(self, epoch):
self.viz.plot('Discriminator', 'train', epoch, np.array(self.hist_D).mean())
self.viz.plot('Generator', 'train', epoch, np.array(self.hist_G).mean())
self.viz.plot('D(X)', 'train', epoch, np.array(self.hist_Dx).mean())
self.viz.plot('D(G(X))', 'train', epoch, np.array(self.hist_DGx).mean())
self.hist_D = []
self.hist_G = []
self.hist_Dx = []
self.hist_DGx = []
def draw(self, right_images, fake_images):
self.viz.draw('generated images', fake_images.data.cpu().numpy()[:64] * 128 + 128)
self.viz.draw('real images', right_images.data.cpu().numpy()[:64] * 128 + 128)