-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
75 lines (64 loc) · 1.95 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
from torch.utils.data import DataLoader, random_split
from torch import nn
from torchaudio.transforms import MFCC
from torchvision.transforms import Compose
from nntoolbox.learner import SupervisedLearner
from nntoolbox.callbacks import *
from nntoolbox.metrics import *
from torch.optim import Adam
from src.utils import *
from src.models import *
print("Running Nhat's script")
batch_size = 128
frequency = 16000
lr = 0.001
transform_train = Compose(
[
# RandomCropCenter(45000),
MFCC(sample_rate=frequency, n_mfcc=30),
TimePad(216)
]
)
transform_val = Compose(
[
MFCC(sample_rate=frequency, n_mfcc=30),
TimePad(216)
]
)
for i in range(2):
print('===== Run {} ===='.format(i))
model = ICModel()
optimizer = Adam(model.parameters(), lr=lr)
train_val_dataset = ERCDataRaw("data/", True)
train_size = int(0.8 * len(train_val_dataset))
val_size = len(train_val_dataset) - train_size
train_data, val_data = stratified_random_split(
train_val_dataset, lengths=[train_size, val_size], transforms=[transform_train, transform_val]
)
train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True)
val_loader = DataLoader(val_data, batch_size=batch_size)
learner = SupervisedLearner(
train_loader, val_loader, model=model,
criterion=nn.CrossEntropyLoss(),
optimizer=optimizer,
mixup=True,
mixup_alpha=0.1
)
callbacks = [
ToDeviceCallback(),
LossLogger(),
ModelCheckpoint(learner=learner, filepath="weights/model_{}.pt".format(i), monitor='accuracy', mode='max'),
ReduceLROnPlateauCB(optimizer, patience=7, factor=0.5),
ConfusionMatrixCB(),
Tensorboard()
]
metrics = {
"accuracy": Accuracy(),
"loss": Loss()
}
final = learner.learn(
n_epoch=100,
callbacks=callbacks,
metrics=metrics,
final_metric='accuracy'
)