-
Notifications
You must be signed in to change notification settings - Fork 0
/
WAE_mmd_img.py
278 lines (211 loc) · 8.47 KB
/
WAE_mmd_img.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
#!/usr/bin/env python
#-*- coding: utf-8 -*-
"""
@file: WAE_mmd_img.py
@author: ImKe at 2021/12/7
@email: tuisaac163@gmail.com
@feature: #Enter features here
"""
import argparse
import os
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from tqdm import tqdm
from torch.utils.data import DataLoader
from torch.autograd import Variable
from torchvision.datasets import MNIST
from torchvision.transforms import transforms
from torchvision.utils import save_image
from torch.optim.lr_scheduler import StepLR
torch.manual_seed(123)
parser = argparse.ArgumentParser(description='PyTorch MNIST WAE-MMD')
parser.add_argument('-batch_size', type=int, default=100, metavar='N',
help='input batch size for training (default: 100)')
parser.add_argument('-epochs', type=int, default=100, help='number of epochs to train (default: 100)')
parser.add_argument('-lr', type=float, default=0.0001, help='learning rate (default: 0.0001)')
parser.add_argument('-dim_h', type=int, default=128, help='hidden dimension (default: 128)')
parser.add_argument('-n_z', type=int, default=8, help='hidden dimension of z (default: 8)')
parser.add_argument('-LAMBDA', type=float, default=10, help='regularization coef MMD term (default: 10)')
parser.add_argument('-n_channel', type=int, default=1, help='input channels (default: 1)')
parser.add_argument('-sigma', type=float, default=1, help='variance of hidden dimension (default: 1)')
args = parser.parse_args()
trainset = MNIST(root='./data/',
train=True,
transform=transforms.ToTensor(),
download=True)
testset = MNIST(root='./data/',
train=False,
transform=transforms.ToTensor(),
download=True)
train_loader = DataLoader(dataset=trainset,
batch_size=args.batch_size,
shuffle=True)
test_loader = DataLoader(dataset=testset,
batch_size=104,
shuffle=False)
def free_params(module: nn.Module):
for p in module.parameters():
p.requires_grad = True
def frozen_params(module: nn.Module):
for p in module.parameters():
p.requires_grad = False
class Encoder(nn.Module):
def __init__(self, args):
super(Encoder, self).__init__()
self.n_channel = args.n_channel
self.dim_h = args.dim_h
self.n_z = args.n_z
self.main = nn.Sequential(
nn.Conv2d(self.n_channel, self.dim_h, 4, 2, 1, bias=False),
nn.ReLU(True),
nn.Conv2d(self.dim_h, self.dim_h * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.dim_h * 2),
nn.ReLU(True),
nn.Conv2d(self.dim_h * 2, self.dim_h * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.dim_h * 4),
nn.ReLU(True),
nn.Conv2d(self.dim_h * 4, self.dim_h * 8, 4, 2, 1, bias=False),
nn.BatchNorm2d(self.dim_h * 8),
nn.ReLU(True),
)
self.fc = nn.Linear(self.dim_h * (2 ** 3), self.n_z)
def forward(self, x):
x = self.main(x)
x = x.squeeze()
x = self.fc(x)
return x
class Decoder(nn.Module):
def __init__(self, args):
super(Decoder, self).__init__()
self.n_channel = args.n_channel
self.dim_h = args.dim_h
self.n_z = args.n_z
self.proj = nn.Sequential(
nn.Linear(self.n_z, self.dim_h * 8 * 7 * 7),
nn.ReLU()
)
self.main = nn.Sequential(
nn.ConvTranspose2d(self.dim_h * 8, self.dim_h * 4, 4),
nn.BatchNorm2d(self.dim_h * 4),
nn.ReLU(True),
nn.ConvTranspose2d(self.dim_h * 4, self.dim_h * 2, 4),
nn.BatchNorm2d(self.dim_h * 2),
nn.ReLU(True),
nn.ConvTranspose2d(self.dim_h * 2, 1, 4, stride=2),
nn.Sigmoid()
)
def forward(self, x):
x = self.proj(x)
x = x.view(-1, self.dim_h * 8, 7, 7)
x = self.main(x)
return x
def imq_kernel(X: torch.Tensor,
Y: torch.Tensor,
h_dim: int):
batch_size = X.size(0)
norms_x = X.pow(2).sum(1, keepdim=True) # batch_size x 1
prods_x = torch.mm(X, X.t()) # batch_size x batch_size
dists_x = norms_x + norms_x.t() - 2 * prods_x
norms_y = Y.pow(2).sum(1, keepdim=True) # batch_size x 1
prods_y = torch.mm(Y, Y.t()) # batch_size x batch_size
dists_y = norms_y + norms_y.t() - 2 * prods_y
dot_prd = torch.mm(X, Y.t())
dists_c = norms_x + norms_y.t() - 2 * dot_prd
stats = 0
for scale in [.1, .2, .5, 1., 2., 5., 10.]:
C = 2 * h_dim * 1.0 * scale
res1 = C / (C + dists_x)
res1 += C / (C + dists_y)
if torch.cuda.is_available():
res1 = (1 - torch.eye(batch_size).cuda()) * res1
else:
res1 = (1 - torch.eye(batch_size)) * res1
res1 = res1.sum() / (batch_size - 1)
res2 = C / (C + dists_c)
res2 = res2.sum() * 2. / (batch_size)
stats += res1 - res2
return stats
def rbf_kernel(X: torch.Tensor,
Y: torch.Tensor,
h_dim: int):
batch_size = X.size(0)
norms_x = X.pow(2).sum(1, keepdim=True) # batch_size x 1
prods_x = torch.mm(X, X.t()) # batch_size x batch_size
dists_x = norms_x + norms_x.t() - 2 * prods_x
norms_y = Y.pow(2).sum(1, keepdim=True) # batch_size x 1
prods_y = torch.mm(Y, Y.t()) # batch_size x batch_size
dists_y = norms_y + norms_y.t() - 2 * prods_y
dot_prd = torch.mm(X, Y.t())
dists_c = norms_x + norms_y.t() - 2 * dot_prd
stats = 0
for scale in [.1, .2, .5, 1., 2., 5., 10.]:
C = 2 * h_dim * 1.0 / scale
res1 = torch.exp(-C * dists_x)
res1 += torch.exp(-C * dists_y)
if torch.cuda.is_available():
res1 = (1 - torch.eye(batch_size).cuda()) * res1
else:
res1 = (1 - torch.eye(batch_size)) * res1
res1 = res1.sum() / (batch_size - 1)
res2 = torch.exp(-C * dists_c)
res2 = res2.sum() * 2. / batch_size
stats += res1 - res2
return stats
encoder, decoder = Encoder(args), Decoder(args)
criterion = nn.MSELoss()
encoder.train()
decoder.train()
if torch.cuda.is_available():
encoder, decoder = encoder.cuda(), decoder.cuda()
one = torch.Tensor([1])
mone = one * -1
if torch.cuda.is_available():
one = one.cuda()
mone = mone.cuda()
# Optimizers
enc_optim = optim.Adam(encoder.parameters(), lr=args.lr)
dec_optim = optim.Adam(decoder.parameters(), lr=args.lr)
enc_scheduler = StepLR(enc_optim, step_size=30, gamma=0.5)
dec_scheduler = StepLR(dec_optim, step_size=30, gamma=0.5)
for epoch in range(args.epochs):
step = 0
for (images, _) in tqdm(train_loader):
if torch.cuda.is_available():
images = images.cuda()
enc_optim.zero_grad()
dec_optim.zero_grad()
# ======== Train Generator ======== #
batch_size = images.size()[0]
z = encoder(images)
x_recon = decoder(z)
recon_loss = criterion(x_recon, images)
# ======== MMD Kernel Loss ======== #
z_fake = Variable(torch.randn(images.size()[0], args.n_z) * args.sigma)
if torch.cuda.is_available():
z_fake = z_fake.cuda()
z_real = encoder(images)
mmd_loss = imq_kernel(z_real, z_fake, h_dim=encoder.n_z)
mmd_loss = mmd_loss / batch_size
total_loss = recon_loss + mmd_loss
total_loss.backward()
enc_optim.step()
dec_optim.step()
step += 1
if (step + 1) % 300 == 0:
print("Epoch: [%d/%d], Step: [%d/%d], Reconstruction Loss: %.4f, MMD Loss %.4f" %
(epoch + 1, args.epochs, step + 1, len(train_loader), recon_loss.data.item(),
mmd_loss.item()))
if (epoch + 1) % 1 == 0:
batch_size = 104
test_iter = iter(test_loader)
test_data = next(test_iter)
z_real = encoder(Variable(test_data[0]).cuda())
reconst = decoder(z_real).cpu().view(batch_size, 1, 28, 28)
sample = decoder(torch.randn_like(z_real)).cpu().view(batch_size, 1, 28, 28)
if not os.path.isdir('./data/reconst_images'):
os.makedirs('data/reconst_images')
save_image(test_data[0].view(-1, 1, 28, 28), './data/reconst_images/wae_mmd_input.png')
save_image(reconst.data, './data/reconst_images/wae_mmd_images_%d.png' % (epoch + 1))
save_image(sample.data, './data/reconst_images/wae_mmd_samples_%d.png' % (epoch + 1))