forked from gzhu06/Filler-semi-CRF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
166 lines (127 loc) · 6.02 KB
/
train.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
import os, glob
import numpy as np
from time import time
import torch
from torch.nn import functional as F
from torch import optim
from torch.utils.data import DataLoader
from sklearn.model_selection import train_test_split
from model import SEGCRF
import train_utils
from data_utils import MelLabelIntervalCollate, MelLabelIntervalLoader
from params import ARGS
os.environ['CUDA_VISIBLE_DEVICES'] = ARGS.gpu_idx
device = torch.device("cuda:0" if torch.cuda.is_available() and ARGS.cuda else "cpu")
pkl_foldername = 'train'
def ce_loss(inputs, target, mask):
ce = F.cross_entropy(inputs, target, reduction='none')
total_preds = torch.sum(torch.ones_like(mask) * mask)
return torch.sum(ce) / total_preds
def main(args):
"""Assume Single Node Multi GPUs Training Only"""
assert torch.cuda.is_available(), "CPU training is not allowed."
pklList = glob.glob(os.path.join(args.feature_path,
pkl_foldername+'_'+str(args.seg_len)+'_re','*.pkl'))
pklList_train, pklList_val = train_test_split(pklList, test_size=0.03,
random_state=2021,
shuffle=True)
train_and_eval(pklList_train, pklList_val)
def train_and_eval(train_list, val_list):
segmenter = SEGCRF(inputdim=ARGS.feature_bins,
numclass=ARGS.num_class,
backbone_type='s4',
labelres=int(ARGS.label_res/ARGS.seg_len)).to(device)
optimizer = optim.Adam(segmenter.parameters(), lr=ARGS.lr)
# dataset define
train_dataset = MelLabelIntervalLoader(train_list, specAug=ARGS.spec_aug,
num_class=ARGS.num_class,
label_res=ARGS.seg_len*1.0/ARGS.label_res,
label_len=ARGS.label_res,
label_map=ARGS.label_map)
val_dataset = MelLabelIntervalLoader(val_list, num_class=ARGS.num_class,
label_res=ARGS.seg_len*1.0/ARGS.label_res,
label_len=ARGS.label_res,
label_map=ARGS.label_map)
collate_fn = MelLabelIntervalCollate(ARGS.num_class)
# loader define
train_loader = DataLoader(train_dataset, num_workers=ARGS.num_workers,
shuffle=False, batch_size=ARGS.batch_size,
pin_memory=True, drop_last=False,
collate_fn=collate_fn)
val_loader = DataLoader(val_dataset, num_workers=ARGS.num_workers,
shuffle=False, batch_size=ARGS.batch_size,
pin_memory=True, drop_last=False,
collate_fn=collate_fn)
epoch_str = 0
best_acc = 0
iteration = 0
# load from checkpoint
os.makedirs(ARGS.ckpt_path, exist_ok=True)
os.makedirs(os.path.join(ARGS.ckpt_path, 'ckpts'), exist_ok=True)
for epoch in range(epoch_str, ARGS.epoch):
iteration = train(epoch,
iteration,
segmenter,
optimizer,
train_loader,
ARGS.ckpt_path)
best_acc = evaluate(iteration,
segmenter,
val_loader,
ARGS.ckpt_path,
best_acc)
torch.save({'backbone_dict':segmenter.state_dict(),
'optimizer_state_dict':optimizer.state_dict()},
'{0}/ckpts/model_{1}'.format(ARGS.ckpt_path, epoch))
# Learning rate decay
if epoch % ARGS.lr_step == 0 and epoch > 0:
optimizer.param_groups[0]['lr'] *= ARGS.lr_decay
def train(epoch, iteration, model, optimizer, train_loader, ckpt_folder):
model.train()
for _, (mels, labels) in enumerate(train_loader):
# Optimization Routine
optimizer.zero_grad()
orig_time = time()
mels = mels.to(device, non_blocking=True)
# Train model
logp, crf = model(mels, labels)
loss = (-logp.sum(-1).mean())
# Compute accuracy
train_acc = train_utils.accuracy_crf(crf, labels, ARGS.label_res)
# backwards
loss.backward()
optimizer.step()
# Print training message
mesg = "Time:{0:.2f}, Epoch:{1}, Iteration:{2}, Loss:{3:.3f}, " \
"Train Accuracy:{4:.3f}, Learning Rate:{5:.6f}".format(time()-orig_time,
epoch, iteration, loss.item(), train_acc, optimizer.param_groups[0]['lr'])
print(mesg)
with open(os.path.join(ckpt_folder, 'train_loss.txt'), "a") as f:
f.write("{0},{1},{2}\n".format(iteration, loss.item(), train_acc))
iteration += 1
return iteration
def evaluate(iteration, model, val_loader, ckpt_folder, best_accuracy):
model.eval()
losses = []
accs = []
with torch.no_grad():
for _, (mels, labels) in enumerate(val_loader):
mels = mels.to(device, non_blocking=True)
# evaluate model
logp, crf = model(mels, labels)
loss = (-logp.sum(-1).mean())
acc = train_utils.accuracy_crf(crf, labels, ARGS.label_res)
losses.append(loss.item())
accs.append(acc)
loss_val = np.mean(np.array(losses))
acc_val = np.mean(np.array(accs))
with open(os.path.join(ckpt_folder, 'val_loss.txt'), "a") as f:
f.write("{0},{1},{2}\n".format(iteration, loss_val, acc_val))
if acc_val > best_accuracy:
best_accuracy = acc_val
torch.save({'backbone_dict':model.state_dict()},
os.path.join(ckpt_folder, 'best_embedding.pt'))
print('Best model saved!')
return best_accuracy
if __name__ == "__main__":
main(ARGS)