-
Notifications
You must be signed in to change notification settings - Fork 0
/
train_rnn.py
79 lines (65 loc) · 1.81 KB
/
train_rnn.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
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 *
batch_size = 128
frequency = 16000
lr = 0.001
transform_train = Compose(
[
RandomCropCenter(30000),
MFCC(sample_rate=frequency),
TimePad(280)
]
)
transform_val = Compose(
[
MFCC(sample_rate=frequency),
TimePad(280)
]
)
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 = random_split_before_transform(
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)
model = RNNModelV2()
optimizer = Adam(model.parameters(), lr=lr)
learner = SupervisedLearner(
train_loader, val_loader, model=model,
criterion=nn.CrossEntropyLoss(),
optimizer=optimizer
)
callbacks = [
ToDeviceCallback(),
LossLogger(),
ModelCheckpoint(learner=learner, filepath="weights/model.pt", monitor='accuracy', mode='max'),
ReduceLROnPlateauCB(optimizer, patience=7),
Tensorboard()
]
metrics = {
"accuracy": Accuracy(),
"loss": Loss()
}
final = learner.learn(
n_epoch=500,
callbacks=callbacks,
metrics=metrics,
final_metric='accuracy'
)
class ConvGRUCell(nn.Module):
def forward(self, input, hidden=None):
"""
:param input: (N, C, H, W)
:param hidden: (N, C_hidden, H, W)
:return:
"""