Пример #1
0
def decode_best_path(probs, ref=None, blank=0):
    """
    解码最佳路径
    """

    # 计算最可能的path
    best_path = np.argmax(probs,axis=0).tolist()
  
    # 折叠phone 
    hyp = []
    for i,b in enumerate(best_path):
        # 忽略blank
        if b == blank:
            continue
        # 忽略重复label
        elif i != 0 and  b == best_path[i-1]:
            continue
        else:
            hyp.append(b)

    # 计算错误率
    dist = 0
    if ref is not None:
        ref = ref.tolist()
        dist,_,_,_,_ = ed.edit_distance(ref,hyp)
    
    return hyp,dist
Пример #2
0
def decode_best_path(probs, ref=None, blank=0):
    """
    Computes best path given sequence of probability distributions per frame.
    Simply chooses most likely label at each timestep then collapses result to
    remove blanks and repeats.
    Optionally computes edit distance between reference transcription
    and best path if reference provided.
    Returns hypothesis transcription and edit distance if requested.
    """

    # Compute best path
    best_path = np.argmax(probs,axis=0).tolist()
  
    # Collapse phone string
    hyp = []
    for i,b in enumerate(best_path):
	# ignore blanks
	if b == blank:
	    continue
	# ignore repeats
	elif i != 0 and  b == best_path[i-1]:
	    continue
	else:
	    hyp.append(b)

    # Optionally compute phone error rate to ground truth
    dist = 0
    if ref is not None:
	ref = ref.tolist()
	dist,_,_,_,_ = ed.edit_distance(ref,hyp)
    
    return hyp,dist
Пример #3
0
def main(args):
    with open(args.pk_file1, 'rb') as fin:
        hyps1 = pickle.load(fin)
        refs = pickle.load(fin)
        pickle.load(fin)  # hypscores
        pickle.load(fin)  # refscores
        numphones = pickle.load(fin)
    with open(args.pk_file2, 'rb') as fin:
        hyps2 = pickle.load(fin)
    assert len(hyps1) == len(hyps2), 'hyps have different lengths'

    differ = difflib.Differ()

    num_diff = 0
    hyp1_better = 0
    hyp2_better = 0
    for (hyp1, hyp2, ref) in zip(hyps1, hyps2, refs):
        if hyp1 == hyp2:
            continue
        num_diff += 1

        label1 = 'hyp1:'
        label2 = 'hyp2:'

        if args.score:
            dist1, _, _, _, _ = ed.edit_distance(ref, hyp1)
            dist2, _, _, _, _ = ed.edit_distance(ref, hyp2)
            if dist1 < dist2:
                hyp1_better += 1
                label1 = blue(label1)
                label2 = red(label2)
            else:
                hyp2_better += 1
                label1 = red(label1)
                label2 = blue(label2)

        print label1, collapse_seq(hyp1)
        print label2, collapse_seq(hyp2)
        pprint(list(differ.compare([collapse_seq(hyp1)],
                                   [collapse_seq(hyp2)])))
        print green(' ref:'), collapse_seq(ref)
        print '-' * 80

    if args.score:
        print 'hyp1 better: %d' % hyp1_better
        print 'hyp2 better: %d' % hyp2_better
    print 'Differ on %d/%d utts' % (num_diff, len(refs))
