-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathmain.py
executable file
·162 lines (112 loc) · 4.11 KB
/
main.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
#!/usr/bin/env python3
import random
from torch.utils.tensorboard import SummaryWriter
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.utils import data
from morse import ALPHABET, generate_sample
from itertools import groupby
num_tags = len(ALPHABET)
# 0: blank label
tag_to_idx = {c: i + 1 for i, c in enumerate(ALPHABET)}
idx_to_tag = {i + 1: c for i, c in enumerate(ALPHABET)}
def prediction_to_str(seq):
if not isinstance(seq, list):
seq = seq.tolist()
# remove duplicates
seq = [i[0] for i in groupby(seq)]
# remove blanks
seq = [s for s in seq if s != 0]
# convert to string
seq = "".join(idx_to_tag[c] for c in seq)
return seq
def get_training_sample(*args, **kwargs):
_, spec, y = generate_sample(*args, **kwargs)
spec = torch.from_numpy(spec)
spec = spec.permute(1, 0)
y_tags = [tag_to_idx[c] for c in y]
y_tags = torch.tensor(y_tags)
return spec, y_tags
class Net(nn.Module):
def __init__(self, num_tags, spectrogram_size):
super(Net, self).__init__()
num_tags = num_tags + 1 # 0: blank
hidden_dim = 256
lstm_dim1 = 256
self.dense1 = nn.Linear(spectrogram_size, hidden_dim)
self.dense2 = nn.Linear(hidden_dim, hidden_dim)
self.dense3 = nn.Linear(hidden_dim, hidden_dim)
self.dense4 = nn.Linear(hidden_dim, lstm_dim1)
self.lstm1 = nn.LSTM(lstm_dim1, lstm_dim1, batch_first=True)
self.dense5 = nn.Linear(lstm_dim1, num_tags)
def forward(self, x):
x = F.relu(self.dense1(x))
x = F.relu(self.dense2(x))
x = F.relu(self.dense3(x))
x = F.relu(self.dense4(x))
x, _ = self.lstm1(x)
x = self.dense5(x)
x = F.log_softmax(x, dim=2)
return x
def count_parameters(self):
return sum(p.numel() for p in self.parameters() if p.requires_grad)
class Dataset(data.Dataset):
def __len__(self):
return 2048
def __getitem__(self, index):
length = random.randrange(10, 20)
pitch = random.randrange(100, 950)
wpm = random.randrange(10, 40)
noise_power = random.randrange(0, 200)
amplitude = random.randrange(10, 150)
return get_training_sample(length, pitch, wpm, noise_power, amplitude)
def collate_fn_pad(batch):
xs, ys = zip(*batch)
input_lengths = torch.tensor([t.shape[0] for t in xs])
output_lengths = torch.tensor([t.shape[0] for t in ys])
seqs = nn.utils.rnn.pad_sequence(xs, batch_first=True)
ys = nn.utils.rnn.pad_sequence(ys, batch_first=True)
return input_lengths, output_lengths, seqs, ys
if __name__ == "__main__":
batch_size = 64
spectrogram_size = generate_sample()[1].shape[0]
device = torch.device("cuda")
writer = SummaryWriter()
# Set up trainer & evaluator
model = Net(num_tags, spectrogram_size).to(device)
print("Number of params", model.count_parameters())
# Lower learning rate to 1e-4 after about 1500 epochs
optimizer = optim.Adam(model.parameters(), lr=1e-3)
ctc_loss = nn.CTCLoss()
train_loader = torch.utils.data.DataLoader(
Dataset(),
batch_size=batch_size,
num_workers=4,
collate_fn=collate_fn_pad,
)
random.seed(0)
epoch = 0
# Resume training
if epoch != 0:
model.load_state_dict(torch.load(f"models/{epoch:06}.pt", map_location=device))
model.train()
while True:
for (input_lengths, output_lengths, x, y) in train_loader:
x, y = x.to(device), y.to(device)
optimizer.zero_grad()
y_pred = model(x)
m = torch.argmax(y_pred[0], 1)
y_pred = y_pred.permute(1, 0, 2)
loss = ctc_loss(y_pred, y, input_lengths, output_lengths)
loss.backward()
optimizer.step()
writer.add_scalar("training/loss", loss.item(), epoch)
if epoch % 10 == 0:
torch.save(model.state_dict(), f"models/{epoch:06}.pt")
print(prediction_to_str(y[0]))
print(prediction_to_str(m))
print(loss.item())
print()
epoch += 1