-
Notifications
You must be signed in to change notification settings - Fork 1
/
train.py
553 lines (437 loc) · 23.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
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
import os
import numpy as np, argparse, time, pickle, random
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torch.utils.data.sampler import SubsetRandomSampler, WeightedRandomSampler
from dataloader import IEMOCAPDataset, MELDDataset
from model import MaskedNLLLoss, LSTMModel, GRUModel, Model, MaskedMSELoss, FocalLoss
from sklearn.metrics import f1_score, confusion_matrix, accuracy_score, classification_report, precision_recall_fscore_support
import pandas as pd
import pickle as pk
import datetime
import ipdb
# seed = 1475 # We use seed = 1475 on IEMOCAP and seed = 67137 on MELD
def seed_everything(seed):
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
def _init_fn(worker_id):
np.random.seed(int(args.seed)+worker_id)
def get_train_valid_sampler(trainset, valid=0.1, dataset='IEMOCAP'):
size = len(trainset)
idx = list(range(size))
split = int(valid*size)
return SubsetRandomSampler(idx[split:]), SubsetRandomSampler(idx[:split])
def get_MELD_loaders(batch_size=32, valid=0.1, num_workers=0, pin_memory=False):
trainset = MELDDataset('data/MELD_features_raw1.pkl')
train_sampler, valid_sampler = get_train_valid_sampler(trainset, valid, 'MELD')
train_loader = DataLoader(trainset,
batch_size=batch_size,
sampler=train_sampler,
collate_fn=trainset.collate_fn,
num_workers=num_workers,
pin_memory=pin_memory)
valid_loader = DataLoader(trainset,
batch_size=batch_size,
sampler=valid_sampler,
collate_fn=trainset.collate_fn,
num_workers=num_workers,
pin_memory=pin_memory)
testset = MELDDataset('data/MELD_features_raw1.pkl', train=False)
test_loader = DataLoader(testset,
batch_size=batch_size,
collate_fn=testset.collate_fn,
num_workers=num_workers,
pin_memory=pin_memory)
return train_loader, valid_loader, test_loader
def get_IEMOCAP_loaders(batch_size=32, valid=0.1, num_workers=0, pin_memory=False):
trainset = IEMOCAPDataset()
train_sampler, valid_sampler = get_train_valid_sampler(trainset, valid)
train_loader = DataLoader(trainset,
batch_size=batch_size,
collate_fn=trainset.collate_fn,
num_workers=num_workers,
pin_memory=pin_memory, worker_init_fn=_init_fn)
valid_loader = DataLoader(trainset,
batch_size=batch_size,
sampler=valid_sampler,
collate_fn=trainset.collate_fn,
num_workers=num_workers,
pin_memory=pin_memory)
testset = IEMOCAPDataset(train=False)
test_loader = DataLoader(testset,
batch_size=batch_size,
collate_fn=testset.collate_fn,
num_workers=num_workers,
pin_memory=pin_memory, worker_init_fn=_init_fn)
return train_loader, valid_loader, test_loader
def train_or_eval_model(model, loss_function, dataloader, epoch, optimizer=None, train=False):
"""
"""
losses, preds, labels, masks = [], [], [], []
alphas, alphas_f, alphas_b, vids = [], [], [], []
max_sequence_len = []
assert not train or optimizer!=None
if train:
model.train()
else:
model.eval()
seed_everything(args.seed)
for data in dataloader:
if train:
optimizer.zero_grad()
textf, visuf, acouf, qmask, umask, label = [d.cuda() for d in data[:-1]] if cuda else data[:-1]
max_sequence_len.append(textf.size(0))
log_prob, alpha, alpha_f, alpha_b, _ = model(textf, qmask, umask)
lp_ = log_prob.transpose(0,1).contiguous().view(-1, log_prob.size()[2])
labels_ = label.view(-1)
loss = loss_function(lp_, labels_, umask)
pred_ = torch.argmax(lp_,1)
preds.append(pred_.data.cpu().numpy())
labels.append(labels_.data.cpu().numpy())
masks.append(umask.view(-1).cpu().numpy())
losses.append(loss.item()*masks[-1].sum())
if train:
loss.backward()
if args.tensorboard:
for param in model.named_parameters():
writer.add_histogram(param[0], param[1].grad, epoch)
optimizer.step()
else:
alphas += alpha
alphas_f += alpha_f
alphas_b += alpha_b
vids += data[-1]
if preds!=[]:
preds = np.concatenate(preds)
labels = np.concatenate(labels)
masks = np.concatenate(masks)
else:
return float('nan'), float('nan'), [], [], [], float('nan'),[]
avg_loss = round(np.sum(losses)/np.sum(masks), 4)
avg_accuracy = round(accuracy_score(labels,preds, sample_weight=masks)*100, 2)
avg_fscore = round(f1_score(labels,preds, sample_weight=masks, average='weighted')*100, 2)
return avg_loss, avg_accuracy, labels, preds, masks, avg_fscore, [alphas, alphas_f, alphas_b, vids]
def train_or_eval_graph_model(model, loss_function, dataloader, epoch, cuda, modals, optimizer=None, train=False, dataset='IEMOCAP'):
losses, preds, labels = [], [], []
scores, vids = [], []
ei, et, en, el = torch.empty(0).type(torch.LongTensor), torch.empty(0).type(torch.LongTensor), torch.empty(0), []
if cuda:
ei, et, en = ei.cuda(), et.cuda(), en.cuda()
assert not train or optimizer!=None
if train:
model.train()
else:
model.eval()
seed_everything(args.seed)
for data in dataloader:
if train:
optimizer.zero_grad()
textf1,textf2,textf3,textf4, visuf, acouf, qmask, umask, label = [d.cuda() for d in data[:-1]] if cuda else data[:-1]
if args.multi_modal:
if args.mm_fusion_mthd=='concat':
if modals == 'avl':
textf = torch.cat([acouf, visuf, textf1,textf2,textf3,textf4],dim=-1)
elif modals == 'av':
textf = torch.cat([acouf, visuf],dim=-1)
elif modals == 'vl':
textf = torch.cat([visuf, textf1,textf2,textf3,textf4],dim=-1)
elif modals == 'al':
textf = torch.cat([acouf, textf1,textf2,textf3,textf4],dim=-1)
else:
raise NotImplementedError
elif args.mm_fusion_mthd=='gated':
textf = textf
else:
if modals == 'a':
textf = acouf
elif modals == 'v':
textf = visuf
elif modals == 'l':
textf = textf
else:
raise NotImplementedError
lengths = [(umask[j] == 1).nonzero(as_tuple=False).tolist()[-1][0] + 1 for j in range(len(umask))]
if args.multi_modal and args.mm_fusion_mthd=='gated':
log_prob, e_i, e_n, e_t, e_l = model(textf, qmask, umask, lengths, acouf, visuf)
elif args.multi_modal and args.mm_fusion_mthd=='concat_subsequently':
log_prob, e_i, e_n, e_t, e_l = model([textf1,textf2,textf3,textf4], qmask, umask, lengths, acouf, visuf, epoch)
elif args.multi_modal and args.mm_fusion_mthd=='concat_DHT':
log_prob, e_i, e_n, e_t, e_l = model([textf1,textf2,textf3,textf4], qmask, umask, lengths, acouf, visuf, epoch)
else:
log_prob, e_i, e_n, e_t, e_l = model(textf, qmask, umask, lengths)
label = torch.cat([label[j][:lengths[j]] for j in range(len(label))])
loss = loss_function(log_prob, label)
preds.append(torch.argmax(log_prob, 1).cpu().numpy())
labels.append(label.cpu().numpy())
losses.append(loss.item())
if train:
loss.backward()
optimizer.step()
if preds!=[]:
preds = np.concatenate(preds)
labels = np.concatenate(labels)
else:
return float('nan'), float('nan'), [], [], float('nan'), [], [], [], [], []
vids += data[-1]
ei = ei.data.cpu().numpy()
et = et.data.cpu().numpy()
en = en.data.cpu().numpy()
el = np.array(el)
labels = np.array(labels)
preds = np.array(preds)
vids = np.array(vids)
avg_loss = round(np.sum(losses)/len(losses), 4)
avg_accuracy = round(accuracy_score(labels, preds)*100, 2)
avg_fscore = round(f1_score(labels,preds, average='weighted')*100, 2)
return avg_loss, avg_accuracy, labels, preds, avg_fscore, vids, ei, et, en, el
if __name__ == '__main__':
path = './saved/IEMOCAP/'
parser = argparse.ArgumentParser()
parser.add_argument('--no-cuda', action='store_true', default=False, help='does not use GPU')
parser.add_argument('--base-model', default='LSTM', help='base recurrent model, must be one of DialogRNN/LSTM/GRU')
parser.add_argument('--graph-model', action='store_true', default=True, help='whether to use graph model after recurrent encoding')
parser.add_argument('--nodal-attention', action='store_true', default=True, help='whether to use nodal attention in graph model: Equation 4,5,6 in Paper')
parser.add_argument('--windowp', type=int, default=10, help='context window size for constructing edges in graph model for past utterances')
parser.add_argument('--windowf', type=int, default=10, help='context window size for constructing edges in graph model for future utterances')
parser.add_argument('--lr', type=float, default=0.0001, metavar='LR', help='learning rate')
parser.add_argument('--l2', type=float, default=0.00003, metavar='L2', help='L2 regularization weight')
parser.add_argument('--rec-dropout', type=float, default=0.1, metavar='rec_dropout', help='rec_dropout rate')
parser.add_argument('--dropout', type=float, default=0.5, metavar='dropout', help='dropout rate')
parser.add_argument('--batch-size', type=int, default=32, metavar='BS', help='batch size')
parser.add_argument('--epochs', type=int, default=60, metavar='E', help='number of epochs')
parser.add_argument('--class-weight', action='store_true', default=True, help='use class weights')
parser.add_argument('--active-listener', action='store_true', default=False, help='active listener')
parser.add_argument('--attention', default='general', help='Attention type in DialogRNN model')
parser.add_argument('--tensorboard', action='store_true', default=False, help='Enables tensorboard log')
parser.add_argument('--graph_type', default='relation', help='relation/GCN3/DeepGCN/MMGCN/MMGCN2')
parser.add_argument('--use_topic', action='store_true', default=False, help='whether to use topic information')
parser.add_argument('--alpha', type=float, default=0.2, help='alpha')
parser.add_argument('--multiheads', type=int, default=6, help='multiheads')
parser.add_argument('--graph_construct', default='full', help='single/window/fc for MMGCN2; direct/full for others')
parser.add_argument('--use_gcn', action='store_true', default=False, help='whether to combine spectral and none-spectral methods or not')
parser.add_argument('--use_residue', action='store_true', default=False, help='whether to use residue information or not')
parser.add_argument('--multi_modal', action='store_true', default=False, help='whether to use multimodal information')
parser.add_argument('--mm_fusion_mthd', default='concat', help='method to use multimodal information: concat, gated, concat_subsequently')
parser.add_argument('--modals', default='avl', help='modals to fusion')
parser.add_argument('--av_using_lstm', action='store_true', default=False, help='whether to use lstm in acoustic and visual modality')
parser.add_argument('--Deep_GCN_nlayers', type=int, default=4, help='Deep_GCN_nlayers')
parser.add_argument('--Dataset', default='IEMOCAP', help='dataset to train and test')
parser.add_argument('--use_speaker', action='store_true', default=True, help='whether to use speaker embedding')
parser.add_argument('--use_modal', action='store_true', default=False, help='whether to use modal embedding')
parser.add_argument('--norm', default='LN2', help='NORM type')
parser.add_argument('--testing', action='store_true', default=False, help='testing')
parser.add_argument('--num_L', type=int, default=3, help='num_hyperconvs')
parser.add_argument('--num_K', type=int, default=4, help='num_convs')
parser.add_argument('--seed', type=int, default=1475)
args = parser.parse_args()
today = datetime.datetime.now()
print(args)
if args.av_using_lstm:
name_ = args.mm_fusion_mthd+'_'+args.modals+'_'+args.graph_type+'_'+args.graph_construct+'using_lstm_'+args.Dataset
else:
name_ = args.mm_fusion_mthd+'_'+args.modals+'_'+args.graph_type+'_'+args.graph_construct+str(args.Deep_GCN_nlayers)+'_'+args.Dataset
if args.use_speaker:
name_ = name_+'_speaker'
if args.use_modal:
name_ = name_+'_modal'
args.cuda = torch.cuda.is_available() and not args.no_cuda
if args.cuda:
print('Running on GPU')
else:
print('Running on CPU')
if args.tensorboard:
from tensorboardX import SummaryWriter
writer = SummaryWriter()
cuda = args.cuda
n_epochs = args.epochs
batch_size = args.batch_size
modals = args.modals
feat2dim = {'IS10':1582,'3DCNN':512,'textCNN':100,'bert':768,'denseface':342,'MELD_text':600,'MELD_audio':300}
D_audio = feat2dim['IS10'] if args.Dataset=='IEMOCAP' else feat2dim['MELD_audio']
D_visual = feat2dim['denseface']
D_text = 1024 #feat2dim['textCNN'] if args.Dataset=='IEMOCAP' else feat2dim['MELD_text']
if args.multi_modal:
if args.mm_fusion_mthd=='concat':
if modals == 'avl':
D_m = D_audio+D_visual+D_text
elif modals == 'av':
D_m = D_audio+D_visual
elif modals == 'al':
D_m = D_audio+D_text
elif modals == 'vl':
D_m = D_visual+D_text
else:
raise NotImplementedError
else:
D_m = 1024
else:
if modals == 'a':
D_m = D_audio
elif modals == 'v':
D_m = D_visual
elif modals == 'l':
D_m = D_text
else:
raise NotImplementedError
D_g = 512 if args.Dataset=='IEMOCAP' else 1024
D_p = 150
D_e = 100
D_h = 100
D_a = 100
graph_h = 512
n_speakers = 9 if args.Dataset=='MELD' else 2
n_classes = 7 if args.Dataset=='MELD' else 6 if args.Dataset=='IEMOCAP' else 1
if args.graph_model:
seed_everything(args.seed)
model = Model(args.base_model,
D_m, D_g, D_p, D_e, D_h, D_a, graph_h,
n_speakers=n_speakers,
max_seq_len=200,
window_past=args.windowp,
window_future=args.windowf,
n_classes=n_classes,
listener_state=args.active_listener,
context_attention=args.attention,
dropout=args.dropout,
nodal_attention=args.nodal_attention,
no_cuda=args.no_cuda,
graph_type=args.graph_type,
use_topic=args.use_topic,
alpha=args.alpha,
multiheads=args.multiheads,
graph_construct=args.graph_construct,
use_GCN=args.use_gcn,
use_residue=args.use_residue,
D_m_v = D_visual,
D_m_a = D_audio,
modals=args.modals,
att_type=args.mm_fusion_mthd,
av_using_lstm=args.av_using_lstm,
Deep_GCN_nlayers=args.Deep_GCN_nlayers,
dataset=args.Dataset,
use_speaker=args.use_speaker,
use_modal=args.use_modal,
norm = args.norm,
num_L = args.num_L,
num_K = args.num_K)
print ('Graph NN with', args.base_model, 'as base model.')
name = 'Graph'
else:
if args.base_model == 'GRU':
model = GRUModel(D_m, D_e, D_h,
n_classes=n_classes,
dropout=args.dropout)
print ('Basic GRU Model.')
elif args.base_model == 'LSTM':
model = LSTMModel(D_m, D_e, D_h,
n_classes=n_classes,
dropout=args.dropout)
print ('Basic LSTM Model.')
else:
print ('Base model must be one of DialogRNN/LSTM/GRU/Transformer')
raise NotImplementedError
name = 'Base'
if cuda:
model.cuda()
if args.Dataset == 'IEMOCAP':
loss_weights = torch.FloatTensor([1/0.086747,
1/0.144406,
1/0.227883,
1/0.160585,
1/0.127711,
1/0.252668])
if args.Dataset == 'MELD':
loss_function = FocalLoss()
else:
if args.class_weight:
if args.graph_model:
#loss_function = FocalLoss()
loss_function = nn.NLLLoss(loss_weights.cuda() if cuda else loss_weights)
else:
loss_function = MaskedNLLLoss(loss_weights.cuda() if cuda else loss_weights)
else:
if args.graph_model:
loss_function = nn.NLLLoss()
else:
loss_function = MaskedNLLLoss()
optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.l2)
lr = args.lr
if args.Dataset == 'MELD':
train_loader, valid_loader, test_loader = get_MELD_loaders(valid=0.0,
batch_size=batch_size,
num_workers=2)
elif args.Dataset == 'IEMOCAP':
train_loader, valid_loader, test_loader = get_IEMOCAP_loaders(valid=0.0,
batch_size=batch_size,
num_workers=2)
else:
print("There is no such dataset")
best_fscore, best_loss, best_label, best_pred, best_mask = None, None, None, None, None
all_fscore, all_acc, all_loss = [], [], []
if args.testing:
if args.Dataset == 'MELD':
state = torch.load("checkpoints/MELD_checkpoint.pkl")
elif args.Dataset == 'IEMOCAP':
state = torch.load("checkpoints/IEMOCAP_checkpoint.pkl")
model.load_state_dict(state)
print('testing loaded model')
test_loss, test_acc, test_label, test_pred, test_fscore, _, _, _, _, _ = train_or_eval_graph_model(model, loss_function, test_loader, 0, cuda, args.modals, dataset=args.Dataset)
print('test_acc:',test_acc,'test_fscore:',test_fscore)
for e in range(n_epochs):
start_time = time.time()
if args.graph_model:
train_loss, train_acc, _, _, train_fscore, _, _, _, _, _ = train_or_eval_graph_model(model, loss_function, train_loader, e, cuda, args.modals, optimizer, True, dataset=args.Dataset)
valid_loss, valid_acc, _, _, valid_fscore, _, _, _, _, _ = train_or_eval_graph_model(model, loss_function, valid_loader, e, cuda, args.modals, dataset=args.Dataset)
test_loss, test_acc, test_label, test_pred, test_fscore, _, _, _, _, _ = train_or_eval_graph_model(model, loss_function, test_loader, e, cuda, args.modals, dataset=args.Dataset)
all_fscore.append(test_fscore)
else:
train_loss, train_acc, _, _, _, train_fscore, _ = train_or_eval_model(model, loss_function, train_loader, e, optimizer, True)
valid_loss, valid_acc, _, _, _, valid_fscore, _ = train_or_eval_model(model, loss_function, valid_loader, e)
test_loss, test_acc, test_label, test_pred, test_mask, test_fscore, attentions = train_or_eval_model(model, loss_function, test_loader, e)
all_fscore.append(test_fscore)
if best_loss == None or best_loss > test_loss:
best_loss, best_label, best_pred = test_loss, test_label, test_pred
if best_fscore == None or best_fscore < test_fscore:
best_fscore = test_fscore
best_label, best_pred = test_label, test_pred
if args.tensorboard:
writer.add_scalar('test: accuracy', test_acc, e)
writer.add_scalar('test: fscore', test_fscore, e)
writer.add_scalar('train: accuracy', train_acc, e)
writer.add_scalar('train: fscore', train_fscore, e)
print('epoch: {}, train_loss: {}, train_acc: {}, train_fscore: {}, test_loss: {}, test_acc: {}, test_fscore: {}, time: {} sec'.\
format(e+1, train_loss, train_acc, train_fscore, test_loss, test_acc, test_fscore, round(time.time()-start_time, 2)))
if (e+1)%10 == 0:
print ('----------best F-Score:', max(all_fscore))
print(classification_report(best_label, best_pred, sample_weight=best_mask,digits=4))
print(confusion_matrix(best_label,best_pred,sample_weight=best_mask))
if args.tensorboard:
writer.close()
if not args.testing:
print('Test performance..')
print ('F-Score:', max(all_fscore))
if not os.path.exists("record_{}_{}_{}.pk".format(today.year, today.month, today.day)):
with open("record_{}_{}_{}.pk".format(today.year, today.month, today.day),'wb') as f:
pk.dump({}, f)
with open("record_{}_{}_{}.pk".format(today.year, today.month, today.day), 'rb') as f:
record = pk.load(f)
key_ = name_
if record.get(key_, False):
record[key_].append(max(all_fscore))
else:
record[key_] = [max(all_fscore)]
if record.get(key_+'record', False):
record[key_+'record'].append(classification_report(best_label, best_pred, sample_weight=best_mask,digits=4))
else:
record[key_+'record'] = [classification_report(best_label, best_pred, sample_weight=best_mask,digits=4)]
with open("record_{}_{}_{}.pk".format(today.year, today.month, today.day),'wb') as f:
pk.dump(record, f)
print(classification_report(best_label, best_pred, sample_weight=best_mask,digits=4))
print(confusion_matrix(best_label,best_pred,sample_weight=best_mask))