-
Notifications
You must be signed in to change notification settings - Fork 8
/
run_eval_model.py
130 lines (109 loc) · 3.19 KB
/
run_eval_model.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import argparse
import os
import torch
import torch.utils.tensorboard
from tqdm import tqdm
from s2s.dset import DSET_OPTS
from s2s.infr import INFR_OPTS
from s2s.model import MODEL_OPTS
from s2s.path import EXP_PATH
from s2s.tknzr import TKNZR_OPTS
from s2s.util import load_cfg, load_model_from_ckpt, set_seed
def parse_arg() -> argparse.Namespace:
parser = argparse.ArgumentParser(
prog='python run_eval_model.py',
description='Evaluate sequence-to-sequence model.',
)
parser.add_argument(
'--batch_size',
help='Evaluation batch size.',
required=True,
type=int,
)
parser.add_argument(
'--ckpt',
help='Checkpoint to evaluate.',
required=True,
type=int,
)
parser.add_argument(
'--dset_name',
choices=DSET_OPTS.keys(),
help='Name of the dataset to evaluate model.',
required=True,
type=str,
)
parser.add_argument(
'--exp_name',
help='Current experiment name.',
required=True,
type=str,
)
parser.add_argument(
'--infr_name',
choices=INFR_OPTS.keys(),
help='Inference method.',
required=True,
type=str,
)
return parser.parse_args()
@torch.no_grad()
def main():
r"""Main function."""
# Load command line arguments.
args = parse_arg()
# Load model configuration.
model_cfg = load_cfg(exp_name=args.exp_name)
# Control random seed.
set_seed(model_cfg.seed)
# Load encoder tokenizer and its configuration.
enc_tknzr_cfg = load_cfg(exp_name=model_cfg.enc_tknzr_exp)
enc_tknzr = TKNZR_OPTS[enc_tknzr_cfg.tknzr_name].load(cfg=enc_tknzr_cfg)
# Load decoder tokenizer and its configuration.
dec_tknzr_cfg = load_cfg(exp_name=model_cfg.dec_tknzr_exp)
dec_tknzr = TKNZR_OPTS[dec_tknzr_cfg.tknzr_name].load(cfg=dec_tknzr_cfg)
# Load evaluation datset and create dataloader.
dset = DSET_OPTS[args.dset_name]()
dldr = torch.utils.data.DataLoader(
dataset=dset,
batch_size=args.batch_size,
shuffle=False,
)
# Get model running device.
device = torch.device('cpu')
if torch.cuda.is_available():
device = torch.device('cuda')
# Load model.
model = MODEL_OPTS[model_cfg.model_name](
dec_tknzr_cfg=dec_tknzr_cfg,
enc_tknzr_cfg=enc_tknzr_cfg,
model_cfg=model_cfg,
)
model = load_model_from_ckpt(
ckpt=args.ckpt,
exp_name=args.exp_name,
model=model,
)
model.eval()
model = model.to(device)
# Load inference method.
infr = INFR_OPTS[args.infr_name](**args.__dict__)
# Record batch inference result.
all_pred = []
for batch in tqdm(dldr):
all_pred.extend(infr.gen(
batch_text=batch[0],
dec_max_len=model_cfg.dec_max_len,
dec_tknzr=dec_tknzr,
device=device,
enc_max_len=model_cfg.enc_max_len,
enc_tknzr=enc_tknzr,
model=model,
))
# Output all dataset result.
print(DSET_OPTS[args.dset_name].batch_eval(
batch_tgt=dset.all_tgt(),
batch_pred=all_pred,
))
if __name__ == '__main__':
main()