forked from ankanbhunia/PIDM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
executable file
·392 lines (290 loc) · 13.7 KB
/
train.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
import os
import warnings
warnings.filterwarnings("ignore")
import time, cv2, torch, wandb
import torch.distributed as dist
from config.diffconfig import DiffusionConfig, get_model_conf
from config.dataconfig import Config as DataConfig
from tensorfn import load_config as DiffConfig
from diffusion import create_gaussian_diffusion, make_beta_schedule, ddim_steps
from tensorfn.optim import lr_scheduler
from torch import nn, optim
from torch.utils import data
from torchvision import transforms
from tqdm import tqdm
import numpy as np
import data as deepfashion_data
from model import UNet
from insightface.app import FaceAnalysis
#zzx!
def init_distributed():
# Initializes the distributed backend which will take care of sychronizing nodes/GPUs
dist_url = "env://" # default
# only works with torch.distributed.launch // torch.run
rank = int(os.environ["RANK"])
world_size = int(os.environ['WORLD_SIZE'])
local_rank = int(os.environ['LOCAL_RANK'])
dist.init_process_group(
backend="nccl",
init_method=dist_url,
world_size=world_size,
rank=rank)
# this will make all .cuda() calls work properly
torch.cuda.set_device(local_rank)
# synchronizes all the threads to reach this point before moving on
dist.barrier()
setup_for_distributed(rank == 0)
def setup_for_distributed(is_master):
"""
This function disables printing when not in master process
"""
import builtins as __builtin__
builtin_print = __builtin__.print
def print(*args, **kwargs):
force = kwargs.pop('force', False)
if is_master or force:
builtin_print(*args, **kwargs)
__builtin__.print = print
def is_main_process():
try:
if dist.get_rank()==0:
return True
else:
return False
except:
return True
def sample_data(loader):
loader_iter = iter(loader)
epoch = 0
while True:
try:
yield epoch, next(loader_iter)
except StopIteration:
epoch += 1
loader_iter = iter(loader)
yield epoch, next(loader_iter)
def accumulate(model1, model2, decay=0.9999):
par1 = dict(model1.named_parameters())
par2 = dict(model2.named_parameters())
for k in par1.keys():
par1[k].data.mul_(decay).add_(par2[k].data, alpha=1 - decay)
def train(conf, loader, val_loader, model, ema, diffusion, betas, optimizer, scheduler, guidance_prob, cond_scale, device, wandb, app_analysis):
import time
i = 0
loss_list = []
loss_mean_list = []
loss_vb_list = []
loss_face_list = []
for epoch in range(300):
if is_main_process: print ('#Epoch - '+str(epoch))
start_time = time.time()
for batch in tqdm(loader):
i = i + 1
img = torch.cat([batch['source_image'], batch['target_image']], 0)
target_img = torch.cat([batch['target_image'], batch['source_image']], 0)
target_pose = torch.cat([batch['target_skeleton'], batch['source_skeleton']], 0)
img = img.to(device)
target_img = target_img.to(device)
target_pose = target_pose.to(device)
time_t = torch.randint(
0,
conf.diffusion.beta_schedule["n_timestep"],
(img.shape[0],),
device=device,
)
loss_dict = diffusion.training_losses(model, x_start = target_img, t = time_t, cond_input = [img, target_pose], prob = 1 - guidance_prob, model_face = app_analysis)
loss = loss_dict['loss'].mean()
loss_mse = loss_dict['mse'].mean()
loss_vb = loss_dict['vb'].mean()
loss_face = loss_dict['face'].mean()
optimizer.zero_grad()
loss.backward()
nn.utils.clip_grad_norm_(model.parameters(), 1)
scheduler.step()
optimizer.step()
loss = loss_dict['loss'].mean()
loss_list.append(loss.detach().item())
loss_mean_list.append(loss_mse.detach().item())
loss_vb_list.append(loss_vb.detach().item())
loss_face_list.append(loss_face.detach().item())
accumulate(
ema, model.module, 0 if i < conf.training.scheduler.warmup else 0.9999
)
# add early stopping
if args.early_stop:
if args.loss_best is None:
args.loss_best = loss
elif loss >= args.loss_best:
args.loss_counter += 1
if args.loss_counter >= args.loss_patience:
print('\n')
print("EarlyStopping: Stop training at iteration {}".format(i))
torch.save(best_model, best_model_path)
wandb.log(best_losses)
args.stop_training = True
break
else:
args.loss_best = loss
args.loss_counter = 0
if conf.distributed:
model_module = model.module
else:
model_module = model
best_model = {
"model": model_module.state_dict(),
"ema": ema.state_dict(),
"scheduler": scheduler.state_dict(),
"optimizer": optimizer.state_dict(),
"conf": conf,
}
best_model_path = conf.training.ckpt_path + f"/model_{str(i).zfill(6)}.pt"
best_losses = {'loss':(loss.detach().item()),
'loss_vb':(loss_vb.detach().item()),
'loss_mean':(loss_mse.detach().item()),
'loss_id':(loss_face.detach().item()),
'epoch':epoch,'steps':i}
if i%args.save_wandb_logs_every_iters == 0 and is_main_process():
wandb.log({'loss':(sum(loss_list)/len(loss_list)),
'loss_vb':(sum(loss_vb_list)/len(loss_vb_list)),
'loss_mean':(sum(loss_mean_list)/len(loss_mean_list)),
'loss_id':(sum(loss_face_list)/len(loss_face_list)),
'epoch':epoch,'steps':i})
loss_list = []
loss_mean_list = []
loss_vb_list = []
loss_face_list = []
if i%args.save_checkpoints_every_iters == 0 and is_main_process():
if conf.distributed:
model_module = model.module
else:
model_module = model
torch.save(
{
"model": model_module.state_dict(),
"ema": ema.state_dict(),
"scheduler": scheduler.state_dict(),
"optimizer": optimizer.state_dict(),
"conf": conf,
},
conf.training.ckpt_path + f"/model_{str(i).zfill(6)}.pt"
)
if args.stop_training:
break
if is_main_process():
print ('Epoch Time '+str(int(time.time()-start_time))+' secs')
print ('Model Saved Successfully for #epoch '+str(epoch)+' #steps '+str(i))
if conf.distributed:
model_module = model.module
else:
model_module = model
torch.save(
{
"model": model_module.state_dict(),
"ema": ema.state_dict(),
"scheduler": scheduler.state_dict(),
"optimizer": optimizer.state_dict(),
"conf": conf,
},
conf.training.ckpt_path + '/last.pt'
)
if (epoch)%args.save_wandb_images_every_epochs==0:
print ('Generating samples at epoch number ' + str(epoch))
val_batch = next(val_loader)
val_img = val_batch['source_image'].cuda()
val_pose = val_batch['target_skeleton'].cuda()
with torch.no_grad():
if args.sample_algorithm == 'ddpm':
print ('Sampling algorithm used: DDPM')
samples = diffusion.p_sample_loop(ema, x_cond = [val_img, val_pose], progress = True, cond_scale = cond_scale)
elif args.sample_algorithm == 'ddim':
print ('Sampling algorithm used: DDIM')
nsteps = 50
noise = torch.randn(val_img.shape).cuda()
seq = range(0, 1000, 1000//nsteps)
xs, x0_preds = ddim_steps(noise, seq, ema, betas.cuda(), [val_img, val_pose])
samples = xs[-1].cuda()
grid = torch.cat([val_img, val_pose[:,:3], samples], -1)
gathered_samples = [torch.zeros_like(grid) for _ in range(dist.get_world_size())]
dist.all_gather(gathered_samples, grid)
if is_main_process():
wandb.log({'samples':wandb.Image(torch.cat(gathered_samples, -2))})
def main(settings, EXP_NAME, app_analysis):
[args, DiffConf, DataConf] = settings
if is_main_process(): wandb.init(project="person-synthesis", name = EXP_NAME, settings = wandb.Settings(code_dir="."))
if DiffConf.ckpt is not None:
DiffConf.training.scheduler.warmup = 0
DiffConf.distributed = True
local_rank = int(os.environ['LOCAL_RANK'])
DataConf.data.train.batch_size = args.batch_size//2 #src -> tgt , tgt -> src
val_dataset, train_dataset = deepfashion_data.get_train_val_dataloader(DataConf.data, labels_required = True, distributed = True)
def cycle(iterable):
while True:
for x in iterable:
yield x
val_dataset = iter(cycle(val_dataset))
model = get_model_conf().make_model()
model = model.to(args.device)
ema = get_model_conf().make_model()
ema = ema.to(args.device)
if DiffConf.distributed:
model = nn.parallel.DistributedDataParallel(
model,
device_ids=[local_rank],
find_unused_parameters=True
)
optimizer = DiffConf.training.optimizer.make(model.parameters())
scheduler = DiffConf.training.scheduler.make(optimizer)
if DiffConf.ckpt is not None:
ckpt = torch.load(DiffConf.ckpt, map_location=lambda storage, loc: storage)
if DiffConf.distributed:
model.module.load_state_dict(ckpt["model"])
else:
model.load_state_dict(ckpt["model"])
ema.load_state_dict(ckpt["ema"])
scheduler.load_state_dict(ckpt["scheduler"])
if is_main_process(): print ('model loaded successfully')
betas = DiffConf.diffusion.beta_schedule.make()
diffusion = create_gaussian_diffusion(betas, predict_xstart = False)
train(
DiffConf, train_dataset, val_dataset, model, ema, diffusion, betas, optimizer, scheduler, args.guidance_prob, args.cond_scale, args.device, wandb, app_analysis
)
if __name__ == "__main__":
init_distributed()
import argparse
parser = argparse.ArgumentParser(description='help')
parser.add_argument('--exp_name', type=str, default='pidm_deepfashion')
parser.add_argument('--DiffConfigPath', type=str, default='./config/diffusion.conf')
parser.add_argument('--DataConfigPath', type=str, default='./config/data.yaml')
parser.add_argument('--dataset_path', type=str, default='./dataset/deepfashion')
parser.add_argument('--save_path', type=str, default='checkpoints')
parser.add_argument('--cond_scale', type=int, default=2)
parser.add_argument('--guidance_prob', type=int, default=0.1)
parser.add_argument('--sample_algorithm', type=str, default='ddim') # ddpm, ddim
parser.add_argument('--batch_size', type=int, default=2)
parser.add_argument('--save_wandb_logs_every_iters', type=int, default=50)
parser.add_argument('--save_checkpoints_every_iters', type=int, default=2000)
parser.add_argument('--save_wandb_images_every_epochs', type=int, default=10)
parser.add_argument('--device', type=str, default='cuda')
parser.add_argument('--n_gpu', type=int, default=8)
parser.add_argument('--n_machine', type=int, default=1)
parser.add_argument('--local_rank', type=int, default=0)
parser.add_argument("opts", default=None, nargs=argparse.REMAINDER)
# zzx!
parser.add_argument('--early_stop', default=False, type=bool)
parser.add_argument('--loss_patience', default=10, type=int, help='Patience to help judge early stop')
parser.add_argument('--loss_best',default=None, type=float, help='Save the best value of loss')
parser.add_argument('--loss_counter', default=0, type=int, help='Count to help judge early stop')
parser.add_argument('--stop_training', default=False, type=bool, help='Stop training or not')
args = parser.parse_args()
print ('Experiment: '+ args.exp_name)
DiffConf = DiffConfig(DiffusionConfig, args.DiffConfigPath, args.opts, False)
DataConf = DataConfig(args.DataConfigPath)
DiffConf.training.ckpt_path = os.path.join(args.save_path, args.exp_name)
DataConf.data.path = args.dataset_path
if is_main_process():
if not os.path.isdir(args.save_path): os.mkdir(args.save_path)
if not os.path.isdir(DiffConf.training.ckpt_path): os.mkdir(DiffConf.training.ckpt_path)
DiffConf.ckpt = "checkpoints/last.pt"
app_analysis = FaceAnalysis(providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
app_analysis.prepare(ctx_id=0, det_size=(640, 640))
main(settings = [args, DiffConf, DataConf], EXP_NAME = args.exp_name, app_analysis = app_analysis)