-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner_m3bert.py
454 lines (392 loc) · 18.2 KB
/
runner_m3bert.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
# -*- coding: utf-8 -*- #
"""*********************************************************************************************"""
# FileName [ runner_m3bert.py ]
# Synopsis [ runner for the m3bert model ]
# Author [ Timothy D. Greer (timothydgreer) ]
# Copyright [ Copyleft(c), SAIL Lab, USC, USA ]
"""*********************************************************************************************"""
###############
# IMPORTATION #
###############
import yaml
import torch
import random
import argparse
import numpy as np
from utility.timer import Timer
import os
#############################
# M3BERT CONFIGURATIONS #
#############################
def get_m3bert_args():
parser = argparse.ArgumentParser(description='Argument Parser for the m3bert project.')
# setting
parser.add_argument('--config', default='config/m3bert.yaml', type=str, help='Path to experiment config.')
parser.add_argument('--seed', default=1337, type=int, help='Random seed for reproducable results.', required=False)
# Logging
parser.add_argument('--logdir', default='log/log_m3bert/', type=str, help='Logging path.', required=False)
parser.add_argument('--name', default=None, type=str, help='Name for logging.', required=False)
# model ckpt
parser.add_argument('--load', action='store_true', help='Load pre-trained model to restore training, no need to specify this during testing.')
parser.add_argument('--ckpdir', default='result/result_m3bert/', type=str, help='Checkpoint/Result path.', required=False)
parser.add_argument('--ckpt', default=None, type=str, help='path to m3bert model checkpoint.', required=False)
parser.add_argument('--dckpt', default='baseline_sentiment_libri_sd1337/baseline_sentiment-500000.ckpt', type=str, help='path to downstream checkpoint.', required=False)
# mockingjay
parser.add_argument('--train', action='store_true', help='Train the model.')
parser.add_argument('--run_m3bert', action='store_true', help='train and test the downstream tasks using m3bert representations.')
parser.add_argument('--fine_tune', action='store_true', help='fine tune the m3bert model with downstream task.')
parser.add_argument('--frozen', action='store_true', help='frozen parameters of the backbone in the training of the downstream task.')
parser.add_argument('--plot', action='store_true', help='Plot model generated results during testing.')
# genre task
parser.add_argument('--train_genre', action='store_true', help='Train the genre classifier on mel or m3bert representations.')
parser.add_argument('--test_genre', action='store_true', help='Test mel or m3bert representations using the trained genre classifier.')
# autotagging task
parser.add_argument('--train_autotagging', action='store_true', help='Train the autotagging classifier on mel or m3bert representations.')
parser.add_argument('--test_autotagging', action='store_true', help='Test mel or m3bert representations using the trained autotagging classifier.')
# extended ballroom task
parser.add_argument('--train_eb', action='store_true', help='Train the extended ballroom classifier on mel or m3bert representations.')
parser.add_argument('--test_eb', action='store_true', help='Test mel or m3bert representations using the trained extended ballroom classifier.')
# RWC task
parser.add_argument('--train_rwc', action='store_true', help='Train the RWC classifier on mel or m3bert representations.')
parser.add_argument('--test_rwc', action='store_true', help='Test mel or m3bert representations using the trained RWC classifier.')
# autotagging task
parser.add_argument('--train_deam', action='store_true', help='Train the DEAM classifier on mel or m3bert representations.')
parser.add_argument('--test_deam', action='store_true', help='Test mel or m3bert representations using the trained DEAM classifier.')
# MTL task
parser.add_argument('--train_mtl', action='store_true', help='Train the MTL classifier on mel or m3bert representations.')
parser.add_argument('--test_mtl', action='store_true', help='Test mel or m3bert representations using the trained MTL classifier.')
# phone task
parser.add_argument('--train_phone', action='store_true', help='Train the phone classifier on mel or m3bert representations.')
parser.add_argument('--test_phone', action='store_true', help='Test mel or m3bert representations using the trained phone classifier.')
# sentiment task
parser.add_argument('--train_sentiment', action='store_true', help='Train the sentiment classifier on mel or m3bert representations.')
parser.add_argument('--test_sentiment', action='store_true', help='Test mel or m3bert representations using the trained sentiment classifier.')
# speaker verification task
parser.add_argument('--train_speaker', action='store_true', help='Train the speaker classifier on mel or m3bert representations.')
parser.add_argument('--test_speaker', action='store_true', help='Test mel or m3bert representations using the trained speaker classifier.')
# Options
parser.add_argument('--with_head', action='store_true', help='inference with the spectrogram head, the model outputs spectrogram.')
parser.add_argument('--output_attention', action='store_true', help='plot attention')
parser.add_argument('--load_ws', default='result/result_m3bert_sentiment/10111754-10170300-weight_sum/best_val.ckpt', help='load weighted-sum weights from trained downstream model')
parser.add_argument('--cpu', action='store_true', help='Disable GPU training.')
parser.add_argument('--no-msg', action='store_true', help='Hide all messages.')
parser.add_argument('--mfcc', action='store_true',help='Train using mfccs')
parser.add_argument('--vgg', action='store_true',help='Train using VGGs')
args = parser.parse_args()
setattr(args,'gpu', not args.cpu)
setattr(args,'verbose', not args.no_msg)
config = yaml.load(open(args.config,'r'))
config['timer'] = Timer()
if args.logdir is not None:
if not os.path.exists(os.path.dirname('./'+args.logdir+'/'+args.config.split('/')[-1])):
os.makedirs(os.path.dirname('./'+args.logdir+'/'+args.config.split('/')[-1]))
with open('./'+args.logdir+'/'+args.config.split('/')[-1],'w') as myFile:
doc = yaml.dump(config,myFile)
return config, args
########
# MAIN #
########
def main():
# get arguments
config, args = get_m3bert_args()
# Fix seed and make backends deterministic
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if torch.cuda.is_available(): torch.cuda.manual_seed_all(args.seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
# Train M3BERT
if args.train:
from m3bert.solver import Trainer
trainer = Trainer(config, args)
trainer.load_data(split='train')
if args.ckpt is None:
trainer.set_model(inference=False)
else:
trainer.set_model(inference=False,from_path=args.ckpt)
trainer = torch.nn.DataParallel(trainer)
trainer.module.exec()
print("Done and saved in ",args.logdir)
##################################################################################
# Train Multi-Task Learning Task
elif args.train_mtl:
from downstream.mtl_solver import MTLDownstreamTrainer
task = 'm3bert_mtl' if args.run_m3bert \
else 'baseline_mtl'
if args.frozen:
task += '_frozen'
elif args.fine_tune:
task += '_fine_tune'
else:
task += '_scratch'
trainer = MTLDownstreamTrainer(config, args, task=task)
trainer.load_data(split='train', load='mtl')
trainer.set_model(inference=False)
trainer.exec()
# Test Multi-Task Learning Task
elif args.test_mtl:
from downstream.mtl_solver import MTLDownstreamTester
task = 'm3bert_mtl' if args.run_m3bert \
else 'baseline_mtl'
if args.frozen:
task += '_frozen'
elif args.fine_tune:
task += '_fine_tune'
else:
task += '_scratch'
tester = MTLDownstreamTester(config, args, task=task)
tester.load_data(split='test', load='mtl')
tester.set_model(inference=True)
tester.exec()
##################################################################################
# Train Genre (GTZAN) Task
elif args.train_genre:
from downstream.solver import Downstream_Trainer
task = 'm3bert_genre' if args.run_m3bert \
else 'baseline_genre'
if args.frozen:
task += '_frozen'
elif args.fine_tune:
task += '_fine_tune'
else:
task += '_scratch'
trainer = Downstream_Trainer(config, args, task=task)
trainer.load_data(split='train', load='genre')
trainer.set_model(inference=False)
trainer.exec()
# Test Genre (GTZAN) Task
elif args.test_genre:
from downstream.solver import Downstream_Tester
task = 'm3bert_genre' if args.run_m3bert \
else 'baseline_genre'
if args.frozen:
task += '_frozen'
elif args.fine_tune:
task += '_fine_tune'
else:
task += '_scratch'
#import ipdb
#ipdb.set_trace()
tester = Downstream_Tester(config, args, task=task)
tester.load_data(split='test', load='genre')
tester.set_model(inference=True)
tester.exec()
##################################################################################
# Train EB Task
elif args.train_eb:
from downstream.solver import Downstream_Trainer
task = 'm3bert_eb' if args.run_m3bert \
else 'baseline_eb'
if args.frozen:
task += '_frozen'
elif args.fine_tune:
task += '_fine_tune'
else:
task += '_scratch'
trainer = Downstream_Trainer(config, args, task=task)
trainer.load_data(split='train', load='eb')
trainer.set_model(inference=False)
trainer.exec()
# Test EB Task
elif args.test_eb:
from downstream.solver import Downstream_Tester
task = 'm3bert_eb' if args.run_m3bert \
else 'baseline_eb'
if args.frozen:
task += '_frozen'
elif args.fine_tune:
task += '_fine_tune'
else:
task += '_scratch'
tester = Downstream_Tester(config, args, task=task)
tester.load_data(split='test', load='eb')
tester.set_model(inference=True)
tester.exec()
##################################################################################
# Train RWC Task
elif args.train_rwc:
from downstream.solver import Downstream_Trainer
task = 'm3bert_rwc' if args.run_m3bert \
else 'baseline_rwc'
if args.frozen:
task += '_frozen'
elif args.fine_tune:
task += '_fine_tune'
else:
task += '_scratch'
trainer = Downstream_Trainer(config, args, task=task)
trainer.load_data(split='train', load='rwc')
trainer.set_model(inference=False)
trainer.exec()
# Test RWC Task
elif args.test_rwc:
from downstream.solver import Downstream_Tester
task = 'm3bert_rwc' if args.run_m3bert \
else 'baseline_rwc'
if args.frozen:
task += '_frozen'
elif args.fine_tune:
task += '_fine_tune'
else:
task += '_scratch'
tester = Downstream_Tester(config, args, task=task)
tester.load_data(split='test', load='rwc')
tester.set_model(inference=True)
tester.exec()
##################################################################################
# Train DEAM Task
elif args.train_deam:
from downstream.solver import Downstream_Trainer
task = 'm3bert_deam' if args.run_m3bert \
else 'baseline_deam'
if args.frozen:
task += '_frozen'
elif args.fine_tune:
task += '_fine_tune'
else:
task += '_scratch'
trainer = Downstream_Trainer(config, args, task=task)
trainer.load_data(split='train', load='deam')
trainer.set_model(inference=False)
trainer.exec()
# Test DEAM Task
elif args.test_deam:
from downstream.solver import Downstream_Tester
task = 'm3bert_deam' if args.run_m3bert \
else 'baseline_deam'
if args.frozen:
task += '_frozen'
elif args.fine_tune:
task += '_fine_tune'
else:
task += '_scratch'
tester = Downstream_Tester(config, args, task=task)
tester.load_data(split='test', load='deam')
tester.set_model(inference=True)
tester.exec()
##################################################################################
# Train Auto-Tagging Task
elif args.train_autotagging:
from downstream.solver import Downstream_Trainer
task = 'm3bert_autotagging' if args.run_m3bert \
else 'baseline_autotagging'
if args.frozen:
task += '_frozen'
elif args.fine_tune:
task += '_fine_tune'
else:
task += '_scratch'
trainer = Downstream_Trainer(config, args, task=task)
trainer.load_data(split='train', load='autotagging')
trainer.set_model(inference=False)
trainer.exec()
# Test Auto-Tagging Task
elif args.test_autotagging:
from downstream.solver import Downstream_Tester
task = 'm3bert_autotagging' if args.run_m3bert \
else 'baseline_autotagging'
if args.frozen:
task += '_frozen'
elif args.fine_tune:
task += '_fine_tune'
else:
task += '_scratch'
tester = Downstream_Tester(config, args, task=task)
tester.load_data(split='test', load='autotagging')
tester.set_model(inference=True)
tester.exec()
##################################################################################
# Train Phone Task
elif args.train_phone:
from downstream.solver import Downstream_Trainer
task = 'm3bert_phone' if args.run_m3bert \
else 'baseline_phone'
trainer = Downstream_Trainer(config, args, task=task)
trainer.load_data(split='train', load='phone')
trainer.set_model(inference=False)
trainer.exec()
# Test Phone Task
elif args.test_phone:
from downstream.solver import Downstream_Tester
task = 'm3bert_phone' if args.run_m3bert \
else 'baseline_phone'
tester = Downstream_Tester(config, args, task=task)
tester.load_data(split='test', load='phone')
tester.set_model(inference=True)
tester.exec()
##################################################################################
# Train Sentiment Task
elif args.train_sentiment:
from downstream.solver import Downstream_Trainer
task = 'm3bert_sentiment' if args.run_m3bert \
else 'baseline_sentiment'
trainer = Downstream_Trainer(config, args, task=task)
trainer.load_data(split='train', load='sentiment')
trainer.set_model(inference=False)
trainer.exec()
# Test Sentiment Task
elif args.test_sentiment:
from downstream.solver import Downstream_Tester
task = 'm3bert_sentiment' if args.run_m3bert \
else 'baseline_sentiment'
tester = Downstream_Tester(config, args, task=task)
tester.load_data(split='test', load='sentiment')
tester.set_model(inference=True)
tester.exec()
##################################################################################
# Train Speaker Task
elif args.train_speaker:
from downstream.solver import Downstream_Trainer
task = 'm3bert_speaker' if args.run_m3bert \
else 'baseline_speaker'
trainer = Downstream_Trainer(config, args, task=task)
trainer.load_data(split='train', load='speaker')
# trainer.load_data(split='train', load='speaker_large') # Deprecated
trainer.set_model(inference=False)
trainer.exec()
# Test Speaker Task
elif args.test_speaker:
from downstream.solver import Downstream_Tester
task = 'm3bert_speaker' if args.run_m3bert \
else 'baseline_speaker'
tester = Downstream_Tester(config, args, task=task)
tester.load_data(split='test', load='speaker')
# tester.load_data(split='test', load='speaker_large') # Deprecated
tester.set_model(inference=True)
tester.exec()
##################################################################################
# Visualize M3BERT
elif args.plot:
from m3bert.solver import Tester
tester = Tester(config, args)
tester.load_data(split='test', load_mel_only=True)
tester.set_model(inference=True, with_head=args.with_head, output_attention=args.output_attention)
tester.plot(with_head=args.with_head)
config['timer'].report()
########################
# GET M3BERT MODEL #
########################
def get_m3bert_model(from_path='result/result_m3bert/m3bert_libri_sd1337_best/m3bert-500000.ckpt', display_settings=False):
''' Wrapper that loads the m3bert model from checkpoint path '''
# load config and paras
all_states = torch.load(from_path, map_location='cpu')
config = all_states['Settings']['Config']
paras = all_states['Settings']['Paras']
# display checkpoint settings
if display_settings:
for cluster in config:
print(cluster + ':')
for item in config[cluster]:
print('\t' + str(item) + ': ', config[cluster][item])
print('paras:')
v_paras = vars(paras)
for item in v_paras:
print('\t' + str(item) + ': ', v_paras[item])
# load model with Tester
from m3bert.solver import Tester
m3bert = Tester(config, paras)
m3bert.set_model(inference=True, with_head=False, from_path=from_path)
return m3bert
if __name__ == '__main__':
main()