-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_unet.py
360 lines (297 loc) · 7.49 KB
/
train_unet.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
import argparse
def str2bool(v):
if isinstance(v, bool):
return v
if v.lower() in ("yes", "true", "t", "y", "1"):
return True
elif v.lower() in ("no", "false", "f", "n", "0"):
return False
return False
def str2tuple(v):
return tuple([int(c) for c in v.split(",")])
parser = argparse.ArgumentParser(
description="UNET Training Script for MNIST and CIFAR10/CIFAR100",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--clear_gpu_cache",
default=False,
type=str2bool,
help="Flag to determine whether to clear gpu memory. Default is False assuming that you are not using a shared system.",
)
parser.add_argument(
"--loss_type",
default="pred_v",
type=str,
help='Available loss types: \{"pred_v", "pred_x0", "pred_noise" \}',
)
parser.add_argument(
"--min_snr_gamma",
default=5.0,
type=float,
help="Minimum signal to noise ratio for scaling loss weight.",
)
parser.add_argument(
"--timesteps",
default=1000,
type=int,
help="Number of timesteps used for training the model.",
)
parser.add_argument(
"--sampling_steps",
default=250,
type=int,
help="Number of timesteps used for sampling.",
)
parser.add_argument(
"--seed",
default=3867,
type=int,
help="Initial seed for randomization.",
)
parser.add_argument(
"--save_every_k",
default=5,
type=int,
help="The interval rate of which the model's parameters are saved and evaluation is performed.",
)
parser.add_argument(
"--max_to_keep",
default=5,
type=int,
help="The maximum number of model's parameters history to keep. Default is 5 copies.",
)
parser.add_argument(
"--epochs",
default=1000,
type=int,
help="The number of training points.",
)
parser.add_argument(
"--batch_size",
default=128,
type=int,
help="Batch size.",
)
parser.add_argument(
"--num_workers",
default=0,
type=int,
help="Number of workers for data loader.",
)
parser.add_argument(
"--gradient_accummulation_steps",
default=1,
type=int,
help="Number of gradient accummulation steps. Default is 1. If you're working under a limited system, try scaling down batch size and increasing gradient accummulation steps.",
)
parser.add_argument(
"--eta",
default=0.0,
type=float,
help="Stochasticity control variable for DDIM sampling.",
)
parser.add_argument(
"--learning_rate",
default=1e-4,
type=float,
help="Learning rate.",
)
parser.add_argument(
"--weight_decay",
default=1e-4,
type=float,
help="Weight decay value.",
)
parser.add_argument(
"--max_ema_decay",
default=0.9999,
type=float,
help="Maximum value for EMA decay.",
)
parser.add_argument(
"--min_ema_decay",
default=0.0,
type=float,
help="Minimum value for EMA decay.",
)
parser.add_argument(
"--ema_decay_power",
default=2 / 3,
type=float,
help="Decay power for EMA annealing.",
)
parser.add_argument(
"--ema_inv_gamma",
default=1.0,
type=float,
help="Inv gamma for EMA annealing",
)
parser.add_argument(
"--start_ema_update_after",
default=100,
type=int,
help="The number of parameters updates have to be performed before starting EMA update.",
)
parser.add_argument(
"--update_ema_every",
default=10,
type=int,
help="The interval in which EMA update occurs once it is initiated.",
)
parser.add_argument(
"--result_path",
default="./unet",
type=str,
help="Folder path to save model and results.",
)
parser.add_argument(
"--root_folder",
default="../data",
type=str,
help="Folder path to data.",
)
parser.add_argument(
"--dataset",
default="CIFAR10",
type=str,
help="Dataset name: \{CIFAR10, CIFAR100, MNIST \}",
)
parser.add_argument(
"--beta_schedule",
default="cosine",
type=str,
help='Variance scheduler: \{"linear", "cosine", "sigmoid"\}',
)
parser.add_argument(
"--dim",
default=64,
type=int,
help="Embedding dim for Unet.",
)
parser.add_argument(
"--dim_mults",
default=(1, 2, 4, 8),
type=str2tuple,
help="Dim. multipliers for Unet.",
)
parser.add_argument(
"--resnet_block_groups",
default=8,
type=int,
help="Resnet block groups number.",
)
parser.add_argument(
"--out_dim",
default=None,
type=int,
help="Output dim. of Unet. Default is None for reconstruction of input.",
)
parser.add_argument(
"--init_dim",
default=None,
type=int,
help="Initial encoding dimension for latent variable. Default is None to set equal to dim. number.",
)
parser.add_argument(
"--learned_variance",
default=False,
type=str2bool,
help="Learn variance according to ddpm paper.",
)
args = parser.parse_args()
config = vars(args)
import pprint
pprint.pprint(config, width=1)
import os
if config["clear_gpu_cache"]:
os.environ["XLA_PYTHON_CLIENT_ALLOCATOR"] = "platform"
import jax
import copy
import optax
from torchvision import transforms
from torch.utils.data import DataLoader
from torchvision.datasets import MNIST, CIFAR10, CIFAR100
from src import (
linear_schedule,
cosine_schedule,
sigmoid_schedule,
get_var_params,
DiffusionLoader,
train_model,
get_nparams,
Unet,
)
if config["dataset"].lower() == "mnist":
data = MNIST
elif config["dataset"].lower() == "cifar10":
data = CIFAR10
elif config["dataset"].lower() == "cifar100":
data = CIFAR100
else:
raise Exception(
"For this script, we only utilize MNIST and CIFAR datasets. If you wish to try something else, please edit the script."
)
if config["beta_schedule"].lower() == "linear":
beta_schedule = linear_schedule
elif config["beta_schedule"].lower() == "cosine":
beta_schedule = cosine_schedule
elif config["beta_schedule"].lower() == "sigmoid":
beta_schedule = sigmoid_schedule
else:
raise Exception("linear, cosine, and sigmoid are the only available options.")
betas = beta_schedule(config["timesteps"])
var_params = get_var_params(betas)
train_set = data(
root="../data",
download=True,
train=True,
transform=transforms.Compose(
[
transforms.ToTensor(),
]
),
)
train_loader = DataLoader(
train_set,
shuffle=True,
batch_size=config["batch_size"],
num_workers=config["num_workers"],
)
train_loader = DiffusionLoader(
train_loader,
var_params,
loss_type=config["loss_type"],
min_snr_gamma=config["min_snr_gamma"],
)
model = Unet(
dim=config["dim"],
init_dim=config["init_dim"],
out_dim=config["out_dim"],
dim_mults=config["dim_mults"],
resnet_block_groups=config["resnet_block_groups"],
learned_variance=config["learned_variance"],
)
x, _, t, _ = train_loader()
key = jax.random.PRNGKey(config["seed"])
config["key"] = key
config["iteration"] = 0
config["var_params"] = var_params
config["result_path"] = config["result_path"] + "_" + config["loss_type"]
params = model.init(key, x[:1], t[:1])["params"]
ema_params = copy.deepcopy(params)
print("Number of params: ", f"{get_nparams(params):,}")
opt = optax.chain(
optax.clip_by_global_norm(1.0),
optax.adamw(
config["learning_rate"],
b1=0.9,
b2=0.99,
weight_decay=config["weight_decay"],
),
)
opt = optax.MultiSteps(opt, every_k_schedule=config["gradient_accummulation_steps"])
opt_state = opt.init(params)
params, ema_params, opt_state = train_model(
model, params, ema_params, opt, opt_state, train_loader, config=config
)