-
Notifications
You must be signed in to change notification settings - Fork 6
/
trainer_contrastive.py
269 lines (225 loc) · 11.8 KB
/
trainer_contrastive.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
"""
This file regroups the procedures for training the neural networks.
A training uses a configuration json file (e.g. configs/dual_ae_c.json).
"""
from pathlib import Path
from itertools import chain
import torch
from torch.utils import data
from torch import nn, optim
import math
#from torchvision.utils import save_image
try:
from torch.utils.tensorboard import SummaryWriter
except:
pass
import json
from data_loader import InMemoryDataset
from models import *
from utils import kullback_leibler, contrastive_loss
class Trainer():
def __init__(self, params):
self.params = params
self.audio_encoder = None
self.tag_encoder = None
self.train_dataset_file = params['train_dataset_file']
self.validation_dataset_file = params['validation_dataset_file']
self.contrastive_temperature = params['contrastive_temperature']
self.epochs = params['epochs']
self.batch_size = params['batch_size']
self.learning_rate = params['learning_rate']
self.device = torch.device(params['device'])
self.experiment_name = params['experiment_name']
self.log_interval = params['log_interval']
self.save_model_every = params['save_model_every']
self.max_num_tags = 10 # This may be increased and used in data_loader.py
# This can be set here, or maybe automaticaly read from embedding_matrix_128.npy
# don't forget to add +1 to the size of voc for adding idx 0 for no tag.
self.w2v_emb_file = 'embedding_matrix_128.npy'
w2v_embeddings = np.load(self.w2v_emb_file)
# We add one (+1) so we also consider no tag
self.size_voc = w2v_embeddings.shape[0] + 1
self.aggregation = params['aggregation']
self.num_heads = params['num_heads']
self.encoder_type = params['encoder']
self.save_model_loc = '/data1/playlists/models/contrastive'
self.curr_min_val = 10
def init_models(self):
self.audio_encoder = AudioEncoder(128)
if self.aggregation == 'mean':
self.tag_encoder = TagMeanEncoder(self.size_voc, 128, 128, emb_file=self.w2v_emb_file)
elif self.aggregation == 'self':
self.tag_encoder = TagSelfAttentionEncoder(self.max_num_tags, 128, self.num_heads, 128, 128, 128, self.w2v_emb_file,dropout=0.1)
self.cf_encoder = CFEncoder(301, 128)
def load_model_checkpoints(self):
saved_models_folder = Path(self.save_model_loc, self.experiment_name)
try:
last_epoch = max([int(f.stem.split('epoch_')[-1]) for f in saved_models_folder.iterdir()])
self.audio_encoder.load_state_dict(torch.load(str(Path(self.save_model_loc, self.experiment_name, f'audio_encoder_epoch_{last_epoch}.pt'))))
self.tag_encoder.load_state_dict(torch.load(str(Path(self.save_model_loc, self.experiment_name, f'tag_encoder_att_epoch_{last_epoch}.pt'))))
print(f'Model checkpoints from epoch {last_epoch} loaded...')
except ValueError:
last_epoch = 0
print('No model loaded, training from scratch...')
self.iteration_idx = last_epoch * int(self.length_val_dataset / self.batch_size)
self.last_epoch = last_epoch
def train(self):
""" Train models
"""
# Data loaders
loader_params = {
'batch_size': self.batch_size,
'shuffle': True,
'num_workers': 2,
'drop_last': True,
}
dataset_train = InMemoryDataset(self.train_dataset_file)
#dataset_train = InMemoryDataset(self.validation_dataset_file)
dataset_val = InMemoryDataset(self.validation_dataset_file)
self.train_loader = data.DataLoader(dataset_train, **loader_params)
self.val_loader = data.DataLoader(dataset_val, **loader_params)
self.length_train_dataset = len(self.train_loader.dataset)
self.length_val_dataset = len(self.val_loader.dataset)
# folder for model checkpoints
model_checkpoints_folder = Path(self.save_model_loc, self.experiment_name)
if not model_checkpoints_folder.exists():
model_checkpoints_folder.mkdir()
# models
self.init_models()
self.load_model_checkpoints()
self.audio_encoder.to(self.device)
if self.encoder_type == "gnr":
self.tag_encoder.to(self.device)
if self.encoder_type == "MF":
self.cf_encoder.to(self.device)
# optimizers
if self.encoder_type == "gnr":
self.opt = torch.optim.Adam(chain(self.audio_encoder.parameters(), self.tag_encoder.parameters()), lr=self.learning_rate, weight_decay=1e-4)
if self.encoder_type == "MF":
self.opt = torch.optim.Adam(chain(self.audio_encoder.parameters(), self.cf_encoder.parameters()), lr=self.learning_rate, weight_decay=1e-4)
# tensorboard
with SummaryWriter(log_dir=str(Path('runs', self.experiment_name)), max_queue=100) as self.tb:
# Training loop
for epoch in range(self.last_epoch+1, self.epochs + 1):
self.train_one_epoch(epoch)
self.val(epoch)
def train_one_epoch(self, epoch):
""" Train one epoch
"""
self.audio_encoder.train()
if self.encoder_type == "gnr":
self.tag_encoder.train()
if self.encoder_type == "MF":
self.cf_encoder.train()
# losses
train_pairwise_loss = 0
for batch_idx, (data, tags, cf_embeddings, sound_ids) in enumerate(self.train_loader):
self.iteration_idx += 1
# TODO: REMOVE THAT
# tags should already in the tag_idxs form, except for the +1 to indexes to use idx 0 for no tag
# We probably want to add some pre-processing in data_loader.py
# e.g. select random tags from the 100, or select random sepctrogram chunk
"""
tag_idxs = [
([idx+1 for idx, val in enumerate(tag_v) if val]
+ self.max_num_tags*[0])[:self.max_num_tags]
for tag_v in tags
]
"""
# encode
x = data.view(-1, 1, 48, 256).to(self.device)
z_audio, z_d_audio = self.audio_encoder(x)
if self.encoder_type == "gnr":
curr_labels = []
for curr_tags in tags:
non_neg = [i+1 for i in curr_tags if i != -1]
new_tags = np.zeros(self.max_num_tags)
#new_tags[:len(non_neg)] = np.random.choice(non_neg, min(self.max_num_tags, len(non_neg)), replace=False)
new_tags[:min(len(non_neg), 10)] = non_neg[:10]
curr_labels.append(new_tags)
tags_input = torch.tensor(curr_labels, dtype=torch.long).to(self.device)
#tags_input = tags.to(self.device)
z_tags, attn = self.tag_encoder(tags_input, z_d_audio, mask=tags_input.unsqueeze(1))
pairwise_loss = contrastive_loss(z_d_audio, z_tags, self.contrastive_temperature)
if self.encoder_type == "MF" :
cf_input = cf_embeddings.to(self.device)
z_cf = self.cf_encoder(cf_input)
# contrastive loss
pairwise_loss = contrastive_loss(z_d_audio, z_cf, self.contrastive_temperature)
# Optimize models
self.opt.zero_grad()
pairwise_loss.backward()
self.opt.step()
train_pairwise_loss += pairwise_loss.item()
# write to tensorboard
# These are too many data to send to tensorboard, but it can be useful for debugging/developing
if False:
self.tb.add_scalar("iter/contrastive_pairwise_loss", pairwise_loss.item(), self.iteration_idx)
# logs per batch
if batch_idx % self.log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tPairwise loss: {:.4f})'.format(
epoch, batch_idx * len(data), len(self.train_loader.dataset),
100. * batch_idx / len(self.train_loader),
pairwise_loss.item()
)
)
# epoch logs
train_pairwise_loss = train_pairwise_loss / self.length_train_dataset * self.batch_size
print('====> Epoch: {} Pairwise loss: {:.8f}'.format(epoch, train_pairwise_loss))
print('\n')
# tensorboard
self.tb.add_scalar("contrastive_pairwise_loss/train/sum", train_pairwise_loss, epoch)
if epoch%self.save_model_every == 0:
torch.save(self.audio_encoder.state_dict(), str(Path(self.save_model_loc, self.experiment_name, f'audio_encoder_epoch_{epoch}.pt')))
if self.encoder_type == "gnr":
torch.save(self.tag_encoder.state_dict(), str(Path(self.save_model_loc, self.experiment_name, f'tag_encoder_att_epoch_{epoch}.pt')))
if self.encoder_type == "MF":
torch.save(self.cf_encoder.state_dict(), str(Path(self.save_model_loc, self.experiment_name, f'cf_encoder_att_epoch_{epoch}.pt')))
def val(self, epoch):
""" Validation
"""
# A little bit a code repeat here...
self.audio_encoder.eval()
if self.encoder_type == "gnr":
self.tag_encoder.eval()
if self.encoder_type == "MF":
self.cf_encoder.eval()
val_pairwise_loss = 0
with torch.no_grad():
for i, (data, tags, cf_embeddings, sound_ids) in enumerate(self.val_loader):
"""
curr_labels = []
for curr_tags in tags:
non_neg = [i+1 for i in curr_tags if i != -1]
new_tags = np.zeros(self.max_num_tags)
#new_tags[:len(non_neg)] = np.random.choice(non_neg, min(self.max_num_tags, len(non_neg)), replace=False)
new_tags[:min(len(non_neg), 10)] = non_neg[:10]
curr_labels.append(new_tags)
tags_input = torch.tensor(curr_labels, dtype=torch.long).to(self.device)
"""
x = data.view(-1, 1, 48, 256).to(self.device)
cf_input = cf_embeddings.to(self.device)
# encode
z_audio, z_d_audio = self.audio_encoder(x)
if self.encoder_type == "gnr" :
z_tags, attn = self.tag_encoder(tags_input, z_d_audio, mask=tags_input.unsqueeze(1))
# pairwise correspondence loss
pairwise_loss = contrastive_loss(z_d_audio, z_tags, self.contrastive_temperature)
if self.encoder_type == "MF" :
z_cf = self.cf_encoder(cf_input)
# pairwise correspondence loss
pairwise_loss = contrastive_loss(z_d_audio, z_cf, self.contrastive_temperature)
val_pairwise_loss += pairwise_loss.item()
val_pairwise_loss = val_pairwise_loss / self.length_val_dataset * self.batch_size
print('====> Val average pairwise loss: {:.4f}'.format(val_pairwise_loss))
print('\n\n')
# tensorboard
self.tb.add_scalar("contrastive_pairwise_loss/val/sum", val_pairwise_loss, epoch)
if not (math.isinf(val_pairwise_loss) or math.isinf(val_pairwise_loss)):
if val_pairwise_loss<self.curr_min_val:
self.curr_min_val = val_pairwise_loss
torch.save(self.audio_encoder.state_dict(), str(Path(self.save_model_loc, self.experiment_name, f'audio_encoder_epoch_best.pt')))
if self.encoder_type == "gnr":
torch.save(self.tag_encoder.state_dict(), str(Path(self.save_model_loc, self.experiment_name, f'tag_encoder_att_epoch_best.pt')))
if self.encoder_type == "MF":
torch.save(self.cf_encoder.state_dict(), str(Path(self.save_model_loc, self.experiment_name, f'cf_encoder_att_epoch_best.pt')))