-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain.py
326 lines (261 loc) · 14.8 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
import argparse
import time
import numpy as np
import random
from tqdm import tqdm
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torchvision.transforms as T
from torch.utils.data import DataLoader
from dataset.dataset import eMotionGANDataset, load_flow_img_dataset, load_protocol, load_emotions_file
from models.motion_normalizer import MotionNormalizer, MotionDiscriminator, EmotionDiscriminator
from models.motion_warper import MotionWarper
from models.DMUE.eval import load_dmue_model, dmue_inference
def train_epoch(epoch, device, dataloader, motion_normalizer, motion_warper, motion_discriminator, emotion_discriminator):
motion_normalizer.train()
discriminator.train()
emotion_discriminator.train()
motion_warper.train()
norm_loss = 0.0
motion_disc_loss = 0.0
emotion_disc_loss = 0.0
warper_loss = 0.0
running_total_loss = 0
n_iter = epoch*len(dataloader)
pbar = tqdm(enumerate(dataloader), total=len(dataloader))
for i, mini_batch in pbar:
inp_img, tar_img, inp_flow, tar_flow, labels, napex = mini_batch
## to device ['cpu' / 'gpu']
inp_img = inp_img.to(device)
tar_img = tar_img.to(device)
inp_flow = inp_flow.to(device)
tar_flow = tar_flow.to(device)
labels = labels.to(device)
if any(napex):
## image generator
warp_optimizer.zero_grad()
real_warped_img = motion_warper(inp_img[napex], tar_flow[napex])
pred_labels = dmue_inference(dmue_model, real_warped_img, transform, train=True)
label_loss = categorical_crossentropy(pred_labels, labels[napex].long())
warper_loss = motion_warper_loss(tar_img[napex], real_warped_img) + label_loss
warper_loss.backward()
warp_optimizer.step()
## expression discriminator
emotion_disc_opt.zero_grad()
disc_real_labels = emotion_discriminator(-.5*tar_flow+.5)
emotion_disc_loss = categorical_crossentropy(disc_real_labels, labels.long())
emotion_disc_loss.backward()
emotion_disc_opt.step()
## flow generator
norm_optimizer.zero_grad()
norm_flow = motion_normalizer(inp_flow)
disc_output = motion_discriminator(norm_flow)
disc_gen_labels = emotion_discriminator(-.5*norm_flow+.5)
norm_loss, gan_loss = motion_normalizer_loss(norm_flow, tar_flow,
disc_output,
labels, disc_gen_labels)
if phase > 0:
if any(napex):
warped_img = motion_warper(inp_img[napex], norm_flow[napex])
norm_loss += motion_warper_loss(tar_img[napex], warped_img)
norm_loss.backward()
norm_optimizer.step()
## patch discriminator
motion_disc_opt.zero_grad()
fake_logits = motion_discriminator(norm_flow.detach())
real_logits = motion_discriminator(tar_flow)
fake_loss = criterion_GAN(fake_logits, torch.zeros_like(fake_logits))
real_loss = criterion_GAN(real_logits, torch.ones_like(real_logits))
motion_disc_loss = (real_loss + fake_loss) / 2
motion_disc_loss.backward()
motion_disc_opt.step()
running_total_loss += .5 * (norm_loss.item() + warper_loss.item())
pbar.set_description(f'[TRAIN] EpocH {epoch} => total_loss: %.2f | %.1f' % (running_total_loss / (i + 1), running_total_loss))
norm_lr_scheduler.step()
warp_lr_scheduler.step()
return norm_loss, warper_loss, motion_disc_loss, emotion_disc_loss
def evaluate_epoch(epoch, device, dataloader, motion_normalizer, motion_warper, motion_discriminator, emotion_discriminator):
motion_normalizer.eval()
motion_discriminator.eval()
emotion_discriminator.eval()
motion_warper.eval()
with torch.no_grad():
loss_grid_weights = [.5, .5]
running_total_loss = 0
pbar = tqdm(enumerate(dataloader), total=len(dataloader), desc=f'EpocH {epoch}')
loss_array = torch.zeros([len(loss_grid_weights), len(dataloader)], dtype=torch.float32, device=device)
for i, mini_batch in pbar:
inp_img, tar_img, inp_flow, tar_flow, labels, _ = mini_batch
## to device ['cpu' / 'gpu']
inp_img = inp_img.to(device)
tar_img = tar_img.to(device)
inp_flow = inp_flow.to(device)
tar_flow = tar_flow.to(device)
labels = labels.to(device)
## warping loss
norm_flow = motion_normalizer(inp_flow)
warp_img = motion_warper(inp_img, gen_flow)
pred_rgb_labels = dmue_inference(dmue_model, warp_img, transform).to(device)
rgb_loss = categorical_crossentropy(pred_rgb_labels, labels.long())
## normalization loss
pred_flow_labels = emotion_discriminator(gen_flow)
flow_loss = categorical_crossentropy(pred_flow_labels, labels.long())
loss_array[0, i] = rgb_loss
loss_array[1, i] = flow_loss
running_total_loss += loss_grid_weights[0] * rgb_loss.item() + loss_grid_weights[1] * flow_loss.item()
pbar.set_description(f'[VALID] EpocH {epoch} => total loss: %.2f | %.1f' % (running_total_loss / (i + 1), running_total_loss))
mean_loss = torch.mean(loss_array, dim=1)
mean_loss = loss_grid_weights[0] * mean_loss[0] + loss_grid_weights[1] * mean_loss[1]
return mean_loss
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='eMotionGAN trainining')
## paths
parser.add_argument('--exp', type=str,
default=time.strftime('%Y_%m_%d_%H_%M'),
help='name of the experiment')
parser.add_argument('--data_path', type=str,
help='path to data file containing paths to training/validation data.')
parser.add_argument('--proto_path', type=str,
help='protocol path for k-fold evaluation.')
parser.add_argument('--fold', type=int,
help='integer between 0 & k refering to the fold. (k-1) fold for training & 1 fold for evaluation.')
parser.add_argument('--pretrained', dest='pretrained', default=None,
help='path to pre-trained model')
parser.add_argument('--snapshots', type=str, default='./snapshots')
## parameters
parser.add_argument('--norm_lr', type=float, default=1e-5, help='learning rate for motion normalizer')
parser.add_argument('--warp_lr', type=float, default=1e-4, help='learning rate for motion warper')
parser.add_argument('--motion_disc_lr', type=float, default=1e-4, help='learning rate for motion discriminator')
parser.add_argument('--emotion_disc_lr', type=float, default=1e-4, help='learning rate for emotion discriminator')
parser.add_argument('--start_epoch', type=int, default=0, help='starting epoch')
parser.add_argument('--n_epochs', type=int, default=15, help='number of training epochs')
parser.add_argument('--batch_size', type=int, default=4, help='training batch size')
parser.add_argument('--n_threads', type=int, default=8, help='number of parallel threads for dataloaders')
parser.add_argument('--seed', type=int, default=2023,
help='Pseudo-RNG seed')
args = parser.parse_args()
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed(args.seed)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print('device:', device)
## get trainig & testing folds for eMotionGAN
protocol = load_protocol(args.proto_path)
train_fold, valid_fold = train_test_folds(fold, protocol)
flow_args = {'min_mag': None,
'max_mag': 10.0,
'shape': (128, 128)}
# create dataloaders
train_dataset = eMotionGANDataset(args.data_path, flow_args, train_fold)
valid_dataset = eMotionGANDataset(args.data_path, flow_args, valid_fold)
train_dataloader = DataLoader(train_dataset,
batch_size=args.batch_size,
shuffle=True,
num_workers=args.n_threads)
valid_dataloader = DataLoader(valid_dataset,
batch_size=args.batch_size,
shuffle=False,
num_workers=args.n_threads)
## MODELS
motion_normalizer = MotionNormalizer(in_channels=2,
out_channels=2,
dim=64,
encoder_res=2,
decoder_res=5,
n_sampling=4)
motion_warper = MotionWarper(motion_channels=2,
rgb_channels=3,
dim=64,
encoder_res=4,
decoder_res=4,
n_sampling=2)
motion_discriminator = MotionDiscriminator(in_channels=2)
emotion_discriminator = EmotionDiscriminator(in_channels=2, n_classes=6)
## load pretrained DMUE model & put it in eval mode
dmue_model = load_dmue_model()
dmue_model.eval()
motion_normalizer = nn.DataParallel(motion_normalizer).to(device)
motion_discriminator = nn.DataParallel(motion_discriminator).to(device)
motion_warper = nn.DataParallel(motion_warper).to(device)
emotion_discriminator = nn.DataParallel(emotion_discriminator).to(device)
dmue_model = nn.DataParallel(dmue_model).to(device)
## Optimizers
norm_optimizer = optim.Adam(motion_normalizer.parameters(), lr=args.norm_lr, weight_decay=1e-4)
warp_optimizer = optim.Adam(motion_warper.parameters(), lr=args.warp_lr, weight_decay=1e-4)
motion_disc_opt = optim.Adam(motion_discriminator.parameters(), lr=args.motion_disc_lr, weight_decay=1e-4)
emotion_disc_opt = optim.SGD(emotion_discriminator.parameters(), lr=args.emotion_disc_lr, weight_decay=1e-4)
norm_lr_scheduler = optim.lr_scheduler.MultiStepLR(norm_optimizer, milestones=[10, 13], gamma=0.5)
warp_lr_scheduler = optim.lr_scheduler.MultiStepLR(warp_optimizer, milestones=[10, 13], gamma=0.5)
categorical_crossentropy = nn.CrossEntropyLoss()
if args.pretrained:
# reload from pre_trained_model
motion_normalizer, motion_warper, emotion_discriminator, motion_discriminator,\
norm_optimizer, warp_optimizer, motion_disc_opt, emotion_disc_opt,\
norm_lr_scheduler, warp_lr_scheduler,\
start_epoch, best_val = load_checkpoint(motion_normalizer, motion_warper, emotion_discriminator, motion_discriminator,\
norm_optimizer, warp_optimizer, motion_disc_opt, emotion_disc_opt,\
norm_lr_scheduler, warp_lr_scheduler,\
filename=args.pretrained)
cur_snapshot = os.path.basename(os.path.dirname(args.pretrained))
else:
start_epoch = 0
best_val = float('inf')
if not os.path.isdir(args.snapshots):
os.mkdir(args.snapshots)
cur_snapshot = args.exp
if not osp.isdir(os.path.join(args.snapshots, cur_snapshot)):
os.makedirs(os.path.join(args.snapshots, cur_snapshot))
with open(os.path.join(args.snapshots, cur_snapshot, 'args.pkl'), 'wb') as f:
pickle.dump(args, f)
# create summary writer
save_path = os.path.join(args.snapshots, cur_snapshot)
train_writer = SummaryWriter(os.path.join(save_path, 'train'))
valid_writer = SummaryWriter(os.path.join(save_path, 'valid'))
for epoch in range(start_epoch, args.n_epochs):
phase = 0
if epoch >= 5:
phase = 1
## train epoch
norm_loss, warp_loss, motion_disc_loss, emotion_disc_loss = train_epoch(epoch+1,
device,
train_dataloader,
motion_normalizer,
motion_warper,
motion_discriminator,
emotion_discriminator)
print('\t[TRAIN] G_norm: %.3f | G_warp: %.3f | D_patch: %.3f | D_emotion: %.3f' % (norm_loss, warp_loss, motion_disc_loss, emotion_disc_loss))
train_writer.add_scalar('norm loss', norm_loss, epoch)
train_writer.add_scalar('warp loss', warp_loss, epoch)
train_writer.add_scalar('motion disc loss', motion_disc_loss, epoch)
train_writer.add_scalar('emotion disc loss', emotion_disc_loss, epoch)
train_writer.add_scalar('norm_learning_rate', norm_lr_scheduler.get_lr()[0], epoch)
train_writer.add_scalar('warp_learning_rate', warp_lr_scheduler.get_lr()[0], epoch)
## eval epoch
valid_mean_loss = evaluate_epoch(epoch+1,
device,
valid_fold,
motion_normalizer,
motion_warper,
motion_discriminator,
emotion_discriminator)
## save best model
is_best = val_mean_loss < best_val
best_val = min(val_mean_loss, best_val)
print(f"\t[VALID] Mean_loss: %.2f%% | is_best: %s" % (val_mean_loss, is_best))
valid_writer.add_scalar('valid mean loss', valid_mean_loss, epoch)
save_checkpoint({'epoch': epoch+1,
'flow_gen_state_dict': motion_normalizer.state_dict(),
'img_gen_state_dict': motion_warper.state_dict(),
'emotion_disc_state_dict': emotion_discriminator.state_dict(),
'disc_state_dict': motion_discriminator.state_dict(),
'flow_gen_optimizer': norm_optimizer.state_dict(),
'img_gen_optimizer': warp_optimizer.state_dict(),
'disc_optimizer': motion_disc_opt.state_dict(),
'emotion_disc_optimizer': emotion_disc_opt.state_dict(),
'flow_gen_scheduler': norm_lr_scheduler.state_dict(),
'img_gen_scheduler': warp_lr_scheduler.state_dict(),
'best_val': best_val},
save_path, 'epoch_{}.pth'.format(epoch+1), fold)