<jupyter_start><jupyter_text>Deep Learning in Audio Classification in Python<jupyter_code>import os
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from scipy.io import wavfile
from python_speech_features import mfcc
from tqdm import tqdm
from keras.models import load_model
import pickle
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report, confusion_matrix
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))
        Class = fn2class[fn]
        c = classes.index(Class)
        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)
            
            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[fn] = np.mean(y_prob, axis = 0).flatten()
    return y_true, y_pred, fn_prob
class Config:
    def __init__(self, mode= 'conv', nfilt=26, nfeat=13, nfft = 2048, rate = 16000):
        self.mode = mode
        self.nfilt = nfilt
        self.nfeat = nfeat
        self.nfft = nfft
        self.rate = rate
        self.step = int(rate/10)
        self.model_path = os.path.join('models', mode + '.model')
        self.p_path = os.path.join('pickles', mode + '.p')<jupyter_output><empty_output><jupyter_text>Data Extraction<jupyter_code>os.listdir('Temp_Dataset/')
classes = list(os.listdir('Temp_Dataset/validation/'))

print("Number of Classes in the Data Set:", len(classes), "Classes")
print("The classes of the dataset are   :", classes[0], ",", classes[1])<jupyter_output>Number of Classes in the Data Set: 2 Classes
The classes of the dataset are   : not_sick , sick<jupyter_text>Creating the dataframe with basic column names<jupyter_code>column_names = ['Fname','Class', 'Length']
df = pd.DataFrame(columns = column_names)
df.info()
# dataset_directory = 'Dataset/validation/'
dataset_directory = 'Temp_Dataset/train/'
for c in list(classes):
    print('Number of files in the directory \'{}\' are {}'.format(c,len(os.listdir(dataset_directory+c))))
for c in list(classes):
    for n,f in tqdm(enumerate(os.listdir(dataset_directory+c))):
        rate, signal = wavfile.read(dataset_directory+str(c)+"/"+str(f))
        length = signal.shape[0]/rate
        f_df = pd.DataFrame({
            "Fname": str(f),
            "Class": str(c),
            "Length": length}, index = [n])
        df = df.append(f_df)
df.info()
fn2class = dict(zip(df.Fname, df.Class))
len(fn2class)<jupyter_output><empty_output><jupyter_text>Loading Models<jupyter_code>p_path = os.path.join('pickles', 'time.p')
with open(p_path, 'rb') as handle:
    config = pickle.load(handle)
model = load_model(config.model_path)<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>y_true, y_pred, fn_prob = build_predictions('Temp_train')
acc_score = accuracy_score(y_true=y_true, y_pred=y_pred)
acc_score
print(classification_report(y_true=y_true, y_pred=y_pred))
y_probs = []

for i, row in df.iterrows():
    y_prob = fn_prob[row.Fname]
    y_probs.append(y_prob)
    for c, p in zip(classes, y_prob):
        df.at[i,c] = p
y_predictions = [classes[np.argmax(y)] for y in y_probs]
df['y_pred'] = y_predictions
df.to_csv('predictions.csv', index = False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13128205128205128, 'fraction_numerical': 0.007692307692307693, 'mean_word_length': 3.2774122807017543, 'pattern_counts': {'":': 3, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '360414', 'license': 'MIT', 'n_tokens_mistral': 1400, 'n_tokens_neox': 1372, 'n_words': 292, 'path': 'Scripts/Predicting_Class.ipynb', 'repo_name': 'thadanipaarth/Non-Invasive-Public-Safety-Detection-System', 'size': '3900'}