所以,我正在創建一個 ANN 神經網路型別,它可以對說話的人是否是我進行分類,問題是我可以根據我的資料的形狀來訓練它。
X 資料是
(262144,)
y 資料是
(261768,)
如何使我的 .wav 音頻檔案資料具有相同的形狀?
這是我的完整代碼
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
import tensorflow as tf
import numpy as np
from scipy.io import wavfile
from pathlib import Path
import os
### DATASET
pathlist = Path(os.path.abspath('Voiceclassification/Data/me/')).rglob('*.wav')
# My voice data
for path in pathlist:
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}")
data = data.flatten()
#print(f"data: {data}")
pathlist2 = Path(os.path.abspath('Voiceclassification/Data/other/')).rglob('*.wav')
# other voice data
for path2 in pathlist2:
filename2 = str(path2)
samplerate2, data2 = wavfile.read(filename2)
data2 = data2.flatten()
#print(data2)
### ADAPTING THE DATA FOR THE MODEL
X = data # My voice
y = data2 # 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"))
# Second one
ann.add(tf.keras.layers.Dense(units=6, activation="relu"))
# Output layer
ann.add(tf.keras.layers.Dense(units=6, 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=32, epochs=100)
ann.save('train_model.model')
任何想法,我總共有 18 個 .wav 檔案用于每個 X 或 y
uj5u.com熱心網友回復:
您可以將 scipy.io 用于 wav 檔案,只需 5 秒即可重寫檔案,我創建了這個小代碼來幫助您
def trim_wav( originalWavPath, newWavPath , start, new ):
sampleRate, waveData = wavfile.read( originalWavPath )
startSample = int( start * sampleRate )
endSample = int( new * sampleRate )
wavfile.write( newWavPath, sampleRate, waveData[startSample:endSample])
wp = "path of the wav file"
trim_wav(wp, wp.replace(".wav", ".wav"), 0,5)
這將裁剪您的音頻檔案并擺脫不會改變資料形狀的毫秒
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/373172.html
