-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
194 lines (159 loc) · 5.75 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
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
import torch
import math
import queue
import os
import random
import warnings
import time
import json
import argparse
from glob import glob
import numpy as np
from modules.preprocess import preprocessing
from modules.trainer import trainer
from modules.utils import (
get_optimizer,
get_criterion,
get_lr_scheduler,
)
from modules.audio import (
FilterBankConfig,
MelSpectrogramConfig,
MfccConfig,
SpectrogramConfig,
)
from modules.model import build_model
from modules.vocab import KoreanSpeechVocabulary
from modules.data import split_dataset, collate_fn
from modules.utils import Optimizer
from modules.metrics import get_metric
from modules.inference import single_infer
from modules.arguments import get_args #custom
from torch.utils.data import DataLoader
import nsml
from nsml import DATASET_PATH
def bind_model(model, optimizer=None):
def save(path, *args, **kwargs):
state = {
'model': model.state_dict(),
'optimizer': optimizer.state_dict()
}
torch.save(state, os.path.join(path, 'model.pt'))
print('Model saved')
def load(path, *args, **kwargs):
state = torch.load(os.path.join(path, 'model.pt'))
model.load_state_dict(state['model'])
if 'optimizer' in state and optimizer:
optimizer.load_state_dict(state['optimizer'])
print('Model loaded')
# 추론
def infer(path, **kwargs):
return inference(path, model)
nsml.bind(save=save, load=load, infer=infer) # 'nsml.bind' function must be called at the end.
def inference(path, model, **kwargs):
model.eval()
results = []
for i in glob(os.path.join(path, '*')):
results.append(
{
'filename': i.split('/')[-1],
'text': single_infer(model, i)[0]
}
)
return sorted(results, key=lambda x: x['filename'])
def seed_fix(seed): #시드 고정 함수
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
np.random.seed(seed)
random.seed(seed)
def run(config):
device = 'cuda' if config.use_cuda == True else 'cpu'
if hasattr(config, "num_threads") and int(config.num_threads) > 0:
torch.set_num_threads(config.num_threads)
vocab = KoreanSpeechVocabulary(os.path.join(os.getcwd(), 'labels.csv'), output_unit='character')
model = build_model(config, vocab, device)
optimizer = get_optimizer(model, config)
bind_model(model, optimizer=optimizer)
metric = get_metric(metric_name='CER', vocab=vocab)
if config.pause:
nsml.paused(scope=locals())
if config.mode == 'train':
config.dataset_path = os.path.join(DATASET_PATH, 'train', 'train_data')
label_path = os.path.join(DATASET_PATH, 'train', 'train_label')
preprocessing(label_path, os.getcwd(), config)
train_dataset, valid_dataset = split_dataset(config, os.path.join(os.getcwd(), 'transcripts.txt'), vocab)
print(f"train dataset : {len(train_dataset)}")
print(f"valid dataset : {len(valid_dataset)}")
lr_scheduler = get_lr_scheduler(config, optimizer, math.ceil(len(train_dataset)//config.batch_size))
optimizer = Optimizer(optimizer, lr_scheduler, math.ceil(len(train_dataset)//config.batch_size)*config.num_epochs, config.max_grad_norm)
criterion = get_criterion(config, vocab)
num_epochs = config.num_epochs
num_workers = config.num_workers
train_begin_time = time.time()
for epoch in range(num_epochs):
print('[INFO] Epoch %d start' % epoch)
# train
train_loader = DataLoader(
train_dataset,
batch_size=config.batch_size,
shuffle=True,
collate_fn=collate_fn,
num_workers=config.num_workers
)
metric = get_metric(metric_name='CER', vocab=vocab)
model, train_loss, train_cer = trainer(
'train',
config,
train_loader,
optimizer,
model,
criterion,
metric,
train_begin_time,
device
)
print('[INFO] Epoch %d (Training) Loss %0.4f CER %0.4f' % (epoch, train_loss, train_cer))
# valid
valid_loader = DataLoader(
valid_dataset,
batch_size=config.batch_size,
shuffle=True,
collate_fn=collate_fn,
num_workers=config.num_workers
)
metric = get_metric(metric_name='CER', vocab=vocab)
model, valid_loss, valid_cer = trainer(
'valid',
config,
valid_loader,
optimizer,
model,
criterion,
metric,
train_begin_time,
device
)
print('[INFO] Epoch %d (Validation) Loss %0.4f CER %0.4f' % (epoch, valid_loss, valid_cer))
nsml.report(
summary=True,
epoch=epoch,
train_loss=train_loss,
train_cer=train_cer,
step=epoch*len(train_loader),
lr = optimizer.get_lr(),
val_loss=valid_loss,
val_cer=valid_cer
)
if epoch % config.checkpoint_every == 0:
nsml.save(epoch)
torch.cuda.empty_cache()
print(f'[INFO] epoch {epoch} is done')
print('[INFO] train process is done')
if __name__ == '__main__':
config = get_args()
warnings.filterwarnings('ignore')
seed_fix(config.seed)
run(config)