Exploratory data analysis

In [133]:
import matplotlib.pyplot as plt
%matplotlib inline
In [134]:
filename='dataset/cry.wav'
In [135]:
import IPython.display as ipd
import librosa
import librosa.display
In [136]:
plt.figure(figsize = (14,5))
data, sample_rate = librosa.load(filename)
librosa.display.waveplot(data, sr = sample_rate)
ipd.Audio(filename)
Out[136]:
In [137]:
sample_rate
Out[137]:
22050
In [138]:
from scipy.io import wavfile as wav
wav_sample_rate, wave_audio = wav.read(filename)
In [139]:
wav_sample_rate
Out[139]:
16000
In [140]:
wave_audio
Out[140]:
array([    0,     0,     0, ..., -1195,  -592,     0], dtype=int16)
In [141]:
data
Out[141]:
array([ 0.        ,  0.        ,  0.        , ..., -0.00819657,
        0.00099229,  0.        ], dtype=float32)
In [142]:
import pandas as pd
metadata = pd.read_csv('dataset/metadata2.csv')
metadata.head(15)
Out[142]:
slice_file_name fold class_id class
0 cry (1).wav 1 1 cry
1 cry (2).wav 1 1 cry
2 cry (3).wav 1 1 cry
3 cry (4).wav 1 1 cry
4 cry (5).wav 1 1 cry
5 cry (6).wav 1 1 cry
6 cry (7).wav 1 1 cry
7 cry (8).wav 1 1 cry
8 cry (9).wav 1 1 cry
9 cry (10).wav 1 1 cry
10 cry (11).wav 1 1 cry
11 cry (12).wav 1 1 cry
12 cry (13).wav 1 1 cry
13 cry (14).wav 1 1 cry
14 cry (15).wav 1 1 cry
In [143]:
metadata['class'].value_counts()
Out[143]:
laugh    25
cry      24
Name: class, dtype: int64

Data Preprocessing

In [144]:
import librosa
audio_file_path = 'dataset/laugh (1).wav'
librosa_audio_data, librosa_sample_rate = librosa.load(audio_file_path)
In [145]:
print(librosa_audio_data)
[ 0.000000e+00  0.000000e+00  0.000000e+00 ... -6.858751e-04  6.737236e-05
  0.000000e+00]
In [146]:
plt.figure(figsize = (12,4))
plt.plot(librosa_audio_data)
Out[146]:
[<matplotlib.lines.Line2D at 0x2214958fe10>]

Observation

In [147]:
from scipy.io import wavfile as wav
wave_sample_rate, wave_audio = wav.read(audio_file_path)
In [148]:
wave_audio
Out[148]:
array([  0,   0,   0, ..., -36, -38,   0], dtype=int16)
In [149]:
plt.figure(figsize = (12,4))
plt.plot(wave_audio)
Out[149]:
[<matplotlib.lines.Line2D at 0x221499f6748>]

extract features

In [150]:
mfccs = librosa.feature.mfcc(y = librosa_audio_data, sr = librosa_sample_rate, n_mfcc = 40)
print(mfccs.shape)
(40, 245)
In [151]:
mfccs
Out[151]:
array([[-5.79692298e+02, -5.79692298e+02, -5.77897144e+02, ...,
        -5.01305287e+02, -5.13167847e+02, -5.37795172e+02],
       [ 0.00000000e+00,  0.00000000e+00,  2.52062149e+00, ...,
         5.07232846e+01,  5.41927899e+01,  5.35657410e+01],
       [ 0.00000000e+00,  0.00000000e+00,  2.46682566e+00, ...,
         1.79752596e+01,  2.30013599e+01,  4.21937610e+01],
       ...,
       [ 0.00000000e+00,  0.00000000e+00, -1.07298267e+00, ...,
        -1.54333199e-01, -2.76824468e+00, -5.83646462e+00],
       [ 0.00000000e+00,  0.00000000e+00, -9.66578872e-01, ...,
         1.23428353e+00, -3.84696263e+00, -6.14943461e+00],
       [ 0.00000000e+00,  0.00000000e+00, -8.57171196e-01, ...,
         4.02084165e+00, -3.30228640e+00, -5.85212463e+00]])
