Example #1
0
def test():

    opt = parse_args()

    opt.experiment = os.path.join(root_dir, opt.experiment)
    opt.chkpt = os.path.join(opt.experiment, opt.chkpt)
    opt.out_file = os.path.join(opt.experiment, opt.out_file)
    opt.out_json = os.path.join(opt.experiment, opt.out_json)

    sessions = json.loads(open(opt.test_file).read())['sessions']

    # Model loading
    model = make_model(len(opt.word2idx))
    chkpt = torch.load(opt.chkpt, map_location = lambda storage, log: storage)
    model.load_state_dict(chkpt)
    if opt.gpuid >= 0:
        model = model.cuda()

    # ====== *********************** ================
    model.eval()
    # ===============================================

    # decode
    results = []
    print('Decoding ...')
    decode_sessions = {'sessions': []}
    for session in sessions:
        n_session = {}
        n_session['session-id'] = session['session-id']
        n_session['turns'] = []
        for turn in session['turns']:

            asr_hyps = turn['asr-hyps']
            asr_hyp = asr_hyps[0]['asr-hyp']

            string = translate(model, asr_hyp, opt.word2idx, opt.idx2word, opt.cuda)
            if string == '':
                classes = []
            else:
                classes = trp_reverse_process(string, 1)
                results.append((asr_hyp, string))

            slu_hyp = [slot2dic(string) for string in classes]

            n_session['turns'].append(
                {
                    'asr-hyps': asr_hyps,
                    'slu-hyps': [{'slu-hyp': slu_hyp, 'score': 1.0}]
                    }
                )

        decode_sessions['sessions'].append(n_session)
    string = json.dumps(decode_sessions, sort_keys=True, indent=4, separators=(',', ':'))
    with open(opt.out_json, 'w') as f:
        f.write(string)
    print('Decode results saved in {}'.format(opt.save_file))
    with open(opt.out_file, 'w') as f:
        for (enc, dec) in results:
            f.write('{}\t<=>\t{}\n'.format(enc.strip(), dec.strip()))
Example #2
0
def test(opt):

    opt.experiment = os.path.join(root_dir, opt.experiment)
    opt.load_chkpt = os.path.join(opt.experiment, opt.load_chkpt)
    opt.save_decode = os.path.join(opt.experiment, opt.save_decode)
    opt.test_json = os.path.join(opt.data_root, opt.test_json)

    idx2class = {v:k for k,v in opt.class2idx.items()}
    model = make_model(opt)
    
    chkpt = torch.load(opt.load_chkpt, map_location=lambda storage, log: storage)
    model.load_state_dict(chkpt)

    # =======================================
    model.eval()
    # =======================================

    sessions = json.loads(open(opt.test_json).read())['sessions']
    print('Decoding ...')
    decode_sessions = {'sessions': []}
    for session in sessions:
        n_session = {}
        n_session['session-id'] = session['session-id']
        n_session['turns'] = []
        for turn in session['turns']:

            asr_hyps = turn['asr-hyps']
            sent = asr_hyps[0]['asr-hyp']
            tokens = process_sent(sent)

            if len(tokens) == 0:
                slu_hyp = []
            else:
                sent_ids = [opt.word2idx.get(w) if w in opt.word2idx else Constants.UNK for w in tokens]
                datas = torch.from_numpy(np.asarray(sent_ids, dtype='int64')).view(1, -1)
                if opt.cuda:
                    datas = datas.cuda()
                probs = model(datas, None)
                scores = probs.data.cpu().view(-1,).numpy()
                pred_classes = [i for i,p in enumerate(scores) if p > 0.5]
                classes = [idx2class[i] for i in pred_classes]
                slu_hyp = [slot2dic(string) for string in classes]

            n_session['turns'].append(
                {
                    'asr-hyps': asr_hyps,
                    'slu-hyps': [{'slu-hyp': slu_hyp, 'score': 1.0}]
                    }
                )

        decode_sessions['sessions'].append(n_session)

    string = json.dumps(decode_sessions, sort_keys=True, indent=4, separators=(',', ':'))
    with open(opt.save_decode, 'w') as f:
        f.write(string)
    print('Decode results saved in {}'.format(opt.save_decode))
Example #3
0
def test(opt):

    opt.experiment = os.path.join(root_dir, opt.experiment)
    opt.load_chkpt = os.path.join(opt.experiment, opt.save_model)
    opt.save_file = os.path.join(opt.experiment, opt.save_file)
    opt.test_file = os.path.join(opt.data_root, opt.test_file)

    sessions = json.loads(open(opt.test_file).read())['sessions']

    # Model loading
    model = make_model(opt)
    chkpt = torch.load(opt.load_chkpt,
                       map_location=lambda storage, log: storage)
    model.load_state_dict(chkpt)
    if opt.deviceid >= 0:
        model = model.cuda()
    print(model)
    # ====== *********************** ================
    model.eval()
    # ===============================================
    # decode
    print('Decoding ...')
    decode_sessions = {'sessions': []}
    for session in sessions:
        n_session = {}
        n_session['session-id'] = session['session-id']
        n_session['turns'] = []
        for turn in session['turns']:

            asr_hyps = turn['asr-hyps']
            asr_hyp = asr_hyps[0]['asr-hyp']

            classes = decode_slu(model, asr_hyp, opt.memory, opt.cuda)

            slu_hyp = [slot2dic(string) for string in classes]

            n_session['turns'].append({
                'asr-hyps':
                asr_hyps,
                'slu-hyps': [{
                    'slu-hyp': slu_hyp,
                    'score': 1.0
                }]
            })

        decode_sessions['sessions'].append(n_session)
    string = json.dumps(decode_sessions,
                        sort_keys=True,
                        indent=4,
                        separators=(',', ':'))
    with open(opt.save_file, 'w') as f:
        f.write(string)

    print('Decode results saved in {}'.format(opt.save_file))