-
Notifications
You must be signed in to change notification settings - Fork 41
/
wgan_gp.py
545 lines (448 loc) · 23.4 KB
/
wgan_gp.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
import logging
import warnings
from typing import Any, Dict, List, Tuple
import pytorch_lightning as pl
import torch
import torch.autograd as autograd
import torch.nn as nn
import torchmetrics
from torch.utils.data import DataLoader
from torchmetrics.image.inception import InceptionScore
from .utils import FrechetInceptionDistance
_EVALUATE_OUTPUT = List[Dict[str, float]] # 1 dict per DataLoader
log = logging.getLogger('torcheeg')
def gradient_penalty(model, real, fake, *args, **kwargs):
device = real.device
real = real.data
fake = fake.data
alpha = torch.rand(real.size(0), *([1] * (len(real.shape) - 1))).to(device)
inputs = alpha * real + ((1 - alpha) * fake)
inputs.requires_grad_()
outputs = model(inputs, *args, **kwargs)
gradient = autograd.grad(outputs=outputs,
inputs=inputs,
grad_outputs=torch.ones_like(outputs).to(device),
create_graph=True,
retain_graph=True,
only_inputs=True)[0]
gradient = gradient.flatten(1)
return ((gradient.norm(2, dim=1) - 1)**2).mean()
class WGANGPTrainer(pl.LightningModule):
r'''
This class provide the implementation for WGAN-GP. It trains a zero-sum game between the generator and the discriminator, just like the traditional generative networks. The generator is optimized to generate simulation samples that are indistinguishable by the discriminator, and the discriminator is optimized to discriminate false samples generated by the generator. Compared with vanilla GAN, with WGAN-GP we can improve the stability of learning, get rid of problems like mode collapse, and provide meaningful learning curves useful for debugging and hyperparameter searches. Thus, existing work typically uses WGAN-GP to generate simulated EEG signals. For more details, please refer to the following information.
- Paper: Gulrajani I, Ahmed F, Arjovsky M, et al. Improved training of wasserstein gans[J]. Advances in neural information processing systems, 2017, 30.
- URL: https://arxiv.org/abs/1704.00028
- Related Project: https://github.com/eriklindernoren/PyTorch-GAN
.. code-block:: python
g_model = BGenerator(in_channels=128)
d_model = BDiscriminator(in_channels=4)
trainer = WGANGPTrainer(generator, discriminator)
Args:
generator (nn.Module): The generator model for EEG signal generation, whose inputs are Gaussian distributed random vectors, outputs are generated EEG signals. The dimensions of the input vector should be defined on the :obj:`in_channel` attribute. The output layer does not need to have a softmax activation function.
discriminator (nn.Module): The discriminator model to determine whether the EEG signal is real or generated, and the dimension of its output should be equal to the one (i.e., the score to distinguish the real and the fake). The output layer does not need to have a sigmoid activation function.
generator_lr (float): The learning rate of the generator. (default: :obj:`0.0001`)
discriminator_lr (float): The learning rate of the discriminator. (default: :obj:`0.0001`)
weight_gradient_penalty (float): The weight of gradient penalty loss to trade-off between the adversarial training loss and gradient penalty loss. (default: :obj:`1.0`)
weight_decay: (float): The weight decay (L2 penalty). (default: :obj:`0.0`)
latent_channels (int): The dimension of the latent vector. If not specified, it will be inferred from the :obj:`in_channels` attribute of the generator. (default: :obj:`None`)
devices (int): The number of GPUs to use. (default: :obj:`1`)
accelerator (str): The accelerator to use. Available options are: 'cpu', 'gpu'. (default: :obj:`"cpu"`)
metrics (List[str]): The metrics to use. The metrics to use. Available options are: 'fid', 'is'. (default: :obj:`[]`)
metric_extractor (nn.Module): The feature extractor model for computing the FID score. (default: :obj:`None`)
metric_classifier (nn.Module): The classifier model for computing the IS score. (default: :obj:`None`)
metric_num_features (int): The number of features extracted by the metric_extractor. If not specified, it will be inferred from the :obj:`in_channels` attribute of the metric_extractor. (default: :obj:`None`)
.. automethod:: fit
.. automethod:: test
'''
def __init__(self,
generator: nn.Module,
discriminator: nn.Module,
generator_lr: float = 1e-4,
discriminator_lr: float = 1e-4,
weight_decay: float = 0.0,
weight_gradient_penalty: float = 1.0,
latent_channels: int = None,
devices: int = 1,
accelerator: str = "cpu",
metrics: List[str] = [],
metric_extractor: nn.Module = None,
metric_classifier: nn.Module = None,
metric_num_features: int = None):
super().__init__()
self.automatic_optimization = False
self.generator = generator
self.discriminator = discriminator
self.generator_lr = generator_lr
self.discriminator_lr = discriminator_lr
self.weight_decay = weight_decay
self.weight_gradient_penalty = weight_gradient_penalty
if hasattr(generator, 'in_channels') and latent_channels is None:
warnings.warn(
f'No latent_channels specified, use generator.in_channels ({generator.in_channels}) as latent_channels.'
)
latent_channels = generator.in_channels
assert not latent_channels is None, 'The latent_channels should be specified.'
self.latent_channels = latent_channels
self.devices = devices
self.accelerator = accelerator
self.metrics = metrics
self.bce_fn = nn.BCEWithLogitsLoss()
self.metric_extractor = metric_extractor
self.metric_classifier = metric_classifier
self.metric_num_features = metric_num_features
self.init_metrics(metrics)
def init_metrics(self, metrics) -> None:
self.train_g_loss = torchmetrics.MeanMetric()
self.train_d_loss = torchmetrics.MeanMetric()
self.val_g_loss = torchmetrics.MeanMetric()
self.val_d_loss = torchmetrics.MeanMetric()
self.test_g_loss = torchmetrics.MeanMetric()
self.test_d_loss = torchmetrics.MeanMetric()
if 'fid' in metrics:
assert not self.metric_extractor is None, 'The metric_extractor should be specified.'
if hasattr(self.metric_extractor,
'in_channels') and self.metric_num_features is None:
warnings.warn(
f'No metric_num_features specified, use metric_extractor.in_channels ({self.metric_extractor.in_channels}) as metric_num_features.'
)
self.metric_num_features = self.metric_extractor.in_channels
assert not self.metric_num_features is None, 'The metric_num_features should be specified.'
self.test_fid = FrechetInceptionDistance(self.metric_extractor,
self.metric_num_features)
if 'is' in metrics:
assert not self.metric_extractor is None, 'The metric_classifier should be specified.'
self.test_is = InceptionScore(self.metric_classifier)
def fit(self,
train_loader: DataLoader,
val_loader: DataLoader,
max_epochs: int = 300,
*args,
**kwargs) -> Any:
r'''
Args:
train_loader (DataLoader): Iterable DataLoader for traversing the training data batch (:obj:`torch.utils.data.dataloader.DataLoader`, :obj:`torch_geometric.loader.DataLoader`, etc).
val_loader (DataLoader): Iterable DataLoader for traversing the validation data batch (:obj:`torch.utils.data.dataloader.DataLoader`, :obj:`torch_geometric.loader.DataLoader`, etc).
max_epochs (int): Maximum number of epochs to train the model. (default: :obj:`300`)
'''
trainer = pl.Trainer(devices=self.devices,
accelerator=self.accelerator,
max_epochs=max_epochs,
inference_mode=False,
*args,
**kwargs)
return trainer.fit(self, train_loader, val_loader)
def test(self, test_loader: DataLoader, *args,
**kwargs) -> _EVALUATE_OUTPUT:
r'''
Args:
test_loader (DataLoader): Iterable DataLoader for traversing the test data batch (torch.utils.data.dataloader.DataLoader, torch_geometric.loader.DataLoader, etc).
'''
trainer = pl.Trainer(devices=self.devices,
accelerator=self.accelerator,
inference_mode=False,
*args,
**kwargs)
return trainer.test(self, test_loader)
def forward(self, latent: torch.Tensor) -> torch.Tensor:
return self.generator(latent)
def predict_step(self,
batch: Tuple[torch.Tensor],
batch_idx: int,
dataloader_idx: int = 0):
x, _ = batch
latent = torch.normal(mean=0,
std=1,
size=(x.shape[0], self.latent_channels))
latent = latent.type_as(x)
return self(latent)
def training_step(self, batch: Tuple[torch.Tensor],
batch_idx: int) -> torch.Tensor:
x, _ = batch
generator_optimizer, discriminator_optimizer = self.optimizers()
latent = torch.normal(mean=0,
std=1,
size=(x.shape[0], self.latent_channels))
latent = latent.type_as(x)
# train generator
self.toggle_optimizer(generator_optimizer)
gen_x = self.generator(latent)
g_loss = -torch.mean(self.discriminator(gen_x))
g_loss.backward()
generator_optimizer.step()
generator_optimizer.zero_grad()
self.untoggle_optimizer(generator_optimizer)
# train discriminator
self.toggle_optimizer(discriminator_optimizer)
real_loss = self.discriminator(x)
fake_loss = self.discriminator(gen_x.detach())
gp_term = gradient_penalty(self.discriminator, x, gen_x)
d_loss = -torch.mean(real_loss) + torch.mean(
fake_loss) + self.weight_gradient_penalty * gp_term
d_loss.backward()
discriminator_optimizer.step()
discriminator_optimizer.zero_grad()
self.untoggle_optimizer(discriminator_optimizer)
self.log("train_g_loss",
self.train_g_loss(g_loss),
prog_bar=True,
on_epoch=False,
logger=False,
on_step=True)
self.log("train_d_loss",
self.train_d_loss(d_loss),
prog_bar=True,
on_epoch=False,
logger=False,
on_step=True)
def on_train_epoch_end(self) -> None:
self.log("train_g_loss",
self.train_g_loss.compute(),
prog_bar=False,
on_epoch=True,
on_step=False,
logger=True)
self.log("train_d_loss",
self.train_d_loss.compute(),
prog_bar=False,
on_epoch=True,
on_step=False,
logger=True)
# print the metrics
str = "\n[Train] "
for key, value in self.trainer.logged_metrics.items():
if key.startswith("train_"):
str += f"{key}: {value:.3f} "
log.info(str + '\n')
# reset the metrics
self.train_g_loss.reset()
self.train_d_loss.reset()
@torch.enable_grad()
def validation_step(self, batch: Tuple[torch.Tensor],
batch_idx: int) -> torch.Tensor:
x, _ = batch
latent = torch.normal(mean=0,
std=1,
size=(x.shape[0], self.latent_channels))
latent = latent.type_as(x)
gen_x = self.generator(latent)
g_loss = -torch.mean(self.discriminator(gen_x))
real_loss = self.discriminator(x)
fake_loss = self.discriminator(gen_x.detach())
gp_term = gradient_penalty(self.discriminator, x, gen_x)
d_loss = -torch.mean(real_loss) + torch.mean(
fake_loss) + self.weight_gradient_penalty * gp_term
self.val_g_loss.update(g_loss)
self.val_d_loss.update(d_loss)
return g_loss, d_loss
def on_validation_epoch_end(self) -> None:
self.log("val_g_loss",
self.val_g_loss.compute(),
prog_bar=False,
on_epoch=True,
on_step=False,
logger=True)
self.log("val_d_loss",
self.val_d_loss.compute(),
prog_bar=False,
on_epoch=True,
on_step=False,
logger=True)
# print the metrics
str = "\n[VAL] "
for key, value in self.trainer.logged_metrics.items():
if key.startswith("val_"):
str += f"{key}: {value:.3f} "
log.info(str + '\n')
# reset the metrics
self.val_g_loss.reset()
self.val_d_loss.reset()
@torch.enable_grad()
def test_step(self, batch: Tuple[torch.Tensor],
batch_idx: int) -> torch.Tensor:
x, _ = batch
latent = torch.normal(mean=0,
std=1,
size=(x.shape[0], self.latent_channels))
latent = latent.type_as(x)
gen_x = self.generator(latent)
g_loss = -torch.mean(self.discriminator(gen_x))
real_loss = self.discriminator(x)
fake_loss = self.discriminator(gen_x.detach())
gp_term = gradient_penalty(self.discriminator, x, gen_x)
d_loss = -torch.mean(real_loss) + torch.mean(
fake_loss) + self.weight_gradient_penalty * gp_term
self.test_g_loss.update(g_loss)
self.test_d_loss.update(d_loss)
if 'fid' in self.metrics:
self.test_fid.update(x, real=True)
self.test_fid.update(gen_x, real=False)
if 'is' in self.metrics:
self.test_is.update(gen_x)
return g_loss, d_loss
def on_test_epoch_end(self) -> None:
self.log("test_g_loss",
self.test_g_loss.compute(),
prog_bar=False,
on_epoch=True,
on_step=False,
logger=True)
self.log("test_d_loss",
self.test_d_loss.compute(),
prog_bar=False,
on_epoch=True,
on_step=False,
logger=True)
if 'fid' in self.metrics:
self.log("test_fid",
self.test_fid.compute(),
prog_bar=False,
on_epoch=True,
on_step=False,
logger=True)
if 'is' in self.metrics:
self.log("test_is",
self.test_is.compute()[0],
prog_bar=False,
on_epoch=True,
on_step=False,
logger=True)
# print the metrics
str = "\n[TEST] "
for key, value in self.trainer.logged_metrics.items():
if key.startswith("test_"):
str += f"{key}: {value:.3f} "
log.info(str + '\n')
# reset the metrics
self.test_g_loss.reset()
self.test_d_loss.reset()
if 'fid' in self.metrics:
self.test_fid.reset()
if 'is' in self.metrics:
self.test_is.reset()
def configure_optimizers(self):
generator_optimizer = torch.optim.Adam(self.generator.parameters(),
lr=self.generator_lr,
weight_decay=self.weight_decay)
discriminator_optimizer = torch.optim.Adam(
self.discriminator.parameters(),
lr=self.discriminator_lr,
weight_decay=self.weight_decay)
return [generator_optimizer, discriminator_optimizer], []
class CWGANGPTrainer(WGANGPTrainer):
r'''
This class provide the implementation for WGAN-GP. It trains a zero-sum game between the generator and the discriminator, just like the traditional generative networks. The generator is optimized to generate simulation samples that are indistinguishable by the discriminator, and the discriminator is optimized to discriminate false samples generated by the generator. Compared with vanilla GAN, with WGAN-GP we can improve the stability of learning, get rid of problems like mode collapse, and provide meaningful learning curves useful for debugging and hyperparameter searches. Thus, existing work typically uses WGAN-GP to generate simulated EEG signals. In particular, the expected labels are additionally provided to guide the discriminator to distinguish whether the sample fits the data distribution of the class. For more details, please refer to the following information.
- Paper: Gulrajani I, Ahmed F, Arjovsky M, et al. Improved training of wasserstein gans[J]. Advances in neural information processing systems, 2017, 30.
- URL: https://arxiv.org/abs/1704.00028
- Related Project: https://github.com/eriklindernoren/PyTorch-GAN
.. code-block:: python
g_model = BGenerator(in_channels=128)
d_model = BDiscriminator(in_channels=4)
trainer = WGANGPTrainer(generator, discriminator)
trainer.fit(train_loader, val_loader)
trainer.test(test_loader)
Args:
generator (nn.Module): The generator model for EEG signal generation, whose inputs are Gaussian distributed random vectors, outputs are generated EEG signals. The dimensions of the input vector should be defined on the :obj:`in_channel` attribute. The output layer does not need to have a softmax activation function.
discriminator (nn.Module): The discriminator model to determine whether the EEG signal is real or generated, and the dimension of its output should be equal to the one (i.e., the score to distinguish the real and the fake). The output layer does not need to have a sigmoid activation function.
generator_lr (float): The learning rate of the generator. (default: :obj:`0.0001`)
discriminator_lr (float): The learning rate of the discriminator. (default: :obj:`0.0001`)
weight_gradient_penalty (float): The weight of gradient penalty loss to trade-off between the adversarial training loss and gradient penalty loss. (default: :obj:`1.0`)
weight_decay: (float): The weight decay (L2 penalty). (default: :obj:`0.0`)
latent_channels (int): The dimension of the latent vector. If not specified, it will be inferred from the :obj:`in_channels` attribute of the generator. (default: :obj:`None`)
devices (int): The number of GPUs to use. (default: :obj:`1`)
accelerator (str): The accelerator to use. Available options are: 'cpu', 'gpu'. (default: :obj:`"cpu"`)
metrics (List[str]): The metrics to use. The metrics to use. Available options are: 'fid', 'is'. (default: :obj:`[]`)
.. automethod:: fit
.. automethod:: test
'''
def training_step(self, batch: Tuple[torch.Tensor],
batch_idx: int) -> torch.Tensor:
x, y = batch
generator_optimizer, discriminator_optimizer = self.optimizers()
latent = torch.normal(mean=0,
std=1,
size=(x.shape[0], self.latent_channels))
latent = latent.type_as(x)
# train generator
self.toggle_optimizer(generator_optimizer)
gen_x = self.generator(latent, y)
g_loss = -torch.mean(self.discriminator(gen_x, y))
g_loss.backward()
generator_optimizer.step()
generator_optimizer.zero_grad()
self.untoggle_optimizer(generator_optimizer)
# train discriminator
self.toggle_optimizer(discriminator_optimizer)
real_loss = self.discriminator(x, y)
fake_loss = self.discriminator(gen_x.detach(), y)
gp_term = gradient_penalty(self.discriminator, x, gen_x, y)
d_loss = -torch.mean(real_loss) + torch.mean(
fake_loss) + self.weight_gradient_penalty * gp_term
d_loss.backward()
discriminator_optimizer.step()
discriminator_optimizer.zero_grad()
self.untoggle_optimizer(discriminator_optimizer)
self.log("train_g_loss",
self.train_g_loss(g_loss),
prog_bar=True,
on_epoch=False,
logger=False,
on_step=True)
self.log("train_d_loss",
self.train_d_loss(d_loss),
prog_bar=True,
on_epoch=False,
logger=False,
on_step=True)
@torch.enable_grad()
def validation_step(self, batch: Tuple[torch.Tensor],
batch_idx: int) -> torch.Tensor:
x, y = batch
latent = torch.normal(mean=0,
std=1,
size=(x.shape[0], self.latent_channels))
latent = latent.type_as(x)
gen_x = self.generator(latent, y)
g_loss = -torch.mean(self.discriminator(gen_x, y))
real_loss = self.discriminator(x, y)
fake_loss = self.discriminator(gen_x.detach(), y)
gp_term = gradient_penalty(self.discriminator, x, gen_x, y)
d_loss = -torch.mean(real_loss) + torch.mean(
fake_loss) + self.weight_gradient_penalty * gp_term
self.val_g_loss.update(g_loss)
self.val_d_loss.update(d_loss)
return g_loss, d_loss
@torch.enable_grad()
def test_step(self, batch: Tuple[torch.Tensor],
batch_idx: int) -> torch.Tensor:
x, y = batch
latent = torch.normal(mean=0,
std=1,
size=(x.shape[0], self.latent_channels))
latent = latent.type_as(x)
gen_x = self.generator(latent, y)
g_loss = -torch.mean(self.discriminator(gen_x, y))
real_loss = self.discriminator(x, y)
fake_loss = self.discriminator(gen_x.detach(), y)
gp_term = gradient_penalty(self.discriminator, x, gen_x, y)
d_loss = -torch.mean(real_loss) + torch.mean(
fake_loss) + self.weight_gradient_penalty * gp_term
self.test_g_loss.update(g_loss)
self.test_d_loss.update(d_loss)
if 'fid' in self.metrics:
self.test_fid.update(x, real=True)
self.test_fid.update(gen_x, real=False)
if 'is' in self.metrics:
self.test_is.update(gen_x)
return g_loss, d_loss
def forward(self, latent: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
return self.generator(latent, y)
def predict_step(self,
batch: Tuple[torch.Tensor],
batch_idx: int,
dataloader_idx: int = 0):
x, y = batch
latent = torch.normal(mean=0,
std=1,
size=(x.shape[0], self.latent_channels))
latent = latent.type_as(x)
return self(latent, y)