-
Notifications
You must be signed in to change notification settings - Fork 80
/
train.py
155 lines (120 loc) · 7.24 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
from tqdm import trange
import torch
from torch.utils.data import DataLoader
from logger import Logger
from modules.losses import generator_loss, discriminator_loss, generator_loss_names, discriminator_loss_names
from torch.optim.lr_scheduler import MultiStepLR
from sync_batchnorm import DataParallelWithCallback
def split_kp(kp_joined, detach=False):
if detach:
kp_video = {k: v[:, 1:].detach() for k, v in kp_joined.items()}
kp_appearance = {k: v[:, :1].detach() for k, v in kp_joined.items()}
else:
kp_video = {k: v[:, 1:] for k, v in kp_joined.items()}
kp_appearance = {k: v[:, :1] for k, v in kp_joined.items()}
return {'kp_driving': kp_video, 'kp_source': kp_appearance}
class GeneratorFullModel(torch.nn.Module):
"""
Merge all generator related updates into single model for better multi-gpu usage
"""
def __init__(self, kp_extractor, generator, discriminator, train_params):
super(GeneratorFullModel, self).__init__()
self.kp_extractor = kp_extractor
self.generator = generator
self.discriminator = discriminator
self.train_params = train_params
def forward(self, x):
kp_joined = self.kp_extractor(torch.cat([x['source'], x['video']], dim=2))
generated = self.generator(x['source'],
**split_kp(kp_joined, self.train_params['detach_kp_generator']))
video_prediction = generated['video_prediction']
video_deformed = generated['video_deformed']
kp_dict = split_kp(kp_joined, False)
discriminator_maps_generated = self.discriminator(video_prediction, **kp_dict)
discriminator_maps_real = self.discriminator(x['video'], **kp_dict)
generated.update(kp_dict)
losses = generator_loss(discriminator_maps_generated=discriminator_maps_generated,
discriminator_maps_real=discriminator_maps_real,
video_deformed=video_deformed,
loss_weights=self.train_params['loss_weights'])
return tuple(losses) + (generated, kp_joined)
class DiscriminatorFullModel(torch.nn.Module):
"""
Merge all generator related updates into single model for better multi-gpu usage
"""
def __init__(self, kp_extractor, generator, discriminator, train_params):
super(DiscriminatorFullModel, self).__init__()
self.kp_extractor = kp_extractor
self.generator = generator
self.discriminator = discriminator
self.train_params = train_params
def forward(self, x, kp_joined, generated):
kp_dict = split_kp(kp_joined, self.train_params['detach_kp_discriminator'])
discriminator_maps_generated = self.discriminator(generated['video_prediction'].detach(), **kp_dict)
discriminator_maps_real = self.discriminator(x['video'], **kp_dict)
loss = discriminator_loss(discriminator_maps_generated=discriminator_maps_generated,
discriminator_maps_real=discriminator_maps_real,
loss_weights=self.train_params['loss_weights'])
return loss
def train(config, generator, discriminator, kp_detector, checkpoint, log_dir, dataset, device_ids):
train_params = config['train_params']
optimizer_generator = torch.optim.Adam(generator.parameters(), lr=train_params['lr'], betas=(0.5, 0.999))
optimizer_discriminator = torch.optim.Adam(discriminator.parameters(), lr=train_params['lr'], betas=(0.5, 0.999))
optimizer_kp_detector = torch.optim.Adam(kp_detector.parameters(), lr=train_params['lr'], betas=(0.5, 0.999))
if checkpoint is not None:
start_epoch, it = Logger.load_cpk(checkpoint, generator, discriminator, kp_detector,
optimizer_generator, optimizer_discriminator, optimizer_kp_detector)
else:
start_epoch = 0
it = 0
scheduler_generator = MultiStepLR(optimizer_generator, train_params['epoch_milestones'], gamma=0.1,
last_epoch=start_epoch - 1)
scheduler_discriminator = MultiStepLR(optimizer_discriminator, train_params['epoch_milestones'], gamma=0.1,
last_epoch=start_epoch - 1)
scheduler_kp_detector = MultiStepLR(optimizer_kp_detector, train_params['epoch_milestones'], gamma=0.1,
last_epoch=start_epoch - 1)
dataloader = DataLoader(dataset, batch_size=train_params['batch_size'], shuffle=True, num_workers=4, drop_last=True)
generator_full = GeneratorFullModel(kp_detector, generator, discriminator, train_params)
discriminator_full = DiscriminatorFullModel(kp_detector, generator, discriminator, train_params)
generator_full_par = DataParallelWithCallback(generator_full, device_ids=device_ids)
discriminator_full_par = DataParallelWithCallback(discriminator_full, device_ids=device_ids)
with Logger(log_dir=log_dir, visualizer_params=config['visualizer_params'], **train_params['log_params']) as logger:
for epoch in trange(start_epoch, train_params['num_epochs']):
for x in dataloader:
out = generator_full_par(x)
loss_values = out[:-2]
generated = out[-2]
kp_joined = out[-1]
loss_values = [val.mean() for val in loss_values]
loss = sum(loss_values)
loss.backward(retain_graph=not train_params['detach_kp_discriminator'])
optimizer_generator.step()
optimizer_generator.zero_grad()
optimizer_discriminator.zero_grad()
if train_params['detach_kp_discriminator']:
optimizer_kp_detector.step()
optimizer_kp_detector.zero_grad()
generator_loss_values = [val.detach().cpu().numpy() for val in loss_values]
loss_values = discriminator_full_par(x, kp_joined, generated)
loss_values = [val.mean() for val in loss_values]
loss = sum(loss_values)
loss.backward()
optimizer_discriminator.step()
optimizer_discriminator.zero_grad()
if not train_params['detach_kp_discriminator']:
optimizer_kp_detector.step()
optimizer_kp_detector.zero_grad()
discriminator_loss_values = [val.detach().cpu().numpy() for val in loss_values]
logger.log_iter(it,
names=generator_loss_names(train_params['loss_weights']) + discriminator_loss_names(),
values=generator_loss_values + discriminator_loss_values, inp=x, out=generated)
it += 1
scheduler_generator.step()
scheduler_discriminator.step()
scheduler_kp_detector.step()
logger.log_epoch(epoch, {'generator': generator,
'discriminator': discriminator,
'kp_detector': kp_detector,
'optimizer_generator': optimizer_generator,
'optimizer_discriminator': optimizer_discriminator,
'optimizer_kp_detector': optimizer_kp_detector})