-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_gmmn.py
executable file
·269 lines (223 loc) · 9.2 KB
/
train_gmmn.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
#!/usr/bin/env python
import datetime
from functools import partial
from glob import glob
import json
import os
import re
import shlex
import shutil
import sys
import torch
from torch.utils.data import DataLoader
from tqdm import tqdm
from igms.datasets import get_dataset
from igms.featurize import load_featurizer
from igms.generator import make_generator
from igms.mmd import mmd2, Estimator
from igms.kernels import pick_kernel
from igms.utils import ArgumentParser, get_optimizer, pil, set_other_seeds, str2bool
def parse_args(argv=None, check_dir=True):
parser = ArgumentParser()
parser.add_argument("outdir")
parser.add_argument(
"--force", action="store_true", help="Delete OUTDIR if it exists."
)
parser.add_argument(
"--resume",
action="store_true",
help=(
"Resume the last checkpoint from OUTDIR. "
"Overrides all other arguments except DEVICE and DATA_WORKERS."
),
)
ds = parser.add_argument_group("dataset options")
ds.add_argument("--dataset", default="celebA")
ds.add_argument("--out-size", type=int, default=64)
ds.add_argument("--out-channels", type=int, default=3)
ds.add_argument("--download-dataset", type=str2bool, default=True)
gen = parser.add_argument_group("generator options")
gen.add_argument("--z-dim", type=int, default=128)
gen.add_argument("--batch-size", type=int, default=128)
gen.add_argument("--generator", default="dcgan")
opt = parser.add_argument_group("optimizer options")
opt.add_argument("--optimizer", default="adam:1e-4")
opt.add_argument("--epochs", type=int, default=25)
loss = parser.add_argument_group("loss options")
loss.add_argument("--featurizer", default="through:smoothing")
loss.add_argument("--image-noise", type=float, default=0)
loss.add_argument("--kernel", default="linear")
loss.add_argument(
"--mmd-estimator",
default="unbiased",
type=lambda s: s.upper().replace("-", "_"),
)
comp = parser.add_argument_group("computation options")
comp.add_argument("--data-workers", type=int, default=2)
comp.add_argument("--device", default="cuda")
args = parser.parse_args(argv)
if args.resume:
checkpoints = glob(f"{args.outdir}/checkpoint_*.pt")
if not checkpoints:
parser.error(f"Couldn't find any checkpoints to resume in {args.outdir}")
pat = re.compile(r"/checkpoint_(\d+)_(\d+)\.pt$")
def checkpoint_idx(s):
return tuple(int(x) for x in pat.search(s).groups())
checkpoint = torch.load(
max(checkpoints, key=checkpoint_idx), map_location=args.device
)
checkpoint_args = checkpoint["args"]
checkpoint_args.device = args.device
checkpoint_args.data_workers = args.data_workers
checkpoint_args.resume = True
return checkpoint_args, checkpoint
elif check_dir:
if os.path.exists(args.outdir):
ls = set(os.listdir(args.outdir))
if args.force or ls.issubset({"args.json", "argv.txt"}):
shutil.rmtree(args.outdir)
else:
parser.error(
f"{args.outdir} already exists; "
"use --resume, or --force to delete"
)
os.makedirs(args.outdir)
v = parser._read_args_from_files(sys.argv[1:] if argv is None else argv)
with open(os.path.join(args.outdir, "argv.txt"), "x") as f:
print(
"# This file has the effective arguments passed, one per line.\n"
"# Note there might be duplicates (later taking precedence).\n"
"# You can run them again with "
f"`{os.path.basename(__file__)} @path/to/argv.txt`.\n"
"# See args.json for how they were parsed by argparse.\n",
file=f,
)
for arg in v:
print(shlex.quote(arg), file=f)
with open(os.path.join(args.outdir, "args.json"), "w") as f:
json.dump(vars(args), f, indent=2)
return args, {}
def main():
args, checkpoint = parse_args()
device = torch.device(args.device)
if device.type == "cuda":
assert torch.cuda.is_available()
assert torch.backends.cudnn.enabled
torch.backends.cudnn.benchmark = True
generator = make_generator(
args.generator,
output_size=args.out_size,
z_dim=args.z_dim,
output_channels=args.out_channels,
).to(device)
opt = get_optimizer(args.optimizer)(generator.parameters())
if checkpoint:
generator.load_state_dict(checkpoint["generator_state_dict"])
opt.load_state_dict(checkpoint["optimizer_state_dict"])
log_latents = checkpoint["log_latents"].to(device)
resume = (checkpoint["epoch"], checkpoint["batches"])
else:
log_latents = torch.empty(64, generator.z_dim, device=device)
log_latents.uniform_(-1, 1)
resume = None
featurizer = load_featurizer(args.featurizer, map_location=device).to(device)
top_kernel = pick_kernel(args.kernel)
estimator = Estimator[args.mmd_estimator]
if args.image_noise:
real_noise = torch.empty(
args.batch_size, *generator.output_shape, device=device
)
fake_noise = torch.empty(
args.batch_size, *generator.output_shape, device=device
)
def loss_fn(real_imgs, fake_imgs):
if args.image_noise:
real_imgs = real_imgs + real_noise.normal_(std=args.image_noise)
fake_imgs = fake_imgs + fake_noise.normal_(std=args.image_noise)
real_reps = featurizer(real_imgs)
fake_reps = featurizer(fake_imgs)
return mmd2(top_kernel(real_reps, fake_reps), estimator=estimator)
dataset = get_dataset(
args.dataset, out_size=args.out_size, download=args.download_dataset
)
dataloader = DataLoader(
dataset,
batch_size=args.batch_size,
pin_memory=True,
shuffle=True,
drop_last=True,
num_workers=args.data_workers,
worker_init_fn=set_other_seeds,
)
train_loop(args, dataloader, generator, loss_fn, opt, log_latents, resume)
def train_loop(args, dataloader, generator, loss_fn, opt, log_latents, resume=None):
fn = partial(os.path.join, args.outdir)
device = args.device
latents = torch.empty(args.batch_size, generator.z_dim, device=device)
generator.train()
with open(fn("log.txt"), "a", buffering=1) as log_f:
def write_line(line):
s = f"[{datetime.datetime.now():%Y-%m-%d %H:%m:%S}] {line}"
tqdm.write(s)
log_f.write(s + "\n")
def checkpoint(epoch, batches=0):
d = {
"args": args,
"generator_state_dict": generator.state_dict(),
"optimizer_state_dict": opt.state_dict(),
"log_latents": log_latents,
"epoch": epoch,
"batches": batches,
}
name = orig_name = f"checkpoint_{epoch}_{batches}.pt"
while os.path.exists(fn(name)):
name += "~"
torch.save(d, fn(name))
if name != orig_name:
write_line(
f"ERROR: checkpoint {orig_name} already exists! "
f"Saved to {name} instead, now aborting."
)
sys.exit(3)
else:
write_line(f"saved checkpoint: {name}")
def save_samples(name):
generator.eval()
file = f"fake_{name}.jpg"
pil(generator(log_latents)).save(fn(file))
generator.train()
write_line(f"saved samples: {file}")
if resume:
resume_epoch, resume_batch = resume
epochs = range(resume_epoch, args.epochs)
skip_batches = resume_batch + 1
write_line(f"Resuming from {resume_epoch} / {resume_batch}")
else:
pil(next(iter(dataloader))[0][: log_latents.shape[0]]).save(fn("real.jpg"))
checkpoint(0)
epochs = range(args.epochs)
skip_batches = 0
for epoch in tqdm(epochs, unit="epoch", dynamic_ncols=True):
with tqdm(dataloader, unit="batch", dynamic_ncols=True) as bar:
for batch_i, (real_imgs, real_ys) in enumerate(bar):
if skip_batches:
skip_batches -= 1
continue
real_imgs = real_imgs.to(device)
opt.zero_grad()
latents.uniform_(-1, 1)
fake_imgs = generator(latents)
loss = loss_fn(real_imgs, fake_imgs)
loss.backward()
opt.step()
l_str = f"{loss.item():,.3f}"
bar.set_postfix(loss="=" + l_str.rjust(11))
if batch_i % 100 == 0:
write_line(f"{epoch:>3} / {batch_i:>7,}: {l_str:>14}")
if batch_i % 500 == 0:
save_samples(f"{epoch}_{batch_i}")
checkpoint(epoch, batch_i)
write_line(f"{epoch:>3} / {batch_i:>7,}: {l_str:>14}")
save_samples(f"{epoch}_{batch_i}")
if __name__ == "__main__":
main()