-
Notifications
You must be signed in to change notification settings - Fork 16
/
main.py
159 lines (140 loc) · 8.18 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
import os
import time
import torch
import numpy as np
from torch.utils.data import DataLoader
from src.cli import get_args
from src.datasets import get_dataset_iemocap, collate_fn, HCFDataLoader, get_dataset_mosei, collate_fn_hcf_mosei
# from src.models.e2e import MME2E
from src.models.sparse_e2e import MME2E_Sparse
from src.models.e2e import MME2E
from src.models.baselines.lf_rnn import LF_RNN
from src.models.baselines.lf_transformer import LF_Transformer
from src.trainers.emotiontrainer import IemocapTrainer
if __name__ == "__main__":
start = time.time()
args = get_args()
# Fix seed for reproducibility
seed = args['seed']
torch.manual_seed(seed)
np.random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
# Set device
os.environ["CUDA_VISIBLE_DEVICES"] = args['cuda']
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# device = torch.device(f"cuda:{args['cuda']}" if torch.cuda.is_available() else 'cpu')
# torch.cuda.set_device(int(args['cuda']))
print("Start loading the data....")
if args['dataset'] == 'iemocap':
train_dataset = get_dataset_iemocap(data_folder=args['datapath'], phase='train',
img_interval=args['img_interval'], hand_crafted_features=args['hand_crafted'])
valid_dataset = get_dataset_iemocap(data_folder=args['datapath'], phase='valid',
img_interval=args['img_interval'], hand_crafted_features=args['hand_crafted'])
test_dataset = get_dataset_iemocap(data_folder=args['datapath'], phase='test',
img_interval=args['img_interval'], hand_crafted_features=args['hand_crafted'])
if args['hand_crafted']:
train_loader = HCFDataLoader(dataset=train_dataset, feature_type=args['audio_feature_type'],
batch_size=args['batch_size'], shuffle=True, num_workers=2)
valid_loader = HCFDataLoader(dataset=valid_dataset, feature_type=args['audio_feature_type'],
batch_size=args['batch_size'], shuffle=False, num_workers=2)
test_loader = HCFDataLoader(dataset=test_dataset, feature_type=args['audio_feature_type'],
batch_size=args['batch_size'], shuffle=False, num_workers=2)
else:
train_loader = DataLoader(train_dataset, batch_size=args['batch_size'], shuffle=True,
num_workers=2, collate_fn=collate_fn)
valid_loader = DataLoader(valid_dataset, batch_size=args['batch_size'], shuffle=False,
num_workers=2, collate_fn=collate_fn)
test_loader = DataLoader(test_dataset, batch_size=args['batch_size'], shuffle=False,
num_workers=2, collate_fn=collate_fn)
elif args['dataset'] == 'mosei':
train_dataset = get_dataset_mosei(data_folder=args['datapath'], phase='train', img_interval=args['img_interval'], hand_crafted_features=args['hand_crafted'])
valid_dataset = get_dataset_mosei(data_folder=args['datapath'], phase='valid', img_interval=args['img_interval'], hand_crafted_features=args['hand_crafted'])
test_dataset = get_dataset_mosei(data_folder=args['datapath'], phase='test', img_interval=args['img_interval'], hand_crafted_features=args['hand_crafted'])
train_loader = DataLoader(train_dataset, batch_size=args['batch_size'], shuffle=True, num_workers=2, collate_fn=collate_fn_hcf_mosei if args['hand_crafted'] else collate_fn)
valid_loader = DataLoader(valid_dataset, batch_size=args['batch_size'], shuffle=False, num_workers=2, collate_fn=collate_fn_hcf_mosei if args['hand_crafted'] else collate_fn)
test_loader = DataLoader(test_dataset, batch_size=args['batch_size'], shuffle=False, num_workers=2, collate_fn=collate_fn_hcf_mosei if args['hand_crafted'] else collate_fn)
print(f'# Train samples = {len(train_loader.dataset)}')
print(f'# Valid samples = {len(valid_loader.dataset)}')
print(f'# Test samples = {len(test_loader.dataset)}')
dataloaders = {
'train': train_loader,
'valid': valid_loader,
'test': test_loader
}
lr = args['learning_rate']
if args['model'] == 'mme2e':
model = MME2E(args=args, device=device)
model = model.to(device=device)
# When using a pre-trained text modal, you can use text_lr_factor to give a smaller leraning rate to the textual model parts
if args['text_lr_factor'] == 1:
optimizer = torch.optim.Adam(model.parameters(), lr=args['learning_rate'], weight_decay=args['weight_decay'])
else:
optimizer = torch.optim.Adam([
{'params': model.T.parameters(), 'lr': lr / args['text_lr_factor']},
{'params': model.t_out.parameters(), 'lr': lr / args['text_lr_factor']},
{'params': model.V.parameters()},
{'params': model.v_flatten.parameters()},
{'params': model.v_transformer.parameters()},
{'params': model.v_out.parameters()},
{'params': model.A.parameters()},
{'params': model.a_flatten.parameters()},
{'params': model.a_transformer.parameters()},
{'params': model.a_out.parameters()},
{'params': model.weighted_fusion.parameters()},
], lr=lr, weight_decay=args['weight_decay'])
elif args['model'] == 'mme2e_sparse':
model = MME2E_Sparse(args=args, device=device)
model = model.to(device=device)
# When using a pre-trained text modal, you can use text_lr_factor to give a smaller leraning rate to the textual model parts
if args['text_lr_factor'] == 1:
optimizer = torch.optim.Adam(model.parameters(), lr=args['learning_rate'], weight_decay=args['weight_decay'])
else:
optimizer = torch.optim.Adam([
{'params': model.T.parameters(), 'lr': lr / args['text_lr_factor']},
{'params': model.t_out.parameters(), 'lr': lr / args['text_lr_factor']},
{'params': model.V.parameters()},
{'params': model.v_flatten.parameters()},
{'params': model.v_transformer.parameters()},
{'params': model.v_out.parameters()},
{'params': model.A.parameters()},
{'params': model.a_flatten.parameters()},
{'params': model.a_transformer.parameters()},
{'params': model.a_out.parameters()},
{'params': model.weighted_fusion.parameters()},
], lr=lr, weight_decay=args['weight_decay'])
elif args['model'] == 'lf_rnn':
model = LF_RNN(args)
model = model.to(device=device)
optimizer = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=args['weight_decay'])
elif args['model'] == 'lf_transformer':
model = LF_Transformer(args)
model = model.to(device=device)
optimizer = torch.optim.Adam(model.parameters(), lr=lr, weight_decay=args['weight_decay'])
else:
raise ValueError('Incorrect model name!')
if args['scheduler']:
scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=args['epochs'] * len(train_loader.dataset) // args['batch_size'])
else:
scheduler = None
if args['loss'] == 'l1':
criterion = torch.nn.L1Loss()
elif args['loss'] == 'mse':
criterion = torch.nn.MSELoss()
elif args['loss'] == 'ce':
criterion = torch.nn.CrossEntropyLoss()
elif args['loss'] == 'bce':
pos_weight = train_dataset.getPosWeight()
pos_weight = torch.tensor(pos_weight).to(device)
criterion = torch.nn.BCEWithLogitsLoss(pos_weight=pos_weight)
# criterion = torch.nn.BCEWithLogitsLoss()
if args['dataset'] == 'iemocap' or 'mosei':
trainer = IemocapTrainer(args, model, criterion, optimizer, scheduler, device, dataloaders)
if args['test']:
trainer.test()
elif args['valid']:
trainer.valid()
else:
trainer.train()
end = time.time()
print(f'Total time usage = {(end - start) / 3600:.2f} hours.')