for i, sample in enumerate(samples): wave = sample['wave'] trn = sample['trn'] copyfile(wave, 'audios/audio_{}.wav'.format(i)) feature = extract_feature(input_file=wave, feature='fbank', dim=input_dim, cmvn=True) feature = build_LFR_features(feature, m=LFR_m, n=LFR_n) # feature = np.expand_dims(feature, axis=0) input = torch.from_numpy(feature).to(device) input_length = [input[0].shape[0]] input_length = torch.LongTensor(input_length).to(device) nbest_hyps = model.recognize(input, input_length, char_list, args) out_list = [] for hyp in nbest_hyps: out = hyp['yseq'] out = [char_list[idx] for idx in out] out = ''.join(out) out_list.append(out) print('OUT_LIST: {}'.format(out_list)) gt = [char_list[idx] for idx in trn] gt = ''.join(gt) print('GT: {}\n'.format(gt)) results.append({ 'out_list_{}'.format(i): out_list, 'gt_{}'.format(i): gt
samples = random.sample(samples, 10) for sample in samples: sentence_in = sample['in'] sentence_out = sample['out'] input = torch.from_numpy(np.array(sentence_in, dtype=np.long)).to(device) input_length = torch.LongTensor([len(sentence_in)]).to(device) sentence_in = ' '.join([src_idx2char[idx] for idx in sentence_in]) sentence_out = ''.join([tgt_idx2char[idx] for idx in sentence_out]) sentence_out = sentence_out.replace('<sos>', '').replace('<eos>', '') print('< ' + sentence_in) print('= ' + sentence_out) with torch.no_grad(): nbest_hyps = model.recognize(input=input, input_length=input_length, char_list=tgt_idx2char) # print(nbest_hyps) for hyp in nbest_hyps: out = hyp['yseq'] out = [tgt_idx2char[idx] for idx in out] out = ''.join(out) out = out.replace('<sos>', '').replace('<eos>', '') print('> {}'.format(out))
for i in [0, 50]: input_id = samples['input_corpus'][i] output_id = samples['output_corpus'][i] input_sent = torch.from_numpy(np.array(input_id, dtype=np.long)).to( Config.device) input_len = torch.LongTensor([len(output_id)]).to(Config.device) output_sent = torch.from_numpy(np.array(output_id, dtype=np.long)).to( Config.device) sent = [id2vocab.get(i, 'unk') for i in input_id] sent = ''.join(sent) c, q = sent.split('q:') ans = [id2vocab.get(i, 'unk') for i in output_id] ans = ''.join(ans) ans = ans.replace('<sos>', '').replace('<eos>', '') print("问题:", q) print("真实答案:", ans) with torch.no_grad(): nbest_hyps = model.recognize(input=input_sent, input_length=input_len, char_list=id2vocab) for hyp in nbest_hyps: out = hyp['yseq'] out = [id2vocab[idx] for idx in out] out = ''.join(out) out = out.replace('<sos>', '').replace('<eos>', '') print("预测答案:", out)