-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
376 lines (331 loc) · 10.7 KB
/
main.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
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
# Copyright 2022 The Hume AI Authors. All Rights Reserved.
# Code available under a Creative Commons Attribution-Non Commercial-No Derivatives 4.0
# International Licence (CC BY-NC-ND) license.
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
import argparse
import csv
import time
import sys
from pathlib import Path
import random
import warnings
from sklearn.preprocessing import StandardScaler
from sklearn import preprocessing
import numpy as np
import pandas as pd
import torch
from torch import nn
from train import validation
from train import train
from dataloader import Dataloader
from utils import EarlyStopping, EvalMetrics, Processing, StorePredictions
# from utils import CCCLoss
from models import MLPReg, MLPClass
# from end2you.models.model_provider import get_model
warnings.filterwarnings("ignore")
if torch.cuda.is_available():
dev = torch.device("cuda:0")
else:
dev = torch.device("cpu")
csv.field_size_limit(sys.maxsize)
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--directory", help="working directory")
parser.add_argument("-f", "--features", help="feature type")
parser.add_argument("-t", "--task", help="['high','two','culture','type']")
parser.add_argument("-e", "--epochs", default=20, type=int, help="number of epochs")
parser.add_argument(
"-lr", "--learning_rate", default=0.001, type=float, help="learning rate"
)
parser.add_argument("-bs", "--batch_size", default=8, type=int, help="batch size")
parser.add_argument(
"-p", "--patience", default=5, type=int, help="early stopping patience"
)
parser.add_argument(
"--n_seeds",
type=int,
default=5,
choices=range(1, 25),
help="number of seeds to try (default: 5, max: 10).",
)
parser.add_argument(
"--verbose", action="store_true", help="Degree of verbosity, default low"
)
args = parser.parse_args()
feat_dict = {
"ComParE": [";", "infer", 2, 6373],
"eGeMAPS": [";", "infer", 2, 88],
"w2v2-R-emo-vad": [",", "infer", 1, 1027],
"w2v2-R-emo": [",", "infer", 1, 1024],
"w2v2-base": [",", None, 0, 768],
"w2v2-large": [",", None, 0, 1024],
"w2v2-lr": [",", None, 0, 1024],
"w2v2-lr-300": [",", None, 0, 1024],
"w2v2-lr-960": [",", None, 0, 1024],
"w2v2-xlsr": [",", None, 0, 1024],
}
feature_type = args.features
label_dict = {
"high": [
"high_info",
[
"Awe",
"Excitement",
"Amusement",
"Awkwardness",
"Fear",
"Horror",
"Distress",
"Triumph",
"Sadness",
"Surprise",
],
],
"two": ["two_info", ["Valence", "Arousal"]],
"culture": [
"culture_info",
[
"China_Awe",
"China_Excitement",
"China_Amusement",
"China_Awkwardness",
"China_Fear",
"China_Horror",
"China_Distress",
"China_Triumph",
"China_Sadness",
"United States_Awe",
"United States_Excitement",
"United States_Amusement",
"United States_Awkwardness",
"United States_Fear",
"United States_Horror",
"United States_Distress",
"United States_Triumph",
"United States_Sadness",
"South Africa_Awe",
"South Africa_Excitement",
"South Africa_Amusement",
"South Africa_Awkwardness",
"South Africa_Fear",
"South Africa_Horror",
"South Africa_Distress",
"South Africa_Triumph",
"South Africa_Sadness",
"Venezuela_Awe",
"Venezuela_Excitement",
"Venezuela_Amusement",
"Venezuela_Awkwardness",
"Venezuela_Fear",
"Venezuela_Horror",
"Venezuela_Distress",
"Venezuela_Triumph",
"Venezuela_Sadness",
"China_Surprise",
"United States_Surprise",
"South Africa_Surprise",
"Venezuela_Surprise",
],
],
"type": ["type_info", "Voc_Type"],
}
verbose = args.verbose
task = args.task
data_dir = args.directory
sep_type = feat_dict[feature_type][0]
header_type = feat_dict[feature_type][1]
columns_rm = feat_dict[feature_type][2]
feat_dimensions = feat_dict[feature_type][3]
labels = pd.read_csv(f"{args.directory}/labels/{label_dict[task][0]}.csv")
classes = label_dict[task][1]
if "/" in feature_type:
store_name = feature_type.replace("/", "")
else:
store_name = feature_type
X, y, val_filename_group, test_filename_group = Dataloader.create(
store_name, task, data_dir, feature_type, labels, classes, sep_type, columns_rm
)
if task == "type":
le = preprocessing.LabelEncoder()
y[0] = le.fit_transform(y[0])
y[1] = le.transform(y[1])
if verbose:
print(le.classes_)
scaler = StandardScaler()
X, y = Processing.normalise(scaler, X, y, task)
lr = args.learning_rate
bs = args.batch_size
num_epochs = args.epochs
inputs = torch.from_numpy(X[0].astype(np.float32)).to(dev)
val_inputs = torch.from_numpy(X[1].astype(np.float32)).to(dev)
seed_list = [101, 102, 103, 104, 105, 106, 107, 108, 109, 110,
111, 112, 113, 114, 115, 116, 117, 118, 119, 120]
# seed_list = [105, 106, 107, 108, 109]
# seed_list = [109]
seed_list = random.sample(seed_list, args.n_seeds)
score_list = []
timestamp = time.strftime("%d%m%Y-%H%M%S")
val_file_ids, test_file_ids = val_filename_group, test_filename_group
for seed in seed_list:
if task != "type":
model = MLPReg(feat_dimensions, len(classes)).to(dev)
else:
model = MLPClass(feat_dimensions, len(classes)).to(dev)
es = EarlyStopping(patience=args.patience, verbose=False, delta=0.01)
optimizer = torch.optim.AdamW(model.parameters(), lr=lr, weight_decay=0.01)
torch.manual_seed(seed)
print(
f"===== \nTask: A-VB {task.capitalize()} | Seed: {seed} | lr: {lr} | bs: {bs} | Max Epochs: {num_epochs}"
)
lmse, lclass = nn.MSELoss(), nn.CrossEntropyLoss()
lclass = nn.CrossEntropyLoss()
loss_res, val_loss_res, val_result, val_loss = [], [], [], 0
for epoch in range(num_epochs):
train_permutation, val_permutation = torch.randperm(
inputs.size()[0]
), torch.randperm(val_inputs.size()[0])
score, loss = train(
X[0],
optimizer,
lmse,
lclass,
model,
inputs,
train_permutation,
y[0],
bs,
classes,
task,
dev,
)
loss_res.append(loss.item())
val_loss = validation(lmse, lclass, model, val_inputs, y[1], classes, task, dev)
if task != "type":
if verbose:
print(
f"{epoch+1}/{num_epochs}\tEmoCCC: {np.round(np.mean(score),3)}\tTrainLoss: {np.round(loss.item(),3)} \tValLoss: {np.round(val_loss, 3)}"
)
else:
if verbose:
print(
f"{epoch+1}/{num_epochs}\tUAR: {np.round(np.mean(score),3)}\tTrainLoss: {np.round(loss.item(),3)} \tValLoss: {np.round(val_loss, 3)}"
)
val_loss_res.append(val_loss)
es(val_loss, model)
if es.early_stop:
print(f"Early stopping at epoch {epoch}")
break
val_pred = model(torch.from_numpy(X[1].astype(np.float32)).to(dev))
for index, i in enumerate(val_pred):
val_pred[index] = val_pred[index].cpu()
if task != "type":
for j in classes:
identifier = classes.index(j)
ccc_val = EvalMetrics.CCC(
y[1].iloc[:, identifier],
val_pred[:, identifier].cpu().flatten().detach().numpy(),
)
if verbose:
print(f"Val CCC \t {j.capitalize()}: \t {ccc_val}", flush=True)
val_result.append(ccc_val)
score_list.append(np.mean(val_result))
print(
f"------\nEmotion Mean CCC: {np.round(np.mean(val_result),4)}\nSTD: {np.round(np.std(val_result),4)}"
)
else:
pred = torch.max(val_pred, 1)
uar = EvalMetrics.UAR(y[1], pred.indices.cpu())
print(f"UAR: {np.round(uar,4)}")
score_list.append(uar)
v_pred = le.inverse_transform(pred.indices.cpu())
val_dict_info = {"File_ID": list(val_file_ids.values), "Voc_Type": v_pred}
# val_prediction_csv = pd.DataFrame.from_dict(val_dict_info).sort_values(
# by="File_ID"
# )
# val_prediction_csv.to_csv(
# f"preds/Validation_A-VB_{timestamp}_{task}_{seed}_{store_name}_lr-{args.learning_rate}_bs-{args.batch_size}.csv",
# index=False,
# )
torch.save(
model.state_dict(), f"tmp/{timestamp}_{store_name}_model_{seed}_{task}.pth"
)
max_score = max(score_list)
index_score = score_list.index(max_score)
seed_score = seed_list[index_score]
print("------")
print(f"Task: A-VB-{task.capitalize()}")
print(f"Max across seeds {np.round(max_score,4)}, seed {seed_score}")
print(f"Mean across seeds {np.round(np.mean(score_list),4)}")
print(f"STD across seeds {np.round(np.std(score_list),4)}")
print("------")
if not Path("preds/").is_file():
Path("preds/").mkdir(exist_ok=True)
if not Path("results/").is_file():
Path("results/").mkdir(exist_ok=True)
dict_results = {
"timestamp": timestamp,
"set": "validation",
"feature_type": feature_type,
"learning_rate": args.learning_rate,
"batch_size": args.batch_size,
"max_score": max_score,
"std_score": np.std(score_list),
"seed": seed_score,
"n_seeds": args.n_seeds,
"task": task,
}
results_csv = pd.DataFrame([dict_results])
results_csv.to_csv(
f"results/Validation_{timestamp}_{store_name}_{args.learning_rate}_{args.batch_size}_results.csv",
index=False,
)
print("Test predictions saved")
if task == "high":
StorePredictions.storehigh(
task,
feat_dimensions,
classes,
dev,
timestamp,
store_name,
seed_score,
X,
test_file_ids,
)
if task == "two":
StorePredictions.storetwo(
task,
feat_dimensions,
classes,
dev,
timestamp,
store_name,
seed_score,
X,
test_file_ids,
)
if task == "culture":
StorePredictions.storeculture(
task,
feat_dimensions,
classes,
dev,
timestamp,
store_name,
seed_score,
X,
test_file_ids,
)
if task == "type":
StorePredictions.storetype(
task,
feat_dimensions,
classes,
dev,
timestamp,
store_name,
seed_score,
X,
test_file_ids,
le,
)