我已经创建了一个模型来预测声音样本的情绪,该模型由下面的代码组成:
共有8种情绪:中性、冷静、快乐、悲伤、愤怒、厌恶、惊讶
首先提取每个语音样本的特征,并将其放入数据帧中,然后加载它们依次指向X和(标签指向Y),然后按如下所示分割数据:
x_train, x_test, y_train, y_test = train_test_split(X, Y, random_state=0, shuffle=True)
scaler = StandardScaler()
x_train = scaler.fit_transform(x_train)
x_test = scaler.transform(x_test)
x_train = np.expand_dims(x_train, axis=2)
x_test = np.expand_dims(x_test, axis=2)
model=Sequential()
model.add(Conv1D(256, kernel_size=5, strides=1, padding='same', activation='relu', input_shape=(x_train.shape[1], 1)))
model.add(MaxPooling1D(pool_size=5, strides = 2, padding = 'same'))
model.add(Conv1D(256, kernel_size=5, strides=1, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=5, strides = 2, padding = 'same'))
model.add(Conv1D(128, kernel_size=5, strides=1, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=5, strides = 2, padding = 'same'))
model.add(Dropout(0.2))
model.add(Conv1D(64, kernel_size=5, strides=1, padding='same', activation='relu'))
model.add(MaxPooling1D(pool_size=5, strides = 2, padding = 'same'))
model.add(Flatten())
model.add(Dense(units=32, activation='relu'))
model.add(Dropout(0.3))
model.add(Dense(units=8, activation='softmax'))
model.compile(optimizer = 'adam' , loss = 'categorical_crossentropy' , metrics = ['accuracy'])
model.summary()
rlrp = ReduceLROnPlateau(monitor='loss', factor=0.4, verbose=0, patience=2, min_lr=0.0000001)
history=model.fit(x_train, y_train, batch_size=64, epochs=75, validation_data=(x_test, y_test), callbacks=[rlrp])
获得89%的正确率
现在我想用一个新的数据集进行预测。我需要做什么?
如果new_data_x_test
和new_data_y_true
是你的新数据集,那么你在训练模型后需要做的就是:
scaler = StandardScaler()
new_data_x_test = scaler.transform(new_data_x_test )
new_data_x_test= np.expand_dims(new_data_x_test, axis=2)
model.load_weight(h5)
new_data_y_pred = model.predict(new_data_x_test )
问题是,您应该根据模型要求对其进行转换。其次,用合适的评价指标对new_data_y_true
和new_data_y_pred
进行评价。