-
Notifications
You must be signed in to change notification settings - Fork 0
/
GAN.py
219 lines (192 loc) · 7.06 KB
/
GAN.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
import argparse
from collections import defaultdict
from typing import Any, Dict, List, Tuple
import pytorch_lightning as pl
import torch
from torch import Tensor
from torchvision import transforms
from firewood.common.types import INT
from firewood.models.gan.GAN import Discriminator, Generator
from firewood.trainer.callbacks import (
LatentDimInterpolator,
LatentImageSampler,
ModelCheckpoint,
)
from firewood.trainer.metrics import FrechetInceptionDistance, gan_loss
from firewood.trainer.utils.data import (
get_dataloaders,
get_train_val_test_datasets,
torchvision_train_val_test_datasets,
)
class GAN(pl.LightningModule):
def __init__(
self,
latent_dim: int = 32,
resolution: INT = 28,
channels: int = 1,
learning_rate: float = 2e-4,
):
super().__init__()
self.save_hyperparameters()
self.generator = Generator(
latent_dim=latent_dim,
resolution=resolution,
channels=channels,
)
self.discriminator = Discriminator(
resolution=resolution, channels=channels
)
# metrics
self.fid = FrechetInceptionDistance()
def forward(self, input: Tensor) -> Tensor:
return self.generator(input)
def generate_latent(self, batch_size: int) -> Tensor:
return torch.randn(
size=(batch_size, self.hparams.latent_dim), device=self.device
)
def generator_step(self, input: Tensor) -> Dict[str, Tensor]:
latent = self.generate_latent(input.size(0))
generated_image: Tensor = self.generator(latent)
score_fake: Tensor = self.discriminator(generated_image)
loss = gan_loss(score_fake, True)
return {"loss/gen": loss}
def discriminator_step(self, input: Tensor) -> Dict[str, Tensor]:
latent = self.generate_latent(input.size(0))
with torch.no_grad():
generated_image: Tensor = self.generator(latent)
score_fake: Tensor = self.discriminator(generated_image)
score_real: Tensor = self.discriminator(input)
loss_fake = gan_loss(score_fake, False)
loss_real = gan_loss(score_real, True)
loss = loss_fake + loss_real
return {
"loss/dis": loss,
"score/real": score_real.mean(),
"score/fake": score_fake.mean(),
}
def training_step(
self, batch: Tensor, batch_idx: int, optimizer_idx: int
) -> Tensor:
real_images, _ = batch
if optimizer_idx == 0:
log_dict = self.discriminator_step(real_images)
key = "loss/dis"
else:
log_dict = self.generator_step(real_images)
key = "loss/gen"
loss = log_dict.pop(key)
self.log(key, loss, prog_bar=True)
self.log_dict(log_dict)
return loss
def validation_step(
self, batch: Tensor, batch_idx: int
) -> Dict[str, Tensor]:
real_images, _ = batch
latent = self.generate_latent(real_images.size(0))
generated_image: Tensor = self.generator(latent)
score_fake: Tensor = self.discriminator(generated_image)
score_real: Tensor = self.discriminator(real_images)
loss_fake = gan_loss(score_fake, False)
loss_real = gan_loss(score_real, True)
loss = loss_fake + loss_real
self.fid.update(real_images, True)
self.fid.update(generated_image, False)
return {
"val/loss": loss,
"val/score_real": score_real.mean(),
"val/score_fake": score_fake.mean(),
}
def validation_epoch_end(self, outputs: List[Dict[str, Tensor]]) -> None:
outputs_cache = defaultdict(list)
for output in outputs:
for key, value in output.items():
outputs_cache[key].append(value)
log_dict = {
key: torch.stack(value).mean()
for key, value in outputs_cache.items()
}
log_dict["val/fid"] = self.fid.compute()
self.log_dict(log_dict, sync_dist=True)
def configure_optimizers(self) -> Tuple[Any]:
lr = self.hparams.learning_rate
generator_optimizer = torch.optim.Adam(
self.generator.parameters(), lr=lr
)
discriminator_optimizer = torch.optim.Adam(
self.discriminator.parameters(), lr=lr
)
# discriminator first, generator second
return (
{"optimizer": discriminator_optimizer},
{"optimizer": generator_optimizer},
)
def main():
# fmt: off
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--input", "-i", type=str,
help="Input Datasets Directory")
group.add_argument("--dataset", "-d", type=str,
help="Dataset Name predefined in torchvision")
step_group = parser.add_mutually_exclusive_group()
step_group.add_argument("--epoch", "-e", type=int, default=-1)
step_group.add_argument("--step", "-s", type=int, default=-1)
parser.add_argument("--checkpoint", "-ckpt", type=str, default=None)
parser.add_argument("--batch_size", "-b", type=int, default=64)
parser.add_argument("--latent_dim", "-l", type=int, default=32)
parser.add_argument("--learning_rate", "-lr", type=float, default=2e-4)
args = vars(parser.parse_args())
# fmt: on
pl.seed_everything(0)
transform = transforms.Compose(
[transforms.ToTensor(), transforms.Normalize(0.5, 0.5)]
)
if args["input"]:
datasets = get_train_val_test_datasets(
root=args["input"],
transform=transform,
loader_mode="L", # "L" for GrayScale
split="train/val",
)
else:
datasets = torchvision_train_val_test_datasets(
name=args["dataset"], root="./datasets", transform=transform
)
train_dataloader, val_dataloader, test_dataloader = get_dataloaders(
datasets=datasets,
batch_size=args["batch_size"],
shuffle=True,
# when pin_memory=True, data will be pinned to the rank 0 gpu
pin_memory=False,
)
sample_image: Tensor = next(iter(train_dataloader))[0] # (N, C, H, W)
channels, resolution = sample_image.shape[1:3]
gan = GAN(
latent_dim=args["latent_dim"],
resolution=resolution,
channels=channels,
learning_rate=args["learning_rate"],
)
callbacks = [
ModelCheckpoint(save_last_k=3),
LatentImageSampler(step=100, on_epoch_end=False, log_fixed_batch=True),
LatentDimInterpolator(),
]
gpus = torch.cuda.device_count()
trainer = pl.Trainer(
accelerator="gpu",
devices=gpus,
max_epochs=args["epoch"],
max_steps=args["step"],
check_val_every_n_epoch=10,
callbacks=callbacks,
strategy="ddp" if gpus > 1 else None,
)
trainer.logger._default_hp_metric = False
trainer.fit(
gan,
train_dataloaders=train_dataloader,
val_dataloaders=val_dataloader or test_dataloader,
)
if __name__ == "__main__":
main()