Пример #1
0
def final_results_fixed_conditions(dataset,
                                   feature,
                                   ranks,
                                   penalty_weight,
                                   init="tucker",
                                   update_rule="hals",
                                   beta=None,
                                   n_iter_max=1000,
                                   annotations_type="MIREX10",
                                   subdivision=96,
                                   penalty_func="modulo8",
                                   convolution_type="eight_bands",
                                   legend="in unknown conditions."):
    """
    Segmentation results when ranks and penalty_weight are fixed before computation.
    """
    annotations_folder = "{}/{}".format(paths.path_annotation_rwc,
                                        annotations_type)
    if dataset == "full":
        dataset_path = paths.path_entire_rwc
    elif dataset == "odd_songs":
        dataset_path = paths.path_odd_songs_rwc
    elif dataset == "even_songs":
        dataset_path = paths.path_even_songs_rwc
    elif dataset == "debug":
        dataset_path = paths.path_debug_rwc
    else:
        raise err.InvalidArgumentValueException(
            f"Dataset type not understood: {dataset}") from None

    list_songs = scr.load_RWC_dataset(dataset_path, annotations_type)
    hop_length = 32
    hop_length_seconds = 32 / 44100
    zero_five_results = []
    three_results = []
    deviation = []

    for song_and_annotations in list_songs:
        song_number = song_and_annotations[0].replace(".wav", "")

        annot_path = "{}/{}".format(annotations_folder,
                                    song_and_annotations[1])
        annotations = dm.get_segmentation_from_txt(annot_path,
                                                   annotations_type)
        references_segments = np.array(annotations)[:, 0:2]

        bars, spectrogram = scr.load_or_save_spectrogram_and_bars(
            paths.path_data_persisted_rwc, f"{dataset_path}/{song_number}",
            feature, hop_length)

        tensor_spectrogram = tf.tensorize_barwise(spectrogram, bars,
                                                  hop_length_seconds,
                                                  subdivision)
        if update_rule == "hals":
            persisted_arguments = f"_{song_number}_{feature}_{init}_{subdivision}"
        elif update_rule == "mu":
            persisted_arguments = f"mu_slow_{song_number}_beta{beta}_{feature}_{init}_{subdivision}_n_iter_max{n_iter_max}"
        else:
            raise err.InvalidArgumentValueException(
                f"Update rule type not understood: {update_rule}")

        q_factor = scr.NTD_decomp_as_script(paths.path_data_persisted_rwc,
                                            persisted_arguments,
                                            tensor_spectrogram,
                                            ranks,
                                            init=init,
                                            update_rule=update_rule,
                                            beta=beta)[1][2]
        autosimilarity = as_seg.get_autosimilarity(q_factor,
                                                   transpose=True,
                                                   normalize=True)

        segments = as_seg.dynamic_convolution_computation(
            autosimilarity,
            penalty_weight=penalty_weight,
            penalty_func=penalty_func,
            convolution_type=convolution_type)[0]
        segments_in_time = dm.segments_from_bar_to_time(segments, bars)

        tp, fp, fn = dm.compute_rates_of_segmentation(references_segments,
                                                      segments_in_time,
                                                      window_length=0.5)
        prec, rap, f_mes = dm.compute_score_of_segmentation(
            references_segments, segments_in_time, window_length=0.5)
        zero_five_results.append(
            [tp, fp, fn,
             round(prec, 4),
             round(rap, 4),
             round(f_mes, 4)])

        tp, fp, fn = dm.compute_rates_of_segmentation(references_segments,
                                                      segments_in_time,
                                                      window_length=3)
        prec, rap, f_mes = dm.compute_score_of_segmentation(
            references_segments, segments_in_time, window_length=3)
        three_results.append(
            [tp, fp, fn,
             round(prec, 4),
             round(rap, 4),
             round(f_mes, 4)])

        r_to_e, e_to_r = dm.compute_median_deviation_of_segmentation(
            references_segments, segments_in_time)
        deviation.append([r_to_e, e_to_r])

    results_at_zero_five = np.array(
        [np.mean(np.array(zero_five_results)[:, i]) for i in range(6)])
    dataframe_zero_five = pd.DataFrame(
        results_at_zero_five,
        index=[
            'True Positives', 'False Positives', 'False Negatives',
            'Precision', 'Recall', 'F measure'
        ],
        columns=[
            f"Results of {feature} with 0.5 seconds tolerance window {legend}"
        ])
    display(dataframe_zero_five.T)

    results_at_three = np.array(
        [np.mean(np.array(three_results)[:, i]) for i in range(6)])
    dataframe_three = pd.DataFrame(
        results_at_three,
        index=[
            'True Positives', 'False Positives', 'False Negatives',
            'Precision', 'Recall', 'F measure'
        ],
        columns=[
            f"Results of {feature} with 3 seconds tolerance window {legend}"
        ])
    display(dataframe_three.T)

    # mean_deviation = np.array([np.mean(np.array(deviation)[:,i]) for i in range(2)])
    # dataframe_deviation = pd.DataFrame(mean_deviation, index = ['Reference to Estimation mean deviation','Estimation to Reference mean deviation'], columns = ["Mean deviation between estimations and references{}".format(legend)])
    # display(dataframe_deviation.T)

    return results_at_zero_five, results_at_three
