Beispiel #1
0
                             downsample=args.downsample)
    reader.initialize()

    since = time.time()
    batch_size = args.batch_size
    fout = open(args.output, 'w')
    while True:
        src_seq, src_mask, utts = reader.read_batch_utt(batch_size)
        if len(utts) == 0: break
        with torch.no_grad():
            src_seq, src_mask = src_seq.to(device), src_mask.to(device)
            hypos, scores = beam_search(model,
                                        src_seq,
                                        src_mask,
                                        device,
                                        args.beam_size,
                                        args.max_len,
                                        len_norm=args.len_norm,
                                        coverage=args.coverage,
                                        lm=lm,
                                        lm_scale=args.lm_scale)
            hypos, scores = hypos.tolist(), scores.tolist()
            if args.format == 'ctm':
                write_ctm(hypos, scores, fout, utts, dic, word_dic, args.space)
            else:
                write_text(hypos, scores, fout, utts, dic, args.space)

    fout.close()
    time_elapsed = time.time() - since
    print("  Elapsed Time: %.0fm %.0fs" %
          (time_elapsed // 60, time_elapsed % 60))
Beispiel #2
0
            src_seq, src_mask, utts = reader.read_batch_utt(1)
            if len(utts) == 0: break
            utt = utts[0]

            fout = open(utt + '.info', 'w')
            time_len = src_seq.size(1)
            for i in range((time_len - 1) // win + 1):
                e = min(time_len, win * (i + 1))
                src = src_seq[:, 0:e, :]
                mask = src_mask[:, 0:e]

                src, mask = src.to(device), mask.to(device)
                hypos, scores = beam_search(model,
                                            src,
                                            mask,
                                            device,
                                            beam_size,
                                            max_len,
                                            len_norm=args.len_norm)
                hypo, score = [], []
                for token, s in zip(hypos[0], scores[0]):
                    if token == 2: break
                    hypo.append(token)
                    score.append(s)
                print(hypo)

                tgt = torch.LongTensor([1] + hypo + [2]).to(device).view(1, -1)
                attn = model.attend(src, mask, tgt)[1]
                attn = attn[0]

                #print(attn)
                             fp16=args.fp16,
                             downsample=args.downsample)
    reader.initialize()

    since = time.time()
    batch_size = args.batch_size
    fout = open(args.output, 'w')
    while True:
        src_seq, src_mask, utts = reader.read_batch_utt(batch_size)
        if len(utts) == 0: break
        with torch.no_grad():
            src_seq, src_mask = src_seq.to(device), src_mask.to(device)
            hypos, scores = beam_search(model,
                                        src_seq,
                                        src_mask,
                                        device,
                                        args.beam_size,
                                        args.max_len,
                                        len_norm=args.len_norm)
            hypos, scores = hypos.tolist(), scores.tolist()
            if args.format == 'ctm':
                write_ctm(hypos, scores, fout, utts, dic, word_dic, args.space)
            elif args.format == 'stm':
                write_stm(hypos, fout, utts, dic, word_dic, args.space)
            else:
                write_text(hypos, fout, utts, dic, word_dic, args.space)

    fout.close()
    time_elapsed = time.time() - since
    print("  Elapsed Time: %.0fm %.0fs" %
          (time_elapsed // 60, time_elapsed % 60))