Beispiel #1
0
def getTrainingFilenames(data_path):
    """
    training filenames
    :param data_path:
    :return:
    """
    fns = getRecordings(data_path)
    train_fns = [fn.replace('feature_', '') for fn in fns if 'feature' in fn]
    # train_fns = [fn.replace(fn.split('_')[0]+'_', '') for fn in fns]

    return train_fns
Beispiel #2
0
def getTrainingFilenames(annotation_path, cv_filename):
    """
    annotation filenames - cv test filenames
    :param annotation_path:
    :param cv_filename:
    :return:
    """
    annotation_fns = getRecordings(annotation_path)
    test_fns = annotationCvParser(cv_filename)
    train_fns = [x for x in annotation_fns if x not in test_fns]
    return train_fns
def dump_feature_label_sample_weights_onset_phrase_bock(
        audio_path, annotation_path, path_output):
    """
    dump feature, label, sample weights for each phrase with bock annotation format
    :param audio_path:
    :param annotation_path:
    :param path_output:
    :return:
    """
    log_mel_line_all = []
    for fn in getRecordings(annotation_path):

        # from the annotation to get feature, frame start and frame end of each line, frames_onset
        log_mel, frames_onset, frame_start, frame_end = dump_feature_onset_helper(
            audio_path, annotation_path, fn, 1)

        # simple sample weighting
        feature, label, sample_weights = \
            feature_onset_phrase_label_sample_weights(frames_onset,
                                                      frame_start,
                                                      frame_end,
                                                      log_mel)

        # save feature, label and weights
        feature_label_weights_saver(path_output, fn, feature, label,
                                    sample_weights)

        log_mel_line_all.append(feature)

    log_mel_line_all = np.concatenate(log_mel_line_all)

    scaler = preprocessing.StandardScaler()

    scaler.fit(log_mel_line_all)

    pickle.dump(scaler, open(path_output, 'wb'))

    return True
Beispiel #4
0
def save_feature_label_sample_weights_onset_phrase(wav_path, textgrid_path,
                                                   path_output, tier_parent,
                                                   tier_child):
    """
    phrase level feature and sample weights dump
    :param wav_path:
    :param textgrid_path:
    :param recordings:
    :param tier_parent:
    :param tier_child
    :return:
    """
    recordings = getRecordings(textgrid_path)

    log_mel_line_all = []
    for recording_name in recordings:

        nested_utterance_lists, log_mel = dump_training_data_textgrid_helper(
            wav_path=wav_path,
            textgrid_path=textgrid_path,
            recording_name=recording_name,
            tier_parent=tier_parent,
            tier_child=tier_child)
        # create the ground truth lab files
        for idx, u_list in enumerate(nested_utterance_lists):
            print 'Processing feature collecting ... ' + recording_name + ' phrase ' + str(
                idx + 1)

            if not len(u_list[1]):
                continue

            frames_onset, frame_start, frame_end = \
                get_onset_in_frame_helper(recording_name, idx, lab=False, u_list=u_list)

            log_mel_line, label, sample_weights = \
                feature_onset_phrase_label_sample_weights(frames_onset, frame_start, frame_end, log_mel)

            # save feature in h5py
            filename_log_mel_line = join(
                path_output,
                'feature' + '_' + recording_name + '_' + str(idx) + '.h5')
            h5f = h5py.File(filename_log_mel_line, 'w')
            h5f.create_dataset('feature_all', data=log_mel_line)
            h5f.close()

            # dumpy label
            filename_label = join(
                path_output,
                'label' + '_' + recording_name + '_' + str(idx) + '.pkl')
            pickle.dump(label, open(filename_label, 'wb'), protocol=2)

            # dump sample weights
            filename_sample_weights = join(
                path_output, 'sample_weights' + '_' + recording_name + '_' +
                str(idx) + '.pkl')
            pickle.dump(sample_weights,
                        open(filename_sample_weights, 'wb'),
                        protocol=2)

            log_mel_line_all.append(log_mel_line)

    log_mel_line_all = np.concatenate(log_mel_line_all)

    # dump scaler
    scaler = preprocessing.StandardScaler()

    scaler.fit(log_mel_line_all)

    filename_scaler = join(path_output, 'scaler_phrase.pkl')
    pickle.dump(scaler, open(filename_scaler, 'wb'))

    return True
