예제 #1
0
파일: main.py 프로젝트: sz128/Unaligned-SLU
def gen(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.class_file = os.path.join(opt.data_root, opt.class_file)

    # 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 ...')
    with codecs.open(opt.class_file, 'r') as f:
        classes = f.readlines()
    res = []
    for cls in classes:
        class_string = cls.strip()
        pred_utterances = decode_utterance(model, class_string, opt.memory, opt.cuda, opt.nbest)
        for i in range(len(pred_utterances)):
            res.append((pred_utterances[i], class_string))

    with open(opt.save_file, 'w') as f:
        for (utterance, class_string) in res:
            f.write('{}\t<=>\t{}\n'.format(utterance, class_string))

    print('Decode results saved in {}'.format(opt.save_file))
예제 #2
0
파일: main.py 프로젝트: sz128/Unaligned-SLU
def gen(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.data_root, opt.save_file)
    opt.class_file = os.path.join(opt.data_root, opt.class_file)

    dir_name = os.path.abspath(os.path.dirname(opt.save_file))
    print(dir_name)
    if not os.path.exists(dir_name):
        os.makedirs(dir_name)

    # 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 ...')
    classes = json.loads(open(opt.class_file, 'r').read())
    res = []
    for cls in classes:
        triple = cls[0].strip()
        class_string = cls[1].strip()
        value_dict = cls[2]
        pred_utterances = decode_utterance(model, class_string, opt.memory,
                                           opt.cuda, opt.nbest)
        for i in range(len(pred_utterances)):
            utt = pred_utterances[i]
            trp = triple
            for key in value_dict:
                utt = utt.replace(key, value_dict[key])
                trp = trp.replace(key, value_dict[key])
            res.append((utt, trp))

    #res = list(set(res))
    with open(opt.save_file, 'w') as f:
        for (utterance, class_string) in res:
            f.write('{}\t<=>\t{}\n'.format(utterance, class_string))

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