Exemple #1
0
def h5features_stack_fbanks(fbanks_file, stacked_fbanks_file):
    import h5features
    index = h5features.read_index(fbanks_file)
    files = index['files']
    for f in files:
        times, fbanks = h5features.read(
            fbanks_file, 'features', from_internal_file=f, index=index)
        stacked_fbanks = stack_fbanks(fbanks[f])
        h5features.write(stacked_fbanks_file, 'features', [f],
                         [times[f]], [stacked_fbanks])
Exemple #2
0
def decode_from_features(model_path,
                         wavefile_list,
                         output_file,
                         features_file,
                         verbose=0):
    """Function to calculate the embeddings for a list of wavefiles using
    a trained ABnet in the h5features format

    Parameters:
    -----------

    model_path: string, path to the abnet

    wavefile_list: string, path to the list of wavefiles (wavefiles
      are specified by their full path, one file per line)

    output_file: string, path to the outputfile, it will contains the
      embeddings in h5features format

    """
    with open(wavefile_list) as fin:
        wavefiles = [w.strip() for w in fin]
    nnet_file = p.join(model_path, 'model.pickle')
    mean_std_file = p.join(model_path, 'pairs_mean_std.npz')
    stacked_fb_file = p.join(model_path, 'stackedfeats.h5f')
    prepare.h5features_feats2stackedfeats(features_file, stacked_fb_file)
    if verbose:
        print('stacked fbanks calculated')
    with open(nnet_file, 'rb') as f:
        nnet = cPickle.load(f)

    with open(p.join(model_path, 'nframes')) as fin:
        NFRAMES = int(fin.readline().strip())

    transform = nnet.transform_x1()
    tmp = np.load(mean_std_file)
    mean = np.tile(tmp['mean'], NFRAMES)
    std = np.tile(tmp['std'], NFRAMES)

    if verbose:
        print('model loaded')

    index = h5features.read_index(stacked_fb_file)

    # TODO maybe normalize embedded features ???
    def do_transform(fname):
        times, feats = h5features.read(stacked_fb_file,
                                       from_internal_file=fname,
                                       index=index)

        times = times[fname]
        feats = feats[fname]
        X = np.asarray((feats - mean) / std, dtype='float32')
        # times = np.arange(0.01, 0.01*npz.shape[0], 0.01)
        emb_wrd, emb_spkr = transform(X)
        return emb_wrd

    prepare.h5features_fbanks(index['files'],
                              output_file,
                              featfunc=do_transform)
    if verbose:
        print('bottleneck features calculated, all done.')
 def __init__(self, feature_file):
     self.feature_file = feature_file
     self.index = h5features.read_index(feature_file)