-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.py
174 lines (133 loc) · 5.17 KB
/
train.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
import numpy as np
import pytorch_lightning as pl
import torch
from pytorch_lightning import LightningModule, Trainer
from pytorch_lightning.callbacks import ModelCheckpoint
from pytorch_lightning.loggers.wandb import WandbLogger
from pytorch_lightning.strategies import DDPStrategy
from torch import nn
from torch.utils.data import DataLoader
from transformers import HubertModel
from dataset import KMeansDataset
class TeacherModel(LightningModule):
def __init__(self):
super().__init__()
self.model = HubertModel.from_pretrained("TencentGameMate/chinese-hubert-base")
self.cluster_centers = nn.Parameter(
torch.from_numpy(np.load("cluster_centers.npy")).float()
)
def forward(self, input_values, attention_mask=None):
x = self.model(input_values, attention_mask=attention_mask)
x = x.last_hidden_state
# X in shape (batch_size, seq_len, 768)
# cluster_centers in shape (128, 768)
x = self.kmeans(x)
return x
def kmeans(self, x):
distances = torch.cdist(x, self.cluster_centers)
x = torch.argmin(distances, dim=-1)
return x
class MyLightningModule(LightningModule):
def __init__(self):
super().__init__()
self.save_hyperparameters()
self.model = HubertModel.from_pretrained("TencentGameMate/chinese-hubert-base")
self.teacher = TeacherModel()
self.teacher.freeze()
embed_dim = 256
self.proj = nn.Sequential(nn.Dropout(0.1), nn.Linear(768, embed_dim))
self.label_embedding = nn.Embedding(128, embed_dim)
self.loss = nn.CrossEntropyLoss()
def logits(self, x: torch.Tensor) -> torch.Tensor:
logits = torch.cosine_similarity(
x.unsqueeze(2),
self.label_embedding.weight.unsqueeze(0).unsqueeze(0),
dim=-1,
)
return logits / 0.1
def forward(self, input_values, attention_mask=None):
x = self.model(input_values, attention_mask=attention_mask)
x = self.proj(x.last_hidden_state)
return x
def _step(self, batch, batch_idx, mode="train"):
input_values = batch["input_values"]
attention_mask = batch["attention_mask"]
with torch.no_grad():
labels = self.teacher(input_values, attention_mask=attention_mask)
logits = self(input_values, attention_mask=attention_mask)
logits = self.logits(logits)
loss = self.loss(logits.flatten(0, 1), labels.flatten(0, 1))
self.log(f"{mode}_loss", loss, prog_bar=False, sync_dist=True)
x = logits.argmax(-1)
avg_acc = (x == labels).float().mean()
self.log(f"{mode}_acc", avg_acc, prog_bar=True, sync_dist=True)
return loss
def training_step(self, batch, batch_idx):
return self._step(batch, batch_idx, mode="train")
def validation_step(self, batch, batch_idx):
return self._step(batch, batch_idx, mode="val")
def configure_optimizers(self):
optim = torch.optim.AdamW(
self.model.parameters(), lr=2e-5, weight_decay=1e-2, betas=(0.9, 0.98)
)
return optim
def collate(batch):
# pad the inputs on the right up to the maximum length
input_values = [i["input_values"] for i in batch]
max_input_length = max([len(i) for i in input_values])
input_values_mask = []
for i in input_values:
input_values_mask.append([1] * len(i) + [0] * (max_input_length - len(i)))
input_values_padded = [
torch.nn.functional.pad(i, (0, max_input_length - len(i)), value=0)
for i in input_values
]
return dict(
input_values=torch.stack(input_values_padded),
attention_mask=torch.tensor(input_values_mask),
)
if __name__ == "__main__":
pl.seed_everything(42)
dataset = KMeansDataset()
split = int(len(dataset) * 0.95)
train_dataset, val_dataset = torch.utils.data.random_split(
dataset, [split, len(dataset) - split]
)
print(f"Train dataset size: {len(train_dataset)}")
print(f"Val dataset size: {len(val_dataset)}")
train_loader = DataLoader(
train_dataset, batch_size=4, collate_fn=collate, shuffle=True, num_workers=4
)
val_loader = DataLoader(
val_dataset, batch_size=4, collate_fn=collate, shuffle=True, num_workers=2
)
model = MyLightningModule()
trainer = Trainer(
accelerator="gpu",
devices=-1,
strategy=DDPStrategy(find_unused_parameters=True),
gradient_clip_val=10,
accumulate_grad_batches=16,
val_check_interval=10000,
check_val_every_n_epoch=None,
max_epochs=100,
precision=16,
callbacks=[
ModelCheckpoint(
filename="{epoch}-{val_acc:.2f}",
monitor="val_acc",
mode="max",
save_top_k=3,
save_last=True,
)
],
logger=WandbLogger(
project="hubert",
save_dir="logs",
log_model="all",
entity="fish-audio",
# resume="must", id="9srjca5y"
),
# resume_from_checkpoint="logs/asr/9srjca5y/checkpoints/last.ckpt"
)
trainer.fit(model, train_loader, val_loader)