-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlocal.py
More file actions
192 lines (178 loc) · 7.24 KB
/
local.py
File metadata and controls
192 lines (178 loc) · 7.24 KB
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
from __future__ import unicode_literals
import matplotlib.pyplot as plt
from scipy.io import wavfile
import numpy as np
import wave
import sys
import librosa
import librosa.display
import os
import torch.nn as nn
import torch.nn.functional as F
import torch
import torchvision
import torchvision.transforms as transforms
import numpy as np
from sklearn.model_selection import StratifiedShuffleSplit
from local_model import *
from torch.utils import data
# 0 = hyenas 1 = lions
mfccs = []
deltas = []
delta_deltas = []
label = []
X = []
min_length = 430
path_hyena = 'data/processed data9/hyenas/'
path_lion = 'data/processed data9/lions/'
min_shape = 1000
for file in os.listdir(path_hyena):
hyena = np.load(path_hyena + file)
if min_shape > hyena[0].shape[1]:
min_shape = hyena[0].shape[1]
mfcc = np.asarray(hyena[0][:, :min_length])
delta = np.asarray(hyena[1][:, :min_length])
delta_delta = np.asarray(hyena[2][:, :min_length])
mfccs.append(mfcc)
deltas.append(delta)
delta_deltas.append(delta_delta)
X.append([mfcc, delta, delta_delta])
label.append(0)
for file in os.listdir(path_lion):
lion = np.load(path_lion + file)
if min_shape > lion[0].shape[1]:
min_shape = lion[0].shape[1]
mfcc = np.asarray(lion[0][:, :min_length])
delta = np.asarray(lion[1][:, :min_length])
delta_delta = np.asarray(lion[2][:, :min_length])
mfccs.append(mfcc)
deltas.append(delta)
delta_deltas.append(delta_delta)
X.append([mfcc, delta, delta_delta])
label.append(1)
print('Minimum length all of our tensors is: %i' % (min_shape))
split_test = StratifiedShuffleSplit(n_splits=1, test_size=0.2, random_state=0)
split_val = split_1 = StratifiedShuffleSplit(n_splits=1, test_size=0.1, random_state=0)
X = np.asarray(X)
label = np.asarray(label)
print('done')
for train_index, test_index in split_test.split(X, label):
X_train, X_test = X[train_index], X[test_index]
y_train, y_test = label[train_index], label[test_index]
for train_index, test_index in split_val.split(X_train, y_train):
X_train, X_val = X_train[train_index], X_train[test_index]
y_train, y_val = y_train[train_index], y_train[test_index]
# printing out number of classes in each
print('Training has %d samples of class 0 and %d samples of class 1' % (
np.unique(y_train, return_counts=True)[1][0], np.unique(y_train, return_counts=True)[1][1]))
print('Validation has %d samples of class 0 and %d samples of class 1' % (
np.unique(y_val, return_counts=True)[1][0], np.unique(y_val, return_counts=True)[1][1]))
print('Testing has %d samples of class 0 and %d samples of class 1' % (
np.unique(y_test, return_counts=True)[1][0], np.unique(y_test, return_counts=True)[1][1]))
class testingDataset(data.Dataset):
def __init__(self, data, labels):
self.labels = labels
self.data = data
def __len__(self):
return len(self.data)
def __getitem__(self, index):
sample = self.data[index]
sample_label = self.labels[index]
return sample, sample_label
seed = 1
batch_size = 64
def main_test():
torch.manual_seed(seed)
model, loss_function, optimizer = load_model(lr, seed, mfcc_total)
train_dataset = testingDataset(X_train,y_train)
train_loader = data.DataLoader(train_dataset,batch_size = batch_size, shuffle=True)
valid_dataset = testingDataset(X_val,y_val)
valid_loader = data.DataLoader(valid_dataset,batch_size = batch_size)
tloss = []
tacc = []
vloss = []
vacc = []
for epoch in range(epochs):
print(epoch)
taccumloss = 0
tcorrect = 0
ttotal = 0
tnumberofBatches = 0
vnumberofBatches = 0
vaccumloss = 0
vcorrect = 0
vtotal = 0
for i, batch in enumerate(train_loader):
tmp_prediction = []
input, label = batch
optimizer.zero_grad()
# Obtaining our mfcc, delta and delta_delta from X and converting to tensor
for j in range(len(input)):
mfcc = input[j][0].unsqueeze(0).unsqueeze(0)
delta = input[j][1].unsqueeze(0).unsqueeze(0)
delta_delta = input[j][2].unsqueeze(0).unsqueeze(0)
prediction = model(mfcc,delta,delta_delta)
tmp_prediction.append(prediction)
tmp_prediction = torch.stack(tmp_prediction)
loss = loss_function(input = tmp_prediction.squeeze(),target = label.float())
loss.backward()
optimizer.step()
with torch.no_grad():
for batch in train_loader:
tmp_prediction = []
input, label = batch
optimizer.zero_grad()
# Obtaining our mfcc, delta and delta_delta from X and converting to tensor
for j in range(len(input)):
mfcc = input[j][0].unsqueeze(0).unsqueeze(0)
delta = input[j][1].unsqueeze(0).unsqueeze(0)
delta_delta = input[j][2].unsqueeze(0).unsqueeze(0)
prediction = model(mfcc, delta, delta_delta)
tmp_prediction.append(prediction)
tmp_prediction = torch.tensor(tmp_prediction, requires_grad=True)
loss = loss_function(input=tmp_prediction, target=label.float())
taccumloss += loss.item()
for i in range(len(tmp_prediction)):
if tmp_prediction[i] >= 0.5 and label[i] == 1:
tcorrect += 1
elif tmp_prediction[i] < 0.5 and label[i] == 0:
tcorrect += 1
ttotal += label.float().size(0)
tnumberofBatches += 1
with torch.no_grad():
print("in loop")
for batch in valid_loader:
tmp_prediction = []
input, label = batch
# Obtaining our mfcc, delta and delta_delta from X and converting to tensor
for j in range(len(input)):
mfcc = input[j][0].unsqueeze(0).unsqueeze(0)
delta = input[j][1].unsqueeze(0).unsqueeze(0)
delta_delta = input[j][2].unsqueeze(0).unsqueeze(0)
prediction = model(mfcc, delta, delta_delta)
tmp_prediction.append(prediction)
tmp_prediction = torch.tensor(tmp_prediction, requires_grad=True)
loss = loss_function(input=tmp_prediction, target=label.float())
vaccumloss += loss.item()
for i in range(len(tmp_prediction)):
if tmp_prediction[i] >= 0.5 and label[i] == 1:
vcorrect += 1
elif tmp_prediction[i] < 0.5 and label[i] == 0:
vcorrect += 1
vtotal += label.size(0)
vnumberofBatches += 1
print(vnumberofBatches)
tacc.append(tcorrect/ttotal)
tloss.append(taccumloss/tnumberofBatches)
vacc.append(vcorrect / vtotal)
vloss.append(vaccumloss / vnumberofBatches)
###### FILL THIS OUT ######
#torch.save(model,'test_working.pt')
return tloss, tacc, vloss, vacc
tloss, tacc, vloss, vacc = main_test()
epoch = range(50)
plt.plot(epoch, tloss, label='Training Loss')
print(tloss)
print(vloss)
print(tacc)
print(vacc)