-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain_test.py
64 lines (54 loc) · 2.51 KB
/
main_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
import os
import torch
import torch.utils.data
import argparse
from tqdm import tqdm
from dataset import collate_fn, SeqDataset
from transformer.Translator import Translator
from preprocess import load_file, convert_w2id_seq
def main():
parser = argparse.ArgumentParser(description='main_test.py')
parser.add_argument('-model', required=True,
help='Path to the model checkpoint file')
parser.add_argument('-src', required=True,
help='Source test data to decode (one line per sequence)')
parser.add_argument('-vocab', required=True,
help='Source data to extract vocabs (one line per sequence)')
parser.add_argument('-output_dir', required=True,
help="Dir to store the decoded outputs")
parser.add_argument('-beam_size', type=int, default=5,
help='Beam size')
parser.add_argument('-batch_size', type=int, default=64,
help='Batch size')
parser.add_argument('-n_best', type=int, default=1,
help="""If verbose is set, will output the n_best decoded sentences""")
parser.add_argument('-no_cuda', action='store_true')
args = parser.parse_args()
args.cuda = not args.no_cuda
if not os.path.exists(args.output_dir):
os.mkdir(args.output_dir)
# Prepare DataLoader
preprocess_data = torch.load(args.vocab)
preprocess_settings = preprocess_data['settings']
test_src_word_insts = load_file(args.src, preprocess_settings.max_word_seq_len, preprocess_settings.keep_case)
test_src_insts = convert_w2id_seq(test_src_word_insts, preprocess_data['dict']['src'])
test_loader = torch.utils.data.DataLoader(
SeqDataset(
src_word2idx=preprocess_data['dict']['src'],
tgt_word2idx=preprocess_data['dict']['tgt'],
src_insts=test_src_insts),
num_workers=2,
batch_size=args.batch_size,
collate_fn=collate_fn)
translator = Translator(args)
with open(args.output_dir + '/test_out.txt', 'w') as f:
for batch in tqdm(test_loader, mininterval=2, desc=' - (Test)', leave=False):
all_hyp, all_scores = translator.translate_batch(*batch)
for idx_seqs in all_hyp:
for idx_seq in idx_seqs:
pred_line = ' '.join([test_loader.dataset.tgt_idx2word[idx] for idx in idx_seq])
f.write(pred_line + '\n')
print('[Info] Finished.')
if __name__ == "__main__":
main()