-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_0.952.py
77 lines (61 loc) · 2.61 KB
/
train_0.952.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
from fastai.vision.all import *
from fastaudio.core.all import *
from fastaudio.augment.all import *
import numpy as np
import torch
import random
import pandas as pd
def random_seed(seed_value):
np.random.seed(seed_value)
torch.manual_seed(seed_value)
random.seed(seed_value)
torch.cuda.manual_seed(seed_value)
torch.cuda.manual_seed_all(seed_value)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def get_model(dls):
# Using mobilenet v2
model = mobilenet_v2(pretrained=False, num_classes=dls.c)
return model
if __name__ == "__main__":
random_seed(42)
df = pd.read_csv(f"dataset/Train.csv")
classes = sorted(list(set(df['class'].values.tolist())))
class2id = {x: i for i, x in enumerate(classes)}
num_classes = len(classes)
df = df[['id', 'class']]
df['id'] = df['id'].apply(lambda x: os.path.join("dataset/train", f"{x}.wav"))
df_test = pd.read_csv(f"dataset/SampleSubmission.csv")
df_test = df_test[['id']]
df_test['id'] = df_test['id'].apply(lambda x: os.path.join("dataset/test", f"{x}.wav"))
DBMelSpec = SpectrogramTransformer(mel=True, to_db=True)
aud2spec = DBMelSpec(n_mels=128, f_max=10000, n_fft=1024, hop_length=128, top_db=100)
aud_digit = DataBlock(blocks=(AudioBlock(force_mono=True),
CategoryBlock),
splitter=TrainTestSplitter(0.05,
stratify=df["class"],
shuffle=True),
get_x=ColReader(0),
get_y=ColReader(1),
item_tfms=[
RemoveSilence(),
ResizeSignal(2000),
aud2spec,
MaskFreq(size=10),
Delta(),
]
)
dls = aud_digit.dataloaders(df, bs=128, num_workers=4)
learn = Learner(dls, get_model(dls),
metrics=[accuracy],
loss_func=CrossEntropyLossFlat())
learn.lr_find()
learn.fine_tune(epochs=30,
cbs=SaveModelCallback(monitor='valid_loss'))
test_dl = dls.test_dl(df_test)
preds, _ = learn.tta(dl=test_dl)
preds = np.argmax(preds, axis=1)
df_test["class"] = [classes[x] for x in preds]
df_test["id"] = df_test["id"].apply(lambda x: x[13:-4])
df_test.to_csv("result/Submission_cnn_0.952.csv", index=False)
learn.export('saved_model/model_0.952.pkl')