<jupyter_start><jupyter_text><jupyter_code>from google.colab import drive
drive.mount('/content/drive')
import os
import librosa.display as lbd
import librosa 
import sklearn
from sklearn.model_selection import train_test_split, cross_val_score
import numpy as np
import pandas as pd
import seaborn as sns

import matplotlib.pyplot as plt
%matplotlib inline
import warnings
warnings.filterwarnings('ignore')

import scipy.io.wavfile as sci_wav
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import *
train_data = pd.read_csv('/content/drive/MyDrive/Deep learning task-20210403T043535Z-001/Deep learning task/train_test_split.csv')
train_data.head(4)
#making training, test separate dfs
cat_train = train_data[['train_cat']].dropna().rename(index=str, columns={"train_cat":"file"}).assign(label=0)

dog_train = train_data[['train_dog']].dropna().rename(index=str, columns={"train_dog":"file"}).assign(label=1)

cat_test = train_data[['test_cat']].dropna().rename(index=str, columns={"test_cat":"file"}).assign(label=0)

dog_test = train_data[['test_dog']].dropna().rename(index=str, columns={"test_dog":"file"}).assign(label=1)

## concatenate all
train_df = pd.concat([cat_train, dog_train]).reset_index(drop=True)
test_df = pd.concat([cat_test, dog_test]).reset_index(drop=True)
df = pd.concat([train_df, test_df]).reset_index(drop=True)
df<jupyter_output><empty_output><jupyter_text>MFCC processing- The MFCC summarises the frequency distribution across the window size, so it is possible to analyse both the frequency and time characteristics of the sound. These audio representations will allow us to identify features for classification<jupyter_code>def features(file):
  y,sr = librosa.load(file, res_type='kaiser_fast')
  mfccs = librosa.feature.mfcc(y=y, sr=sr, n_mfcc=40)
  #applying transpose of mfcc and np.mean to get scaled value
  mfcc_scaled_feature = np.mean(mfccs.T, axis=0)
  return mfcc_scaled_feature<jupyter_output><empty_output><jupyter_text>Now applying the function to all the wav files present<jupyter_code>from tqdm import tqdm 

audio_dataset_path='/content/drive/MyDrive/Deep learning task-20210403T043535Z-001/Deep learning task/full_dataset/'
extracted_features = []
for idx_num, row in tqdm(df.iterrows()):
  file_name = os.path.join(os.path.abspath(audio_dataset_path),str(row["file"]))
  final_class_labels=row["label"]
  data=features(file_name)
  extracted_features.append([data,final_class_labels])
### converting extracted_features to Pandas dataframe
extracted_features_df=pd.DataFrame(extracted_features,columns=['feature','class'])
extracted_features_df.head()<jupyter_output><empty_output><jupyter_text>**Now we split the data into dependent and independent sets**<jupyter_code>x = np.array(extracted_features_df['feature'].tolist())
y = np.array(extracted_features_df['class'].tolist())<jupyter_output><empty_output><jupyter_text>Model Selection<jupyter_code>x_train, x_test, y_train, y_test = train_test_split(x,y,test_size=0.2, random_state=45)
x_train.shape<jupyter_output><empty_output><jupyter_text>Machine Learning Model<jupyter_code>from sklearn.pipeline import Pipeline
from sklearn.preprocessing import MinMaxScaler
from sklearn.ensemble import RandomForestClassifier

pipe = Pipeline([ ('scaler', MinMaxScaler()),
                 ('classifier', RandomForestClassifier())])
pipe.fit(x_train, y_train)
pipe.score(x_test,y_test)
y_pred = pipe.predict(x_test)

print('Classification Report \n{}'.format(sklearn.metrics.classification_report(y_test, y_pred)))<jupyter_output>Classification Report 
              precision    recall  f1-score   support

           0       0.83      1.00      0.91        34
           1       1.00      0.68      0.81        22

    accuracy                           0.88        56
   macro avg       0.91      0.84      0.86        56
weighted avg       0.90      0.88      0.87        56<jupyter_text>Cross Val Score<jupyter_code>cv=4
score = cross_val_score(pipe, x,y, cv=cv)
print('Cross-validation score for {cv} folds is \n{score}'.format(cv = cv, score = score))<jupyter_output>Cross-validation score for 4 folds is 
[0.9        0.89855072 0.85507246 0.86956522]<jupyter_text>Confusion Matrix<jupyter_code>sns.heatmap(sklearn.metrics.confusion_matrix(y_test, y_pred),annot=True, cmap='viridis')<jupyter_output><empty_output><jupyter_text>ANN<jupyter_code>model = Sequential()
model.add(Dense(64, activation='relu', input_shape=(40,)))
model.add(Dropout(0.2))

model.add(Dense(32, activation='relu'))
model.add(Dropout(0.3))

model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
history = model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=15, batch_size=64, verbose=1)
test_accuracy=model.evaluate(x_test,y_test,verbose=0)
print(test_accuracy[1])
sns.heatmap(sklearn.metrics.confusion_matrix(model.predict_classes(x_test), y_test),annot=True, cmap='viridis')
print('Classification Report \n{}'.format(sklearn.metrics.classification_report(y_test, model.predict_classes(x_test))))<jupyter_output>Classification Report 
              precision    recall  f1-score   support

           0       0.88      0.85      0.87        34
           1       0.78      0.82      0.80        22

    accuracy                           0.84        56
   macro avg       0.83      0.84      0.83        56
weighted avg       0.84      0.84      0.84        56<jupyter_text>Models Testing on Data RandomForest Model<jupyter_code>from sklearn.preprocessing import LabelEncoder

filename="/content/drive/MyDrive/Deep learning task-20210403T043535Z-001/Deep learning task/full_dataset/dog_barking_0.wav"
audio, sample_rate = librosa.load(filename, res_type='kaiser_fast') 
mfccs_features = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)
mfccs_scaled_features = np.mean(mfccs_features.T,axis=0)

mfccs_scaled_features=mfccs_scaled_features.reshape(1,-1)


predicted_label=pipe.predict_proba(mfccs_scaled_features)
print(predicted_label)<jupyter_output>[[0.11 0.89]]<jupyter_text>ANN Model<jupyter_code>audio, sample_rate = librosa.load(filename, res_type='kaiser_fast') 
mfccs_features = librosa.feature.mfcc(y=audio, sr=sample_rate, n_mfcc=40)
mfccs_scaled_features = np.mean(mfccs_features.T,axis=0)

mfccs_scaled_features=mfccs_scaled_features.reshape(1,-1)


predicted_label=model.predict_classes(mfccs_scaled_features)
print(predicted_label)
prediction_class = labelencoder.inverse_transform(predicted_label) 
prediction_class<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1152624413855695, 'fraction_numerical': 0.03524428981999698, 'mean_word_length': 5.444444444444445, 'pattern_counts': {'":': 4, '<': 41, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '607275', 'license': 'Apache-2.0', 'n_tokens_mistral': 2436, 'n_tokens_neox': 2305, 'n_words': 399, 'path': 'Vegam_task.ipynb', 'repo_name': 'digs1998/Audio-Classification-Cats-and-Dogs', 'size': '6611'}