Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.75 KB | None | 0 0
  1. import os
  2. import librosa
  3. import pandas as pd
  4. import numpy as np
  5. import time
  6. from keras.models import load_model
  7. from keras import Sequential
  8. from keras.layers import LSTM, Dense
  9. from keras.optimizers import Adam
  10.  
  11. from main import load_song_features, read_labels_from_file
  12.  
  13. trainPath = "D:\\Audio - dataset\\trainGender.tsv"
  14. testPath = "D:\\Audio - dataset\\trainGender.tsv"
  15. trainData = pd.read_csv(trainPath, sep='\t')
  16. testData = pd.read_csv(trainPath, sep='\t')
  17.  
  18.  
  19. def windows(data, window_size):
  20. start = 0
  21. while start < len(data):
  22. yield start, start + window_size
  23. start += (window_size // 2)
  24.  
  25.  
  26. def extract_features(parent_dir, bands=20, frames=41):
  27. window_size = 512 * (frames - 1)
  28. mfccs = []
  29. labels = []
  30. for i in range(1):
  31. path1 = os.path.join(parent_dir, trainData.path[i] + ".mp3")
  32. sound_clip, s = librosa.load(path1)
  33. label = trainData.gender[i]
  34. if label == 'male':
  35. label = 1
  36. else:
  37. label = 0
  38.  
  39. for (start, end) in windows(sound_clip, window_size):
  40. if len(sound_clip[start:end]) == window_size:
  41. signal = sound_clip[start:end]
  42. mfcc = librosa.feature.mfcc(y=signal, sr=s, n_mfcc=bands).T.flatten()[:, np.newaxis].T
  43. mfccs.append(mfcc)
  44. labels.append(label)
  45.  
  46. features = np.asarray(mfccs).reshape(len(mfccs), frames, bands)
  47. print(labels)
  48. return np.array(features), np.array(labels, dtype=np.int)
  49.  
  50.  
  51. def one_hot_encode(labels):
  52. n_labels = len(labels)
  53. n_unique_labels = len(np.unique(labels))
  54. one_hot_encode = np.zeros((n_labels, n_unique_labels))
  55. one_hot_encode[np.arange(n_labels), labels] = 1
  56. return one_hot_encode
  57.  
  58.  
  59. #shape_features = [77839, 41, 20]
  60. #tr_features = load_song_features('song_features/features_file.txt', shape_features)
  61. #tr_labels = read_labels_from_file('song_features/labels_file.txt')
  62. #tr_features, tr_labels = extract_features("D:\\Audio - dataset\\clips")
  63. #tr_labels = one_hot_encode(tr_labels)
  64.  
  65. """"
  66. input_shape = (tr_features.shape[1], tr_features.shape[2])
  67.  
  68. model = Sequential()
  69. model.add(LSTM(units=128, dropout=0.05, recurrent_dropout=0.35, return_sequences=True, input_shape=input_shape))
  70. model.add(LSTM(units=128, dropout=0.05, recurrent_dropout=0.35, return_sequences=True, input_shape=input_shape))
  71. model.add(LSTM(units=128, dropout=0.05, recurrent_dropout=0.35, return_sequences=True, input_shape=input_shape))
  72. model.add(LSTM(units=32, dropout=0.05, recurrent_dropout=0.35, return_sequences=False))
  73. model.add(Dense(units=tr_labels.shape[1], activation="softmax"))
  74.  
  75. opt = Adam()
  76. model.compile(loss="categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
  77. model.summary()
  78.  
  79. batch_size = 70 # num of training examples per minibatch
  80. num_epochs = 20
  81. model.fit(
  82. tr_features,
  83. tr_labels,
  84. batch_size=batch_size,
  85. epochs=num_epochs,
  86. )
  87.  
  88. ynew = model.predict_classes(tr_features)
  89.  
  90. model.save('my_model.h5')
  91. corr_guess = 0
  92. for i in range(len(tr_labels)):
  93. anss = 1
  94. if tr_labels[i][0] == 1:
  95. anss = 0
  96. corr_guess += anss == ynew[i]
  97.  
  98. print(corr_guess/len(tr_labels))
  99. """""
  100. mfccs = []
  101. for i in range(1):
  102. window_size = 512 * 40
  103. sound_clip, s = librosa.load("test_samples/diana2.aac")
  104. for (start, end) in windows(sound_clip, window_size):
  105. if len(sound_clip[start:end]) == window_size:
  106. signal = sound_clip[start:end]
  107. mfcc = librosa.feature.mfcc(y=signal, sr=s, n_mfcc=20).T.flatten()[:, np.newaxis].T
  108. mfccs.append(mfcc)
  109.  
  110.  
  111. features = np.asarray(mfccs).reshape(len(mfccs), 41, 20)
  112. tr_features = np.array(features)
  113.  
  114. model = load_model('my_model.h5')
  115. ynew = model.predict_classes(tr_features)
  116.  
  117. print(ynew)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement