-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
104 lines (83 loc) · 3.33 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
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
import numpy as np
import torch
from model.model import NEURAL
from dataset.dataset import DataMain
import time
import os
batch_size = 400
def test(data, model, label_mapping, noise_sd):
correct = 0
tot = 0
X, GT = data.sequential_test_batch()
while X is not None:
X = X.cuda() # to(device)
X = X + torch.randn_like(X).cuda() * noise_sd
GT = torch.from_numpy(label_mapping[GT.numpy()]).cuda()
Y = model(X)
Y = torch.argmax(Y, dim=1)
this_batch_size = len(Y)
for i in range(this_batch_size):
tot += 1
if GT[i] == Y[i]:
correct += 1
X, GT = data.sequential_test_batch()
acc = 100*correct / tot
return acc
print('[Data] Preparing .... ')
data = DataMain(batch_size=batch_size)
data.data_set_up(istrain=False)
data.greeting()
print('[Data] Done .... ')
for sigma in [0.12]:
print("sigma = %.2f" % sigma)
print("Hierarchy => Main")
mappings = np.array([
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]]).astype(np.int64)
for i in range(12):
model = NEURAL(n_class=1, n_channel=3)
if os.path.exists("pretrained_models/hier/model_%d_%.2f_5.pt" % (i, sigma)):
model.load_state_dict(torch.load("pretrained_models/hier/model_%d_%.2f_5.pt" % (i, sigma)))
else:
model.load_state_dict(torch.load("pretrained_models/hier/model_%d_%.2f.pt" % (i, sigma)))
model = model.cuda()
model.eval()
label_mapping = mappings[i]
acc = test(data, model, label_mapping, sigma)
print(f"test accuracy of sensor of class {i}: {acc}")
mappings = np.array([
[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0],
[1, 0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1],
[0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]]).astype(np.int64)
print("Main => Attribute")
for i in range(13):
model = NEURAL(n_class=1, n_channel=3)
if os.path.exists("pretrained_models/attr/model_%d_%.2f_5.pt" % (i, sigma)):
model.load_state_dict(torch.load("pretrained_models/attr/model_%d_%.2f_5.pt" % (i, sigma)))
else:
model.load_state_dict(torch.load("pretrained_models/attr/model_%d_%.2f.pt" % (i, sigma)))
model = model.cuda()
model.eval()
label_mapping = mappings[i]
acc = test(data, model, label_mapping, sigma)
print(f"test accuracy of sensor of attribute {i}: {acc}")