-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.py
More file actions
145 lines (130 loc) · 3.98 KB
/
Copy pathtest.py
File metadata and controls
145 lines (130 loc) · 3.98 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import os
import argparse
import torch
import torch.nn.functional as F
from tqdm import tqdm
import data_loader.data_loaders as module_data
from data_loader.data_loaders import load_word_dict, load_rel_dict
import model.loss as module_loss
import model.metric as module_metric
import model.model as module_arch
from train import get_instance
from trainer import assign_to_device
import numpy as np
import pdb
import pickle
def main(config, args):
torch.set_default_tensor_type("torch.cuda.FloatTensor")
# setup data_loader instances
word2id, word2vec = load_word_dict(
os.path.join(args.input, "word_vec.json"), anony=False
)
rel2id = load_rel_dict(os.path.join(args.input, "rel2id.json"))
# build model architecture
model = get_instance(
module_arch, "arch", config, word_vec_mat=word2vec, relation_num=len(rel2id)
)
print(model)
# get function handles of metrics
metric_fns = [getattr(module_metric, met) for met in config["eval_metrics"]]
# load state dict
checkpoint = torch.load(args.resume)
state_dict = checkpoint["state_dict"]
if config["n_gpu"] > 1:
model = torch.nn.DataParallel(model)
model.load_state_dict(state_dict)
# prepare model for testing
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = model.to(device)
model.eval()
save = args.save
# setup data_loader instances
data_dir = args.input
test_data_loader = module_data.BaseNytLoader(
data_dir,
word2id,
rel2id,
120,
2000,
validation_split=0,
mode="test",
src="test",
method=config["data_loader"]["args"]["method"]
if args.method is None
else args.method,
batch_type=1,
select=0,
filtering_mode=0,
shuffle=False,
num_workers=1,
anonymization=False,
)
print("%d entities pairs in total" % len(test_data_loader.subset))
total_output = []
total_target = []
with torch.no_grad():
for i, (data, target) in enumerate(tqdm(test_data_loader)):
data, target = (
assign_to_device(data, device),
assign_to_device(target, device),
)
output = model(data, is_train=False)
total_output.append(output)
total_target.append(target)
total_output = torch.cat(total_output)
total_target = torch.cat(total_target)
total_metrics = np.zeros(len(metric_fns))
for i, metric in enumerate(metric_fns):
total_metrics[i], _ = metric(
total_output, total_target, is_train=False, save=save
)
log = {}
log.update(
{
met.__name__: total_metrics[i].item()
for i, met in enumerate(metric_fns)
}
)
for metric_name, metric_value in log.items():
print("%s: %.4f"%(metric_name, metric_value))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="PyTorch Template")
parser.add_argument(
"-r",
"--resume",
default=None,
required=True,
type=str,
help="path to latest checkpoint (default: None)",
)
parser.add_argument(
"-d",
"--device",
default=None,
type=str,
help="indices of GPUs to enable (default: all)",
)
parser.add_argument(
"-i",
"--input",
default=None,
required=True,
type=str,
help="input data for evauation"
)
parser.add_argument(
"-s", "--save", default=None, type=str, help="path to save evaluation results"
)
parser.add_argument(
"-m",
"--method",
default=None,
type=int,
help="method for RE, 0 for BASE, 1 for MERGE, 2 for REDS2"
)
args = parser.parse_args()
if args.device:
os.environ["CUDA_VISIBLE_DEVICES"] = args.device
if args.resume:
config = torch.load(args.resume)["config"]
main(config, args)