Exemple #1
0
def Train(save_model_name: str):
    Config.save_model_name = save_model_name
    x_train, x_test, y_train, y_test = Opensmile_Feature.get_data(
        Config.DATA_PATH, Config.TRAIN_FEATURE_PATH_OPENSMILE, train=True)
    model = MLP_Model()
    model.train(x_train, y_train)
    model.evaluate(x_test, y_test)
    model.save_model(save_model_name)

    return model
Exemple #2
0
def Train(save_model_name: str):
    Config.save_model_name = save_model_name
    x_train, x_test, y_train, y_test = Librosa_Feature.get_data(
        Config.DATA_PATH, Config.TRAIN_FEATURE_PATH_LIBROSA, train=True)
    model = MLP_Model()
    model.train(x_train, y_train)
    model.evaluate(x_test, y_test)
    model.save_model(save_model_name)

    return model
def Train(model_name: str,
          save_model_name: str,
          if_load: bool = True,
          feature_method: str = 'opensmile'):

    # 提取特征
    if (feature_method == 'o'):
        if (if_load == True):
            x_train, x_test, y_train, y_test = of.load_feature(
                feature_path=Config.TRAIN_FEATURE_PATH_OPENSMILE, train=True)
        else:
            x_train, x_test, y_train, y_test = of.get_data(
                Config.DATA_PATH,
                Config.TRAIN_FEATURE_PATH_OPENSMILE,
                train=True)

    elif (feature_method == 'l'):
        if (if_load == True):
            x_train, x_test, y_train, y_test = lf.load_feature(
                feature_path=Config.TRAIN_FEATURE_PATH_LIBROSA, train=True)
        else:
            x_train, x_test, y_train, y_test = lf.get_data(
                Config.DATA_PATH,
                Config.TRAIN_FEATURE_PATH_LIBROSA,
                train=True)

    # 创建模型
    if (model_name == 'svm'):
        model = SVM_Model()
    elif (model_name == 'mlp'):
        model = MLP_Model()
    elif (model_name == 'lstm'):
        y_train = np_utils.to_categorical(y_train)
        y_val = np_utils.to_categorical(y_test)

        model = LSTM_Model(input_shape=x_train.shape[1],
                           num_classes=len(Config.CLASS_LABELS))

        # 二维数组转三维(samples, time_steps, input_dim)
        x_train = np.reshape(x_train, (x_train.shape[0], 1, x_train.shape[1]))
        x_test = np.reshape(x_test, (x_test.shape[0], 1, x_test.shape[1]))

    # 训练模型
    print(
        '-------------------------------- Start --------------------------------'
    )
    if (model_name == 'svm' or model_name == 'mlp'):
        model.train(x_train, y_train)
    elif (model_name == 'lstm'):
        model.train(x_train, y_train, x_test, y_val, n_epochs=Config.epochs)

    model.evaluate(x_test, y_test)
    model.save_model(save_model_name)
    print(
        '---------------------------------- End ----------------------------------'
    )

    return model