-
Notifications
You must be signed in to change notification settings - Fork 0
/
vis.py
132 lines (102 loc) · 3.71 KB
/
vis.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
import aleatoric, epistemic, combined, neural_net
import torch
from torchvision import transforms, datasets
from scipy.stats import entropy
import matplotlib.pyplot as plt
import numpy as np
from tqdm import tqdm
import sys
def show_digits(images, w, h):
for i, image in enumerate(images):
plt.subplot(w, h, i+1)
plt.imshow(image.reshape(28,28), cmap='gray')
plt.axis('off')
def slice_by_digit(images, digits, uncertainty, n=10):
high = []
low = []
for i in range(1, 10+1):
images_i = images[digits == i]
uncertainty_i = uncertainty[digits == i]
indices = uncertainty_i.argsort()
high.extend(images_i[indices[::-1][:n]])
low.extend(images_i[indices[:n]])
return high, low
if __name__ == '__main__':
variant = sys.argv[1]
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('data', train=False,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=256, shuffle=False)
if variant == 'combined':
net = torch.load('combined.pt')
epistemic, aleatoric = [], []
images, digits = [], []
with torch.no_grad():
for data, target in tqdm(test_loader):
x = data.view(len(data), -1).float()
mu, log_sigma2 = net(x)
aleatoric.extend([ np.linalg.norm(s) for s in np.exp(0.5*log_sigma2.detach().numpy()) ])
epistemic.extend([ entropy(p) for p in combined.predict(x, net).detach().numpy() ])
images.extend(data.detach().numpy())
digits.extend(target.detach().numpy())
images, digits = np.array(images), np.array(digits)
epistemic, aleatoric = np.array(epistemic), np.array(aleatoric)
high, low = slice_by_digit(images, digits, epistemic)
plt.figure()
show_digits(high, 10, 10)
plt.savefig('combined_epistemic_high.png')
plt.figure()
show_digits(low, 10, 10)
plt.savefig('combined_epistemic_low.png')
high, low = slice_by_digit(images, digits, aleatoric)
plt.figure()
show_digits(high, 10, 10)
plt.savefig('combined_aleatoric_high.png')
plt.figure()
show_digits(low, 10, 10)
plt.savefig('combined_aleatoric_low.png')
elif variant == 'epistemic':
net = torch.load('epistemic.pt')
predict = epistemic.predict
epistemic = []
images, digits = [], []
with torch.no_grad():
for data, target in tqdm(test_loader):
x = data.view(len(data), -1).float()
epistemic.extend([ entropy(p) for p in predict(x, net).detach().numpy() ])
images.extend(data.detach().numpy())
digits.extend(target.detach().numpy())
images, digits = np.array(images), np.array(digits)
epistemic= np.array(epistemic)
high, low = slice_by_digit(images, digits, epistemic)
plt.figure()
show_digits(high, 10, 10)
plt.savefig('epistemic_high.png')
plt.figure()
show_digits(low, 10, 10)
plt.savefig('epistemic_low.png')
elif variant == 'aleatoric':
net = torch.load('aleatoric.pt')
aleatoric = []
images, digits = [], []
with torch.no_grad():
for data, target in tqdm(test_loader):
x = data.view(len(data), -1).float()
mu, log_sigma2 = net(x)
aleatoric.extend([ np.linalg.norm(s) for s in np.exp(0.5*log_sigma2.detach().numpy()) ])
images.extend(data.detach().numpy())
digits.extend(target.detach().numpy())
images, digits = np.array(images), np.array(digits)
aleatoric = np.array(aleatoric)
high, low = slice_by_digit(images, digits, aleatoric)
plt.figure()
show_digits(high, 10, 10)
plt.savefig('aleatoric_high.png')
plt.figure()
show_digits(low, 10, 10)
plt.savefig('aleatoric_low.png')
else:
print ('variant must be one of combined,aleatoric,epistemic')