-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_for_wgangp.py
321 lines (244 loc) · 10.6 KB
/
train_for_wgangp.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
import matplotlib.pyplot as plt
import numpy as np
import torch.nn as nn
import torch.optim as optim
import torchvision
from model.covmodel import CovGenerator, CovDiscriminator
from model.vitmodel import VITGenerator, VITDiscriminator
from utils import *
class Generator(nn.Module):
def __init__(self, model_name):
super().__init__()
self.module = ""
if model_name == "cov":
self.module = CovGenerator()
elif model_name == "vit":
self.module = VITGenerator()
def forward(self, noise):
return self.module(noise)
class Discriminator(nn.Module):
def __init__(self, model_name):
super().__init__()
self.module = ""
if model_name == "cov":
self.module = CovDiscriminator()
elif model_name == "vit":
self.module = VITDiscriminator()
def forward(self, img):
return self.module(img)
class WGPGAN(nn.Module):
""" Super class to contain both Discriminator (D) and Generator (G)
"""
def __init__(self, model_name):
super().__init__()
self.__dict__.update(locals())
self.G = Generator(model_name)
self.D = Discriminator(model_name)
if model_name == "cov":
self.z_dim = 100
elif model_name == "vit":
self.z_dim = 1024
else:
raise "You can choose model_name from cov and vit"
class WGPGANTrainer:
""" Object to hold data iterators, train a GAN variant
"""
def __init__(self, model, train_iter, viz=False):
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(self.device)
self.model = model.to(self.device)
self.name = model.__class__.__name__
self.train_iter = train_iter
self.Glosses = []
self.Dlosses = []
self.viz = viz
self.num_epochs = 0
def train(self, num_epochs, G_lr=1e-4, D_lr=1e-4, D_steps=5):
# self.load_model("save_model/save_model.pt")
# Initialize optimizers, do not use adam
G_optimizer = optim.RMSprop(params=[p for p in self.model.G.parameters()
if p.requires_grad], lr=G_lr, weight_decay=1e-5)
D_optimizer = optim.RMSprop(params=[p for p in self.model.D.parameters()
if p.requires_grad], lr=D_lr, weight_decay=1e-5)
# Approximate steps/epoch given D_steps per epoch
# --> roughly train in the same way as if D_step (1) == G_step (1)
epoch_steps = int(np.ceil(len(self.train_iter) / (D_steps)))
# Begin training
for epoch in range(1, num_epochs + 1):
print("***************************************")
print(epoch)
self.model.train()
G_losses, D_losses = [], []
for _ in tqdm(range(epoch_steps)):
D_step_loss = []
for _ in range(D_steps):
# Reshape images
images = self.process_batch(self.train_iter)
# TRAINING D: Zero out gradients for D
D_optimizer.zero_grad()
# Train the discriminator to approximate the Wasserstein
# distance between real, generated distributions
D_loss = self.train_D(images)
# Update parameters
D_loss.backward()
D_optimizer.step()
# Log results, backpropagate the discriminator network
D_step_loss.append(D_loss.item())
# We report D_loss in this way so that G_loss and D_loss have
# the same number of entries.
D_losses.append(np.mean(D_step_loss))
# TRAINING G: Zero out gradients for G
G_optimizer.zero_grad()
# Train the generator to (roughly) minimize the approximated
# Wasserstein distance
G_loss = self.train_G(images)
# Log results, update parameters
G_losses.append(G_loss.item())
G_loss.backward()
G_optimizer.step()
# Save progress
# self.Glosses.extend(G_losses)
# self.Dlosses.extend(D_losses)
# Progress logging
print("Epoch[%d/%d], G Loss: %.4f, D Loss: %.4f"
% (epoch, num_epochs, np.mean(G_losses), np.mean(D_losses)))
self.num_epochs += 1
# Visualize generator progress
if self.viz:
self.generate_images(epoch)
self.save_model(f"save_model/{self.name}_epoch_{epoch}.pt")
def train_D(self, images, LAMBDA=10):
""" Run 1 step of training for discriminator
Input:
images: batch of images (reshaped to [batch_size, -1])
Output:
D_loss: Wasserstein loss for discriminator,
-E[D(x)] + E[D(G(z))] + λE[(||∇ D(εx + (1 − εG(z)))|| - 1)^2]
"""
# ORIGINAL CRITIC STEPS:
# Sample noise, an output from the generator
noise = self.compute_noise(images.shape[0], self.model.z_dim)
G_output = self.model.G(noise)
# Use the discriminator to sample real, generated images
DX_score = self.model.D(images) # D(z)
DG_score = self.model.D(G_output) # D(G(z))
# GRADIENT PENALTY:
# Uniformly sample along one straight line per each batch entry.
epsilon = torch.rand([images.shape[0], 1, 1, 1]).cuda()
# Generate images from the noise, ensure unit gradient norm 1
# See Section 4 and Algorithm 1 of original paper for full explanation.
G_interpolation = epsilon * images + (1 - epsilon) * G_output
D_interpolation = self.model.D(G_interpolation)
# Compute the gradients of D with respect to the noise generated input
weight = torch.ones(D_interpolation.size()).to(self.device)
gradients = torch.autograd.grad(outputs=D_interpolation,
inputs=G_interpolation,
grad_outputs=weight,
only_inputs=True,
create_graph=True,
retain_graph=True)[0]
# Full gradient penalty
grad_penalty = LAMBDA * torch.mean((gradients.norm(2, dim=1) - 1) ** 2)
# Compute WGAN-GP loss for D
D_loss = torch.mean(DG_score) - torch.mean(DX_score) + grad_penalty
return D_loss
def train_G(self, images):
""" Run 1 step of training for generator
Input:
images: batch of images reshaped to [batch_size, -1]
Output:
G_loss: wasserstein loss for generator,
-E[D(G(z))]
"""
# Get noise, classify it using G, then classify the output of G using D.
noise = self.compute_noise(images.shape[0], self.model.z_dim) # z
G_output = self.model.G(noise) # G(z)
DG_score = self.model.D(G_output) # D(G(z))
# Compute WGAN-GP loss for G (same loss as WGAN)
G_loss = -1 * (torch.mean(DG_score))
return G_loss
def compute_noise(self, batch_size, z_dim):
""" Compute random noise for input to the Generator G """
return torch.randn(batch_size, z_dim).to(self.device)
def process_batch(self, iterator):
""" Generate a process batch to be input into the discriminator D """
images = next(iter(iterator))
images = images.to(self.device)
return images
def generate_images(self, epoch, num_outputs=36, save=True):
""" Visualize progress of generator learning """
# Turn off any regularization
self.model.eval()
# Sample noise vector
noise = self.compute_noise(num_outputs, self.model.z_dim)
# Transform noise to image
images = self.model.G(noise)
# Save images if desired
if save:
outname = 'out/'
torchvision.utils.save_image(images,
outname + 'recons_%d.png'
% epoch, nrow=int(num_outputs ** 0.5))
def viz_loss(self):
""" Visualize loss for the generator, discriminator """
# Set style, figure size
plt.style.use('ggplot')
plt.rcParams["figure.figsize"] = (8, 6)
# Plot Discriminator loss in red
plt.plot(np.linspace(1, self.num_epochs, len(self.Dlosses)),
self.Dlosses,
'r')
# Plot Generator loss in green
plt.plot(np.linspace(1, self.num_epochs, len(self.Dlosses)),
self.Glosses,
'g')
# Add legend, title
plt.legend(['Discriminator', 'Generator'])
plt.title(self.name)
plt.show()
def save_model(self, savepath):
""" Save save_model state dictionary """
torch.save(self.model.state_dict(), savepath)
def load_model(self, loadpath):
""" Load state dictionary into save_model """
state = torch.load(loadpath)
self.model.load_state_dict(state)
class WGAN_GP_Test:
def __init__(self, model, model_path):
self.model = model
self.model_path = model_path
self.name = model.__class__.__name__
def compute_noise(self, batch_size, z_dim):
""" Compute random noise for input to the Generator G """
return torch.randn(batch_size, z_dim)
def load_model(self, loadpath):
""" Load state dictionary into save_model """
state = torch.load(loadpath)
self.model.load_state_dict(state)
def test_img(self):
self.load_model(self.model_path)
self.model.eval()
noise = self.compute_noise(36, self.model.z_dim)
images = self.model.G(noise)
torchvision.utils.save_image(images, 'test.png', nrow=6)
if __name__ == "__main__":
# pic preprocess
pic_preprocess("raw_pics", "pics", (64, 64))
# Load data
mydataset = MyDataSet("pics")
train_loader = DataLoader(dataset=mydataset,
batch_size=64,
shuffle=True,
)
# Init save_model
model = WGPGAN(model_name="cov") # cov or vit
# Init trainer
trainer = WGPGANTrainer(model=model,
train_iter=train_loader,
viz=True)
# Train
trainer.train(num_epochs=100,
G_lr=8e-2,
D_lr=1e-3,
D_steps=1)
# trainer.generate_images(epoch=25, num_outputs=36, save=True)