【发布时间】:2020-10-30 15:58:16
【问题描述】:
我正在尝试从 Github HERE 运行语音识别代码来分析语音。 final_results_gender_test.ipynb 中有一个示例说明了训练和推理的步骤。因此,我复制并调整了推理部分,并提出了以下代码,该代码使用经过训练的模型进行推理。但我不知道为什么会出现这个错误,抱怨This LabelEncoder instance is not fitted yet。
如何解决问题?我只是在做推理,为什么我需要适合?
Traceback (most recent call last):
File "C:\Users\myname\Documents\Speech-Emotion-Analyzer-master\audio.py", line 53, in <module>
livepredictions = (lb.inverse_transform((liveabc)))
File "C:\Users\myname\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\preprocessing\label.py", line 272, in inverse_transform
check_is_fitted(self, 'classes_')
File "C:\Users\myname\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\utils\validation.py", line 914, in check_is_fitted
raise NotFittedError(msg % {'name': type(estimator).__name__})
sklearn.exceptions.NotFittedError: This LabelEncoder instance is not fitted yet. Call 'fit' with appropriate arguments before using this method.
这是我从笔记本中复制/调整的代码:
import os
from keras import regularizers
import keras
from keras.callbacks import ModelCheckpoint
from keras.layers import Conv1D, MaxPooling1D, AveragePooling1D, Dense, Embedding, Input, Flatten, Dropout, Activation, LSTM
from keras.models import Model, Sequential, model_from_json
from keras.preprocessing import sequence
from keras.preprocessing.sequence import pad_sequences
from keras.preprocessing.text import Tokenizer
from keras.utils import to_categorical
import librosa
import librosa.display
from matplotlib.pyplot import specgram
from sklearn.metrics import confusion_matrix
from sklearn.preprocessing import LabelEncoder
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tensorflow as tf
opt = keras.optimizers.rmsprop(lr=0.00001, decay=1e-6)
lb = LabelEncoder()
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
# load weights into new model
loaded_model.load_weights("saved_models/Emotion_Voice_Detection_Model.h5")
print("Loaded model from disk")
X, sample_rate = librosa.load('h04.wav', res_type='kaiser_fast',duration=2.5,sr=22050*2,offset=0.5)
sample_rate = np.array(sample_rate)
mfccs = np.mean(librosa.feature.mfcc(y=X, sr=sample_rate, n_mfcc=13),axis=0)
featurelive = mfccs
livedf2 = featurelive
livedf2= pd.DataFrame(data=livedf2)
livedf2 = livedf2.stack().to_frame().T
twodim= np.expand_dims(livedf2, axis=2)
livepreds = loaded_model.predict(twodim, batch_size=32, verbose=1)
livepreds1=livepreds.argmax(axis=1)
liveabc = livepreds1.astype(int).flatten()
livepredictions = (lb.inverse_transform((liveabc)))
print(livepredictions)
【问题讨论】:
-
抱歉,这个问题已经在这里问过了 (stackoverflow.com/q/64601366/3924118),我从 AI SE 迁移过来的时候没有意识到。
-
我之前在这里问过。类似地询问人工智能。但是被迁移到这里
-
对。我将您的问题从 AI SE 迁移到 Stack Overflow,因为“编程问题”在 AI SE 上是题外话。我没有意识到你已经在 Stack Overflow 上问过这个问题,然后在 AI SE 上问这个问题,这就是我迁移的原因,否则,我会把它作为题外话关闭。我赞成你的另一个问题stackoverflow.com/q/64601366/3924118。希望您能得到一些帮助。
-
希望是的!..
标签: python keras voice-recognition speech-recognition scikit-learn