0

I'm trying to train a model for speaker identification using MFCC and HMM, during run time I got the following error

line 55, in <module>
    X = X_train[y_train == speaker]
IndexError: boolean index did not match indexed array along dimension 0; dimension is 1 but corresponding boolean dimension is 3301

which is this line of code

X = X_train[y_train == speaker]

I tried printing the shape of the array to get a better understanding of the problem and getting this results

(1, 3301)
(3301,)
[False False False ...  True  True  True]

but I can't seem to wrap my head around on how to solve it. Any tips and help is greatly appreciated.

here is my code block regarding said issue

import os
import numpy as np
import scipy.io.wavfile as wav
from python_speech_features import mfcc
from hmmlearn import hmm
import matplotlib.pyplot as plt

# Define the number of MFCC features and HMM states
num_features = 13
num_states = 5

# Define the directory containing the training data
data_dir = "speech-data"

# Define the names of the speaker subdirectories in the training data directory
speakers = ["speaker1", "speaker2"]

# Create a list to hold the training feature vectors and labels
X_train = []
y_train = []

# Loop over the speaker subdirectories
for speaker in speakers:
    # Get the path to the speaker's training subdirectory
    train_dir = os.path.join(data_dir, speaker, "train")

    # Loop over the audio files in the speaker's training subdirectory
    for file in os.listdir(train_dir):
        # Get the path to the audio file
        file_path = os.path.join(train_dir, file)

        # Load the audio signal
        rate, signal = wav.read(file_path)

        # Extract MFCC features from the signal
        features = mfcc(signal, rate, numcep=num_features)

        # Append the features and label to the training lists
        X_train.append(features)
        y_train.append(speaker)

# Convert the training data to numpy arrays
X_train = np.array([X_train], dtype=object)
y_train = np.array(y_train)

print(X_train.shape)
print(y_train.shape)
print(y_train == speaker)


# Create an HMM for each speaker
models = []
for speaker in speakers:
    # Get the training feature vectors and labels for the current speaker
    X = X_train[y_train == speaker]

    # Create an HMM for the current speaker
    model = hmm.GaussianHMM(n_components=num_states)
    model.fit(X)

    # Add the HMM to the list of models
    models.append(model)

I tried reshaping the array but to no avail of fixing the error. I was expecting that by reshaping the array based on the dimensions of my samples would be better.

so update, i tried changing the code based on what Chrysophylaxs said and changed it from this

    # Convert the training data to numpy arrays
X_train = np.array([X_train], dtype=object)
y_train = np.array(y_train)

print(X_train.shape)
print(y_train.shape)
print(y_train == speaker)


# Create an HMM for each speaker
models = []
for speaker in speakers:
    # Get the training feature vectors and labels for the current speaker
    X = X_train[y_train == speaker]

to this

 # Convert the training data to numpy arrays
X_train = np.array([X_train], dtype=object)
y_train = np.array(y_train)

print(X_train.shape)
print(y_train.shape)
print(y_train == speaker)


# Create an HMM for each speaker
models = []
for speaker in speakers:
    # Get the training feature vectors and labels for the current speaker
    X = X_train[:, y_train == speaker]

now i am getting a type error, mainly this one

TypeError: only size-1 arrays can be converted to Python scalars

MStudent
  • 1
  • 3
  • Your 3301 element long mask is being used to index first axis of `X_train` ("dimension 0"). Evidently that axis has length 1, so numpy is confused and does not know how to use 3301 boolean values to select from that axis! You probably want to index the second axis, given that it has length 3301 as well. So prepend a slice to keep the first axis unchanged: `X = X_train[:, y_train == speaker]`. – Chrysophylaxs Apr 03 '23 at 16:47
  • okay, thank you for the information, i'll to change the program based on what you said and try to notify you of any changes – MStudent Apr 04 '23 at 01:39

1 Answers1

0

Found the answer to my question, just need to reshape my array and pad my array with zero to create a equal size of array, and added this line of code for my X

X = X.reshape(-1, X.shape[-1])
MStudent
  • 1
  • 3