-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_TRANSFORMER_DEC.py
322 lines (233 loc) · 11.8 KB
/
train_TRANSFORMER_DEC.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
import torch
import torch.nn.functional as F
import wandb
from data.vocaset import *
from utils import *
from transformers import *
import lstmDecoder
import tqdm
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
hyper = {
'LANDMARK_DIM' : 68,
'INPUT_DIM' : 68*3,
'HID_DIM' : 128,
'BATCH_SIZE': 1,
'EPOCHS': 5000,
'NUM_LAYERS': 2,
'LR': 3e-4,
'SERVER':'windu',
'WEIGHT_SIMILARITY': 1,
'WEIGHT_CTCLOSS': 1
}
hyper_a = {
'LANDMARK_DIM' : 768,
'INPUT_DIM' : 768*1,
'HID_DIM' : 128,
'BATCH_SIZE': 1,
'EPOCHS': 5000,
'NUM_LAYERS': 2,
'LR': 3e-4,
'SERVER':'W'
}
"""Train the transformers decoder variants. From here you will get the weights of an MLP
from the train_MLP pipeline, that can e used to train an LSTM in train_LSTM"""
def model_pipeline():
with wandb.init(project="AudioLand-3D", config=hyper, mode="disabled"):
#access all HPs through wandb.config, so logging matches executing
config = wandb.config
#make the model, data and optimization problem
model, ctc_loss, optimizer,trainloader, valloader, vocabulary = create(config)
#train the model
mean_loss = train(model, ctc_loss, optimizer,trainloader, vocabulary,config,valloader )
#test the model
#print("Accuracy test: ",test(model, valloader, vocabulary))
return model
def create(config):
#get dataloader
#trainset = vocadataset("train", landmark=True)
trainset = vocadataset("train", landmark=True, onlyAudio=True)
#valset = vocadataset("val", landmark=True)
valset = vocadataset("val", landmark=True, onlyAudio=True)
trainloader = DataLoader(trainset, batch_size=config.BATCH_SIZE, collate_fn=collate_fn, num_workers=8, shuffle=True, pin_memory=True)
valloader = DataLoader(valset, batch_size=config.BATCH_SIZE, collate_fn=collate_fn, num_workers=8, pin_memory=True)
#define the vocabulary
vocabulary = create_vocabulary(blank='@')
# define the models
#IF YOU WANT TO TRAIN THE TRANSFORMER WITH ONLY AN MLP HEAD
#model = Transformer_Decoder(len(vocabulary)).to(device)
#IF YOU WANT TO TRAIN THE TRANSFORMER WITH LSTM HEAD
model = Transformer_Decoder_LSTM(len(vocabulary)).to(device)
# Define the CTC loss function
ctc_loss = nn.CTCLoss()
# Define the optimizer
optimizer = optim.Adam(model.parameters(), lr=config.LR)
return model, ctc_loss, optimizer,trainloader, valloader, vocabulary
def train_MLP(model):
model_emb = lstmDecoder.MLP_emb().to(device)
optimizer = optim.Adam(model_emb.parameters(), lr=3e-2)
mse_loss = nn.MSELoss()
trainset = vocadataset("train", landmark=True, onlyAudio=True)
#valset = vocadataset("val", landmark=True)
valset = vocadataset("val", landmark=True, onlyAudio=True)
trainloader = DataLoader(trainset, batch_size=1, collate_fn=collate_fn, num_workers=8, shuffle=True, pin_memory=True)
valloader = DataLoader(valset, batch_size=1, collate_fn=collate_fn, num_workers=8, pin_memory=True)
#Load wav2vec2
bundle = torchaudio.pipelines.WAV2VEC2_ASR_BASE_960H
wav2vec = bundle.get_model().to(device)
sample_rate = 22000
wav2vec.eval()
def train_emb(model, model_emb, mse_loss, optimizer,trainloader,valloader, epochs, modeltitle="exp"):
model.train()
# Training loop
for epoch in range(epochs):
losses = []
progress_bar = tqdm.tqdm(total=len(trainloader), unit='step')
for landmarks, len_landmark, _, _, audio in trainloader:
# reshape the batch from [batch_size, frame_size, num_landmark, 3] to [batch_size, frame_size, num_landmark * 3]
landmarks = torch.reshape(landmarks, (landmarks.shape[0], landmarks.shape[1], landmarks.shape[2]*landmarks.shape[3]))
# move data to GPU!
landmarks = landmarks.to(device)
len_landmark = len_landmark.to(device)
audio = audio.to(device)
if sample_rate != bundle.sample_rate:
audio = torchaudio.functional.resample(audio, sample_rate, bundle.sample_rate)
with torch.inference_mode():
audio_features, _ = wav2vec.extract_features(audio[0])
audio_input = audio_features[-1].clone().requires_grad_()
#len_audio[0] = audio_input.shape[1]
optimizer.zero_grad()
output = model_emb(landmarks)
with torch.no_grad():
_, features = model(landmarks,len_landmark, audio_input)
loss = mse_loss(output, features)
loss.backward()
optimizer.step()
losses.append(loss.item())
#progress bar stuff
progress_bar.set_description(f"emb:Epoch {epoch+1}/{epochs}")
#progress_bar.set_postfix(loss=loss.item()) # Update the loss value
progress_bar.set_postfix(loss=np.mean(losses)) # Update the loss value
progress_bar.update(1)
torch.save(model_emb.state_dict(), "models/model_emb"+str(modeltitle)+".pt")
def test_emb(model, model_emb, valloader, mse_loss):
losses = []
with torch.no_grad():
for landmarks, len_landmark, _, _, audio in valloader:
# reshape the batch from [batch_size, frame_size, num_landmark, 3] to [batch_size, frame_size, num_landmark * 3]
landmarks = torch.reshape(landmarks, (landmarks.shape[0], landmarks.shape[1], landmarks.shape[2]*landmarks.shape[3]))
# move data to GPU!
landmarks = landmarks.to(device)
len_landmark = len_landmark.to(device)
audio = audio.to(device)
audio_features, _ = wav2vec.extract_features(audio[0])
audio_input = audio_features[-1].clone()
output = model_emb(landmarks)
_, features = model(landmarks,len_landmark, audio_input)
# scrittura nel file del outuput e della frase originale
loss = mse_loss(output, features)
losses.append(loss.item())
print("validation emb:",np.mean(losses))
train_emb(model, model_emb, mse_loss, optimizer,trainloader,valloader, 5)
test_emb(model, model_emb, valloader, mse_loss)
# Function to train a model.
def train(model, ctc_loss, optimizer,trainloader, vocabulary, config,valloader, modeltitle= "_transformer_decoder"):
#telling wand to watch
#if wandb.run is not None:
wandb.watch(model, optimizer, log="all", log_freq=320)
#Load wav2vec2
bundle = torchaudio.pipelines.WAV2VEC2_ASR_BASE_960H
wav2vec = bundle.get_model().to(device)
sample_rate = 22000
model.train()
# Training loop
for epoch in range(config.EPOCHS):
#list to save sentences
real_sentences = []
pred_sentences = []
losses = []
progress_bar = tqdm.tqdm(total=len(trainloader), unit='step')
for landmarks, len_landmark, label, len_label, audio in trainloader:
# reshape the batch from [batch_size, frame_size, num_landmark, 3] to [batch_size, frame_size, num_landmark * 3]
landmarks = torch.reshape(landmarks, (landmarks.shape[0], landmarks.shape[1], landmarks.shape[2]*landmarks.shape[3]))
#variable to recover later the target sequences
label_list = label
# label char to index
label = char_to_index_batch(label, vocabulary)
# move data to GPU!
landmarks = landmarks.to(device)
len_landmark = len_landmark.to(device)
label = label.to(device)
len_label = len_label.to(device)
audio = audio.to(device)
if sample_rate != bundle.sample_rate:
audio = torchaudio.functional.resample(audio, sample_rate, bundle.sample_rate)
with torch.inference_mode():
audio_features, _ = wav2vec.extract_features(audio[0])
audio_input = audio_features[-1].clone().requires_grad_()
#len_audio[0] = audio_input.shape[1]
optimizer.zero_grad()
output, _ = model(landmarks,len_landmark, audio_input)
output = output.permute(1, 0, 2)#had to permute for the ctc loss. it acceprs [seq_len, batch_size, "num_class"]
loss = ctc_loss(torch.nn.functional.log_softmax(output, dim=2), label, len_landmark, len_label)
loss.backward()
torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
optimizer.step()
losses.append(loss.item())
#progress bar stuff
progress_bar.set_description(f"Epoch {epoch+1}/{config.EPOCHS}")
#progress_bar.set_postfix(loss=loss.item()) # Update the loss value
progress_bar.set_postfix(loss=np.mean(losses)) # Update the loss value
progress_bar.update(1)
if epoch%1 == 0:
real_sentences, pred_sentences = write_results(len_label, label_list, output.detach(), trainloader.batch_size, vocabulary, real_sentences, pred_sentences)
# endfor batch
#if wandb.run is not None:
wandb.log({"epoch":epoch, "loss":np.mean(losses)})
# save the model
if epoch%1 == 0:
val_accuracy = test(model, valloader, vocabulary, ctc_loss)
wandb.log({"val_loss":val_accuracy})
if epoch%5 == 0:
torch.save(model.state_dict(), "models/model"+str(modeltitle)+".pt")
if epoch%1 == 0:
save_results(f"./results/results_{epoch}.txt", real_sentences, pred_sentences, overwrite=True)
"""if epoch % 5 == 0:
train_MLP(model)"""
return
def test(model, valloader, vocabulary, ctc_loss):
model.eval()
real_sentences = []
pred_sentences = []
losses = []
bundle = torchaudio.pipelines.WAV2VEC2_ASR_BASE_960H
wav2vec = bundle.get_model().to(device)
sample_rate = 22000
wav2vec.eval()
with torch.no_grad():
for landmarks, len_landmark, label, len_label, audio in valloader:
# reshape the batch from [batch_size, frame_size, num_landmark, 3] to [batch_size, frame_size, num_landmark * 3]
landmarks = torch.reshape(landmarks, (landmarks.shape[0], landmarks.shape[1], landmarks.shape[2]*landmarks.shape[3]))
#variable to recover later the target sequences
label_list = label
# label char to index
label = char_to_index_batch(label, vocabulary)
# move data to GPU!
landmarks = landmarks.to(device)
len_landmark = len_landmark.to(device)
label = label.to(device)
len_label = len_label.to(device)
audio = audio.to(device)
audio_features, _ = wav2vec.extract_features(audio[0])
audio_input = audio_features[-1].clone()
output, _ = model(landmarks,len_landmark, audio_input)
output = output.permute(1, 0, 2)
# scrittura nel file del outuput e della frase originale
loss = ctc_loss(torch.nn.functional.log_softmax(output, dim=2), label, len_landmark, len_label)
losses.append(loss.item())
real_sentences, pred_sentences = write_results(len_label, label_list, output.detach(), valloader.batch_size, vocabulary, real_sentences, pred_sentences)
print(":>",np.mean(losses))
pred_sentences = list(map(lambda x:process_string(x),pred_sentences))
save_results(f"./results/validation.txt", real_sentences, pred_sentences, overwrite=True)
model.train()
return np.mean(losses)
model_pipeline()