-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
131 lines (109 loc) · 4.38 KB
/
main.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
import librosa
import torch
import os
import pandas as pd
import torch.nn as nn
import torch.optim as optim
class Dataset(torch.utils.data.Dataset):
def __init__(self, data_path, label_path=None, input_len=16, mode='train', sr=16000, input_dim=63):
self.input_len = input_len
self.mode = mode
gt = pd.read_csv(label_path)
self.mfcc_samples = torch.zeros((gt.shape[0]+input_len, input_dim), dtype=torch.float)
print("loading data now")
for i, path in enumerate(gt['track']):
y, sr = librosa.load(os.path.join(data_path, path), sr=32000)
mfcc = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=1).squeeze()
mfcc_norm = librosa.util.normalize(mfcc)
mfcc_norm = torch.from_numpy(mfcc_norm).float()
self.mfcc_samples[i+input_len] = mfcc_norm
if mode == 'train':
self.target = torch.stack([torch.tensor(score) for score in gt['score']])
def __getitem__(self, index):
output = self.mfcc_samples[index:index+self.input_len]
if self.mode == 'train':
target = self.target[index]
return output, target
else:
return output
def __len__(self):
return len(self.mfcc_samples)-self.input_len
class GRU_regression(nn.Module):
def __init__(self, input_size, hidden_size, num_layers):
super().__init__()
self.gru = nn.GRU(input_size, hidden_size, num_layers)
self.fc = nn.Linear(hidden_size, 1)
def forward(self, _x):
x, h_n = self.gru(_x)
s, b, h = x.shape
x = x.reshape(s*b, h)
x = self.fc(x)
x = x.reshape(s, b, 1)
return x
def train(args):
dataset_train = Dataset('audios/clips','train.csv', input_len=args.input_len, \
mode='train', sr=args.sr, input_dim=args.input_dim)
train_loader = torch.utils.data.DataLoader(
dataset_train,
batch_size=1,
shuffle=False,
num_workers=0,
)
loss_function = nn.MSELoss()
model = GRU_regression(args.input_dim, hidden_size=args.hidden_dim, num_layers=1)
opt = optim.SGD(model.parameters(), lr=0.2)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
model.train()
for epoch in range(args.epochs):
losses = 0
for x, t in train_loader:
opt.zero_grad()
x = x.squeeze().reshape(args.input_len, 1, args.input_dim).to(device)
t = t.reshape(-1, 1, 1).to(device)
out = model(x)
loss = loss_function(out[-1].sigmoid(), t)
loss.backward()
opt.step()
losses += loss.item()
print('loss:', losses)
torch.save(model.state_dict(), 'model_final.pth')
def test(args):
model = GRU_regression(args.input_dim, hidden_size=args.hidden_dim, num_layers=1)
model.load_state_dict(torch.load('model_final.pth'))
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
model.eval()
dataset_test = Dataset('audios/clips','test.csv', input_len=args.input_len, mode='test', \
sr=args.sr, input_dim=args.input_dim)
test_loader = torch.utils.data.DataLoader(
dataset_test,
batch_size=1,
shuffle=False,
num_workers=0,
)
print('output test result:')
for x in test_loader:
x = x.squeeze().reshape(args.input_len, 1, args.input_dim).to(device)
out = model(x)
out = out[-1].sigmoid()
print(out.item())
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--device', default='0', type=str, help='which GPU')
parser.add_argument('--no_cuda', action='store_true', help='use CPU')
parser.add_argument('--epochs', default=1000, help='training time')
parser.add_argument('--input_len', default=32, help='GRU time step')
parser.add_argument('--input_dim', default=313, help='input dimension')
parser.add_argument('--hidden_dim', default=32, help='hidden dimension')
parser.add_argument('--sr', default=16000, help='sample rate')
parser.add_argument('--mode', default='train', help='chose training or testing')
args = parser.parse_args()
print(args)
if args.mode == 'train':
train(args)
else:
test(args)
if __name__ == '__main__':
main()