-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodelmodule.py
143 lines (113 loc) · 4.35 KB
/
modelmodule.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
import torch
import torchaudio.transforms as T
from model.conformer import Conformer
import pytorch_lightning as pl
import torchmetrics
from utils import TextProcess
from transformers import get_linear_schedule_with_warmup
class ConformerModule(pl.LightningModule):
def __init__(
self, cfg, blank: int = 0, text_process: TextProcess = None, batch_size: int = 4
):
super().__init__()
self.cfg = cfg
self.lr = cfg.training.lr
self.conformer = Conformer(**cfg.model)
self.cal_loss = T.RNNTLoss(blank=blank)
self.cal_wer = torchmetrics.WordErrorRate()
self.text_process = text_process
self.save_hyperparameters()
def forward(self, inputs, input_lengths):
return self.conformer.recognize(inputs, input_lengths)
def configure_optimizers(self):
optimizer = torch.optim.AdamW(self.parameters(), lr=self.lr, **self.cfg.optim)
lr_scheduler = get_linear_schedule_with_warmup(optimizer, **self.cfg.sched)
return [optimizer], [lr_scheduler]
def get_batch(self, batch):
inputs, input_lengths, targets, target_lengths = batch
batch_size = inputs.size(0)
zeros = torch.zeros((batch_size, 1)).to(device=self.device)
compute_targets = torch.cat((zeros, targets), dim=1).to(
device=self.device, dtype=torch.int
)
compute_target_lengths = (target_lengths + 1).to(device=self.device)
return (
inputs,
input_lengths,
targets,
target_lengths,
compute_targets,
compute_target_lengths,
)
def training_step(self, batch, batch_idx):
(
inputs,
input_lengths,
targets,
target_lengths,
compute_targets,
compute_target_lengths,
) = self.get_batch(batch)
outputs, output_lengths = self.conformer(
inputs, input_lengths, compute_targets, compute_target_lengths
)
loss = self.cal_loss(outputs, targets, output_lengths, target_lengths)
self.log("train_loss", loss)
self.log("lr", self.lr)
return loss
def validation_step(self, batch, batch_idx):
(
inputs,
input_lengths,
targets,
target_lengths,
compute_targets,
compute_target_lengths,
) = self.get_batch(batch)
outputs, output_lengths = self.conformer(
inputs, input_lengths, compute_targets, compute_target_lengths
)
loss = self.cal_loss(outputs, targets, output_lengths, target_lengths)
predicts = self.forward(inputs, input_lengths)
predicts = [self.text_process.int2text(sent) for sent in predicts]
targets = [self.text_process.int2text(sent) for sent in targets]
list_wer = torch.tensor(
[self.cal_wer(i, j).item() for i, j in zip(predicts, targets)]
)
wer = torch.mean(list_wer)
if batch_idx % 100 == 0:
self.log_output(predicts[0], targets[0], wer)
self.log("val_loss", loss)
self.log("val_batch_wer", wer)
return loss, wer
def test_step(self, batch, batch_idx):
(
inputs,
input_lengths,
targets,
target_lengths,
compute_targets,
compute_target_lengths,
) = self.get_batch(batch)
outputs, output_lengths = self.conformer(
inputs, input_lengths, compute_targets, compute_target_lengths
)
loss = self.cal_loss(outputs, targets, output_lengths, target_lengths)
predicts = self.forward(inputs, input_lengths)
predicts = [self.text_process.int2text(sent) for sent in predicts]
targets = [self.text_process.int2text(sent) for sent in targets]
list_wer = torch.tensor(
[self.cal_wer(i, j).item() for i, j in zip(predicts, targets)]
)
wer = torch.mean(list_wer)
if batch_idx % 100 == 0:
self.log_output(predicts[0], targets[0], wer)
self.log("test_loss", loss)
self.log("test_batch_wer", wer)
return loss, wer
def log_output(self, predict, target, wer):
print("=" * 50)
print("Sample Predicts: ", predict)
print("Sample Targets:", target)
print("Mean WER:", wer)
print("=" * 50)