Python Speech Recognizer TypeError: '>' not supported between instances of 'float' and 'NoneType'
Python Speech Recognizer TypeError: '>' not supported between instances of 'float' and 'NoneType'
我正在使用 Python 3.6 中的语音识别器代码,其中包含隐马尔可夫模型 (HMM)。
由 .wav
个文件组成的训练数据(输入文件夹)组织为
train
pineapple
apple
banana
orange
kiwi
peach
lime
类似的模式用于 test
数据文件夹。
代码是 运行 来自命令提示符:
python Speech-Recognizer.py --input-folder train
代码贴在下面:
import os
import argparse
import numpy as np
from scipy.io import wavfile
from hmmlearn import hmm
from python_speech_features import mfcc
# Function to parse input arguments
def build_arg_parser():
parser = argparse.ArgumentParser(description='Trains the HMM classifier')
parser.add_argument("--input-folder", dest="input_folder", required=True,
help="Input folder containing the audio files in subfolders")
return parser
# Class to handle all HMM related processing
class HMMTrainer(object):
def __init__(self, model_name='GaussianHMM', n_components=4, cov_type='diag', n_iter=1000):
self.model_name = model_name
self.n_components = n_components
self.cov_type = cov_type
self.n_iter = n_iter
self.models = []
if self.model_name == 'GaussianHMM':
self.model = hmm.GaussianHMM(n_components=self.n_components,
covariance_type=self.cov_type, n_iter=self.n_iter)
else:
raise TypeError('Invalid model type')
# X is a 2D numpy array where each row is 13D
def train(self, X):
np.seterr(all='ignore')
self.models.append(self.model.fit(X))
# Run the model on input data
def get_score(self, input_data):
return self.model.score(input_data)
if __name__ == '__main__':
args = build_arg_parser().parse_args()
input_folder = args.input_folder
hmm_models = []
# Parse the input directory
for dirname in os.listdir(input_folder):
# Get the name of the subfolder
subfolder = os.path.join(input_folder, dirname)
if not os.path.isdir(subfolder):
continue
# Extract the label
label = subfolder[subfolder.rfind('/') + 1:]
# Initialize variables
X = np.array([])
y_words = []
# Iterate through the audio files (leaving 1 file for testing in each class)
for filename in [x for x in os.listdir(subfolder) if x.endswith('.wav')][:-1]:
# Read the input file
filepath = os.path.join(subfolder, filename)
sampling_freq, audio = wavfile.read(filepath)
# Extract MFCC features
mfcc_features = mfcc(audio, sampling_freq)
# Append to the variable X
if len(X) == 0:
X = mfcc_features
else:
X = np.append(X, mfcc_features, axis=0)
# Append the label
y_words.append(label)
print('X.shape =', X.shape)
# Train and save HMM model
hmm_trainer = HMMTrainer()
hmm_trainer.train(X)
hmm_models.append((hmm_trainer, label))
hmm_trainer = None
# Test files
input_files = [
'test/pineapple/pineapple15.wav',
'test/orange/orange15.wav',
'test/apple/apple15.wav',
'test/kiwi/kiwi15.wav'
]
# Classify input data
for input_file in input_files:
# Read input file
sampling_freq, audio = wavfile.read(input_file)
# Extract MFCC features
mfcc_features = mfcc(audio, sampling_freq)
# Define variables
max_score = None
output_label = None
# Iterate through all HMM models and pick
# the one with the highest score
for item in hmm_models:
hmm_model, label = item
score = hmm_model.get_score(mfcc_features)
if score > max_score:
max_score = score
output_label = label
# Print the output
print("\nTrue:", input_file[input_file.find('/') + 1:input_file.rfind('/')])
print("Predicted:", output_label)
我在 运行 上面的代码中遇到以下错误:
Traceback (most recent call last):
File "Speech-Recognizer.py", line 113, in <module>
if score > max_score:
TypeError: '>' not supported between instances of 'float' and 'NoneType'
max_score = None
...
if score > max_score:
您正在尝试将浮点数与 None 进行比较。
用 max_score = 0 代替 max_score = None 怎么样?
问这个问题已经有一段时间了,但我想我找到了解决办法,因为我也遇到了这个问题。此特定代码取自 Python (2.7) 中 Prateek Joshi 的机器学习书籍。由于现在很多人使用 3.x,作者的代码可能无法在我们的环境中正常工作。我看到您已经更改了库的名称和 print() 函数,但是要使代码完全正常工作,您应该尝试:
# Define variables
max_score = -np.inf
output_label = None
那么应该可以了。事实上,您不能将 float 与 None 进行比较,但是使用 np.inf 可以解决问题并且 HMM 可以正常工作。在 PyCharm 2018.3.2 macOS Mojave 上测试。
我正在使用 Python 3.6 中的语音识别器代码,其中包含隐马尔可夫模型 (HMM)。
由 .wav
个文件组成的训练数据(输入文件夹)组织为
train
pineapple
apple
banana
orange
kiwi
peach
lime
类似的模式用于 test
数据文件夹。
代码是 运行 来自命令提示符:
python Speech-Recognizer.py --input-folder train
代码贴在下面:
import os
import argparse
import numpy as np
from scipy.io import wavfile
from hmmlearn import hmm
from python_speech_features import mfcc
# Function to parse input arguments
def build_arg_parser():
parser = argparse.ArgumentParser(description='Trains the HMM classifier')
parser.add_argument("--input-folder", dest="input_folder", required=True,
help="Input folder containing the audio files in subfolders")
return parser
# Class to handle all HMM related processing
class HMMTrainer(object):
def __init__(self, model_name='GaussianHMM', n_components=4, cov_type='diag', n_iter=1000):
self.model_name = model_name
self.n_components = n_components
self.cov_type = cov_type
self.n_iter = n_iter
self.models = []
if self.model_name == 'GaussianHMM':
self.model = hmm.GaussianHMM(n_components=self.n_components,
covariance_type=self.cov_type, n_iter=self.n_iter)
else:
raise TypeError('Invalid model type')
# X is a 2D numpy array where each row is 13D
def train(self, X):
np.seterr(all='ignore')
self.models.append(self.model.fit(X))
# Run the model on input data
def get_score(self, input_data):
return self.model.score(input_data)
if __name__ == '__main__':
args = build_arg_parser().parse_args()
input_folder = args.input_folder
hmm_models = []
# Parse the input directory
for dirname in os.listdir(input_folder):
# Get the name of the subfolder
subfolder = os.path.join(input_folder, dirname)
if not os.path.isdir(subfolder):
continue
# Extract the label
label = subfolder[subfolder.rfind('/') + 1:]
# Initialize variables
X = np.array([])
y_words = []
# Iterate through the audio files (leaving 1 file for testing in each class)
for filename in [x for x in os.listdir(subfolder) if x.endswith('.wav')][:-1]:
# Read the input file
filepath = os.path.join(subfolder, filename)
sampling_freq, audio = wavfile.read(filepath)
# Extract MFCC features
mfcc_features = mfcc(audio, sampling_freq)
# Append to the variable X
if len(X) == 0:
X = mfcc_features
else:
X = np.append(X, mfcc_features, axis=0)
# Append the label
y_words.append(label)
print('X.shape =', X.shape)
# Train and save HMM model
hmm_trainer = HMMTrainer()
hmm_trainer.train(X)
hmm_models.append((hmm_trainer, label))
hmm_trainer = None
# Test files
input_files = [
'test/pineapple/pineapple15.wav',
'test/orange/orange15.wav',
'test/apple/apple15.wav',
'test/kiwi/kiwi15.wav'
]
# Classify input data
for input_file in input_files:
# Read input file
sampling_freq, audio = wavfile.read(input_file)
# Extract MFCC features
mfcc_features = mfcc(audio, sampling_freq)
# Define variables
max_score = None
output_label = None
# Iterate through all HMM models and pick
# the one with the highest score
for item in hmm_models:
hmm_model, label = item
score = hmm_model.get_score(mfcc_features)
if score > max_score:
max_score = score
output_label = label
# Print the output
print("\nTrue:", input_file[input_file.find('/') + 1:input_file.rfind('/')])
print("Predicted:", output_label)
我在 运行 上面的代码中遇到以下错误:
Traceback (most recent call last):
File "Speech-Recognizer.py", line 113, in <module>
if score > max_score:
TypeError: '>' not supported between instances of 'float' and 'NoneType'
max_score = None
...
if score > max_score:
您正在尝试将浮点数与 None 进行比较。
用 max_score = 0 代替 max_score = None 怎么样?
问这个问题已经有一段时间了,但我想我找到了解决办法,因为我也遇到了这个问题。此特定代码取自 Python (2.7) 中 Prateek Joshi 的机器学习书籍。由于现在很多人使用 3.x,作者的代码可能无法在我们的环境中正常工作。我看到您已经更改了库的名称和 print() 函数,但是要使代码完全正常工作,您应该尝试:
# Define variables
max_score = -np.inf
output_label = None
那么应该可以了。事实上,您不能将 float 与 None 进行比较,但是使用 np.inf 可以解决问题并且 HMM 可以正常工作。在 PyCharm 2018.3.2 macOS Mojave 上测试。