-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_model.py
More file actions
103 lines (67 loc) · 2.94 KB
/
Copy pathtest_model.py
File metadata and controls
103 lines (67 loc) · 2.94 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
# -*- coding: utf-8 -*-
"""
Created on Mon Dec 4 18:01:12 2017
@author: Amir Lashkari
ML Test
"""
from os import listdir
from os.path import isfile, join
from scipy.io import wavfile
from scipy import signal
import resampy
import sys
import numpy as np
import keras as ke
from keras.models import load_model
if __name__ == "__main__":
model_path = sys.argv[1]
data_path = sys.argv[2]
processed = sys.argv[3]
if processed==0:
print("Loading & Preprocessing Data!!")
onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path, f))]
# adr = mypath + '\\' + onlyfiles[0]
j=0
FS = 4410
X = np.zeros([(len(onlyfiles)-1),128**2])
Y = np.zeros([(len(onlyfiles)-1),1])
for i in range(1,(len(onlyfiles)-1)):
adr = data_path + '\\' + onlyfiles[i]
try:
# Read WAV file from the directory
fs, data = wavfile.read(adr)
# Changes the sampling rate of the WAV file into 4410 Hz
z = resampy.resample(data[:,0], fs, FS)
# Fixing the length of all files into 4 seconds (zero padding for shorter files and cutting for longer files)
if (data.shape[0]>(fs*4)):
x = z[0:(FS*4)]
else:
x = np.zeros([(FS*4),1])
x[0:z.shape[0]] = np.reshape(z,(z.shape[0],1))
# Calculatinf the short-time Fourier transform of the WAV file.
f, t, xx = signal.stft(x[:,0], FS, nperseg=277)
# Discarding the frequencies abouve 20KHz. The output will be a 128x128 matrix
xx = xx[:128,:]
# Reshaping the 128*128 matrix into a vector, and saving it with its corresponding label.
X[j,:] = np.reshape(np.abs(xx),[np.shape(xx)[0]**2])
Y[j] = int(onlyfiles[i][-5])
j+=1
print("sample " + repr(i) + ": Read & Preprocessed")
except:
print("sample " + repr(i) + ": Passed")
X = X[0:(j-1),:]
Y =Y [0:(j-1)]
X/=np.max(X)
y_tst = ke.utils.to_categorical(Y, 10)
else:
print("Loading Preprocessed Data!!")
data = np.load(data_path)
X = np.float32(data['X_test'])
y_tst = np.float32(data['y_test'])
# Reshaping the input samples for Keras
x_tst = X.reshape(X.shape[0], 128, 128, 1)
print("Loading Model!!")
model = load_model(model_path)
score = model.evaluate(x_tst, y_tst, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])