forked from Thinklab-SJTU/ThinkMatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.py
135 lines (101 loc) · 4.69 KB
/
eval.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
131
132
133
134
135
import torch
import time
from datetime import datetime
from pathlib import Path
from utils.hungarian import hungarian
from data.data_loader import GMDataset, get_dataloader
from utils.evaluation_metric import matching_accuracy
from parallel import DataParallel
from utils.model_sl import load_model
from utils.config import cfg
def eval_model(model, dataloader, eval_epoch=None, verbose=False):
print('Start evaluation...')
since = time.time()
device = next(model.parameters()).device
if eval_epoch is not None:
model_path = str(Path(cfg.OUTPUT_PATH) / 'params' / 'params_{:04}.pt'.format(eval_epoch))
print('Loading model parameters from {}'.format(model_path))
load_model(model, model_path)
was_training = model.training
model.eval()
ds = dataloader.dataset
classes = ds.classes
cls_cache = ds.cls
lap_solver = hungarian
accs = torch.zeros(len(classes), device=device)
for i, cls in enumerate(classes):
if verbose:
print('Evaluating class {}: {}/{}'.format(cls, i, len(classes)))
running_since = time.time()
iter_num = 0
ds.cls = cls
acc_match_num = torch.zeros(1, device=device)
acc_total_num = torch.zeros(1, device=device)
for inputs in dataloader:
if 'images' in inputs:
data1, data2 = [_.cuda() for _ in inputs['images']]
inp_type = 'img'
elif 'features' in inputs:
data1, data2 = [_.cuda() for _ in inputs['features']]
inp_type = 'feat'
else:
raise ValueError('no valid data key (\'images\' or \'features\') found from dataloader!')
P1_gt, P2_gt = [_.cuda() for _ in inputs['Ps']]
n1_gt, n2_gt = [_.cuda() for _ in inputs['ns']]
e1_gt, e2_gt = [_.cuda() for _ in inputs['es']]
G1_gt, G2_gt = [_.cuda() for _ in inputs['Gs']]
H1_gt, H2_gt = [_.cuda() for _ in inputs['Hs']]
KG, KH = [_.cuda() for _ in inputs['Ks']]
perm_mat = inputs['gt_perm_mat'].cuda()
batch_num = data1.size(0)
iter_num = iter_num + 1
with torch.set_grad_enabled(False):
s_pred, pred = \
model(data1, data2, P1_gt, P2_gt, G1_gt, G2_gt, H1_gt, H2_gt, n1_gt, n2_gt, KG, KH, inp_type)
s_pred_perm = lap_solver(s_pred, n1_gt, n2_gt)
_, _acc_match_num, _acc_total_num = matching_accuracy(s_pred_perm, perm_mat, n1_gt)
acc_match_num += _acc_match_num
acc_total_num += _acc_total_num
if iter_num % cfg.STATISTIC_STEP == 0 and verbose:
running_speed = cfg.STATISTIC_STEP * batch_num / (time.time() - running_since)
print('Class {:<8} Iteration {:<4} {:>4.2f}sample/s'.format(cls, iter_num, running_speed))
running_since = time.time()
accs[i] = acc_match_num / acc_total_num
if verbose:
print('Class {} acc = {:.4f}'.format(cls, accs[i]))
time_elapsed = time.time() - since
print('Evaluation complete in {:.0f}m {:.0f}s'.format(time_elapsed // 60, time_elapsed % 60))
model.train(mode=was_training)
ds.cls = cls_cache
print('Matching accuracy')
for cls, single_acc in zip(classes, accs):
print('{} = {:.4f}'.format(cls, single_acc))
print('average = {:.4f}'.format(torch.mean(accs)))
return accs
if __name__ == '__main__':
from utils.dup_stdout_manager import DupStdoutFileManager
from utils.parse_args import parse_args
from utils.print_easydict import print_easydict
args = parse_args('Deep learning of graph matching evaluation code.')
import importlib
mod = importlib.import_module(cfg.MODULE)
Net = mod.Net
torch.manual_seed(cfg.RANDOM_SEED)
image_dataset = GMDataset(cfg.DATASET_FULL_NAME,
sets='test',
length=cfg.EVAL.SAMPLES,
obj_resize=cfg.PAIR.RESCALE)
dataloader = get_dataloader(image_dataset)
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
model = Net()
model = model.to(device)
model = DataParallel(model, device_ids=cfg.GPUS)
if not Path(cfg.OUTPUT_PATH).exists():
Path(cfg.OUTPUT_PATH).mkdir(parents=True)
now_time = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
with DupStdoutFileManager(str(Path(cfg.OUTPUT_PATH) / ('eval_log_' + now_time + '.log'))) as _:
print_easydict(cfg)
classes = dataloader.dataset.classes
pcks = eval_model(model, dataloader,
eval_epoch=cfg.EVAL.EPOCH if cfg.EVAL.EPOCH != 0 else None,
verbose=True)