-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_interface.py
179 lines (146 loc) · 7.32 KB
/
train_interface.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
"""
Train interface for speech enhancement!
You can just run this file.
"""
import os
import argparse
import torch
import options
import utils
import datetime
import random
import numpy as np
import time
from dataloader import create_dataloader
######################################################################################################################
# Parser init #
######################################################################################################################
opt = options.Options().init(argparse.ArgumentParser(description='speech enhancement')).parse_args()
print(opt)
######################################################################################################################
# Set a model (check point) and a log folder #
######################################################################################################################
if not opt.pretrained or opt.pretrained_init:
dir_name = os.path.dirname(os.path.abspath(__file__)) # absolute path
print(dir_name)
date = datetime.datetime.now()
log_dir = os.path.join(dir_name, 'log', opt.arch + '_' + str(date.month) + str(date.day) + '_' + opt.env)
utils.mkdir(log_dir)
print("Now time is : ", date.isoformat())
tboard_dir = os.path.join(log_dir, 'logs')
model_dir = os.path.join(log_dir, 'models')
utils.mkdir(model_dir) # make a dir if there is no dir (given path)
utils.mkdir(tboard_dir)
else:
model_dir = opt.pretrain_model_path[:-17] + 'models'
tboard_dir = opt.pretrain_model_path[:-17] + 'logs'
######################################################################################################################
# Model init #
######################################################################################################################
# set device
DEVICE = torch.device(opt.device)
# set seeds
random.seed(1234)
np.random.seed(1234)
torch.manual_seed(1234)
torch.cuda.manual_seed(1234)
torch.cuda.manual_seed_all(1234)
# define model
model = utils.get_arch(opt)
total_params = utils.cal_total_params(model)
print('total params : %d (%.2f M, %.2f MBytes)\n' %
(total_params,
total_params / 1000000.0,
total_params * 4.0 / 1000000.0))
# define loss type
trainer, validator = utils.get_train_mode(opt)
loss_calculator = utils.get_loss(opt)
# define optimizer
optimizer = torch.optim.AdamW(model.parameters(), lr=opt.lr_initial)
model = model.to(DEVICE)
if opt.stage1_training:
from s3prl.nn import S3PRLUpstream, Featurizer
srl_model = S3PRLUpstream("wavlm_large").to(DEVICE)
featurizer = Featurizer(srl_model)
featurizer = featurizer.to(DEVICE)
# load the params if there is pretrained model
epoch_start_idx = 1
if opt.pretrained:
print('Load the pretrained model...')
chkpt = torch.load(opt.pretrain_model_path, map_location=DEVICE)
model.load_state_dict(chkpt['model'], strict=False)
if opt.stage1_training:
featurizer.load_state_dict(chkpt['featurizer'], strict=False)
if not opt.pretrained_init:
optimizer.load_state_dict(chkpt['optimizer'])
epoch_start_idx = chkpt['epoch'] + 1
utils.optimizer_to(optimizer, DEVICE)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=opt.decay_epoch, gamma=0.5)
######################################################################################################################
# Create Dataloader #
######################################################################################################################
train_loader = create_dataloader(opt, mode='train')
valid_loader = create_dataloader(opt, mode='valid')
print("Sizeof training set: ", train_loader.__len__(),
", sizeof validation set: ", valid_loader.__len__())
######################################################################################################################
######################################################################################################################
# Main program - train #
######################################################################################################################
######################################################################################################################
writer = utils.Writer(tboard_dir)
train_log_fp = open(model_dir + '/train_log.txt', 'a')
print('Train start...')
if opt.stage1_training:
for epoch in range(epoch_start_idx, opt.nepoch + 1):
st_time = time.time()
# train
train_loss = trainer(model, srl_model, featurizer, train_loader, loss_calculator, optimizer,
writer, epoch, DEVICE, opt)
# save checkpoint file to resume training
save_path = str(model_dir + '/chkpt_%d.pt' % epoch)
torch.save({
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'featurizer': featurizer.state_dict(),
'epoch': epoch
}, save_path)
# validate
valid_loss, pesq, stoi = validator(model, srl_model, featurizer, valid_loader, loss_calculator,
writer, epoch, DEVICE, opt)
print('EPOCH[{}] T {:.6f} | V {:.6f} takes {:.3f} seconds'
.format(epoch, train_loss, valid_loss, time.time() - st_time))
print('PESQ {:.6f} | STOI {:.6f}'.format(pesq, stoi))
# write train log
train_log_fp.write('EPOCH[{}] T {:.6f} | V {:.6f} takes {:.3f} seconds'
.format(epoch, train_loss, valid_loss, time.time() - st_time))
train_log_fp.write('PESQ {:.6f} | STOI {:.6f}\n'.format(pesq, stoi))
# scheduler
scheduler.step()
else:
for epoch in range(epoch_start_idx, opt.nepoch + 1):
st_time = time.time()
# train
train_loss = trainer(model, train_loader, loss_calculator, optimizer,
writer, epoch, DEVICE, opt)
# save checkpoint file to resume training
save_path = str(model_dir + '/chkpt_%d.pt' % epoch)
torch.save({
'model': model.state_dict(),
'optimizer': optimizer.state_dict(),
'epoch': epoch
}, save_path)
# validate
valid_loss, pesq, stoi = validator(model, valid_loader, loss_calculator,
writer, epoch, DEVICE, opt)
print('EPOCH[{}] T {:.6f} | V {:.6f} takes {:.3f} seconds'
.format(epoch, train_loss, valid_loss, time.time() - st_time))
print('PESQ {:.6f} | STOI {:.6f}'.format(pesq, stoi))
# write train log
train_log_fp.write('EPOCH[{}] T {:.6f} | V {:.6f} takes {:.3f} seconds'
.format(epoch, train_loss, valid_loss, time.time() - st_time))
train_log_fp.write('PESQ {:.6f} | STOI {:.6f}\n'.format(pesq, stoi))
# scheduler
scheduler.step()
print('Training has been finished.')
train_log_fp.close()