Example #1
0
def main():
    # Case of loading pre-extracted features and/or pre-trained feature
    pre_trained_model_file = ""

    # Set Conditions
    run_feature_extraction = False
    run_training = True

    # Instantiate mgc main class
    MGC = MusicGenreClassification(AudioDatasetMaker,
                                   AudioFeatureExtraction,
                                   Classifier,
                                   music_dataset_path="processed_data",
                                   setting_file="config/master_config.ini")

    # Make label from genre names in processed_music_data
    MGC.make_label()

    # Feature extraction/ Load pre-extracted feature
    if run_feature_extraction is True:
        # Apply feature extraction
        directory_files_feature_dict, label_list = MGC.feature_extraction()

        print("Cleaning extracted feature...")

        # Apply data processing to extracted feature
        expert_feature_array, mel_spectrogram_array, label_array = MGC.dict2array(directory_files_feature_dict,
                                                                                  label_list,
                                                                                  MGC.cfg.normalize)

        # Save extracted data
        expert_feature_array, mel_spectrogram_array, label_array = MGC.save_data(expert_feature_array,
                                                                                 mel_spectrogram_array,
                                                                                 label_array)
        print("Saved feature!")

    # Load pre-extracted feature. Train/Test separation
    if MGC.CLF.selected_classifier == 'cnn' or MGC.CLF.selected_classifier == 'resnet' or MGC.CLF.selected_classifier == 'rnn':
        data_array, label_array = DataProcess.read_data_from_array("backend/feature/mel_spectrogram")
        train_data, test_data, train_label, test_label = MGC.make_dataset_from_array(data_array, label_array)
    else:
        data_array, label_array = DataProcess.read_data_from_array("backend/feature/expert")
        train_data, test_data, train_label, test_label = MGC.make_dataset_from_array(data_array, label_array)

    # Run training or load pre-trained model
    print("Training model...")
    model = MGC.training(run_training,
                         train_data,
                         train_label,
                         pre_trained_model_file,
                         output_model_directory_path="backend/model")

    print("Trained model!")

    # Write metadata json file
    metadata = {"words": [label for label in os.listdir('processed_data')]}
    FileUtil.dict2json(metadata, 'backend/model/metadata.json')

    # Test model performance
    loss, accuracy = MGC.test(model, test_data, test_label)
Example #2
0
def main():
    # Case of loading pre-extracted features and/or pre-trained feature
    pre_trained_model_file = ""

    # Set Conditions
    run_feature_extraction = True
    run_training = True

    # Instantiate mgc main class
    MGC = MusicGenreClassification(AudioDatasetMaker, AudioFeatureExtraction, Classifier,
                                   music_dataset_path="processed_music_data_small",
                                   setting_file="config/master_config.ini")

    # Make label from genre names in processed_music_data
    MGC.make_label()

    # Feature extraction/ Load pre-extracted feature
    if run_feature_extraction is True:
        # Apply feature extraction
        directory_files_feature_dict, label_list = MGC.feature_extraction()

        # Apply data processing to extracted feature
        expert_feature_array, mel_spectrogram_array, label_array = MGC.dict2array(directory_files_feature_dict,
                                                                                  label_list,
                                                                                  MGC.cfg.normalize)

        # Save extracted data
        expert_feature_array, mel_spectrogram_array, label_array = MGC.save_data(expert_feature_array, mel_spectrogram_array, label_array)

    # Load pre-extracted feature. Train/Test separation
    if MGC.CLF.selected_classifier == 'cnn' or MGC.CLF.selected_classifier == 'resnet' or MGC.CLF.selected_classifier == 'rnn':
        data_array, label_array = DataProcess.read_data_from_array("backend/feature/mel_spectrogram")
        train_data, test_data, train_label, test_label = MGC.make_dataset_from_array(data_array, label_array)
    else:
        data_array, label_array = DataProcess.read_data_from_array("backend/feature/expert")
        train_data, test_data, train_label, test_label = MGC.make_dataset_from_array(data_array, label_array)

    # Run training or load pre-trained model
    model = MGC.training(run_training, train_data, train_label, pre_trained_model_file, output_model_directory_path="backend/model")

    # Test model performance
    accuracy = MGC.test(model, test_data, test_label)
    print("Test accuracy is {0}% \n".format(accuracy))