Exemplo n.º 1
0
def main():

    args = check_argv()
    pairs_fn = args.pairs_fn
    features_fn = args.features_fn
    distances_fn = args.distances_fn
    normalize_feats = args.normalize_feats
    only = args.only

    if args.metric == "cosine":
        dtw_cost_func = _dtw.multivariate_dtw_cost_cosine
    elif args.metric == "euclidean":
        dtw_cost_func = _dtw.multivariate_dtw_cost_euclidean
        # normalize_feats = False
    elif args.metric == "euclidean_squared":
        dtw_cost_func = _dtw.multivariate_dtw_cost_euclidean_squared

    # Read the pairs and the archive
    print "Start time: " + str(datetime.datetime.now())
    print "Reading pairs from:", pairs_fn
    pairs = read_pairs(pairs_fn, only)
    print "Reading features from:", features_fn
    if args.input_fmt == "kaldi_txt":
        ark = read_kaldi_ark(features_fn)
    elif args.input_fmt == "npz":
        ark = np.load(features_fn)
        ark = dict(ark)

    # sys.stdout.flush()

    # Normalize features per frame
    if normalize_feats:
        print "Normalizing features"
        for utt_id in ark:
            N = ark[utt_id].shape[0]
            for i in range(N):
                ark[utt_id][i, :] = ark[utt_id][i, :] / np.linalg.norm(
                    ark[utt_id][i, :])

    # Calculate distances
    print "Calculating distances"
    costs = np.zeros(len(pairs))
    for i_pair, pair in enumerate(pairs):
        utt_id_1, utt_id_2 = pair
        costs[i_pair] = dtw_cost_func(np.array(ark[utt_id_1], dtype=np.double),
                                      np.array(ark[utt_id_2], dtype=np.double),
                                      True)

    # Write to file
    if args.binary_dists:
        print "Writing distances to binary file:", distances_fn
        np.asarray(costs, dtype=np.float32).tofile(distances_fn)
    else:
        print "Writing distances to text file:", distances_fn
        np.asarray(costs, dtype=np.float32).tofile(distances_fn, "\n")
        open(distances_fn, "a").write("\n")  # add final newline
    print "End time: " + str(datetime.datetime.now())
Exemplo n.º 2
0
def main():

    args = check_argv()
    pairs_fn = args.pairs_fn
    features_fn = args.features_fn
    distances_fn = args.distances_fn
    normalize_feats = args.normalize_feats

    if args.metric == "cosine":
        dtw_cost_func = _dtw.multivariate_dtw_cost_cosine
    elif args.metric == "euclidean":
        dtw_cost_func = _dtw.multivariate_dtw_cost_euclidean
        # normalize_feats = False
    elif args.metric == "euclidean_squared":
        dtw_cost_func = _dtw.multivariate_dtw_cost_euclidean_squared

    # Read the pairs and the archive
    print "Start time: " + str(datetime.datetime.now())
    print "Reading pairs from:", pairs_fn
    pairs = read_pairs(pairs_fn)
    print "Reading features from:", features_fn
    if args.input_fmt == "kaldi_txt":
        ark = read_kaldi_ark(features_fn)
    elif args.input_fmt == "npz":
        ark = np.load(features_fn)
        ark = dict(ark)

    # sys.stdout.flush()

    # Normalize features per frame
    if normalize_feats:
        print "Normalizing features"
        for utt_id in ark:
            N = ark[utt_id].shape[0]
            for i in range(N):
                ark[utt_id][i, :] = ark[utt_id][i, :]/np.linalg.norm(ark[utt_id][i, :])

    # Calculate distances
    print "Calculating distances"
    costs = np.zeros(len(pairs))
    for i_pair, pair in enumerate(pairs):
        utt_id_1, utt_id_2 = pair
        costs[i_pair] = dtw_cost_func(
            np.array(ark[utt_id_1], dtype=np.double), np.array(ark[utt_id_2], dtype=np.double), True
            )

    # Write to file
    if args.binary_dists:
        print "Writing distances to binary file:", distances_fn
        np.asarray(costs, dtype=np.float32).tofile(distances_fn)
    else:
        print "Writing distances to text file:", distances_fn
        np.asarray(costs, dtype=np.float32).tofile(distances_fn, "\n")
        open(distances_fn, "a").write("\n")  # add final newline
    print "End time: " + str(datetime.datetime.now())
Exemplo n.º 3
0
def main():
    args = check_argv()

    print str(datetime.datetime.now())

    print "Reading Kaldi archive:", args.kaldi_ark_fn
    kaldi_ark = read_kaldi_ark(args.kaldi_ark_fn)
    print "Number of keys in archive:", len(kaldi_ark.keys())

    # all_features = np.asarray(np.concatenate(kaldi_ark.values(), axis=0), dtype=np.float32)
    # print "Number of feature vectors:", all_features.shape[0]
    # print "Feature vector dimensions:", all_features.shape[1]

    print "Writing feature vectors to file:", args.npz_fn
    np.savez(args.npz_fn, **kaldi_ark)

    print str(datetime.datetime.now())
Exemplo n.º 4
0
def main():
    args = check_argv()

    print str(datetime.datetime.now())

    print "Reading Kaldi archive:", args.kaldi_ark_fn
    kaldi_ark = read_kaldi_ark(args.kaldi_ark_fn)
    print "Number of keys in archive:", len(kaldi_ark.keys())

    # all_features = np.asarray(np.concatenate(kaldi_ark.values(), axis=0), dtype=np.float32)
    # print "Number of feature vectors:", all_features.shape[0]
    # print "Feature vector dimensions:", all_features.shape[1]

    print "Writing feature vectors to file:", args.npz_fn
    np.savez(args.npz_fn, **kaldi_ark)

    print str(datetime.datetime.now())
