-
Notifications
You must be signed in to change notification settings - Fork 62
/
Copy pathtest.py
158 lines (130 loc) · 4.83 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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
import torch
from torch.autograd import Variable
import torch.nn.functional as F
import torchvision.transforms as transforms
import os
import numpy as np
from matplotlib import pyplot as plt
from PIL import Image
from losses import relu_evidence
from helpers import rotate_img, one_hot_embedding, get_device
def test_single_image(model, img_path, uncertainty=False, device=None):
img = Image.open(img_path).convert("L")
if not device:
device = get_device()
num_classes = 10
trans = transforms.Compose([transforms.Resize((28, 28)), transforms.ToTensor()])
img_tensor = trans(img)
img_tensor.unsqueeze_(0)
img_variable = Variable(img_tensor)
img_variable = img_variable.to(device)
if uncertainty:
output = model(img_variable)
evidence = relu_evidence(output)
alpha = evidence + 1
uncertainty = num_classes / torch.sum(alpha, dim=1, keepdim=True)
_, preds = torch.max(output, 1)
prob = alpha / torch.sum(alpha, dim=1, keepdim=True)
output = output.flatten()
prob = prob.flatten()
preds = preds.flatten()
print("Predict:", preds[0])
print("Probs:", prob)
print("Uncertainty:", uncertainty)
else:
output = model(img_variable)
_, preds = torch.max(output, 1)
prob = F.softmax(output, dim=1)
output = output.flatten()
prob = prob.flatten()
preds = preds.flatten()
print("Predict:", preds[0])
print("Probs:", prob)
labels = np.arange(10)
fig = plt.figure(figsize=[6.2, 5])
fig, axs = plt.subplots(1, 2, gridspec_kw={"width_ratios": [1, 3]})
plt.title("Classified as: {}, Uncertainty: {}".format(preds[0], uncertainty.item()))
axs[0].set_title("One")
axs[0].imshow(img, cmap="gray")
axs[0].axis("off")
axs[1].bar(labels, prob.cpu().detach().numpy(), width=0.5)
axs[1].set_xlim([0, 9])
axs[1].set_ylim([0, 1])
axs[1].set_xticks(np.arange(10))
axs[1].set_xlabel("Classes")
axs[1].set_ylabel("Classification Probability")
fig.tight_layout()
plt.savefig("./results/{}".format(os.path.basename(img_path)))
def rotating_image_classification(
model, img, filename, uncertainty=False, threshold=0.5, device=None
):
if not device:
device = get_device()
num_classes = 10
Mdeg = 180
Ndeg = int(Mdeg / 10) + 1
ldeg = []
lp = []
lu = []
classifications = []
scores = np.zeros((1, num_classes))
rimgs = np.zeros((28, 28 * Ndeg))
for i, deg in enumerate(np.linspace(0, Mdeg, Ndeg)):
nimg = rotate_img(img.numpy()[0], deg).reshape(28, 28)
nimg = np.clip(a=nimg, a_min=0, a_max=1)
rimgs[:, i * 28 : (i + 1) * 28] = nimg
trans = transforms.ToTensor()
img_tensor = trans(nimg)
img_tensor.unsqueeze_(0)
img_variable = Variable(img_tensor)
img_variable = img_variable.to(device)
if uncertainty:
output = model(img_variable)
evidence = relu_evidence(output)
alpha = evidence + 1
uncertainty = num_classes / torch.sum(alpha, dim=1, keepdim=True)
_, preds = torch.max(output, 1)
prob = alpha / torch.sum(alpha, dim=1, keepdim=True)
output = output.flatten()
prob = prob.flatten()
preds = preds.flatten()
classifications.append(preds[0].item())
lu.append(uncertainty.mean())
else:
output = model(img_variable)
_, preds = torch.max(output, 1)
prob = F.softmax(output, dim=1)
output = output.flatten()
prob = prob.flatten()
preds = preds.flatten()
classifications.append(preds[0].item())
scores += prob.detach().cpu().numpy() >= threshold
ldeg.append(deg)
lp.append(prob.tolist())
labels = np.arange(10)[scores[0].astype(bool)]
lp = np.array(lp)[:, labels]
c = ["black", "blue", "red", "brown", "purple", "cyan"]
marker = ["s", "^", "o"] * 2
labels = labels.tolist()
fig = plt.figure(figsize=[6.2, 5])
fig, axs = plt.subplots(3, gridspec_kw={"height_ratios": [4, 1, 12]})
for i in range(len(labels)):
axs[2].plot(ldeg, lp[:, i], marker=marker[i], c=c[i])
if uncertainty:
labels += ["uncertainty"]
axs[2].plot(ldeg, lu, marker="<", c="red")
print(classifications)
axs[0].set_title('Rotated "1" Digit Classifications')
axs[0].imshow(1 - rimgs, cmap="gray")
axs[0].axis("off")
plt.pause(0.001)
empty_lst = []
empty_lst.append(classifications)
axs[1].table(cellText=empty_lst, bbox=[0, 1.2, 1, 1])
axs[1].axis("off")
axs[2].legend(labels)
axs[2].set_xlim([0, Mdeg])
axs[2].set_ylim([0, 1])
axs[2].set_xlabel("Rotation Degree")
axs[2].set_ylabel("Classification Probability")
plt.savefig(filename)