Exemplo n.º 1
0
def test_batch():
    data_args.data_type = 'test'
    data_args.shuffle = False
    data_args.batch_size = 1
    test_data = get_data(data_args)
    am_batch = test_data.get_am_batch()
    word_num = 0
    word_error_num = 0
    for i in range(10):
        print('\n the ', i, 'th example.')
        # 载入训练好的模型,并进行识别
        inputs, _ = next(am_batch)
        x = inputs['the_inputs']
        y = test_data.pny_lst[i]
        result = am.model.predict(x, steps=1)
        # 将数字结果转化为文本结果
        _, text = decode_ctc(result, train_data.am_vocab)
        text = ' '.join(text)
        print('文本结果:', text)
        print('原文结果:', ' '.join(y))
        with sess.as_default():
            text = text.strip('\n').split(' ')
            x = np.array([train_data.pny_vocab.index(pny) for pny in text])
            x = x.reshape(1, -1)
            preds = sess.run(lm.preds, {lm.x: x})
            label = test_data.han_lst[i]
            got = ''.join(train_data.han_vocab[idx] for idx in preds[0])
            print('原文汉字:', label)
            print('识别结果:', got)
            word_error_num += min(len(label), GetEditDistance(label, got))
            word_num += len(label)
    print('词错误率:', word_error_num / word_num)
    sess.close()
# 4. 进行测试-------------------------------------------
am_batch = test_data.get_am_batch()
word_num = 0
word_error_num = 0
for i in range(10):
    print('\n the ', i, 'th example.')
    # 载入训练好的模型,并进行识别
    inputs, _ = next(am_batch)
    x = inputs['the_inputs']
    y = test_data.pny_lst[i]
    result = am.model.predict(x, steps=1)
    # 将数字结果转化为文本结果
    _, text = decode_ctc(result, train_data.am_vocab)
    text = ' '.join(text)
    print('文本結果:', text)
    print('原文結果:', ' '.join(y))
    with sess.as_default():
        text = text.strip('\n').split(' ')
        x = np.array([train_data.pny_vocab.index(pny) for pny in text])
        x = x.reshape(1, -1)
        preds = sess.run(lm.preds, {lm.x: x})
        label = test_data.han_lst[i]
        got = ''.join(train_data.han_vocab[idx] for idx in preds[0])
        print('原文漢字:', label)
        print('识别結果:', got)
        word_error_num += min(len(label), GetEditDistance(label, got))
        word_num += len(label)
print('詞错誤率:', word_error_num / word_num)
sess.close()
Exemplo n.º 3
0
am_args = am_hparams()
am_args.vocab_size = len(am_vocab)
am = Am(am_args)
print('loading acoustic model...')
am.ctc_model.load_weights(model_save_path)

data_args.data_type = 'test'
data_args.shuffle = False
data_args.batch_size = 1
test_data = get_data(data_args)

# 4. 进行测试-------------------------------------------
am_batch = test_data.get_am_batch()
word_num = 0
word_error_num = 0
for i in range(1):
    print('\n the ', i, 'th example.')
    # 载入训练好的模型,并进行识别
    inputs, _ = next(am_batch)
    x = inputs['the_inputs']
    y = test_data.pny_lst[i]
    result = am.model.predict(x, steps=1)
    # 将数字结果转化为文本结果
    _, text = decode_ctc(result, am_vocab)
    text = ' '.join(text)
    print('文本结果:', text)
    print('原文结果:', ' '.join(y))

    word_error_num += min(len(y), GetEditDistance(y, text.split(' ')))
    word_num += len(y)
print('词错误率:', word_error_num / word_num)
Exemplo n.º 4
0
def recognize(args):
    model, LFR_m, LFR_n = Transformer.load_model(args.model_path)
    print(model)
    model.eval()
    model.cuda()
    char_list, sos_id, eos_id = process_dict(args.dict)
    assert model.decoder.sos_id == sos_id and model.decoder.eos_id == eos_id
    tr_dataset = AudioDataset('test', args.batch_size)
    path_list = tr_dataset.path_lst
    label_list = tr_dataset.han_lst
    num_data = tr_dataset.path_count
    ran_num = random.randint(0, num_data - 1)

    num = args.count
    words_num = 0
    word_error_num = 0
    seq_error = 0
    data = ''
    with torch.no_grad():
        for index in range(num):
            try:
                print('\nthe ', index + 1, 'th example.')
                data += 'the ' + str(index + 1) + 'th example.\n'
                index = (ran_num + index) % num_data
                standard_label = label_list[index]
                feature, label = get_fbank_and_hanzi_data(
                    index, args.feature_dim, char_list, path_list, label_list)
                if len(feature) > 1600:
                    continue
                input = build_LFR_features(feature, args.LFR_m, args.LFR_n)
                input = torch.from_numpy(input).float()
                input_length = torch.tensor([input.size(0)], dtype=torch.int)
                input = input.cuda()
                nbest_hyps = model.recognize(input, input_length, char_list,
                                             args)
                pred_label = nbest_hyps[0]['yseq'][1:-1]
                pred_res = ''.join([char_list[index] for index in pred_label])
                print("stand:", label)
                print("pred :", pred_label)
                data += "stand:" + str(standard_label) + '\n'
                data += "pred :" + str(pred_res) + '\n'
                words_n = len(label)
                words_num += words_n
                word_distance = GetEditDistance(pred_label, label)
                if (word_distance <= words_n):
                    word_error_num += word_distance
                else:
                    word_error_num += words_n

                if pred_label != label:
                    seq_error += 1
            except ValueError:
                continue
    print('WER = ', (1 - word_error_num / words_num) * 100, '%')
    print('CER = ', (1 - seq_error / args.count) * 100, '%')
    data += 'WER = ' + str((1 - word_error_num / words_num) * 100) + '%'
    data += 'CER = ' + str((1 - seq_error / args.count) * 100) + '%'
    with open('../../model_log/pred/test_' + str(args.count) + '.txt',
              'w',
              encoding='utf-8') as f:
        f.writelines(data)