-
Notifications
You must be signed in to change notification settings - Fork 0
/
lstm_mfcc.py
148 lines (131 loc) · 5.05 KB
/
lstm_mfcc.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
from torch.utils.data import dataset,dataloader
import pickle
import librosa
from config import model_config
from torch.utils.data.sampler import SubsetRandomSampler
from tqdm import tqdm
import torch
import numpy as np
from torch import optim
import torch.nn as nn
import torch.nn.functional as F
class soundDataset(dataset.Dataset):
def __init__(self):
# audio_vecs = pickle.load(open('file_vector_pickle.pkl', 'rb'))
self.emotion_list=pickle.load(open('emos.pkl','rb'))
self.audio_vec_list = pickle.load(open('audio_feats.pkl', 'rb'))
# self.emotion_list=[]
# self.audio_vec_list=[]
# for file,audio_vec in tqdm(audio_vecs.items()):
# self.emotion_list.append(int(file.split('-')[2])-1)
# self.audio_vec_list.append(librosa.feature.mfcc(audio_vec,sr=model_config['sr']).T)
# #we can specify hoplength here if needed
# fhand=open('audio_feats.pkl','wb')
# pickle.dump(self.audio_vec_list,fhand)
# fhand.close()
# fhand=open('emos.pkl','wb')
# pickle.dump(self.emotion_list, fhand)
# fhand.close()
def __getitem__(self, index):
return torch.from_numpy(self.audio_vec_list[index]),self.emotion_list[index]
def __len__(self):
return len(self.audio_vec_list)
class LSTM_mfcc(nn.Module):
def __init__(self):
super(LSTM_mfcc,self).__init__()
self.n_layers=2
self.input_dim=20
self.hidden_dim=40
self.output_dim=8
self.dropout=0.2
self.rnn=nn.LSTM(self.input_dim,self.hidden_dim,bias=True,num_layers=2,dropout=self.dropout,bidirectional=True)
self.out=nn.Linear(self.hidden_dim*2,self.output_dim)
self.softmax=F.softmax
def forward(self,input_seq):
rnn_output,(hidden,_)=self.rnn(input_seq)
#NO support for bidirectionals
x = torch.cat((rnn_output[-1][:, :40], rnn_output[0][:, 40:]))
x=self.out(x.view(1,self.hidden_dim*2))
class_scores=F.softmax(x,dim=1)
return class_scores
# class LSTM_mfcc(nn.Module):
# def __init__(self):
# super(LSTM_mfcc, self).__init__()
# self.n_layers = 2
# self.input_dim = 20
# self.hidden_dim = 40
# self.output_dim = 8
# self.dropout = 0.2
# self.rnn = nn.LSTM(self.input_dim, self.hidden_dim, bias=True,
# num_layers=2, dropout=self.dropout, bidirectional=False)
# self.out = nn.Linear(self.hidden_dim, self.output_dim)
# self.softmax = F.softmax
# def forward(self, input_seq):
# rnn_output, (hidden, _) = self.rnn(input_seq)
# #NO support for bidirectionals
# # print(hidden.shape)
# class_scores = F.softmax(self.out(rnn_output[-1]), dim=1)
# return class_scores
device = 'cpu'
model=LSTM_mfcc()
dset=soundDataset()
indices=np.array(list(range(len(dset))))
np.random.seed(42)
np.random.shuffle(indices)
dsplit=int(np.floor(len(dset)*0.8))
train_indices,test_indices=indices[:dsplit],indices[dsplit:]
train_sampler=SubsetRandomSampler(train_indices)
test_sampler=SubsetRandomSampler(test_indices)
train_loader=dataloader.DataLoader(dset,batch_size=1,sampler=train_sampler)
test_loader = dataloader.DataLoader(
dset, batch_size=1, sampler=test_sampler)
num_epochs=10
criterion=nn.CrossEntropyLoss()
optimizer=optim.Adam(model.parameters(),lr=0.00001)
for epoch in range(num_epochs):
model.train()
i=0
for data,target in train_loader:
print('train iteration',i)
i+=1
inputs=data[0].unsqueeze(1)
targets=target
inputs = inputs.to(device)
targets = targets.to(device)
model.zero_grad()
optimizer.zero_grad()
# print(inputs.shape)
predictions=model(inputs)
predictions=predictions.to(device)
loss=criterion(predictions,targets)
loss.backward()
optimizer.step()
if(i%10==0):
with torch.no_grad():
correct = 0
j = 0
for data, target in test_loader:
# print('test iteration', i)
j += 1
inputs = data[0].unsqueeze(1)
targets = target
inputs = inputs.to(device)
targets = targets.to(device)
predictions = model(inputs)
if(np.argmax(predictions, axis=1)[0].item() == targets[0].item()):
correct += 1
print('Accuracy is {:.2f}'.format(correct/len(test_indices)))
with torch.no_grad():
correct=0
i=0
for data, target in test_loader:
# print('test iteration',i)
i+=1
inputs = data[0].unsqueeze(1)
targets = target
inputs = inputs.to(device)
targets = targets.to(device)
predictions = model(inputs)
if(np.argmax(predictions, axis=1)[0].item() == targets[0].item()):
correct+=1
print('Accuracy is {:.2f}'.format(correct/len(test_indices)))