-
Notifications
You must be signed in to change notification settings - Fork 23
/
traintest_cavmae.py
222 lines (182 loc) · 10.6 KB
/
traintest_cavmae.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
# -*- coding: utf-8 -*-
# @Time : 6/10/21 11:00 PM
# @Author : Yuan Gong
# @Affiliation : Massachusetts Institute of Technology
# @Email : [email protected]
# @File : traintest.py
# not rely on supervised feature
import sys
import os
import datetime
sys.path.append(os.path.dirname(os.path.dirname(sys.path[0])))
from utilities import *
import time
import torch
from torch import nn
import numpy as np
import pickle
from torch.cuda.amp import autocast,GradScaler
def train(audio_model, train_loader, test_loader, args):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print('running on ' + str(device))
torch.set_grad_enabled(True)
batch_time, per_sample_time, data_time, per_sample_data_time, per_sample_dnn_time = AverageMeter(), AverageMeter(), AverageMeter(), AverageMeter(), AverageMeter()
loss_av_meter, loss_a_meter, loss_v_meter, loss_c_meter = AverageMeter(), AverageMeter(), AverageMeter(), AverageMeter()
progress = []
best_epoch, best_loss = 0, np.inf
global_step, epoch = 0, 0
start_time = time.time()
exp_dir = args.exp_dir
def _save_progress():
progress.append([epoch, global_step, best_epoch, best_loss,
time.time() - start_time])
with open("%s/progress.pkl" % exp_dir, "wb") as f:
pickle.dump(progress, f)
if not isinstance(audio_model, nn.DataParallel):
audio_model = nn.DataParallel(audio_model)
audio_model = audio_model.to(device)
trainables = [p for p in audio_model.parameters() if p.requires_grad]
print('Total parameter number is : {:.3f} million'.format(sum(p.numel() for p in audio_model.parameters()) / 1e6))
print('Total trainable parameter number is : {:.3f} million'.format(sum(p.numel() for p in trainables) / 1e6))
optimizer = torch.optim.Adam(trainables, args.lr, weight_decay=5e-7, betas=(0.95, 0.999))
# use adapt learning rate scheduler, for preliminary experiments only, should not use for formal experiments
if args.lr_adapt == True:
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode='max', factor=0.5, patience=args.lr_patience, verbose=True)
print('Override to use adaptive learning rate scheduler.')
else:
scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, list(range(args.lrscheduler_start, 1000, args.lrscheduler_step)),gamma=args.lrscheduler_decay)
print('The learning rate scheduler starts at {:d} epoch with decay rate of {:.3f} every {:d} epoches'.format(args.lrscheduler_start, args.lrscheduler_decay, args.lrscheduler_step))
print('now training with {:s}, learning rate scheduler: {:s}'.format(str(args.dataset), str(scheduler)))
# #optional, save epoch 0 untrained model, for ablation study on model initialization purpose
# torch.save(audio_model.state_dict(), "%s/models/audio_model.%d.pth" % (exp_dir, epoch))
epoch += 1
scaler = GradScaler()
print("current #steps=%s, #epochs=%s" % (global_step, epoch))
print("start training...")
result = np.zeros([args.n_epochs, 10]) # for each epoch, 10 metrics to record
audio_model.train()
while epoch < args.n_epochs + 1:
begin_time = time.time()
end_time = time.time()
audio_model.train()
print('---------------')
print(datetime.datetime.now())
print("current #epochs=%s, #steps=%s" % (epoch, global_step))
print('current masking ratio is {:.3f} for both modalities; audio mask mode {:s}'.format(args.masking_ratio, args.mask_mode))
for i, (a_input, v_input, _) in enumerate(train_loader):
B = a_input.size(0)
a_input = a_input.to(device, non_blocking=True)
v_input = v_input.to(device, non_blocking=True)
data_time.update(time.time() - end_time)
per_sample_data_time.update((time.time() - end_time) / a_input.shape[0])
dnn_start_time = time.time()
with autocast():
loss, loss_mae, loss_mae_a, loss_mae_v, loss_c, mask_a, mask_v, c_acc = audio_model(a_input, v_input, args.masking_ratio, args.masking_ratio, mae_loss_weight=args.mae_loss_weight, contrast_loss_weight=args.contrast_loss_weight, mask_mode=args.mask_mode)
# this is due to for torch.nn.DataParallel, the output loss of 4 gpus won't be automatically averaged, need to be done manually
loss, loss_mae, loss_mae_a, loss_mae_v, loss_c, c_acc = loss.sum(), loss_mae.sum(), loss_mae_a.sum(), loss_mae_v.sum(), loss_c.sum(), c_acc.mean()
optimizer.zero_grad()
scaler.scale(loss).backward()
scaler.step(optimizer)
scaler.update()
# loss_av is the main loss
loss_av_meter.update(loss.item(), B)
loss_a_meter.update(loss_mae_a.item(), B)
loss_v_meter.update(loss_mae_v.item(), B)
loss_c_meter.update(loss_c.item(), B)
batch_time.update(time.time() - end_time)
per_sample_time.update((time.time() - end_time)/a_input.shape[0])
per_sample_dnn_time.update((time.time() - dnn_start_time)/a_input.shape[0])
print_step = global_step % args.n_print_steps == 0
early_print_step = epoch == 0 and global_step % (args.n_print_steps/10) == 0
print_step = print_step or early_print_step
if print_step and global_step != 0:
print('Epoch: [{0}][{1}/{2}]\t'
'Per Sample Total Time {per_sample_time.avg:.5f}\t'
'Per Sample Data Time {per_sample_data_time.avg:.5f}\t'
'Per Sample DNN Time {per_sample_dnn_time.avg:.5f}\t'
'Train Total Loss {loss_av_meter.val:.4f}\t'
'Train MAE Loss Audio {loss_a_meter.val:.4f}\t'
'Train MAE Loss Visual {loss_v_meter.val:.4f}\t'
'Train Contrastive Loss {loss_c_meter.val:.4f}\t'
'Train Contrastive Acc {c_acc:.3f}\t'.format(
epoch, i, len(train_loader), per_sample_time=per_sample_time, per_sample_data_time=per_sample_data_time,
per_sample_dnn_time=per_sample_dnn_time, loss_av_meter=loss_av_meter, loss_a_meter=loss_a_meter, loss_v_meter=loss_v_meter, loss_c_meter=loss_c_meter, c_acc=c_acc), flush=True)
if np.isnan(loss_av_meter.avg):
print("training diverged...")
return
end_time = time.time()
global_step += 1
print('start validation')
eval_loss_av, eval_loss_mae, eval_loss_mae_a, eval_loss_mae_v, eval_loss_c, eval_c_acc = validate(audio_model, test_loader, args)
print("Eval Audio MAE Loss: {:.6f}".format(eval_loss_mae_a))
print("Eval Visual MAE Loss: {:.6f}".format(eval_loss_mae_v))
print("Eval Total MAE Loss: {:.6f}".format(eval_loss_mae))
print("Eval Contrastive Loss: {:.6f}".format(eval_loss_c))
print("Eval Total Loss: {:.6f}".format(eval_loss_av))
print("Eval Contrastive Accuracy: {:.6f}".format(eval_c_acc))
print("Train Audio MAE Loss: {:.6f}".format(loss_a_meter.avg))
print("Train Visual MAE Loss: {:.6f}".format(loss_v_meter.avg))
print("Train Contrastive Loss: {:.6f}".format(loss_c_meter.avg))
print("Train Total Loss: {:.6f}".format(loss_av_meter.avg))
# train audio mae loss, train visual mae loss, train contrastive loss, train total loss
# eval audio mae loss, eval visual mae loss, eval contrastive loss, eval total loss, eval contrastive accuracy, lr
result[epoch-1, :] = [loss_a_meter.avg, loss_v_meter.avg, loss_c_meter.avg, loss_av_meter.avg, eval_loss_mae_a, eval_loss_mae_v, eval_loss_c, eval_loss_av, eval_c_acc, optimizer.param_groups[0]['lr']]
np.savetxt(exp_dir + '/result.csv', result, delimiter=',')
print('validation finished')
if eval_loss_av < best_loss:
best_loss = eval_loss_av
best_epoch = epoch
if best_epoch == epoch:
torch.save(audio_model.state_dict(), "%s/models/best_audio_model.pth" % (exp_dir))
torch.save(optimizer.state_dict(), "%s/models/best_optim_state.pth" % (exp_dir))
if args.save_model == True:
torch.save(audio_model.state_dict(), "%s/models/audio_model.%d.pth" % (exp_dir, epoch))
if isinstance(scheduler, torch.optim.lr_scheduler.ReduceLROnPlateau):
scheduler.step(-eval_loss_av)
else:
scheduler.step()
print('Epoch-{0} lr: {1}'.format(epoch, optimizer.param_groups[0]['lr']))
_save_progress()
finish_time = time.time()
print('epoch {:d} training time: {:.3f}'.format(epoch, finish_time-begin_time))
epoch += 1
batch_time.reset()
per_sample_time.reset()
data_time.reset()
per_sample_data_time.reset()
per_sample_dnn_time.reset()
loss_av_meter.reset()
loss_a_meter.reset()
loss_v_meter.reset()
loss_c_meter.reset()
def validate(audio_model, val_loader, args):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
batch_time = AverageMeter()
if not isinstance(audio_model, nn.DataParallel):
audio_model = nn.DataParallel(audio_model)
audio_model = audio_model.to(device)
audio_model.eval()
end = time.time()
A_loss, A_loss_mae, A_loss_mae_a, A_loss_mae_v, A_loss_c, A_c_acc = [], [], [], [], [], []
with torch.no_grad():
for i, (a_input, v_input, _) in enumerate(val_loader):
a_input = a_input.to(device)
v_input = v_input.to(device)
with autocast():
loss, loss_mae, loss_mae_a, loss_mae_v, loss_c, mask_a, mask_v, c_acc = audio_model(a_input, v_input, args.masking_ratio, args.masking_ratio, mae_loss_weight=args.mae_loss_weight, contrast_loss_weight=args.contrast_loss_weight, mask_mode=args.mask_mode)
loss, loss_mae, loss_mae_a, loss_mae_v, loss_c, c_acc = loss.sum(), loss_mae.sum(), loss_mae_a.sum(), loss_mae_v.sum(), loss_c.sum(), c_acc.mean()
A_loss.append(loss.to('cpu').detach())
A_loss_mae.append(loss_mae.to('cpu').detach())
A_loss_mae_a.append(loss_mae_a.to('cpu').detach())
A_loss_mae_v.append(loss_mae_v.to('cpu').detach())
A_loss_c.append(loss_c.to('cpu').detach())
A_c_acc.append(c_acc.to('cpu').detach())
batch_time.update(time.time() - end)
end = time.time()
loss = np.mean(A_loss)
loss_mae = np.mean(A_loss_mae)
loss_mae_a = np.mean(A_loss_mae_a)
loss_mae_v = np.mean(A_loss_mae_v)
loss_c = np.mean(A_loss_c)
c_acc = np.mean(A_c_acc)
return loss, loss_mae, loss_mae_a, loss_mae_v, loss_c, c_acc