-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
92 lines (66 loc) · 2.58 KB
/
predict.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Mar 23 18:18:48 2020
@author: tsuyogbasnet
"""
import os
import sys
import pickle
from tqdm import tqdm
from scipy.io import wavfile
from python_speech_features import mfcc
from keras.models import load_model
import pandas as pd
from sklearn.metrics import accuracy_score
import numpy as np
def build_predictions(audio_dir):
y_true = []
y_pred = []
fn_prob = {}
if len(os.listdir(audio_dir)) == 0:
print("No files found for classification")
return False, False, False
print("Extracting feature from audio files")
for file in tqdm(os.listdir(audio_dir)):
try:
rate, signal = wavfile.read(os.path.join(audio_dir, file))
label = filename_to_class[file]
c = classes.index(label)
y_prob = []
print("Classifying audio files")
for i in tqdm(range(0, signal.shape[0]-config.step, config.step)):
sample = signal[i:i+config.step]
x = mfcc(sample, rate, numcep=config.nfeat, nfilt=config.nfilt, nfft=config.nfft)
x = (x-config._min) / (config._max - config._min)
if config.mode == 'conv':
x = x.reshape(1, x.shape[0], x.shape[1], 1)
elif config.mode == 'time':
x = np.expand_dims(x, axis=0)
y_hat = model.predict(x)
y_prob.append(y_hat)
y_pred.append(np.argmax(y_hat))
y_true.append(c)
fn_prob[file] = np.mean(y_prob, axis=0).flatten()
except:
print("Something went wrong some files", sys.exc_info()[0])
return y_true, y_pred, fn_prob
data_frame = pd.read_csv('instruments.csv')
classes = list(np.unique(data_frame.label))
filename_to_class = dict(zip(data_frame.fname,data_frame.label))
p_path = os.path.join('pickles','conv.p')
with open(p_path, 'rb') as handle:
config = pickle.load(handle)
model = load_model(config.model_path)
y_true, y_pred, fn_prob = build_predictions('testcleanfiles')
if(y_true and y_pred and fn_prob):
acc_score = accuracy_score(y_true=y_true, y_pred=y_pred)
y_probs = []
for i, row in data_frame.iterrows():
y_prob = fn_prob[row.fname]
y_probs.append(y_prob)
for c, p in zip(classes, y_prob):
data_frame.at[i,c] = p
y_pred = [classes[np.argmax(y)] for y in y_probs]
data_frame['y_pred'] = y_pred
data_frame.to_csv('prediction.csv', index=False)