Пример #2
0
def several_ranks_with_cross_validation_of_param_RWC(
        learning_dataset,
        testing_dataset,
        feature,
        ranks_frequency,
        ranks_rhythm,
        ranks_pattern,
        penalty_range,
        init="tucker",
        update_rule="hals",
        beta=None,
        n_iter_max=1000,
        annotations_type="MIREX10",
        penalty_func="modulo8",
        convolution_type="eight_bands"):
    """
    Segmentation results when ranks and penalty parameter are fitted by cross validation.
    Results are shown for the test dataset.
    """
    if learning_dataset == "odd_songs":
        learning_dataset_path = paths.path_odd_songs_rwc
    elif learning_dataset == "even_songs":
        learning_dataset_path = paths.path_even_songs_rwc
    elif learning_dataset == "debug":
        learning_dataset_path = paths.path_debug_rwc
    else:
        raise err.InvalidArgumentValueException(
            f"Dataset type not understood: {learning_dataset}") from None

    if testing_dataset == "odd_songs":
        testing_dataset_path = paths.path_odd_songs_rwc
    elif testing_dataset == "even_songs":
        testing_dataset_path = paths.path_even_songs_rwc
    elif testing_dataset == "debug":
        testing_dataset_path = paths.path_debug_rwc
    else:
        raise err.InvalidArgumentValueException(
            f"Dataset type not understood: {testing_dataset_path}") from None

    if learning_dataset == testing_dataset:
        warnings.warn(
            "Careful: using the same dataset as test and learn, normal?")

    annotations_folder = "{}/{}".format(paths.path_annotation_rwc,
                                        annotations_type)
    hop_length = 32
    hop_length_seconds = 32 / 44100
    subdivision = 96

    learning_dataset_songs = scr.load_RWC_dataset(learning_dataset_path,
                                                  annotations_type)

    zero_five = -math.inf * np.ones(
        (len(learning_dataset_songs), len(ranks_frequency), len(ranks_rhythm),
         len(ranks_pattern), len(penalty_range), 1))
    three = -math.inf * np.ones(
        (len(learning_dataset_songs), len(ranks_frequency), len(ranks_rhythm),
         len(ranks_pattern), len(penalty_range), 1))

    for song_idx, song_and_annotations in enumerate(learning_dataset_songs):
        #printmd('**Chanson courante: {}**'.format(song_and_annotations[0]))
        song_number = song_and_annotations[0].replace(".wav", "")

        annot_path = "{}/{}".format(annotations_folder,
                                    song_and_annotations[1])
        annotations = dm.get_segmentation_from_txt(annot_path,
                                                   annotations_type)
        references_segments = np.array(annotations)[:, 0:2]

        bars, spectrogram = scr.load_or_save_spectrogram_and_bars(
            paths.path_data_persisted_rwc,
            "{}/{}".format(learning_dataset_path,
                           song_number), feature, hop_length)

        tensor_spectrogram = tf.tensorize_barwise(spectrogram, bars,
                                                  hop_length_seconds,
                                                  subdivision)

        for w, rank_W in enumerate(ranks_frequency):
            for h, rank_h in enumerate(ranks_rhythm):
                for q, rank_q in enumerate(ranks_pattern):
                    ranks = [rank_W, rank_h, rank_q]
                    if update_rule == "hals":
                        persisted_arguments = f"_{song_number}_{feature}_{init}_{subdivision}"
                    elif update_rule == "mu":
                        persisted_arguments = f"mu_slow_{song_number}_beta{beta}_{feature}_{init}_{subdivision}_n_iter_max{n_iter_max}"
                    else:
                        raise err.InvalidArgumentValueException(
                            f"Update rule type not understood: {update_rule}")

                    q_factor = scr.NTD_decomp_as_script(
                        paths.path_data_persisted_rwc,
                        persisted_arguments,
                        tensor_spectrogram,
                        ranks,
                        init=init,
                        update_rule=update_rule,
                        beta=beta)[1][2]

                    autosimilarity = as_seg.get_autosimilarity(q_factor,
                                                               transpose=True,
                                                               normalize=True)

                    for p, penalty in enumerate(penalty_range):
                        segments = as_seg.dynamic_convolution_computation(
                            autosimilarity,
                            penalty_weight=penalty,
                            penalty_func=penalty_func,
                            convolution_type=convolution_type)[0]
                        segments_in_time = dm.segments_from_bar_to_time(
                            segments, bars)

                        prec, rap, f_mes = dm.compute_score_of_segmentation(
                            references_segments,
                            segments_in_time,
                            window_length=0.5)
                        zero_five[song_idx, w, h, q, p] = round(f_mes, 4)

                        prec, rap, f_mes = dm.compute_score_of_segmentation(
                            references_segments,
                            segments_in_time,
                            window_length=3)
                        three[song_idx, w, h, q, p] = round(f_mes, 4)

    best_mean = 0
    best_params = []
    for w, rank_W in enumerate(ranks_frequency):
        for h, rank_h in enumerate(ranks_rhythm):
            for q, rank_q in enumerate(ranks_pattern):
                for p, penalty in enumerate(penalty_range):
                    this_avg = np.mean(zero_five[:, w, h, q, p])
                    if this_avg > best_mean:
                        best_mean = this_avg
                        best_params = [rank_W, rank_h, rank_q, penalty]

    display(
        pd.DataFrame(np.array(
            [best_params[0], best_params[1], best_params[2], best_params[3]]),
                     index=[
                         'Best rank for $W$', 'Best rank for $H$',
                         'Best rank for $Q$',
                         'Best lambda: ponderation parameter.'
                     ],
                     columns=["Learned parameters"]).T)

    learned_ranks = [best_params[0], best_params[1], best_params[2]]
    results_at_zero_five, results_at_three = final_results_fixed_conditions(
        testing_dataset,
        feature,
        learned_ranks,
        best_params[3],
        init=init,
        update_rule=update_rule,
        beta=beta,
        n_iter_max=n_iter_max,
        annotations_type=annotations_type,
        penalty_func=penalty_func,
        legend="on test dataset.",
        convolution_type=convolution_type)

    return best_params, results_at_zero_five, results_at_three