输入0与层conv2d_5不兼容:预期ndim = 4,在以下位置找到ndim = 1

时间:2019-03-05 06:42:43

标签: python machine-learning

输入0与conv2d_5层不兼容:预期ndim = 4,在

中找到ndim = 1

使用以下代码对python语言中的语音识别进行Macine学习,我在model.fet函数中遇到一些问题,并出现以下错误:“输入0与conv2d_5层不兼容:预期ndim = 4,找到ndim = 1 “:

<div class="spin" *ngIf="spinner == 'true'">
   <ion-spinner name="bubbles"></ion-spinner>
</div>

.spin{
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100vh;
}

我收到此错误

29]:

当我使用这段代码时,我遇到了这类错误

import numpy as np
import librosa
import os
from sklearn.model_selection import train_test_split
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
from keras.utils import to_categorical
def wav2mfcc(file_path, max_pad_len=11):
    wave, sr = librosa.load(file_path, mono=True, sr=None)
    wave = wave[::3]
    mfcc = librosa.feature.mfcc(wave, sr=16000)
    pad_width = abs(max_pad_len - mfcc.shape[1])
    mfcc = np.pad(mfcc, pad_width=((0, 0), (0, pad_width)), mode='constant')
    return mfcc
DATA_PATH = "C:/source/repos/WebApplication2/WebApplication2/tst/data/train/audio/"
# Input: Folder Path
# Output: Tuple (Label, Indices of the labels, one-hot encoded labels)
def get_labels(path=DATA_PATH):
    labels = os.listdir(path)
    label_indices = np.arange(0, len(labels))
    return labels, label_indices, to_categorical(label_indices)
def save_data_to_array(path, max_pad_len):
    labels, _, _ = get_labels(path)
    for label in labels:
        # Init mfcc vectors
        mfcc_vectors = []
        wavfiles = [path + label + '/' + wavfile for wavfile in os.listdir(path + '/' + label)]
        for wavfile in wavfiles:
            mfcc = wav2mfcc(wavfile, max_pad_len=max_pad_len)
            mfcc_vectors.append(mfcc)
            while(len(mfcc_vectors)<10):
                mfcc_vectors.append(0)
        np.save(label + '.npy', mfcc_vectors)        
def get_train_test(split_ratio=0.6, random_state=42):
    # Get available labels
    labels, indices, _ = get_labels(DATA_PATH)
    # Getting first arrays
    print(labels[0])
    X = np.load(labels[0] + '.npy')
    y = np.zeros(X.shape[0])
    # Append all of the dataset into one single array, same goes for y
    for i, label in enumerate(labels[1:]):
        x = np.load(label + '.npy')
        X = np.concatenate((X, x.T),axis=0)
        y = np.append(y, np.full(x.shape[0], fill_value= (i + 1))) 
    assert X.shape[0] == len(y)
    return train_test_split(X, y, test_size=0.5,train_size=0.5, random_state=random_state, shuffle=True)
#save_data_to_array(DATA_PATH,11)
X_train, X_test, y_train, y_test = get_train_test() 
model = Sequential()
model.add(Conv2D(32, kernel_size=(2, 2), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.25))
model.add(Dense(3, activation='softmax'))
model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=100, epochs=200, verbose=1, validation_data=(X_test, y_test))

0 个答案:

没有答案