-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathex_tl.py
377 lines (307 loc) · 10.7 KB
/
ex_tl.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
import logging
import pickle
from pathlib import Path
from typing import Any, List
import numpy as np
import lightning as pl
import torch
from lightning.pytorch.callbacks import ModelCheckpoint
from lightning.pytorch.loggers import TensorBoardLogger
from sacred import Experiment
from sacred.observers import FileStorageObserver
from sklearn import metrics
from torch import optim, nn
from torch.utils.data import Dataset, DataLoader
from helpers.ramp import exp_warmup_linear_down
from config_updates import add_configs
ex = Experiment("ex_tl")
ex.observers.append(FileStorageObserver("exp_logs"))
_logger = logging.getLogger("ex_discogs")
@ex.config
def default_config():
max_epochs = 60
trainer = {
"max_epochs": max_epochs,
"devices": 1,
"num_sanity_val_steps": 0,
}
optimizer = {
"monitor": "val_roc",
"weight_decay": 1e-3,
"scheduler": "exp_warmup_linear_down",
"max_lr": 1e-4,
"max_lr_epochs": 10,
"max_epochs": max_epochs,
# cycliclr
"base_lr": 1e-7,
# exponential
"warmup_epochs": 10,
"gamma": 0.5,
}
model = {
"drop_out": 0.5,
"hidden_units": 512,
}
data = {
"base_dir": "embeddings/mtt/30sec/no_swa/10/",
"metadata_dir": "mtt/",
"batch_size": 128,
"num_workers": 16,
"types": "c",
"reduce": "mean",
"token_size": 768,
"n_classes": 50,
}
# register extra possible configs
add_configs(ex)
class Model(pl.LightningModule):
@ex.capture(prefix="model")
def __init__(
self,
in_features,
n_classes,
drop_out,
hidden_units,
):
super().__init__()
self.model = nn.Sequential(
nn.Linear(in_features, hidden_units),
nn.ReLU(),
nn.Dropout(drop_out),
nn.Linear(hidden_units, n_classes),
)
self.sigmoid = nn.Sigmoid()
self.best_checkpoint_path = "best"
self.val_step_outputs = []
self.test_step_outputs = []
def forward(self, x):
return self.model(x)
def training_step(self, batch, batch_idx):
x, y = batch
y_hat = self.forward(x)
loss = nn.functional.binary_cross_entropy_with_logits(y_hat, y)
self.log("train_loss", loss, on_epoch=True)
return loss
def test_val_step(self, batch, batch_idx, outputs, key):
x, y = batch
y_logits = self.forward(x)
loss = nn.functional.binary_cross_entropy_with_logits(y_logits, y)
self.log(f"{key}_loss", loss, prog_bar=True)
y_hat = self.sigmoid(y_logits)
output_dict = {f"{key}_loss": loss, f"{key}_y": y, f"{key}_y_hat": y_hat}
outputs.append(output_dict)
return outputs
def validation_step(self, batch, batch_idx):
return self.test_val_step(batch, batch_idx, self.val_step_outputs, "val")
def test_step(self, batch, batch_idx):
return self.test_val_step(batch, batch_idx, self.test_step_outputs, "test")
def on_val_test_epoch_end(self, outputs: List[Any], key: str) -> None:
y = torch.cat([output[f"{key}_y"] for output in outputs])
y_hat = torch.cat([output[f"{key}_y_hat"] for output in outputs])
y = y.detach().cpu().numpy().astype("int")
y_hat = y_hat.detach().cpu().numpy()
ap = metrics.average_precision_score(y, y_hat, average="macro")
roc = metrics.roc_auc_score(y, y_hat, average="macro")
self.log(f"{key}_ap", ap)
self.log(f"{key}_roc", roc)
outputs.clear()
def on_validation_epoch_end(self) -> None:
self.on_val_test_epoch_end(self.val_step_outputs, "val")
def on_test_epoch_end(self) -> None:
self.on_val_test_epoch_end(self.test_step_outputs, "test")
@ex.capture(prefix="optimizer")
def configure_optimizers(
self,
max_epochs,
max_lr_epochs,
base_lr,
max_lr,
scheduler,
warmup_epochs,
gamma,
weight_decay,
):
optimizer = optim.AdamW(self.parameters(), lr=max_lr, weight_decay=weight_decay)
if scheduler == "cyclic":
schedulers = [
{
"scheduler": optim.lr_scheduler.CyclicLR(
optimizer=optimizer,
base_lr=base_lr,
max_lr=max_lr,
mode="triangular2",
step_size_up=145,
cycle_momentum=False,
),
"interval": "step",
"frequency": 1,
}
]
elif scheduler == "exponential":
schedulers = [
optim.lr_scheduler.LambdaLR(
optimizer=optimizer,
lr_lambda=lambda e: (e + 1e-7) / warmup_epochs
if e < warmup_epochs
else 1,
),
optim.lr_scheduler.ExponentialLR(
optimizer=optimizer,
gamma=gamma,
last_epoch=-1,
),
]
elif scheduler == "exp_warmup_linear_down":
schedulers = [
optim.lr_scheduler.LambdaLR(
optimizer=optimizer,
lr_lambda=exp_warmup_linear_down(
warmup_epochs,
max_epochs - max_lr_epochs,
max_lr_epochs,
base_lr,
),
),
]
return [optimizer], schedulers
@ex.capture(prefix="trainer")
def configure_callbacks(self, monitor="val_roc"):
if "roc" in monitor:
mode = "max"
elif "loss" in monitor:
mode = "min"
return [
ModelCheckpoint(
filename=self.best_checkpoint_path,
save_top_k=1,
monitor=monitor,
mode=mode,
save_weights_only=True,
verbose=True,
),
# LearningRateMonitor(logging_interval="step")
]
class EmbeddingDataset(Dataset):
@ex.capture(prefix="data")
def __init__(self, groundtruth_file, base_dir, types, reduce):
self.base_dir = base_dir
self.types = types
self.reduce = reduce
with open(groundtruth_file, "rb") as gf:
self.groundtruth = pickle.load(gf)
self.filenames = {
i: filename for i, filename in enumerate(list(self.groundtruth.keys()))
}
self.length = len(self.groundtruth)
def __len__(self):
return self.length
def __getitem__(self, index):
filename = self.filenames[index]
target = self.groundtruth[filename]
embedding_path = Path(self.base_dir, filename + ".embeddings.npy")
embedding = np.load(embedding_path)
embedding = self.post_process(embedding)
return embedding, target.astype("float32")
def post_process(self, embedding):
# todo: implement other postprocessing
if len(embedding.shape) == 2:
embedding = np.mean(embedding, axis=0)
if embedding.shape[-1] == 768:
return embedding
embedding = embedding.reshape(3, -1)
embedding_de = {
"c": embedding[0],
"d": embedding[1],
"t": embedding[2],
}
embeddings = [v for k, v in embedding_de.items() if k in self.types]
if self.reduce == "mean":
return np.mean(np.array(embeddings), axis=0)
elif self.reduce == "stack":
return np.hstack(embeddings)
class DataModule(pl.LightningDataModule):
@ex.capture(prefix="data")
def __init__(
self,
base_dir,
metadata_dir,
batch_size,
num_workers,
types,
reduce,
token_size,
n_classes,
train_groundtruth_file=None,
valid_groundtruth_file=None,
test_groundtruth_file=None,
):
super().__init__()
self.base_dir = base_dir
self.metadata_dir = Path(metadata_dir)
self.batch_size = batch_size
self.num_workers = num_workers
train_file = (
train_groundtruth_file if train_groundtruth_file else "groundtruth-train.pk"
)
valid_file = (
valid_groundtruth_file
if valid_groundtruth_file
else "groundtruth-validation.pk"
)
test_file = (
test_groundtruth_file if test_groundtruth_file else "groundtruth-test.pk"
)
self.train_groundtruth_file = self.metadata_dir / train_file
self.val_groundtruth_file = self.metadata_dir / valid_file
self.test_groundtruth_file = self.metadata_dir / test_file
self.types = types
self.reduce = reduce
self.token_size = token_size
self.n_classes = n_classes
if self.reduce == "mean":
self.in_features = self.token_size
elif self.reduce == "stack":
self.in_features = self.token_size * len(self.types)
def setup(self, stage: str):
# Assign train/val datasets for use in dataloaders
if stage == "fit":
self.dataset_train = EmbeddingDataset(
groundtruth_file=self.train_groundtruth_file,
base_dir=self.base_dir,
)
self.dataset_val = EmbeddingDataset(
groundtruth_file=self.val_groundtruth_file,
base_dir=self.base_dir,
)
# Assign test dataset for use in dataloader(s)
if stage == "test":
self.dataset_test = EmbeddingDataset(
groundtruth_file=self.test_groundtruth_file,
base_dir=self.base_dir,
)
def train_dataloader(self):
return DataLoader(
self.dataset_train, batch_size=self.batch_size, num_workers=self.num_workers
)
def val_dataloader(self):
return DataLoader(
self.dataset_val, batch_size=self.batch_size, num_workers=self.num_workers
)
def test_dataloader(self):
return DataLoader(
self.dataset_test, batch_size=self.batch_size, num_workers=self.num_workers
)
@ex.automain
def tl_pipeline(_run, _config):
_logger.info("starting transfer learning experiment")
_logger.info(_config)
datamodule = DataModule()
model = Model(
in_features=datamodule.in_features,
n_classes=datamodule.n_classes,
)
tb_logger = TensorBoardLogger("exp_logs/", version=_run._id)
trainer = pl.Trainer(logger=tb_logger, **_config["trainer"])
trainer.fit(model=model, datamodule=datamodule)
model.eval()
trainer.test(model=model, datamodule=datamodule, ckpt_path="best")