Exemplo n.º 5
0
def main():

    args = check_argv()
    pairs_fn = args.pairs_fn
    features_fn = args.features_fn
    paths_pkl_fn = args.paths_pkl_fn
    normalize_feats = args.normalize_feats

    n_paths_notify = 100

    # if args.metric == "cosine":
    #     dtw_cost_func = _dtw.multivariate_dtw_cost_cosine
    # elif args.metric == "euclidean":
    #     dtw_cost_func = _dtw.multivariate_dtw_cost_euclidean
        # normalize_feats = False

    # Read the pairs and the archive
    print "Start time: " + str(datetime.datetime.now())
    print "Reading pairs from:", pairs_fn
    pairs = read_pairs(pairs_fn)
    print "Reading features from:", features_fn
    if args.input_fmt == "kaldi_txt":
        ark = read_kaldi_ark(features_fn)
    elif args.input_fmt == "npz":
        ark = np.load(features_fn)
        ark = dict(ark)
    print "Number of entries loaded:", len(ark)

    # sys.stdout.flush()

    # Normalize features per frame
    if normalize_feats:
        print "Normalizing features"
        for utt_id in ark:
            N = ark[utt_id].shape[0]
            for i in range(N):
                ark[utt_id][i, :] = ark[utt_id][i, :]/np.linalg.norm(ark[utt_id][i, :])

    # Calculate distances
    print "Calculating paths: ",
    paths = []
    i_paths = 0
    for i_pair, pair in enumerate(pairs):
        utt_id_1, utt_id_2 = pair
        paths.append(_dtw.multivariate_dtw(
            np.array(ark[utt_id_1], dtype=np.double), np.array(ark[utt_id_2], dtype=np.double)
            )[0])
        i_paths += 1
        if i_paths % n_paths_notify == 0:
            sys.stdout.write('.')
    print

    # costs = np.zeros(len(pairs))
    # for i_pair, pair in enumerate(pairs):
    #     utt_id_1, utt_id_2 = pair
    #     costs[i_pair] = dtw_cost_func(ark[utt_id_1], ark[utt_id_2], True)

    # Write to file
    print "Pickling paths:", paths_pkl_fn
    f = open(paths_pkl_fn, "wb")
    cPickle.dump(paths, f, -1)
    f.close()
    # if args.binary_dists:
    #     print "Writing distances to binary file:", distances_fn
    #     np.asarray(costs, dtype=np.float32).tofile(distances_fn)
    # else:
    #     print "Writing distances to text file:", distances_fn
    #     np.asarray(costs, dtype=np.float32).tofile(distances_fn, "\n")
    #     open(distances_fn, "a").write("\n")  # add final newline
    print "End time: " + str(datetime.datetime.now())
Exemplo n.º 6
0
def main():

    args = check_argv()
    pairs_fn = args.pairs_fn
    features_fn = args.features_fn
    paths_pkl_fn = args.paths_pkl_fn
    normalize_feats = args.normalize_feats

    n_paths_notify = 100

    # if args.metric == "cosine":
    #     dtw_cost_func = _dtw.multivariate_dtw_cost_cosine
    # elif args.metric == "euclidean":
    #     dtw_cost_func = _dtw.multivariate_dtw_cost_euclidean
    # normalize_feats = False

    # Read the pairs and the archive
    print("Start time: " + str(datetime.datetime.now()))
    print("Reading pairs from:", pairs_fn)
    pairs = read_pairs(pairs_fn)
    print("Reading features from:", features_fn)
    if args.input_fmt == "kaldi_txt":
        ark = read_kaldi_ark(features_fn)
    elif args.input_fmt == "npz":
        ark = np.load(features_fn)
        ark = dict(ark)
    print("Number of entries loaded:", len(ark))

    # sys.stdout.flush()

    # Normalize features per frame
    if normalize_feats:
        print("Normalizing features")
        for utt_id in ark:
            N = ark[utt_id].shape[0]
            for i in range(N):
                ark[utt_id][i, :] = ark[utt_id][i, :] / np.linalg.norm(
                    ark[utt_id][i, :])

    # Calculate distances
    print("Calculating paths: ", )
    paths = []
    i_paths = 0
    for i_pair, pair in enumerate(pairs):
        utt_id_1, utt_id_2 = pair
        paths.append(
            _dtw.multivariate_dtw(np.array(ark[utt_id_1], dtype=np.double),
                                  np.array(ark[utt_id_2], dtype=np.double))[0])
        i_paths += 1
        if i_paths % n_paths_notify == 0:
            sys.stdout.write('.')
    print

    # costs = np.zeros(len(pairs))
    # for i_pair, pair in enumerate(pairs):
    #     utt_id_1, utt_id_2 = pair
    #     costs[i_pair] = dtw_cost_func(ark[utt_id_1], ark[utt_id_2], True)

    # Write to file
    print("Pickling paths:", paths_pkl_fn)
    f = open(paths_pkl_fn, "wb")
    cPickle.dump(paths, f, -1)
    f.close()
    # if args.binary_dists:
    #     print("Writing distances to binary file:", distances_fn)
    #     np.asarray(costs, dtype=np.float32).tofile(distances_fn)
    # else:
    #     print("Writing distances to text file:", distances_fn)
    #     np.asarray(costs, dtype=np.float32).tofile(distances_fn, "\n")
    #     open(distances_fn, "a").write("\n")  # add final newline
    print("End time: " + str(datetime.datetime.now()))