-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_EMBEDDING.py
229 lines (165 loc) · 7.51 KB
/
train_EMBEDDING.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
import torch
import torch.nn.functional as F
import wandb
from data.vocaset import *
from utils import *
from lstmDecoder import *
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':'Local'
}
"""Train of an MLP with landmark inputs, to try and learn an embedding space that contains audio information
At Test time we need a pretrained LSTM model pretrained on audio"""
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
model = MLP_emb(config.INPUT_DIM).to(device)
#m = torch.compile(model)
# Define the CTC loss function
mse_loss = nn.MSELoss()
# Define the optimizer
optimizer = optim.Adam(model.parameters(), lr=config.LR)
return model, mse_loss, optimizer,trainloader, valloader, vocabulary
# Function to train a model.
def train(model, mse_loss, optimizer,trainloader, vocabulary, config,valloader, modeltitle= "_embedding"):
#telling wand to watch
#if wandb.run is not None:
#cate = nn.CrossEntropyLoss()
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')
ct_l_s = []
csim_s = []
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]
audio_input = linear_interpolation(audio_input, 50, 60,output_len=len_landmark)
optimizer.zero_grad()
output = model(landmarks)
loss = mse_loss(output, audio_input)
loss.backward()
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)
# 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, mse_loss)
wandb.log({"val_loss":val_accuracy})
if epoch%5 == 0:
torch.save(model.state_dict(), "models/model"+str(modeltitle)+".pt")
return
def test(model, valloader, vocabulary, mse_loss):
model.eval()
real_sentences = []
pred_sentences = []
losses = []
bundle = torchaudio.pipelines.WAV2VEC2_ASR_BASE_960H
wav2vec = bundle.get_model().to(device)
wav2vec.eval()
sample_rate = 22000
model_audio = only_Decoder(hyper_a['INPUT_DIM'], hyper_a['HID_DIM'], hyper_a['NUM_LAYERS'], len(vocabulary)).to(device)
model_audio.load_state_dict(torch.load("./models/modeltest_24.pt"))
model_audio.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)
if sample_rate != bundle.sample_rate:
audio = torchaudio.functional.resample(audio, sample_rate, bundle.sample_rate)
audio_features, _ = wav2vec.extract_features(audio[0])
audio_input = audio_features[-1].clone()
audio_input = linear_interpolation(audio_input, 50, 60,output_len=len_landmark)
outputs = model(landmarks)
loss = mse_loss(outputs, audio_input)
losses.append(loss.item())
out_test = model_audio(outputs, len_landmark)
out_test = out_test.permute(1, 0, 2)
real_sentences, pred_sentences = write_results(len_label, label_list, out_test.detach(), valloader.batch_size, vocabulary, real_sentences, pred_sentences)
pred_sentences = list(map(lambda x:process_string(x),pred_sentences))
save_results(f"./results/validation.txt", real_sentences, pred_sentences, overwrite=True)
print(":>",np.mean(losses))
model.train()
return np.mean(losses)
model_pipeline()