我正在嘗試用 python 創建神經網路,它是一種用于分類問題的 ANN 網路。神經網路的目的是對說話的人進行分類,無論是我還是其他人。我有 2 個檔案夾中的資料。 檔案夾圖片 一個叫我,是我說話的音頻,另一個叫other,別人說話的音頻。 查看 wav 檔案(音頻資料)
問題是它不能訓練網路,因為資料長度不一樣,如果是!,每個檔案夾中有18個,不是多一個,也不是少一個。
當我做
print(X.shape)
print(y.shape)
給這個。 X, y 形狀的結果 即使每個檔案夾中有18 個音頻檔案,形狀也不相同
模型.py
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')
任何的想法?
uj5u.com熱心網友回復:
是因為你的 wav 音頻檔案可能有不同的大小,它們都可以是 10 秒,但是如果毫秒不同,那會影響你的資料形狀,你可以做的是修剪你的 wav 檔案,讓它們都是 10.00 秒,沒有毫秒
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/373173.html
上一篇:音頻資料集.wav檔案的相同形狀
