-
Notifications
You must be signed in to change notification settings - Fork 0
/
mosfairseq.py
183 lines (153 loc) · 6.65 KB
/
mosfairseq.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
# ==============================================================================
# Copyright (c) 2021, Yamagishi Laboratory, National Institute of Informatics
# Author: Erica Cooper
# All rights reserved.
# ==============================================================================
import os
import argparse
import fairseq
import torch
import torchaudio
import torch.nn as nn
import torch.optim as optim
from torch.utils.data.dataset import Dataset
from torch.utils.data import DataLoader
import random
import time
random.seed(1984)
class MosPredictor(nn.Module):
def __init__(self, ssl_model, ssl_out_dim):
super(MosPredictor, self).__init__()
self.ssl_model = ssl_model
self.ssl_features = ssl_out_dim
self.output_layer = nn.Linear(self.ssl_features, 1)
def forward(self, wav):
wav = wav.squeeze(1) ## [batches, audio_len]
res = self.ssl_model(wav, mask=False, features_only=True)
x = res['x']
x = torch.mean(x, 1)
x = self.output_layer(x)
return x.squeeze(1)
class MyDataset(Dataset):
def __init__(self, wavdir, mos_list):
self.mos_lookup = { }
f = open(mos_list, 'r')
for line in f:
parts = line.strip().split(',')
wavname = parts[0]
mos = float(parts[1])
self.mos_lookup[wavname] = mos
self.wavdir = wavdir
self.wavnames = sorted(self.mos_lookup.keys())
def __getitem__(self, idx):
wavname = self.wavnames[idx]
wavpath = os.path.join(self.wavdir, wavname)
wav = torchaudio.load(wavpath)[0]
score = self.mos_lookup[wavname]
return wav, score, wavname
def __len__(self):
return len(self.wavnames)
def collate_fn(self, batch): ## zero padding
wavs, scores, wavnames = zip(*batch)
wavs = list(wavs)
max_len = max(wavs, key = lambda x : x.shape[1]).shape[1]
output_wavs = []
for wav in wavs:
amount_to_pad = max_len - wav.shape[1]
padded_wav = torch.nn.functional.pad(wav, (0, amount_to_pad), 'constant', 0)
output_wavs.append(padded_wav)
output_wavs = torch.stack(output_wavs, dim=0)
scores = torch.stack([torch.tensor(x) for x in list(scores)], dim=0)
return output_wavs, scores, wavnames
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--datadir', type=str, required=True, help='Path of your DATA/ directory')
parser.add_argument('--fairseq_base_model', type=str, required=True, help='Path to pretrained fairseq base model')
parser.add_argument('--finetune_from_checkpoint', type=str, required=False, help='Path to your checkpoint to finetune from')
parser.add_argument('--outdir', type=str, required=False, default='checkpoints', help='Output directory for your trained checkpoints')
args = parser.parse_args()
cp_path = args.fairseq_base_model
datadir = args.datadir
ckptdir = args.outdir
my_checkpoint = args.finetune_from_checkpoint
if not os.path.exists(ckptdir):
os.system('mkdir -p ' + ckptdir)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print('DEVICE: ' + str(device))
wavdir = os.path.join(datadir, 'directory_of_wav_files')
trainlist = os.path.join(datadir, 'sets/directory_of_mos_scores_for_training.txt')
validlist = os.path.join(datadir, 'sets/directory_of_mos_scores_for_validation.txt')
ssl_model_type = cp_path.split('/')[-1]
if ssl_model_type == 'wav2vec_small.pt':
SSL_OUT_DIM = 768
elif ssl_model_type in ['w2v_large_lv_fsh_swbd_cv.pt', 'xlsr_53_56k.pt']:
SSL_OUT_DIM = 1024
else:
print('*** ERROR *** SSL model type ' + ssl_model_type + ' not supported.')
exit()
model, cfg, task = fairseq.checkpoint_utils.load_model_ensemble_and_task([cp_path])
ssl_model = model[0]
ssl_model.remove_pretraining_modules()
trainset = MyDataset(wavdir, trainlist)
trainloader = DataLoader(trainset, batch_size=1, shuffle=True, num_workers=1, collate_fn=trainset.collate_fn)
validset = MyDataset(wavdir, validlist)
validloader = DataLoader(validset, batch_size=1, shuffle=True, num_workers=1, collate_fn=validset.collate_fn)
net = MosPredictor(ssl_model, SSL_OUT_DIM)
net = net.to(device)
if my_checkpoint != None: ## do (further) finetuning
net.load_state_dict(torch.load(my_checkpoint,map_location="cuda" if torch.cuda.is_available() else "cpu"))
criterion = nn.L1Loss()
optimizer = optim.SGD(net.parameters(), lr=0.0001, momentum=0.9)
PREV_VAL_LOSS=9999999999
orig_patience=7 #change this value if you would like a higher patience values
patience=orig_patience
start_time=time.time()
for epoch in range(1,1001):
STEPS=0
net.train()
running_loss = 0.0
for i, data in enumerate(trainloader, 0):
inputs, labels, filenames = data
# Adjust sample of input sequences
inputs = torchaudio.functional.resample(inputs,48000,16000)
inputs = inputs.to(device)
labels = labels.to(device)
optimizer.zero_grad()
outputs = net(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
STEPS += 1
running_loss += loss.item()
print('EPOCH: ' + str(epoch))
print('AVG EPOCH TRAIN LOSS: ' + str(running_loss / STEPS))
epoch_val_loss = 0.0
net.eval()
## validation
VALSTEPS=0
for i, data in enumerate(validloader, 0):
VALSTEPS+=1
inputs, labels, filenames = data
inputs = torchaudio.functional.resample(inputs,48000,16000)
inputs = inputs.to(device)
labels = labels.to(device)
outputs = net(inputs)
loss = criterion(outputs, labels)
epoch_val_loss += loss.item()
avg_val_loss=epoch_val_loss/VALSTEPS
print('EPOCH VAL LOSS: ' + str(avg_val_loss))
if avg_val_loss < PREV_VAL_LOSS:
print('Loss has decreased')
PREV_VAL_LOSS=avg_val_loss
PATH = os.path.join(ckptdir, 'ckpt_' + str(epoch))
torch.save(net.state_dict(), PATH)
patience = orig_patience
else:
patience-=1
if patience == 0:
print('loss has not decreased for ' + str(orig_patience) + ' epochs; early stopping at epoch ' + str(epoch))
break
print("Time elapsed:",round(time.time()-start_time,0),'secs',end='\n')
print('Finished Training')
if __name__ == '__main__':
main()