In [152]:
import pandas as pd
import os
import librosa
audio_dataset_path = 'dataset/'
metadata = pd.read_csv('dataset/metadata2.csv')
metadata.head()
Out[152]:
slice_file_name fold class_id class
0 cry (1).wav 1 1 cry
1 cry (2).wav 1 1 cry
2 cry (3).wav 1 1 cry
3 cry (4).wav 1 1 cry
4 cry (5).wav 1 1 cry
In [153]:
def features_extractor(file):
    audio, sample_rate = librosa.load(file_name, res_type='kaiser_fast')
    mfcss_features = librosa.feature.mfcc(y = audio, sr = sample_rate, n_mfcc = 40)
    mfcss_scaled_features = np.mean(mfccs.T,axis = 0)
    return mfcss_scaled_features
In [154]:
import numpy as np
from tqdm import tqdm

extracted_features = []
for index_num, row in tqdm(metadata.iterrows()):
    file_name = os.path.join(os.path.abspath(audio_dataset_path), 'fold' + str(row['fold']) + '/', str(row['slice_file_name']))
    final_class_labels = row['class']
    data = features_extractor(file_name)
    extracted_features.append([data, final_class_labels])
49it [00:05, 10.09it/s]
In [155]:
extracted_features_df = pd.DataFrame(extracted_features,columns = ['feature','class'])
extracted_features_df.head()
Out[155]:
feature class
0 [-286.52198351650003, 129.93930326821018, -40.... cry
1 [-286.52198351650003, 129.93930326821018, -40.... cry
2 [-286.52198351650003, 129.93930326821018, -40.... cry
3 [-286.52198351650003, 129.93930326821018, -40.... cry
4 [-286.52198351650003, 129.93930326821018, -40.... cry
In [156]:
X = np.array(extracted_features_df['feature'].tolist())
y = np.array(extracted_features_df['class'].tolist())
In [157]:
X.shape
Out[157]:
(49, 40)
In [158]:
y
Out[158]:
array(['cry', 'cry', 'cry', 'cry', 'cry', 'cry', 'cry', 'cry', 'cry',
       'cry', 'cry', 'cry', 'cry', 'cry', 'cry', 'cry', 'cry', 'cry',
       'cry', 'cry', 'cry', 'cry', 'cry', 'cry', 'laugh', 'laugh',
       'laugh', 'laugh', 'laugh', 'laugh', 'laugh', 'laugh', 'laugh',
       'laugh', 'laugh', 'laugh', 'laugh', 'laugh', 'laugh', 'laugh',
       'laugh', 'laugh', 'laugh', 'laugh', 'laugh', 'laugh', 'laugh',
       'laugh', 'laugh'], dtype='<U5')
In [159]:
#from sklearn.preprocessing import LabelEncoder
#label_encoder = LabelEncoder()
#y = np.array(pd.get_dummies(y))
from tensorflow.keras.utils import to_categorical
from sklearn.preprocessing import LabelEncoder
labelencoder = LabelEncoder()
y = to_categorical(labelencoder.fit_transform(y))
In [160]:
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
In [161]:
X_train.shape
Out[161]:
(39, 40)
In [162]:
X_test.shape
Out[162]:
(10, 40)
In [163]:
y_train.shape
Out[163]:
(39, 2)
In [164]:
y_test.shape
Out[164]:
(10, 2)

Define The Model

In [165]:
import tensorflow as tf
print(tf.__version__)
1.14.0
In [178]:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.optimizers import Adam
from sklearn import metrics
In [179]:
num_labels = y.shape[1]
In [181]:
Dense
Out[181]:
tensorflow.python.keras.layers.core.Dense
In [245]:
model = Sequential()

model.add(Dense(100, input_shape = (40,)))
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(100))
model.add(Activation('relu'))
model.add(Dropout(0.5))

model.add(Dense(num_labels))
model.add(Activation('sigmoid'))
In [246]:
model.summary()
Model: "sequential_14"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_43 (Dense)             (None, 100)               4100      
_________________________________________________________________
activation_39 (Activation)   (None, 100)               0         
_________________________________________________________________
dropout_28 (Dropout)         (None, 100)               0         
_________________________________________________________________
dense_44 (Dense)             (None, 100)               10100     
_________________________________________________________________
activation_40 (Activation)   (None, 100)               0         
_________________________________________________________________
dropout_29 (Dropout)         (None, 100)               0         
_________________________________________________________________
dense_45 (Dense)             (None, 2)                 202       
_________________________________________________________________
activation_41 (Activation)   (None, 2)                 0         
=================================================================
Total params: 14,402
Trainable params: 14,402
Non-trainable params: 0
_________________________________________________________________
In [247]:
model.compile(loss='binary_crossentropy', metrics=['accuracy'], optimizer='adam')
In [248]:
from tensorflow.keras.callbacks import ModelCheckpoint
from keras.callbacks import EarlyStopping
from datetime import datetime

num_epochs = 100
num_batch_size = 128

checkpointer = ModelCheckpoint(filepath = 'saved_model/audio_classification.hdf5', monitor='val_accuracy', mode='max',
                              verbose = 1, save_best_only = True)
#es = EarlyStopping(monitor='val_acc', mode='min', verbose=1)
#mc = ModelCheckpoint('best_model.h5', monitor='val_accuracy', mode='max', verbose=1, save_best_only=True)
start = datetime.now()

model.fit(X_train, y_train, batch_size = num_batch_size, epochs = num_epochs, validation_data = (X_test, y_test), callbacks = [checkpointer])

duration = datetime.now() - start
print('Training completed in time: ', duration)
Train on 39 samples, validate on 10 samples
Epoch 1/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 1s 30ms/sample - loss: 19.7283 - acc: 0.4872 - val_loss: 6.3030 - val_acc: 0.7000
Epoch 2/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 164us/sample - loss: 18.9412 - acc: 0.5256 - val_loss: 3.7224 - val_acc: 0.7000
Epoch 3/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 261us/sample - loss: 17.2097 - acc: 0.5128 - val_loss: 2.1017 - val_acc: 0.5000
Epoch 4/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 208us/sample - loss: 14.1361 - acc: 0.5256 - val_loss: 2.6188 - val_acc: 0.5000
Epoch 5/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 226us/sample - loss: 13.6244 - acc: 0.5256 - val_loss: 3.3817 - val_acc: 0.3000
Epoch 6/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 343us/sample - loss: 13.1020 - acc: 0.5641 - val_loss: 5.7061 - val_acc: 0.3000
Epoch 7/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 254us/sample - loss: 13.5856 - acc: 0.5256 - val_loss: 7.4595 - val_acc: 0.3000
Epoch 8/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 282us/sample - loss: 18.3836 - acc: 0.4872 - val_loss: 8.0568 - val_acc: 0.3000
Epoch 9/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 247us/sample - loss: 14.2632 - acc: 0.5256 - val_loss: 8.1161 - val_acc: 0.3000
Epoch 10/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 229us/sample - loss: 15.4777 - acc: 0.5256 - val_loss: 7.7828 - val_acc: 0.3000
Epoch 11/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 332us/sample - loss: 17.8290 - acc: 0.4487 - val_loss: 6.8468 - val_acc: 0.3000
Epoch 12/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 240us/sample - loss: 11.3420 - acc: 0.5128 - val_loss: 5.8594 - val_acc: 0.3000
Epoch 13/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 268us/sample - loss: 11.7614 - acc: 0.5256 - val_loss: 4.7173 - val_acc: 0.3000
Epoch 14/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 278us/sample - loss: 13.5376 - acc: 0.5256 - val_loss: 3.5546 - val_acc: 0.3000
Epoch 15/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 258us/sample - loss: 16.7104 - acc: 0.4487 - val_loss: 2.5133 - val_acc: 0.3000
Epoch 16/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 238us/sample - loss: 13.0555 - acc: 0.5513 - val_loss: 1.7887 - val_acc: 0.3000
Epoch 17/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 229us/sample - loss: 12.2309 - acc: 0.5385 - val_loss: 1.3562 - val_acc: 0.5000
Epoch 18/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 265us/sample - loss: 12.7021 - acc: 0.4231 - val_loss: 1.0852 - val_acc: 0.5000
Epoch 19/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 212us/sample - loss: 11.5577 - acc: 0.4103 - val_loss: 0.8984 - val_acc: 0.5000
Epoch 20/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 265us/sample - loss: 10.9789 - acc: 0.4487 - val_loss: 0.7446 - val_acc: 0.5000
Epoch 21/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 413us/sample - loss: 10.5350 - acc: 0.5128 - val_loss: 0.6699 - val_acc: 0.7000
Epoch 22/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 276us/sample - loss: 10.3599 - acc: 0.4487 - val_loss: 0.6583 - val_acc: 0.7000
Epoch 23/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 275us/sample - loss: 8.4671 - acc: 0.5513 - val_loss: 0.6573 - val_acc: 0.7000
Epoch 24/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 207us/sample - loss: 11.8778 - acc: 0.4231 - val_loss: 0.6587 - val_acc: 0.7000
Epoch 25/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 302us/sample - loss: 12.2815 - acc: 0.4359 - val_loss: 0.6811 - val_acc: 0.5000
Epoch 26/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 278us/sample - loss: 9.9429 - acc: 0.4872 - val_loss: 0.7212 - val_acc: 0.5000
Epoch 27/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 307us/sample - loss: 12.0861 - acc: 0.4487 - val_loss: 0.7916 - val_acc: 0.5000
Epoch 28/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 207us/sample - loss: 8.5524 - acc: 0.4872 - val_loss: 0.8728 - val_acc: 0.5000
Epoch 29/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 222us/sample - loss: 8.5431 - acc: 0.5000 - val_loss: 0.9822 - val_acc: 0.3000
Epoch 30/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 273us/sample - loss: 6.8799 - acc: 0.5385 - val_loss: 1.0627 - val_acc: 0.3000
Epoch 31/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 247us/sample - loss: 7.7690 - acc: 0.5513 - val_loss: 1.1652 - val_acc: 0.3000
Epoch 32/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 236us/sample - loss: 8.6232 - acc: 0.5256 - val_loss: 1.2959 - val_acc: 0.3000
Epoch 33/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 227us/sample - loss: 8.0870 - acc: 0.3590 - val_loss: 1.3553 - val_acc: 0.3000
Epoch 34/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 110us/sample - loss: 6.6560 - acc: 0.5897 - val_loss: 1.3988 - val_acc: 0.3000
Epoch 35/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 400us/sample - loss: 6.4243 - acc: 0.5769 - val_loss: 1.4256 - val_acc: 0.3000
Epoch 36/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 270us/sample - loss: 7.2408 - acc: 0.5385 - val_loss: 1.4075 - val_acc: 0.3000
Epoch 37/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 275us/sample - loss: 6.8691 - acc: 0.5513 - val_loss: 1.3447 - val_acc: 0.3000
Epoch 38/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 403us/sample - loss: 8.4990 - acc: 0.4872 - val_loss: 1.1669 - val_acc: 0.3000
Epoch 39/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 227us/sample - loss: 5.9419 - acc: 0.4872 - val_loss: 1.0099 - val_acc: 0.3000
Epoch 40/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 592us/sample - loss: 7.7828 - acc: 0.4615 - val_loss: 0.8620 - val_acc: 0.5000
Epoch 41/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 264us/sample - loss: 7.0359 - acc: 0.4615 - val_loss: 0.7226 - val_acc: 0.5000
Epoch 42/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 249us/sample - loss: 6.7024 - acc: 0.4231 - val_loss: 0.6556 - val_acc: 0.7000
Epoch 43/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 1ms/sample - loss: 5.6128 - acc: 0.5128 - val_loss: 0.6735 - val_acc: 0.7000
Epoch 44/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 254us/sample - loss: 4.0808 - acc: 0.5641 - val_loss: 0.7553 - val_acc: 0.7000
Epoch 45/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 227us/sample - loss: 3.8330 - acc: 0.6026 - val_loss: 0.8624 - val_acc: 0.7000
Epoch 46/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 216us/sample - loss: 7.2372 - acc: 0.5128 - val_loss: 0.9585 - val_acc: 0.7000
Epoch 47/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 298us/sample - loss: 6.0003 - acc: 0.4872 - val_loss: 1.0480 - val_acc: 0.7000
Epoch 48/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 280us/sample - loss: 6.4557 - acc: 0.4487 - val_loss: 1.1456 - val_acc: 0.7000
Epoch 49/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 346us/sample - loss: 4.5243 - acc: 0.6282 - val_loss: 1.1885 - val_acc: 0.7000
Epoch 50/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 294us/sample - loss: 5.3953 - acc: 0.5385 - val_loss: 1.2238 - val_acc: 0.7000
Epoch 51/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 343us/sample - loss: 4.7358 - acc: 0.5513 - val_loss: 1.2456 - val_acc: 0.7000
Epoch 52/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 341us/sample - loss: 4.6182 - acc: 0.5769 - val_loss: 1.2592 - val_acc: 0.7000
Epoch 53/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 292us/sample - loss: 5.1797 - acc: 0.4487 - val_loss: 1.2653 - val_acc: 0.7000
Epoch 54/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 206us/sample - loss: 5.6540 - acc: 0.4487 - val_loss: 1.2507 - val_acc: 0.7000
Epoch 55/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 521us/sample - loss: 4.5650 - acc: 0.4231 - val_loss: 1.2236 - val_acc: 0.7000
Epoch 56/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 281us/sample - loss: 4.8537 - acc: 0.5128 - val_loss: 1.1863 - val_acc: 0.7000
Epoch 57/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 209us/sample - loss: 4.0517 - acc: 0.5128 - val_loss: 1.1411 - val_acc: 0.7000
Epoch 58/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 222us/sample - loss: 5.4408 - acc: 0.5128 - val_loss: 1.0894 - val_acc: 0.7000
Epoch 59/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 224us/sample - loss: 3.7599 - acc: 0.5769 - val_loss: 1.0244 - val_acc: 0.7000
Epoch 60/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 295us/sample - loss: 3.5494 - acc: 0.5385 - val_loss: 0.9595 - val_acc: 0.7000
Epoch 61/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 214us/sample - loss: 3.8181 - acc: 0.5256 - val_loss: 0.9074 - val_acc: 0.7000
Epoch 62/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 286us/sample - loss: 3.9883 - acc: 0.4872 - val_loss: 0.8654 - val_acc: 0.7000
Epoch 63/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 280us/sample - loss: 4.7482 - acc: 0.4487 - val_loss: 0.8374 - val_acc: 0.7000
Epoch 64/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 280us/sample - loss: 4.3871 - acc: 0.4744 - val_loss: 0.8124 - val_acc: 0.7000
Epoch 65/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 167us/sample - loss: 4.9997 - acc: 0.4231 - val_loss: 0.7920 - val_acc: 0.7000
Epoch 66/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 269us/sample - loss: 3.6173 - acc: 0.4872 - val_loss: 0.7726 - val_acc: 0.7000
Epoch 67/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 375us/sample - loss: 3.2226 - acc: 0.5513 - val_loss: 0.7547 - val_acc: 0.7000
Epoch 68/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 240us/sample - loss: 3.5180 - acc: 0.4872 - val_loss: 0.7402 - val_acc: 0.7000
Epoch 69/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 352us/sample - loss: 2.9232 - acc: 0.4872 - val_loss: 0.7308 - val_acc: 0.7000
Epoch 70/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 356us/sample - loss: 3.4833 - acc: 0.5128 - val_loss: 0.7181 - val_acc: 0.7000
Epoch 71/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 241us/sample - loss: 3.8745 - acc: 0.5128 - val_loss: 0.7068 - val_acc: 0.7000
Epoch 72/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 221us/sample - loss: 2.5898 - acc: 0.5513 - val_loss: 0.6989 - val_acc: 0.7000
Epoch 73/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 216us/sample - loss: 4.7924 - acc: 0.4231 - val_loss: 0.6958 - val_acc: 0.7000
Epoch 74/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 320us/sample - loss: 3.3645 - acc: 0.5256 - val_loss: 0.6927 - val_acc: 0.7000
Epoch 75/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 211us/sample - loss: 2.3351 - acc: 0.6154 - val_loss: 0.6902 - val_acc: 0.7000
Epoch 76/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 286us/sample - loss: 2.4415 - acc: 0.6026 - val_loss: 0.6817 - val_acc: 0.7000
Epoch 77/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 270us/sample - loss: 2.7357 - acc: 0.5513 - val_loss: 0.6741 - val_acc: 0.7000
Epoch 78/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 268us/sample - loss: 3.2123 - acc: 0.5256 - val_loss: 0.6650 - val_acc: 0.7000
Epoch 79/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 432us/sample - loss: 2.8111 - acc: 0.5513 - val_loss: 0.6571 - val_acc: 0.7000
Epoch 80/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 316us/sample - loss: 2.9116 - acc: 0.5000 - val_loss: 0.6493 - val_acc: 0.7000
Epoch 81/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 269us/sample - loss: 2.7141 - acc: 0.5128 - val_loss: 0.6404 - val_acc: 0.7000
Epoch 82/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 272us/sample - loss: 2.2678 - acc: 0.5256 - val_loss: 0.6321 - val_acc: 0.7000
Epoch 83/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 222us/sample - loss: 2.2296 - acc: 0.5385 - val_loss: 0.6254 - val_acc: 0.7000
Epoch 84/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 282us/sample - loss: 2.5554 - acc: 0.4615 - val_loss: 0.6198 - val_acc: 0.7000
Epoch 85/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 311us/sample - loss: 2.3365 - acc: 0.5513 - val_loss: 0.6155 - val_acc: 0.7000
Epoch 86/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 263us/sample - loss: 2.4445 - acc: 0.5000 - val_loss: 0.6134 - val_acc: 0.7000
Epoch 87/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 210us/sample - loss: 2.1328 - acc: 0.6154 - val_loss: 0.6122 - val_acc: 0.7000
Epoch 88/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 235us/sample - loss: 3.0165 - acc: 0.5128 - val_loss: 0.6119 - val_acc: 0.7000
Epoch 89/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 200us/sample - loss: 2.7667 - acc: 0.5128 - val_loss: 0.6124 - val_acc: 0.7000
Epoch 90/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 226us/sample - loss: 1.8887 - acc: 0.5897 - val_loss: 0.6140 - val_acc: 0.7000
Epoch 91/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 219us/sample - loss: 1.7160 - acc: 0.5641 - val_loss: 0.6164 - val_acc: 0.7000
Epoch 92/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 239us/sample - loss: 2.0595 - acc: 0.5128 - val_loss: 0.6187 - val_acc: 0.7000
Epoch 93/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 234us/sample - loss: 2.0571 - acc: 0.4615 - val_loss: 0.6209 - val_acc: 0.7000
Epoch 94/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 247us/sample - loss: 2.3137 - acc: 0.4487 - val_loss: 0.6229 - val_acc: 0.7000
Epoch 95/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 205us/sample - loss: 2.1641 - acc: 0.5128 - val_loss: 0.6254 - val_acc: 0.7000
Epoch 96/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 298us/sample - loss: 1.3084 - acc: 0.6154 - val_loss: 0.6268 - val_acc: 0.7000
Epoch 97/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 231us/sample - loss: 2.1908 - acc: 0.4615 - val_loss: 0.6289 - val_acc: 0.7000
Epoch 98/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 283us/sample - loss: 1.8143 - acc: 0.4744 - val_loss: 0.6318 - val_acc: 0.7000
Epoch 99/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 271us/sample - loss: 2.4625 - acc: 0.4487 - val_loss: 0.6353 - val_acc: 0.7000
Epoch 100/100
WARNING:tensorflow:Can save best model only with val_accuracy available, skipping.
39/39 [==============================] - 0s 274us/sample - loss: 2.4434 - acc: 0.3718 - val_loss: 0.6393 - val_acc: 0.7000
Training completed in time:  0:00:04.629118
In [249]:
test_accuracy = model.evaluate(X_test, y_test, verbose = 0)
print(test_accuracy[1])
0.7

Prediction

In [250]:
filename = 'dataest/cry (15).wav'
prediction_feature = features_extractor(filename)
prediction_feature = prediction_feature.reshape(1,-1)
model.predict_classes(prediction_feature)
Out[250]:
array([1], dtype=int64)
In [251]:
metadata['class'].unique()
Out[251]:
array(['cry', 'laugh'], dtype=object)
In [255]:
filename ='dataset/cry.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)

print(mfccs_scaled_features)
mfccs_scaled_features = mfccs_scaled_features.reshape(1,-1)
print(mfccs_scaled_features)
print(mfccs_scaled_features.shape)
predicted_label = model.predict_classes(mfccs_scaled_features)
print(predicted_label)
prediction_class = labelencoder.inverse_transform(predicted_label)
prediction_class
[-3.99796210e+02  7.61916879e+01  2.29104243e+01  1.78994204e+01
 -9.46345203e+00 -8.37782141e+00 -2.30969932e+01 -8.33863294e+00
 -2.56960907e+01 -1.12048259e+01 -4.06579409e+00 -1.72544558e+01
 -6.37701202e-01 -1.03955494e+01  3.92861247e-01 -4.86109754e+00
 -8.36029314e-01  9.16748352e+00  3.13889915e-01  1.25635532e+01
  1.67441403e+01  1.59193494e+01  8.10379443e+00  4.89416942e+00
  9.72422597e-01 -5.65110162e+00 -1.07444778e+00 -2.42749227e+00
 -8.09188618e+00 -9.99735635e+00 -7.89429423e+00 -5.86878411e+00
 -5.88733547e+00 -1.01072932e+01 -6.43957097e+00 -5.52161135e+00
 -5.48039601e+00  1.24237941e+00 -2.31556260e+00  2.03829662e+00]
[[-3.99796210e+02  7.61916879e+01  2.29104243e+01  1.78994204e+01
  -9.46345203e+00 -8.37782141e+00 -2.30969932e+01 -8.33863294e+00
  -2.56960907e+01 -1.12048259e+01 -4.06579409e+00 -1.72544558e+01
  -6.37701202e-01 -1.03955494e+01  3.92861247e-01 -4.86109754e+00
  -8.36029314e-01  9.16748352e+00  3.13889915e-01  1.25635532e+01
   1.67441403e+01  1.59193494e+01  8.10379443e+00  4.89416942e+00
   9.72422597e-01 -5.65110162e+00 -1.07444778e+00 -2.42749227e+00
  -8.09188618e+00 -9.99735635e+00 -7.89429423e+00 -5.86878411e+00
  -5.88733547e+00 -1.01072932e+01 -6.43957097e+00 -5.52161135e+00
  -5.48039601e+00  1.24237941e+00 -2.31556260e+00  2.03829662e+00]]
(1, 40)
[0]
Out[255]:
array(['cry'], dtype='<U5')