-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
49 lines (38 loc) · 1.24 KB
/
predict.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
"""
Evaluate the model
"""
import torch
import torchvision
import torchvision.transforms as transforms
import matplotlib.pyplot as plt
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import data_setup
import model_setup
from pathlib import Path
trainloader, testloader = data_setup.getTrainTestLoaders()
classes = ('plane', 'car', 'bird', 'cat',
'deer', 'dog', 'frog', 'horse', 'ship', 'truck')
dataiter = iter(testloader)
images, labels = next(dataiter)
#
def imshow(img , caption=""):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))
plt.title(caption)
plt.show()
save_path = Path("images")
save_path.mkdir(parents=True, exist_ok=True)
# save_path=str(save_path/'predicted.png')
plt.savefig('images/predicted.png')
plt.close()
model_path = 'models/cifar_net.pth'
net = model_setup.Net()
net.load_state_dict(torch.load(model_path))
outputs = net(images)
_, predicted = torch.max(outputs, 1)
caption = 'Predicted: ', ' '.join(f'{classes[predicted[j]]:5s}' for j in range(4))
imshow(torchvision.utils.make_grid(images), caption)
print('GroundTruth: ', ' '.join(f'{classes[labels[j]]:5s}' for j in range(4)))