-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.py
131 lines (105 loc) · 3.73 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
from model.frontend import CustomFrontend
from model.model import ASRModel
from loader import *
import torch
from preprocess import *
import time
import argparse
from espnet.bin.asr_train import get_parser
from config import *
from tqdm import tqdm
import os
import pickle
from utils import *
from torch.utils.tensorboard import SummaryWriter
import logging
logging.basicConfig(level=logging.ERROR)
writer = SummaryWriter(comment = "ASR-Transformers")
device = torch.device("cuda:1")
#device = torch.device("cpu")
input_size = 80
#Hyper parameters
epochs = 80
batch_size = 8
lr = 0.025
warm_up = 8000
char = True
SAMPLE_RATE = 16000
#path, trg, char2idx = preprocess_data(char = char)
#ret = split_path(path, trg, 0.025, save = True)
with open("./save_model/split_data.pickle", "rb") as f:
ret = pickle.load(f)
with open("./save_model/char2idx.pickle", "rb") as f:
char2idx = pickle.load(f)
train_path = ret["train_path"]
train_trg = ret["train_trg"]
val_path = ret["val_path"]
val_trg = ret["val_trg"]
test_path = ret["test_path"]
test_trg = ret["test_trg"]
dataloader = Batch_Loader(batch_size, device, train_path, train_trg, char2idx)
val_loader = Batch_Loader(batch_size, device, val_path, val_trg, char2idx)
test_loader = Batch_Loader(batch_size, device, test_path, test_trg, char2idx)
'''
dataloader = Batch_Loader(batch_size, device, val_path, val_trg, char2idx)
val_loader = Batch_Loader(batch_size, device, val_path, val_trg, char2idx)
test_loader = Batch_Loader(batch_size, device, test_path, test_trg, char2idx)
'''
token_list = []
for key, value in dataloader.char2idx.items():
token_list.append(key)
token_list.append("<sos>")
vocab_size = len(token_list)
print(vocab_size)
config = Config(token_list)
recog_config = Recog_config()
model = ASRModel(input_size = input_size,
vocab_size = vocab_size,
token_list = token_list,
config = config,
device = device)
model.load_state_dict(torch.load("./save_model/best_ctc_norm_add.pt", map_location = device))
model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr = lr, weight_decay = 1e-5)
emb_size = config.adim
cer, wer, val_acc = val_score(model, val_loader)
print(val_acc)
st = time.time()
total = len(dataloader) // batch_size + 1
best_acc = val_acc
step = 900000
for epoch in range(epochs):
epoch_loss = 0
epoch_acc = 0
model.train()
model.to(device)
for iteration in tqdm(range(1, total)):
for param_group in optimizer.param_groups:
param_group['lr'] = emb_size**(-0.5) * min(step**(-0.5), step * (warm_up**(-1.5)))
lr = param_group['lr']
step += 1
train_batch = dataloader.get_batch()
loss, ret_dict = model(**train_batch)
acc = ret_dict["acc"]
writer.add_scalar("loss/train", loss.item(), step)
writer.add_scalar("acc/train", acc, step)
writer.add_scalar("lr/train", lr, step)
epoch_loss += loss.item()
epoch_acc += acc
optimizer.zero_grad()
loss.backward()
optimizer.step()
epoch_loss /= total
epoch_acc /= total
if epoch % 1 == 0:
current_time = round((time.time() - st) / 3600 , 4)
cer, wer, val_acc = val_score(model, val_loader)
#ys_hat = ret_dict["ys_hat"]
#print(ys_hat[0], trg[0])
print(f"epoch : {epoch} | epoch loss : {epoch_loss} | acc : {epoch_acc} | val acc : {val_acc} | cer : {cer} | wer: {wer} | time : {current_time}")
if best_acc < val_acc:
best_acc = val_acc
save_text(model, test_loader, recog_config, token_list, save_path = "./results/result_norm.txt", char = char)
torch.save(model.state_dict(), "./save_model/best_ctc_last.pt")
writer.close()
#epoch : 25