-
Notifications
You must be signed in to change notification settings - Fork 47
/
test.py
46 lines (35 loc) · 1.14 KB
/
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
"""Evaluation script."""
import argparse
import json
import os
import torch
import models
from datasets.kg_dataset import KGDataset
from utils.train import avg_both, format_metrics
parser = argparse.ArgumentParser(description="Test")
parser.add_argument(
'--model_dir',
help="Model path"
)
def test(model_dir):
# load config
with open(os.path.join(model_dir, "config.json"), "r") as f:
config = json.load(f)
args = argparse.Namespace(**config)
# create dataset
dataset_path = os.path.join(os.environ["DATA_PATH"], args.dataset)
dataset = KGDataset(dataset_path, False)
test_examples = dataset.get_examples("test")
filters = dataset.get_filters()
# load pretrained model weights
model = getattr(models, args.model)(args)
device = 'cuda'
model.to(device)
model.load_state_dict(torch.load(os.path.join(model_dir, 'model.pt')))
# eval
test_metrics = avg_both(*model.compute_metrics(test_examples, filters))
return test_metrics
if __name__ == "__main__":
args = parser.parse_args()
test_metrics = test(args.model_dir)
print(format_metrics(test_metrics, split='test'))