0

我在训练我的机器学习模型时遇到错误,该模型用于检查一个人在说某事时的感受。我正在使用来自 sklearn 的 librosa、soundfile 和 MLPClassifier。这是我的代码:

;imported required libraries
import librosa
import soundfile
import os, glob, pickle
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import accuracy_score

; created a function that basically gathers features from audio files
def extract_feature(file_name, mfcc, chroma, mel):
    with soundfile.SoundFile(file_name) as sound_file:
        X = sound_file.read(dtype="float32")
        sample_rate=sound_file.samplerate
        if chroma:
            stft=np.abs(librosa.stft(X))
            result=np.array([])
        if mfcc:
            mfccs=np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=40).T, axis=0)
            result=np.hstack((result, mfccs))
        if chroma:
            chroma=np.mean(librosa.feature.chroma_stft(S=stft, sr=sample_rate).T,axis=0)
            result=np.hstack((result, chroma))
        if mel:
            mel=np.mean(librosa.feature.melspectrogram(X, sr=sample_rate).T,axis=0)
            result=np.hstack((result, mel))
    return result
; defined emotions
emotions={
  '01':'neutral',
  '02':'calm',
  '03':'happy',
  '04':'sad',
  '05':'angry',
  '06':'fearful',
  '07':'disgust',
  '08':'surprised'
}
observed_emotions=['calm', 'happy', 'fearful', 'disgust']
;to load data
def load_data(test_size=0.2):
    x,y=[],[]
    for file in glob.glob("data\\Actor_*\\*.wav"):
        file_name=os.path.basename(file)
        emotion=emotions[file_name.split("-")[2]]
        if emotion not in observed_emotions:
            continue
        feature=extract_feature(file, mfcc=True, chroma=True, mel=True)
        x.append(feature)
        y.append(emotion)
    return train_test_split(np.array(x), y, test_size=test_size, random_state=9)

x_train,x_test,y_train,y_test=load_data(test_size=0.23)

print((x_train.shape[0], x_test.shape[0]))

; used the mlpclassifier
model=MLPClassifier(alpha=0.01, batch_size=256, epsilon=1e-08, hidden_layer_sizes=(300,), learning_rate='adaptive', max_iter=500)

;trained my model
model.fit(x_train,y_train)

; This is the part used for unit testing and I am getting a lot of errors
a,b = [],[]
file_name=os.path.basename("data/what.wav")
emotion=emotions[file_name.split("-")[2]]
if emotion not in observed_emotions:
    continue
feature=extract_feature(file, mfcc=True, chroma=True, mel=True)
a.append(feature)
b.append(emotion)

这是我得到的错误,当我尝试通过其他方法(如使用 pydub )删除它时,我得到不同类型的错误。我是这方面的初学者,仍然需要学习很多东西。所以我希望能找到解决这个问题的方法。

IndexError                                Traceback (most recent call last)
<ipython-input-41-8b30084e3248> in <module>
      1 a,b = [],[]
      2 file_name=os.path.basename("data/what.wav")
----> 3 emotion=emotions[file_name.split("-")[2]]
      4 if emotion not in observed_emotions:
      5     continue

IndexError: list index out of range

4

1 回答 1

1

您对 os.path.basename("data/what.wav") 的调用返回 'what.wav'

然后,您使用“-”作为拆分器拆分它,它返回 ['what.wav'],一个元素的列表。

但是您随后尝试使用 [2] 引用列表的第三个元素,这会引发异常。

于 2021-11-15T21:11:26.697 回答