<jupyter_start><jupyter_text>Gaussian Mixture Model for Gender Identification from Voice Samples<jupyter_code>df['new']=''
from random import seed
from random import random
# seed random number generator
seed(1)
for i in range(len(df)):
    df['new'][i]=[random()]
df.head()
wav, sr = librosa.load(df['path'][0])
print('sr:', sr)
print ('wav shape:', wav.shape)
length = wav.shape[0]/float(sr)
length
plt.plot(wav)
plt.plot(wav[4000:4200])
DATA_DIR = '../data/ST-AEDS'
# !pip3 install numba==0.48

import librosa

X = []
y = []

count = 1
pad = lambda a, i: a[0: i] if a.shape[0] > i else np.hstack((a, np.zeros(i - a.shape[0])))
for fname in os.listdir(DATA_DIR):
    struct = fname.split('_')
    print(struct)
    gender = struct[0]
    if gender[0] == 'f':
        gender = 0
    elif gender[0] == 'm':
        gender = 1
    wav, sr = librosa.load(DATA_DIR + fname)
    padded = pad(wav, 30000)
    X.append(padded)
    y.append(gender)
    count+=1
    print(count)
    
X = np.vstack(X)
y = np.array(y)
print('X:', X.shape)
print ('y:', y.shape)
len(X)
save('../data/processed/X.npy', X)
save('../data/processed/y.npy', y)
# load array
X = load('../data/processed/X.npy')
y = load('../data/processed/y.npy')
# print the array
print(X)
print(y)
model = Sequential()
model.add(Dense(8, activation='relu', input_shape=(X[0].shape)))
model.add(Dropout(0.5))
model.add(Dense(8, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1, activation='sigmoid'))
# model.compile(optimizer = 'sgd', loss='binary_crossentropy', metrics = ['acc'])
model.summary()


# model = Sequential()
# model.add(Dense, input=(X[0].shape))
# # ip = Input(shape=(X[0].shape))
# model.add(Dense(128, activation='relu'))
# model.Dense(1, activation='sigmoid')
# # model = Model(input=ip, output=op)
# model.summary()
model.compile(loss='binary_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
history = model.fit(X,
          y,
          epochs=30,
          batch_size=8, validation_split=0.20)
plt.plot(history.history['accuracy'], label='Train Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
history_dict = history.history
print(history_dict.keys())
acc = history.history['accuracy']
val_acc = history.history['val_accuracy']
loss_values = history_dict['loss']
val_loss_values = history_dict['val_loss']
epochs = range(1, len(acc) + 1)

plt.plot(epochs, loss_values, 'bo', label='Training loss')
plt.plot(epochs, val_loss_values, 'b', label='Validation loss')
plt.title('Training and Validation loss')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()
plt.plot(epochs, acc, 'bo', label='Training acc')
plt.plot(epochs, val_acc, 'b', label='Validation acc')
plt.title('Training and Validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
def get_mfcc(audio_path):
        rate, audio  = read(audio_path)
        mfcc_feature = mfcc(audio, rate, winlen = 0.05, winstep = 0.01, numcep = 5, nfilt = 30, nfft = 800, appendEnergy = True)
        mfcc_feature  = preprocessing.scale(mfcc_feature)
        deltas        = delta(mfcc_feature, 2)
        double_deltas = delta(deltas, 2)
        combined      = np.hstack((mfcc_feature, deltas, double_deltas))
        return combined
audio_file_path = '../data/external/st_american/m0005_us_m0005_00431.wav'
len(get_mfcc(audio_file_path))
print(get_mfcc(audio_file_path))
for i in range(len(df['path'])):
    df['mfcc'][i] = get_mfcc(df['path'][i])<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18024553571428573, 'fraction_numerical': 0.022042410714285716, 'mean_word_length': 4.566770186335404, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '449275', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1437, 'n_tokens_neox': 1358, 'n_words': 265, 'path': 'notebooks/GenderRecognition.ipynb', 'repo_name': 'Robofied/Voicenet', 'size': '3584'}