Last active
October 2, 2019 16:14
-
-
Save epietrowicz/efd67d072876437d5d83a521539b545f to your computer and use it in GitHub Desktop.
predict
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pickle | |
import os | |
import numpy as np | |
import pandas as pd | |
from tqdm import tqdm | |
from scipy.io import wavfile | |
from python_speech_features import mfcc | |
from keras.models import load_model | |
from sklearn.metrics import accuracy_score | |
def build_predictions(audio_dir): | |
y_true = [] | |
y_pred = [] | |
fn_prob = {} | |
print('Extracting features from audio') | |
for fn in tqdm(os.listdir(audio_dir)): | |
rate, wav = wavfile.read(os.path.join(audio_dir, fn)) | |
label = fn2class[fn] | |
c = classes.index(label) | |
y_prob = [] | |
for i in range(0, wav.shape[0]-config.step, config.step): | |
sample = wav[i:i+config.step] | |
x = mfcc(sample, rate, numcep=config.nfeat, | |
nfilt=config.nfilt, nfft=config.nfft).T | |
x = (x - config.min) / (config.max - config.min) | |
x = x.reshape(1, x.shape[0], x.shape[1], 1) | |
y_hat = model.predict(x) | |
y_prob.append(y_hat) | |
y_pred.append(np.argmax(y_hat)) | |
y_true.append(c) | |
fn_prob[fn] = np.mean(y_prob, axis=0).flatten() | |
print(fn_prob) | |
return y_true, y_pred, fn_prob | |
df = pd.read_csv('office_sounds.csv') | |
classes = list(np.unique(df.class_name)) | |
fn2class = dict(zip(df.slice_file_name, df.class_name)) | |
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('clean') | |
acc_score = accuracy_score(y_true=y_true, y_pred=y_pred) | |
y_probs = [] | |
for i, row in df.iterrows(): | |
y_prob = fn_prob[row.slice_file_name] | |
y_probs.append(y_prob) | |
for c, p in zip(classes, y_prob): | |
df.at[i, c] = p | |
y_pred = [classes[np.argmax(y)] for y in y_probs] | |
df['y_pred'] = y_pred | |
df.to_csv('predictions.csv', index=False) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment