-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_fn.py
More file actions
66 lines (48 loc) · 1.96 KB
/
eval_fn.py
File metadata and controls
66 lines (48 loc) · 1.96 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
# ========================================================================================
# evaluation function for LPGT
# ========================================================================================
import time
import numpy as np
import torch
from utils.utils import compute_pck
def eval_model(model, dataloader, model_path=None):
print("Start evaluation...")
since = time.time()
print("Loading model parameters from {}".format(model_path))
model.load_state_dict(torch.load(model_path))
model.eval()
ds = dataloader.dataset
classes = ds.classes
cls_cache = ds.cls
all_pck = []
category_pck = np.zeros(len(classes))
test_time = []
for i, cls in enumerate(classes):
iter_num = 0
ds.set_cls(cls)
for inputs in dataloader:
input_graphs = inputs['graphs'].to('cuda')
images = inputs['images'].to('cuda')
im_sizes = inputs['im_sizes'].to('cuda')
L_pcks = inputs['L_pcks'].to('cuda')
iter_num = iter_num + 1
with torch.set_grad_enabled(False):
start = time.time()
output_graphs = model(input_graphs, images, 4)
test_time += [time.time() - start]
output_graphs_list = output_graphs.to_data_list()
batch_pck = compute_pck(output_graphs_list, im_sizes, L_pcks)
all_pck += batch_pck
category_pck[i] += batch_pck[0]
category_pck[i] /= len(ds)
pck_avg = np.mean(np.array(all_pck))
time_avg = np.mean(np.array(test_time))
print("Accuracy")
for i, cls in enumerate(classes):
print('Class {} PCK = {:.4f}'.format(cls, category_pck[i]))
print("Mean PCK = {:.4f}".format(pck_avg))
print("Mean generating time = {:.6f}s".format(time_avg))
time_elapsed = time.time() - since
print("Evaluation complete in {:.0f}m {:.0f}s".format(time_elapsed // 60, time_elapsed % 60))
print()
ds.cls = cls_cache