-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_up.py
173 lines (148 loc) · 6.53 KB
/
train_up.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
import torch
import torch.nn as nn
import os
import argparse
import src.utils as utils
import yaml
import os
from src.dataset import create_dataset, gaussian_dequantize
from src.model_utils import flow, get_loss
from src.models import get_model
from src.ema import EMAHelper
from torchvision.utils import make_grid, save_image
import wandb
import time
import numpy as np
def main():
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument('--config', type=str, help='Path to config file.')
group.add_argument('--ckpt', type=str, help='Path to checkpoint file.')
args = parser.parse_args()
if args.ckpt:
ckpt = torch.load(args.ckpt, map_location='cpu')
config = ckpt['config']
else:
config = utils.get_config_and_setup_dirs(args.config)
parser = utils.add_config_to_argparser(config=config, parser=parser)
args = parser.parse_args()
# Update config from command line args, if any.
config.update(vars(args))
log_dir = config['log_dir']
if config['use_wandb'] == True:
wandb.init(project="up", config=config, notes=config['wandb_notes'],
id=config['wandb_id'], resume='allow')
config['run_name'] = wandb.run.name
if not args.ckpt:
with open(os.path.join(log_dir, 'config.yaml'), 'w') as fp:
yaml.dump(config, fp)
# gpu information
device = config['device']
if isinstance(device, list):
multi_gpu = True
device_ids = device
device = torch.device("cuda:" + str(device_ids[0])) # if multi-GPUs, set default device to the first gpu
else:
multi_gpu = False
# initialize data
real_dataloader = create_dataset(config)
data_iterator = iter(real_dataloader)
# create model/optimizer/lr scheduler
model = get_model(config)
if multi_gpu:
model = nn.DataParallel(model, device_ids=device_ids)
model = model.to(device)
optim = torch.optim.Adam(model.parameters(), lr=config['learning_rate'], betas=[0.9, 0.999])
scheduler = torch.optim.lr_scheduler.ExponentialLR(optim, config['scheduler_gamma'])
# create ema helper
if config['ema']:
ema_helper = EMAHelper(mu=config['ema_rate'])
ema_helper.register(model)
# get dataset statistics for fid calculations
with np.load('eval/' + str(config['dataset']) + "_stats.npz") as f:
true_m, true_s = f['mu'][:], f['sigma'][:]
# load config information
n_epochs = config['n_epochs']
scheduler_steps = config['scheduler_steps'].copy()
loss_func = config['loss_function']
f_divergence = config['f_divergence']
eta = config['eta']
noise_factor = config['noise_factor']
n_flow_steps = config['n_flow_steps']
# if loading from ckpt, load ckpt
if args.ckpt:
model.load_state_dict(ckpt['model'])
optim.load_state_dict(ckpt['optim'])
ema_helper.load_state_dict(ckpt['ema'])
ema_helper.to(device) # state_dict from ckpt defaults to cpu
scheduler.load_state_dict(ckpt['scheduler'])
start_step = ckpt['step'] + 1
best_fid = ckpt['best_fid']
while len(scheduler_steps)!=0 and scheduler_steps[0] <= start_step:
scheduler_steps.pop(0)
else:
start_step = 0
best_fid = np.inf
# create test noise for tracking progress of training
test_noise = torch.FloatTensor(config['test_batch_size'], config['n_channels'], config['img_size'],
config['img_size']).uniform_(-1., 1.).to(device)
time_start = time.time()
next_scheduler_step = scheduler_steps.pop(0) if len(scheduler_steps) != 0 else None
for i in range(start_step, n_epochs):
try:
x_de = next(data_iterator)[0].to(device)
except StopIteration:
data_iterator = iter(real_dataloader)
x_de = next(data_iterator)[0].to(device)
x_de = gaussian_dequantize(x_de, sigma=3e-2)
x_nu = torch.FloatTensor(x_de.size(0), config['n_channels'], config['img_size'],
config['img_size']).uniform_(-1., 1.).to(device)
x_nu = flow(x_nu, model, n_flow_steps, eta, noise_factor, f_divergence)
model_x_nu = model(x_nu)
model_x_de = model(x_de)
loss, r_x_nu, r_x_de, first, second = get_loss(loss_func, model_x_nu, model_x_de)
optim.zero_grad()
loss.backward()
optim.step()
if config['ema']:
ema_helper.update(model)
if next_scheduler_step is not None and (i+1) == next_scheduler_step:
scheduler.step()
next_scheduler_step = scheduler_steps.pop(0) if len(scheduler_steps) != 0 else None
if config['use_wandb'] == True:
wandb.log({"first_term": first,
"second_term": second,
"loss": loss.item(),
"r_x_nu": r_x_nu,
"r_x_de": r_x_de
})
if (i+1) % config['print_steps'] == 0 or (i+1) == n_epochs:
print(f"Loss for step {i} is {loss}")
print(f"R_x_nu is {r_x_nu}, r_x_de is {r_x_de}")
time_end = time.time()
print(f"Time taken for steps {i+1-config['print_steps']}-{i} is \
{time_end-time_start} seconds\n")
time_start = time.time()
if (i+1) % config['log_steps'] == 0 or (i+1) == n_epochs:
# generate test samples
test_model = ema_helper.ema_copy(model) if config['ema'] else model
test_x = test_noise.clone().detach().to(device)
test_x = flow(test_x, test_model, n_flow_steps, eta, noise_factor, f_divergence).cpu()
img_path = os.path.join(log_dir, "step_" + str(i) + ".png")
grid = make_grid(test_x.clamp(-1.,1.), nrow=8, normalize=True)
save_image(grid, img_path)
if config['use_wandb'] == True:
images = wandb.Image(grid, caption=f"Step {i}")
wandb.log({"generated images": images})
if (i+1) % config['save_steps'] == 0 or (i+1) == n_epochs:
torch.save({'step': i,
'model': model.state_dict(),
'optim': optim.state_dict(),
'ema': ema_helper.state_dict(),
'scheduler': scheduler.state_dict(),
'config': config,
},
os.path.join(config['ckpt_dir'], config['dataset'] + '_' + config['loss_function'] + \
'_' + config['f_divergence'] + '.pt'))
if __name__ == "__main__":
main()