【发布时间】:2022-01-14 12:01:21
【问题描述】:
我正在尝试为关于我说话的音频和其他人说话的音频的分类问题创建一个神经网络,以便对其进行分类。但是当我训练它时,它给了我这种奇怪的准确性和损失的结果。
这是我的代码。
'''
This is only to read the data and pass it into an array
1. Get the Audio data, my voice so we can visualize it into an array.
2. Build an ANN with the data already into an array. classification problem
3. Real time predictor using pyaudio and trained model
'''
from sklearn.model_selection import train_test_split
from tensorflow.python.keras.layers.core import Dropout
from sklearn.preprocessing import StandardScaler
import tensorflow as tf
import numpy as np
from scipy.io import wavfile
from pathlib import Path
import os
# cut audio to make the same sizes, shape and length
def trim_wav( originalWavPath, newWavPath , start, new ):
'''
:param originalWavPath: the path to the source wav file
:param newWavPath: output wav file * can be same path as original
:param start: time in seconds
:param end: time in seconds
:return:
'''
sampleRate, waveData = wavfile.read( originalWavPath )
startSample = int( start * sampleRate )
endSample = int( new * sampleRate )
wavfile.write( newWavPath, sampleRate, waveData[startSample:endSample])
### DATASET
pathlist = Path(os.path.abspath('Voiceclassification/Data/me/')).rglob('*.wav')
# My voice data
for path in pathlist:
wp = str(path)
# Trim function here for each file
trim_wav(wp, wp.replace(".wav", ".wav"), 0,5)
filename = str(path)
# convert audio to numpy array and then 2D to 1D np Array
samplerate, data = wavfile.read(filename)
#print(f"sample rate: {samplerate}")
#print(f"data: {data}")
pathlist2 = Path(os.path.abspath('Voiceclassification/Data/other/')).rglob('*.wav')
# other voice data
for path2 in pathlist2:
wp2 = str(path2)
trim_wav(wp2, wp2.replace(".wav", ".wav"), 0,5)
filename2 = str(path2)
samplerate2, data2 = wavfile.read(filename2)
#print(data2)
### ADAPTING THE DATA FOR THE MODEL
X = data.reshape(-1, 1) # My voice
y = data2.reshape(-1, 1) # Other data
#print(X_.shape)
#print(y_.shape)
### Trainig the model
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.1, random_state=0)
# Performing future scaling
sc = StandardScaler()
x_train = sc.fit_transform(x_train)
x_test = sc.transform(x_test)
### Creating the ANN
ann = tf.keras.models.Sequential()
# First hidden layer of the ann
ann.add(tf.keras.layers.Dense(units=6, activation="relu"))
ann.add(Dropout(0.05))
# Second one
ann.add(tf.keras.layers.Dense(units=6, activation="relu"))
ann.add(Dropout(0.05))
# Output layer
ann.add(tf.keras.layers.Dense(units=1, activation="sigmoid"))
# Compile our neural network
ann.compile(optimizer="adam",
loss="binary_crossentropy",
metrics=['accuracy'])
# Fit ANN
ann.fit(x_train, y_train, batch_size=1024, epochs=100) ############ batch 32
ann.save('Models/voiceclassification.model')
有人知道我的代码是否有什么问题导致acc非常低吗?
【问题讨论】:
-
您似乎对 X 和 Y 都使用了语音 sn-ps。将 sn-ps(您的和其他人的)作为 X 和扬声器名称不是更有意义吗(你,其他人)作为 Y?
-
@MarkLavin 我所做的是将 x 定义为我的语音数据,将 y 定义为其他人的数据,这会影响它吗?你能更好地解释一下你的意思吗?
-
一般来说,为了训练一个模型,你给出一个输入/输出对的序列,它会“学习”一个将输入映射到输出的函数。对于您的情况,(对我来说......)您的输入是语音 sn-ps 并且输出是说话者的身份是有道理的。因此,您将使用您的语音 sn-ps(输入)并注意相应的输出是“Bernardo”。你会对其他扬声器做类似的事情。然后,您可以使用经过训练的模型来预测,给定一个新的语音 sn-p,它来自哪个说话者。
-
@MarkLavin 嘿嘿,看了很多遍,明白了。但问题是我不知道如何在我的代码中实现它,你能用我的代码发布一个答案吗?对不起,如果我要求太多是因为我是初学者。
-
我认为你需要退后几步,进一步了解机器学习的基本思想;我强烈建议看看 Coursera 机器学习课程 coursera.org/learn/machine-learning 导师 Andrew Ng 非常出色,材料也很平易近人。
标签: python tensorflow machine-learning neural-network