Beispiel #5
0
def dump_feature_label_sample_weights(path_audio,
                                      path_textgrid,
                                      path_output=None,
                                      tier_parent=None,
                                      tier_child=None):
    """
    dump features for all the dataset for onset detection
    :return:
    """

    recording_names = getRecordings(path_textgrid)

    log_mel_p, \
    log_mel_n, \
    sample_weights_p, \
    sample_weights_n \
        = dump_feature_sample_weights_onset(wav_path=path_audio,
                                            textgrid_path=path_textgrid,
                                            recordings=recording_names,
                                            tier_parent=tier_parent,
                                            tier_child=tier_child)

    print('finished feature extraction.')

    sample_weights = np.concatenate((sample_weights_p, sample_weights_n))

    # save h5py separately for postive and negative features
    filename_log_mel_p = join(path_output, 'log_mel_p.h5')
    h5f = h5py.File(filename_log_mel_p, 'w')
    h5f.create_dataset('mfcc_p', data=log_mel_p)
    h5f.close()

    filename_log_mel_n = join(feature_data_path, 'log_mel_n.h5')
    h5f = h5py.File(filename_log_mel_n, 'w')
    h5f.create_dataset('mfcc_n', data=log_mel_n)
    h5f.close()

    del log_mel_p
    del log_mel_n

    feature_all, label_all, scaler = \
        feature_label_concatenation_h5py(filename_log_mel_p,
                                         filename_log_mel_n,
                                         scaling=True)

    print('finished feature concatenation.')

    os.remove(filename_log_mel_p)
    os.remove(filename_log_mel_n)

    feature_all = featureReshape(feature_all, nlen=7)

    print('feature shape:', feature_all.shape)

    # save feature
    filename_feature_all = join(path_output, 'feature.h5')
    h5f = h5py.File(filename_feature_all, 'w')
    h5f.create_dataset('feature_all', data=feature_all)
    h5f.close()

    # save label, sample weights and scaler
    pickle.dump(label_all,
                open(join(path_output, 'labels.pkl'), 'wb'),
                protocol=2)

    pickle.dump(sample_weights,
                open(join(path_output, 'sample_weights.pkl'), 'wb'),
                protocol=2)

    pickle.dump(scaler,
                open(join(path_output, 'scaler.pkl'), 'wb'),
                protocol=2)
        elif v.lower() in ('no', 'false', 'f', 'n', '0'):
            return False
        else:
            raise argparse.ArgumentTypeError('Boolean value expected.')

    parser = argparse.ArgumentParser(description="dump feature, label and sample weights for bock train set.")
    parser.add_argument("-p",
                        "--phrase",
                        type=str2bool,
                        default='true',
                        help="whether to collect feature for each phrase")
    args = parser.parse_args()

    if args.phrase:
        mfcc_line_all = []
        for fn in getRecordings(bock_annotations_path):
            mfcc_line = dump_feature_onset_phrase(audio_path=bock_audio_path,
                                                  annotation_path=bock_annotations_path,
                                                  fn=fn,
                                                  channel=1)
            mfcc_line_all.append(mfcc_line)

        mfcc_line_all = np.concatenate(mfcc_line_all)

        scaler = preprocessing.StandardScaler()

        scaler.fit(mfcc_line_all)

        pickle.dump(scaler, open(join(bock_feature_data_path_madmom_simpleSampleWeighting_phrase,
                                      'scaler_bock_phrase.pkl'), 'wb'))
    # else: