-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
308 lines (256 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
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
import numpy as np, argparse, time
import torch
import torch.optim as optim
from torch.utils.data import DataLoader
from torch.utils.data.sampler import SubsetRandomSampler
from dataloader import IEMOCAPDataset
from model import MaskedNLLLoss, MaskedKLDivLoss, EmotionAwareTransformerModel
from sklearn.metrics import f1_score, confusion_matrix, accuracy_score, classification_report
import pickle as pk
import datetime
import wandb
def get_train_valid_sampler(trainset, valid=0.1, dataset='MELD'):
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_multimodal_features.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_multimodal_features.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,
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 = IEMOCAPDataset(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 train_or_eval_model(model, loss_function, kl_loss, dataloader, epoch, optimizer=None, train=False, gamma_1=1.0, gamma_2=1.0, gamma_3=1.0):
losses, preds, labels, masks = [], [], [], []
emotion_losses = []
if train:
model.train()
else:
model.eval()
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]
qmask = qmask.permute(1, 0, 2)
lengths = [(umask[j] == 1).nonzero().tolist()[-1][0] + 1 for j in range(len(umask))]
log_prob1, log_prob2, log_prob3, all_log_prob, all_prob, \
kl_log_prob1, kl_log_prob2, kl_log_prob3, kl_all_prob, \
emotion_loss = model(textf, visuf, acouf, umask, qmask, lengths, emotion_labels=label if train else None)
lp_1 = log_prob1.view(-1, log_prob1.size()[2])
lp_2 = log_prob2.view(-1, log_prob2.size()[2])
lp_3 = log_prob3.view(-1, log_prob3.size()[2])
lp_all = all_log_prob.view(-1, all_log_prob.size()[2])
labels_ = label.view(-1)
kl_lp_1 = kl_log_prob1.view(-1, kl_log_prob1.size()[2])
kl_lp_2 = kl_log_prob2.view(-1, kl_log_prob2.size()[2])
kl_lp_3 = kl_log_prob3.view(-1, kl_log_prob3.size()[2])
kl_p_all = kl_all_prob.view(-1, kl_all_prob.size()[2])
loss = gamma_1 * loss_function(lp_all, labels_, umask) + \
gamma_2 * (loss_function(lp_1, labels_, umask) +
loss_function(lp_2, labels_, umask) +
loss_function(lp_3, labels_, umask)) + \
gamma_3 * (kl_loss(kl_lp_1, kl_p_all, umask) +
kl_loss(kl_lp_2, kl_p_all, umask) +
kl_loss(kl_lp_3, kl_p_all, umask))
if emotion_loss is not None:
loss += 0.1 * emotion_loss
emotion_losses.append(emotion_loss.item())
lp_ = all_prob.view(-1, all_prob.size()[2])
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()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
if preds:
preds = np.concatenate(preds)
labels = np.concatenate(labels)
masks = np.concatenate(masks)
else:
return float('nan'), float('nan'), [], [], [], float('nan'), float('nan')
avg_loss = round(np.sum(losses)/np.sum(masks), 4)
avg_emotion_loss = round(np.mean(emotion_losses), 4) if emotion_losses else 0
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, avg_emotion_loss
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--no-cuda', action='store_true', default=False, help='does not use GPU')
parser.add_argument('--lr', type=float, default=0.0001, metavar='LR', help='learning rate')
parser.add_argument('--l2', type=float, default=0.00001, metavar='L2', help='L2 regularization weight')
parser.add_argument('--dropout', type=float, default=0.5, metavar='dropout', help='dropout rate')
parser.add_argument('--batch-size', type=int, default=16, metavar='BS', help='batch size')
parser.add_argument('--hidden_dim', type=int, default=1024, metavar='hidden_dim', help='output hidden size')
parser.add_argument('--n_head', type=int, default=8, metavar='n_head', help='number of heads')
parser.add_argument('--epochs', type=int, default=150, metavar='E', help='number of epochs')
parser.add_argument('--temp', type=int, default=1, metavar='temp', help='temp')
parser.add_argument('--tensorboard', action='store_true', default=False, help='Enables tensorboard log')
parser.add_argument('--class-weight', action='store_true', default=True, help='use class weights')
parser.add_argument('--Dataset', default='IEMOCAP', help='dataset to train and test')
parser.add_argument('--emotion_dim', type=int, default=256, help='emotion embedding dimension')
parser.add_argument('--emotion_weight', type=float, default=0.1, help='weight for emotion loss')
args = parser.parse_args()
today = datetime.datetime.now()
print(args)
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
feat2dim = {'IS10':1582, 'denseface':342, 'MELD_audio':300}
D_audio = feat2dim['IS10'] if args.Dataset=='IEMOCAP' else feat2dim['MELD_audio']
D_visual = feat2dim['denseface']
D_text = 1024
D_m = D_audio + D_visual + D_text
n_speakers = 9 if args.Dataset=='MELD' else 2
n_classes = 7 if args.Dataset=='MELD' else 6 if args.Dataset=='IEMOCAP' else 1
print('temp {}'.format(args.temp))
model = EmotionAwareTransformerModel(
dataset=args.Dataset,
temp=args.temp,
D_text=D_text,
D_visual=D_visual,
D_audio=D_audio,
n_head=args.n_head,
n_classes=n_classes,
hidden_dim=args.hidden_dim,
n_speakers=n_speakers,
dropout=args.dropout,
emotion_dim=args.emotion_dim
)
total_params = sum(p.numel() for p in model.parameters())
print('total parameters: {}'.format(total_params))
total_trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
print('training parameters: {}'.format(total_trainable_params))
if cuda:
model.cuda()
kl_loss = MaskedKLDivLoss()
optimizer = optim.Adam(model.parameters(), lr=args.lr, weight_decay=args.l2)
if args.Dataset == 'MELD':
loss_function = MaskedNLLLoss()
train_loader, valid_loader, test_loader = get_MELD_loaders(valid=0.0,
batch_size=batch_size,
num_workers=0)
elif 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])
loss_function = MaskedNLLLoss(loss_weights.cuda() if cuda else loss_weights)
train_loader, valid_loader, test_loader = get_IEMOCAP_loaders(valid=0.0,
batch_size=batch_size,
num_workers=0)
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 = [], [], []
for e in range(n_epochs):
start_time = time.time()
train_loss, train_acc, _, _, _, train_fscore, train_emotion_loss = \
train_or_eval_model(model, loss_function, kl_loss, train_loader, e, optimizer, True)
valid_loss, valid_acc, _, _, _, valid_fscore, valid_emotion_loss = \
train_or_eval_model(model, loss_function, kl_loss, valid_loader, e)
test_loss, test_acc, test_label, test_pred, test_mask, test_fscore, test_emotion_loss = \
train_or_eval_model(model, loss_function, kl_loss, test_loader, e)
wandb.log({
'epoch': e + 1,
'train_loss': train_loss,
'train_acc': train_acc,
'train_fscore': train_fscore,
'train_emotion_loss': train_emotion_loss,
'valid_loss': valid_loss,
'valid_acc': valid_acc,
'valid_fscore': valid_fscore,
'test_loss': test_loss,
'test_acc': test_acc,
'test_fscore': test_fscore,
'test_emotion_loss': test_emotion_loss
})
if best_fscore == None or best_fscore < test_fscore:
best_fscore = test_fscore
best_label, best_pred, best_mask = test_label, test_pred, test_mask
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: {}, train_emotion_loss: {}, valid_loss: {}, valid_acc: {}, valid_fscore: {}, valid_emotion_loss: {}, test_loss: {}, test_acc: {}, test_fscore: {}, test_emotion_loss: {}, time: {} sec'.\
format(e+1, train_loss, train_acc, train_fscore, train_emotion_loss, valid_loss, valid_acc, valid_fscore, valid_emotion_loss, test_loss, test_acc, test_fscore, test_emotion_loss, round(time.time()-start_time, 2)))
if (e+1)%10 == 0:
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()
print('Test performance..')
print('F-Score: {}'.format(max(all_fscore))
print('F-Score-index: {}'.format(all_fscore.index(max(all_fscore)) + 1))
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))
wandb.finish()