forked from ruohoruotsi/LSTM-Music-Genre-Classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lstm_genre_classifier_pytorch.py
226 lines (181 loc) · 8.71 KB
/
lstm_genre_classifier_pytorch.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
PyTorch implementation of a simple 2-layer-deep LSTM for genre classification of musical audio.
Feeding the LSTM stack are spectral {centroid, contrast}, chromagram & MFCC features (33 total values)
Question: Why is there a PyTorch implementation, when we already have Keras/Tensorflow?
Answer: So that we can learn more PyTorch and experiment with modulations on basic
architectures within the space of an "easy problem". For example, SRU or SincNets.
I'm am also curious about the relative performances of both toolkits.
The plan, first start with a torch.nn implementation, then go for the torch.nn.LSTMCell
"""
import os
import torch
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import matplotlib.pyplot as plt
from GenreFeatureData import (
GenreFeatureData,
) # local python class with Audio feature extraction (librosa)
genre_features = GenreFeatureData()
# if all of the preprocessed files do not exist, regenerate them all for self-consistency
if (
os.path.isfile(genre_features.train_X_preprocessed_data)
and os.path.isfile(genre_features.train_Y_preprocessed_data)
and os.path.isfile(genre_features.dev_X_preprocessed_data)
and os.path.isfile(genre_features.dev_Y_preprocessed_data)
and os.path.isfile(genre_features.test_X_preprocessed_data)
and os.path.isfile(genre_features.test_Y_preprocessed_data)
):
print("Preprocessed files exist, deserializing npy files")
genre_features.load_deserialize_data()
else:
print("Preprocessing raw audio files")
genre_features.load_preprocess_data()
train_X = torch.from_numpy(genre_features.train_X).type(torch.Tensor)
dev_X = torch.from_numpy(genre_features.dev_X).type(torch.Tensor)
test_X = torch.from_numpy(genre_features.test_X).type(torch.Tensor)
# Targets is a long tensor of size (N,) which tells the true class of the sample.
train_Y = torch.from_numpy(genre_features.train_Y).type(torch.LongTensor)
dev_Y = torch.from_numpy(genre_features.dev_Y).type(torch.LongTensor)
test_Y = torch.from_numpy(genre_features.test_Y).type(torch.LongTensor)
# Convert {training, test} torch.Tensors
print("Training X shape: " + str(genre_features.train_X.shape))
print("Training Y shape: " + str(genre_features.train_Y.shape))
print("Validation X shape: " + str(genre_features.dev_X.shape))
print("Validation Y shape: " + str(genre_features.dev_Y.shape))
print("Test X shape: " + str(genre_features.test_X.shape))
print("Test Y shape: " + str(genre_features.test_Y.shape))
# class definition
class LSTM(nn.Module):
def __init__(self, input_dim, hidden_dim, batch_size, output_dim=8, num_layers=2):
super(LSTM, self).__init__()
self.input_dim = input_dim
self.hidden_dim = hidden_dim
self.batch_size = batch_size
self.num_layers = num_layers
# setup LSTM layer
self.lstm = nn.LSTM(self.input_dim, self.hidden_dim, self.num_layers)
# setup output layer
self.linear = nn.Linear(self.hidden_dim, output_dim)
def init_hidden(self):
return (
torch.zeros(self.num_layers, self.batch_size, self.hidden_dim),
torch.zeros(self.num_layers, self.batch_size, self.hidden_dim),
)
def forward(self, input):
# lstm step => then ONLY take the sequence's final timetep to pass into the linear/dense layer
# Note: lstm_out contains outputs for every step of the sequence we are looping over (for BPTT)
# but we just need the output of the last step of the sequence, aka lstm_out[-1]
lstm_out, hidden = self.lstm(input)
logits = self.linear(lstm_out[-1])
genre_scores = F.log_softmax(logits, dim=1)
return genre_scores
def get_accuracy(self, logits, target):
""" compute accuracy for training round """
corrects = (
torch.max(logits, 1)[1].view(target.size()).data == target.data
).sum()
accuracy = 100.0 * corrects / self.batch_size
return accuracy.item()
batch_size = 35 # num of training examples per minibatch
num_epochs = 400
# Define model
print("Build LSTM RNN model ...")
model = LSTM(
input_dim=33, hidden_dim=128, batch_size=batch_size, output_dim=8, num_layers=2
)
loss_function = nn.NLLLoss() # expects ouputs from LogSoftmax
optimizer = optim.Adam(model.parameters(), lr=0.001)
train_on_gpu = torch.cuda.is_available()
if train_on_gpu:
print("\nTraining on GPU")
else:
print("\nNo GPU, training on CPU")
# all training data (epoch) / batch_size == num_batches (12)
num_batches = int(train_X.shape[0] / batch_size)
num_dev_batches = int(dev_X.shape[0] / batch_size)
val_loss_list, val_accuracy_list, epoch_list = [], [], []
print("Training ...")
for epoch in range(num_epochs):
train_running_loss, train_acc = 0.0, 0.0
# Init hidden state - if you don't want a stateful LSTM (between epochs)
model.hidden = model.init_hidden()
for i in range(num_batches):
# zero out gradient, so they don't accumulate btw epochs
model.zero_grad()
# train_X shape: (total # of training examples, sequence_length, input_dim)
# train_Y shape: (total # of training examples, # output classes)
#
# Slice out local minibatches & labels => Note that we *permute* the local minibatch to
# match the PyTorch expected input tensor format of (sequence_length, batch size, input_dim)
X_local_minibatch, y_local_minibatch = (
train_X[i * batch_size : (i + 1) * batch_size,],
train_Y[i * batch_size : (i + 1) * batch_size,],
)
# Reshape input & targets to "match" what the loss_function wants
X_local_minibatch = X_local_minibatch.permute(1, 0, 2)
# NLLLoss does not expect a one-hot encoded vector as the target, but class indices
y_local_minibatch = torch.max(y_local_minibatch, 1)[1]
y_pred = model(X_local_minibatch) # fwd the bass (forward pass)
loss = loss_function(y_pred, y_local_minibatch) # compute loss
loss.backward() # reeeeewind (backward pass)
optimizer.step() # parameter update
train_running_loss += loss.detach().item() # unpacks the tensor into a scalar value
train_acc += model.get_accuracy(y_pred, y_local_minibatch)
print(
"Epoch: %d | NLLoss: %.4f | Train Accuracy: %.2f"
% (epoch, train_running_loss / num_batches, train_acc / num_batches)
)
print("Validation ...") # should this be done every N epochs
if epoch % 10 == 0:
val_running_loss, val_acc = 0.0, 0.0
# Compute validation loss, accuracy. Use torch.no_grad() & model.eval()
with torch.no_grad():
model.eval()
model.hidden = model.init_hidden()
for i in range(num_dev_batches):
X_local_validation_minibatch, y_local_validation_minibatch = (
dev_X[i * batch_size : (i + 1) * batch_size,],
dev_Y[i * batch_size : (i + 1) * batch_size,],
)
X_local_minibatch = X_local_validation_minibatch.permute(1, 0, 2)
y_local_minibatch = torch.max(y_local_validation_minibatch, 1)[1]
y_pred = model(X_local_minibatch)
val_loss = loss_function(y_pred, y_local_minibatch)
val_running_loss += (
val_loss.detach().item()
) # unpacks the tensor into a scalar value
val_acc += model.get_accuracy(y_pred, y_local_minibatch)
model.train() # reset to train mode after iterationg through validation data
print(
"Epoch: %d | NLLoss: %.4f | Train Accuracy: %.2f | Val Loss %.4f | Val Accuracy: %.2f"
% (
epoch,
train_running_loss / num_batches,
train_acc / num_batches,
val_running_loss / num_dev_batches,
val_acc / num_dev_batches,
)
)
epoch_list.append(epoch)
val_accuracy_list.append(val_acc / num_dev_batches)
val_loss_list.append(val_running_loss / num_dev_batches)
# visualization loss
plt.plot(epoch_list, val_loss_list)
plt.xlabel("# of epochs")
plt.ylabel("Loss")
plt.title("LSTM: Loss vs # epochs")
plt.show()
# visualization accuracy
plt.plot(epoch_list, val_accuracy_list, color="red")
plt.xlabel("# of epochs")
plt.ylabel("Accuracy")
plt.title("LSTM: Accuracy vs # epochs")
# plt.savefig('graph.png')
plt.show()
print("Testing ...")
# File issue to add pytorch data loaders, is there an open GTZAN pytorch dataloader?
# where to add them? keras or pytorch data repos?