Пример #4
0
def main(args):
    with open(args.pk_file1, 'rb') as fin:
        hyps1 = pickle.load(fin)
        refs = pickle.load(fin)
        pickle.load(fin)  # hypscores
        pickle.load(fin)  # refscores
        numphones = pickle.load(fin)
    with open(args.pk_file2, 'rb') as fin:
        hyps2 = pickle.load(fin)
    assert len(hyps1) == len(hyps2), 'hyps have different lengths'

    differ = difflib.Differ()

    num_diff = 0
    hyp1_better = 0
    hyp2_better = 0
    for (hyp1, hyp2, ref) in zip(hyps1, hyps2, refs):
        if hyp1 == hyp2:
            continue
        num_diff += 1

        label1 = 'hyp1:'
        label2 = 'hyp2:'

        if args.score:
            dist1, _, _, _, _ = ed.edit_distance(ref, hyp1)
            dist2, _, _, _, _ = ed.edit_distance(ref, hyp2)
            if dist1 < dist2:
                hyp1_better += 1
                label1 = blue(label1)
                label2 = red(label2)
            else:
                hyp2_better += 1
                label1 = red(label1)
                label2 = blue(label2)

        print label1, collapse_seq(hyp1)
        print label2, collapse_seq(hyp2)
        pprint(list(differ.compare([collapse_seq(hyp1)], [collapse_seq(hyp2)])))
        print green(' ref:'), collapse_seq(ref)
        print '-' * 80

    if args.score:
        print 'hyp1 better: %d' % hyp1_better
        print 'hyp2 better: %d' % hyp2_better
    print 'Differ on %d/%d utts' % (num_diff, len(refs))
Пример #5
0
def test(opts):
    import editDistance as ed

    print "Testing model %s" % opts.inFile

    phone_map = get_phone_map_swbd()

    with open(opts.inFile, 'r') as fid:
        old_opts = pickle.load(fid)
        _ = pickle.load(fid)
        _ = pickle.load(fid)
        loader = dl.DataLoader(opts.dataDir, old_opts.rawDim,
                               old_opts.inputDim)
        if 'layers' not in dir(old_opts):
            old_opts.layers = [old_opts.layerSize] * old_opts.numLayers
        nn = nnet.NNet(old_opts.inputDim,
                       old_opts.outputDim,
                       old_opts.layers,
                       train=False)
        nn.initParams()
        nn.fromFile(fid)

    totdist = numphones = 0

    fid = open('hyp.txt', 'w')
    for i in range(1, opts.numFiles + 1):
        data_dict, alis, keys, sizes = loader.loadDataFileDict(i)
        for k in keys:
            gp.free_reuse_cache()
            hyp = nn.costAndGrad(data_dict[k])
            hyp = [phone_map[h] for h in hyp]
            ref = [phone_map[int(r)] for r in alis[k]]
            dist, ins, dels, subs, corr = ed.edit_distance(ref, hyp)
            print "Distance %d/%d" % (dist, len(ref))
            fid.write(k + ' ' + ' '.join(hyp) + '\n')
            totdist += dist
            numphones += len(alis[k])

    fid.close()
    print "PER : %f" % (100 * totdist / float(numphones))
Пример #6
0
def test(opts):
    import editDistance as ed

    print "Testing model %s"%opts.inFile

    phone_map = get_phone_map_swbd()

    with open(opts.inFile,'r') as fid:
	old_opts = pickle.load(fid)
	_ = pickle.load(fid)
	_ = pickle.load(fid)
	loader = dl.DataLoader(opts.dataDir,old_opts.rawDim,old_opts.inputDim)
        if 'layers' not in dir(old_opts):
            old_opts.layers = [old_opts.layerSize]*old_opts.numLayers
	nn = nnet.NNet(old_opts.inputDim,old_opts.outputDim,old_opts.layers,train=False)
	nn.initParams()
	nn.fromFile(fid)

    totdist = numphones = 0

    fid = open('hyp.txt','w')
    for i in range(1,opts.numFiles+1):
	data_dict,alis,keys,sizes = loader.loadDataFileDict(i)
	for k in keys:
	    gp.free_reuse_cache()
	    hyp = nn.costAndGrad(data_dict[k])
	    hyp = [phone_map[h] for h in hyp]
	    ref = [phone_map[int(r)] for r in alis[k]]
	    dist,ins,dels,subs,corr = ed.edit_distance(ref,hyp)
	    print "Distance %d/%d"%(dist,len(ref))
	    fid.write(k+' '+' '.join(hyp)+'\n')
	    totdist += dist
	    numphones += len(alis[k])

    fid.close()
    print "PER : %f"%(100*totdist/float(numphones))